本文主要是介绍29. 面向对象的LotusScript(五)之ExtDoc,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
NotesDocument是Lotus Notes的核心对象之一,在开发中会遇到很多与它有关的反复出现的功能需求,可以写成通用的函数,比如针对一个文档,创建回复、取得父文档等等。下面就是一些例子:%REMDescription: Create a response for the given document.Use the given form name. Return the unsaved response.
%END REM
Public Function CreateResponse(doc As NotesDocument, form As String)As NotesDocumentDim response As NotesDocumentSet response=doc.Parentdatabase.Createdocument()response.Form=formCall response.Makeresponse(doc)Set CreateResponse=response
End Function
%REMDescription: Description: Replace the given items of the document collection with the given document's items of the same name. @param: itemNames is an array or a string conainting names separated by '^'other types provided, an error will occur
%END REM
Public Function StampCollection(dc As NotesDocumentCollection, doc As NotesDocument, itemNames As Variant) Dim vIf Not IsArray(itemNames) Thenv=Split(itemNames, "^")Elsev=itemNamesEnd IfForAll itemName In vCall dc.Stampall(itemName, doc.Getitemvalue(itemName))End ForAll
End Function
%REMDescription: Replace the given items of the reponses of the given document with its own items of the same name. @param: itemNames is an array or a string conainting names separated by '^'other types provided, an error will occur
%END REM
Public function StampResponses(doc As NotesDocument, itemNames As Variant)Dim dc As NotesDocumentCollectionSet dc=doc.ResponsesCall StampCollection(dc, doc, itemNames)'Call dc.StampAll(itemName,doc.GetItemValue(itemName))
End Function
这样的函数创建多了,我们便可以发现进一步改进的可能。它们都与NotesDocument有关,按照面向对象语言的规范,应该将它们集中到一个对象里。这样,可以收到使用对象的诸多好处,比如可以省去参数中的NotesDocument,减少了函数名称冲突的可能性。本来根据面向对象的思想,可以考虑扩展NotesDocument成为一个新的具备更多功能的类。但是LotusScript中由产品本身提供的类是不能扩展的。继承不了,我们可以换一种方式。创建一个“包含”NotesDocument的类,在其构造函数中传入需要“增强”的NotesDocument,保存在内部变量中,然后为其增加任意需要的方法,从中引用该NotesDocument。在面向对象的架构里,要创建一个新类,利用已有的类,也不只有继承一径,还可以组合或包含。
%REMClass ExtDocDescription: Comments for Class
%END REM
Public Class ExtDocPublic mdoc As NotesDocument%REMSub NewDescription: Comments for Sub%END REMSub New(doc As NotesDocument)Set me.mdoc=docEnd Sub%REMFunction ReplaceItemValueDescription: Replace the given item's value using the given "from" array to "to" array. %END REMPublic Function ReplaceItemValue(itemName As String, compareArray As Variant, replaceArray As Variant)Dim v As Variantv=me.mdoc.Getitemvalue(itemName)Call me.mdoc.Replaceitemvalue(itemName, ArrayReplace(v, compareArray, replaceArray))End Function%REMFunction CopyAllToDescription: copy the document and all its descendents toanother db.%END REMPublic Function CopyAllTo(dest As NotesDatabase )Call mdoc.Copytodatabase(dest)Dim dc As NotesDocumentCollectionDim rdoc As NotesDocumentDim rext As ExtDocSet dc=mdoc.ResponsesSet rdoc=dc.Getfirstdocument()Do Until rdoc Is NothingSet rext=New ExtDoc(rdoc)Call rext.CopyAllTo(dest)Set rdoc=dc.Getnextdocument(rdoc)LoopCall mdoc.Copytodatabase(dest)End Function%REMFunction RemoveAllDescription: Remove the document and all its descendents%END REMPublic Function RemoveAll(force As Boolean)Dim dc As NotesDocumentCollectionDim docR As NotesDocument, docTmp As NotesDocumentDim ed As ExtDocSet dc=mdoc.ResponsesSet docR=dc.Getfirstdocument()Do Until docR Is Nothing Set docTmp=dc.Getnextdocument(docR)Set ed=New ExtDoc(docR)Call ed.RemoveAll(force)Set docR=docTmpLoopCall mdoc.Remove(force)End Function%REMFunction ComputeAndSaveDescription: Comments for Function%END REMPublic Function ComputeAndSave()Call mdoc.Computewithform(False, False )Call mdoc.save(True, False)End Function%REMFunction CopyItemsFromDescription: Comments for Function%END REMPublic Function CopyItemsFrom(source As NotesDocument, items As Variant)If IsArray(items) ThenForAll itemName In itemsCall mdoc.Replaceitemvalue(itemName, source.Getitemvalue(itemName))End ForAllElseCall mdoc.Replaceitemvalue(items, source.Getitemvalue(items))End IfEnd Function%REMFunction CreateResponseDescription: Comments for Function%END REMPublic Function CreateResponseDoc(form As String ) As NotesDocumentSet me.CreateResponseDoc=CreateResponse(me.mdoc, form)End Function%REMFunction StampResponsesDescription: Replace the given item of the reponses of the wrapped document with its own item of the same name. %END REMPublic Function StampResponseDocs(itemName As Variant)Call StampResponses(me.mdoc, itemName)End Function%REMFunction GetParentDocDescription: Comments for Function%END REMPublic Function GetParentDoc() As NotesDocumentDim docResult As NotesDocumentIf me.mdoc.Isresponse ThenSet docResult=mdoc.Parentdatabase.Getdocumentbyunid(mdoc.Parentdocumentunid)ElseSet docResult=Nothing End IfSet GetParentDoc=docResultEnd Function%REMFunction IsUniqueDescription: Check if the wrapped document is unique in the given view.A document is unique if for the given number -- keyNum -- of sorted columns, there's no other documenthaving the same values. That is, if a GetDocumentByKey is called with an array cotainingkeyNum of keys, no other document is returned.%END REMPublic Function IsUnique(viewName As String, keyNum As Integer) As BooleanDim view As NotesViewSet view=Me.mdoc.ParentDatabase.GetView(viewName)Dim doc As NotesDocumentDim keys As New NArray(-1)ForAll c In view.ColumnsIf c.IsSorted Thenkeys.Add(mdoc.Getitemvalue(c.Itemname)(0))keyNum=keyNum-1If keyNum=0 ThenExit ForAll End IfEnd IfEnd ForAllSet doc=view.Getdocumentbykey(keys.Container, True)If doc Is Nothing ThenIsUnique=TrueElseIf doc.Universalid=mdoc.Universalid ThenIsUnique=TrueElseIsUnique=False End IfEnd If End Function%REMFunction IsValueUniqueDescription: Check if the wrapped document is unique in the given column of the given view.%END REMPublic Function IsValueUnique(value As Variant,viewName As String,columnNum As Integer) As BooleanDim s As New NotesSessionDim view As NotesViewDim doc As NotesDocument Set view=s.CurrentDatabase.GetView(viewName)Set doc=view.GetFirstDocumentDo Until doc Is NothingIf doc.ColumnValues(columnNum)=value And doc.Universalid><mdoc.Universalid Then Me.IsValueUnique=FalseExit FunctionEnd IfSet doc=view.GetNextDocument(doc)LoopMe.IsValueUnique=True End Function%REMDescription: Check if the wrapped document is unique in the given field of the given document collection.%END REMPublic Function IsValueUniqueInDC(Field As String, dc As NotesDocumentCollection) As BooleanDim doc As NotesDocumentSet doc=dc.Getfirstdocument()Do Until doc Is Nothing'cannot compare the item value array, compare the first value insteadIf doc.GetItemValue(Field)(0)=mdoc.Getitemvalue(field)(0) And doc.Universalid><mdoc.Universalid Then Me.IsValueUniqueInDC=FalseExit FunctionEnd IfSet doc=dc.GetNextDocument(doc)LoopMe.IsValueUniqueInDC=True End Function%REMFunction GetDuplicatedDocDescription: Return the first found document in the given view, which has the same valuewith the given document in the given column.%END REMPublic Function GetDuplicatedDoc(value As Variant, viewName As String, columnNum As Integer) As NotesDocument Dim s As New NotesSessionDim view As NotesViewDim doc As NotesDocument Set view=s.CurrentDatabase.GetView(viewName)Set doc=view.GetFirstDocumentDo Until doc Is NothingIf doc.ColumnValues(columnNum)=value And doc.Universalid><mdoc.Universalid Then Set Me.GetDuplicatedDoc=docExit FunctionEnd IfSet doc=view.GetNextDocument(doc)Loop End Function%REMObsolete Function ReplicateToDescription: Make sure the copied document has the same universal id.Direct copied documents will keep the response relations in 8.5. Modifying a document's universal id and saving it will generate another document with still a different OF part of the id.Public Function ReplicateTo(dest As NotesDatabase )Dim doc As NotesDocumentSet doc= mdoc.Copytodatabase(dest)doc.Universalid=mdoc.UniversalidCall doc.Save(True, False)Set me.ReplicateTo=docEnd Function%END REM%REMObsolete Function ReplicateAllToDescription: Comments for FunctionPublic Function ReplicateAllTo(dest As NotesDatabase)Call me.ReplicateTo(dest)Dim dc As NotesDocumentCollectionDim rdoc As NotesDocumentDim rext As ExtDocSet dc=mdoc.ResponsesSet rdoc=dc.Getfirstdocument()Do Until rdoc Is NothingSet rext=New ExtDoc(rdoc)Call rext.ReplicateAllTo(dest)Set rdoc=dc.Getnextdocument(rdoc)LoopEnd Function%END REM
End Class
上面的CreateResponseDoc和StampResponseDocs方法分别调用了CreateResponse和StampResponses函数。当然也可以将这些函数本身包含进类中。大部分方法的作用都一看即知。稍微复杂一些的做了详细的注释,这里再略加说明。GetDuplicatedDoc,IsUnique, IsValueUnique,这些方法提到的Unique, Duplicated的含义都是针对某个视图的某个或某些列;IsUnique可以比较视图的前若干个排序列;IsValueUniqueInDC针对的是文档集合和某个字段。StampResponseDocs使用父文档的域更改子文档,参数中的域名可以是数组或由特殊字符^分隔开的字符串。CopyAllTo和RemoveAll里的All指的是连带所有子文档。ReplicateTo和ReplicateAllTo会修改目的文档的UniversalID,但是在8.5中似乎Notes会自动保持答复关系,故不再需要。
ExtDoc就像一把瑞士军刀,而且你还可以随时丰富它的功能。同样,对于其它Notes对象,你也可以制造你的瑞士军刀。这篇关于29. 面向对象的LotusScript(五)之ExtDoc的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!