29. 面向对象的LotusScript(五)之ExtDoc

2024-02-01 18:58

本文主要是介绍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的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/668162

相关文章

ffmpeg面向对象-待定

1.常用对象 rtsp拉流第一步都是avformat_open_input,其入参可以看下怎么用: AVFormatContext *fmt_ctx = NULL;result = avformat_open_input(&fmt_ctx, input_filename, NULL, NULL); 其中fmt_ctx 如何分配内存的?如下 int avformat_open_input(

chapter06 面向对象基础 知识点Note

文章目录 前言类的设计 属性和行为对象的内存解析 (堆 栈 方法区)类的成员之一 变量(属性) field类的成员之二 方法 method对象数组方法重载 overload可变个数的形参 语法糖方法的值传递机制递归关键字package importMVC设计模式import导入面向对象特征之一 封装类的成员之三 构造器JavaBeanUML类图 前言 ` 面向对象封装 面向

【JVM】JVM栈帧中的动态链接 与 Java的面向对象特性--多态

栈帧 每一次方法调用都会有一个对应的栈帧被压入栈(虚拟机栈)中,每一个方法调用结束后,都会有一个栈帧被弹出。 每个栈帧中包括:局部变量表、操作数栈、动态链接、方法返回地址。 JavaGuide:Java内存区域详解(重点) 动态链接 动态链接:指向运行时常量池中该栈帧所属方法的引用。 多态 多态允许不同类的对象对同一消息做出响应,但表现出不同的行为(即方法的多样性)。 多态

java基础总结15-面向对象11(抽象类)

下面通过一下的小程序深入理解抽象类 因此在类Animal里面只需要定义这个enjoy()方法就可以了,使用abstract关键字把enjoy()方法定义成一个抽象方法,定义如下:public abstract void enjoy();   从某种意义上来说,抽象方法就是被用来重写的,所以在父类声明的抽象方法一定要在子类里面重写。如果真的不想在子类里面重写这个方法,那么可以再在子类里

java基础总结14-面向对象10(多态)

面向对象最核心的机制——动态绑定,也叫多态 1 通过下面的例子理解动态绑定,即多态 package javastudy.summary;class Animal {/*** 声明一个私有的成员变量name。*/private String name;/*** 在Animal类自定义的构造方法* @param name*/Animal(String name) {this.name = n

java基础总结13-面向对象9(对象转型)

对象转型分为两种:一种叫向上转型(父类对象的引用或者叫基类对象的引用指向子类对象,这就是向上转型),另一种叫向下转型。转型的意思是:如把float类型转成int类型,把double类型转成float类型,把long类型转成int类型,这些都叫转型。把一种形式转成另外一种形式就叫转型。除了基础数据类型的转型之外(基础数据类型的转型:大的可以转成小的,小的也可以转成大的。),对象领域里面也有对象之

java基础总结12-面向对象8(Object类)

1 Object类介绍 Object类在JAVA里面是一个比较特殊的类,JAVA只支持单继承,子类只能从一个父类来继承,如果父类又是从另外一个父类继承过来,那他也只能有一个父类,父类再有父类,那也只能有一个,JAVA为了组织这个类组织得比较方便,它提供了一个最根上的类,相当于所有的类都是从这个类继承,这个类就叫Object。所以Object类是所有JAVA类的根基类,是所有JAVA类的老祖宗

java基础总结11-面向对象7(super关键字)

在JAVA类中使用super来引用父类的成分,用this来引用当前对象,如果一个类从另外一个类继承,我们new这个子类的实例对象的时候,这个子类对象里面会有一个父类对象。怎么去引用里面的父类对象呢?使用super来引用,this指的是当前对象的引用,super是当前对象里面的父对象的引用。 1 super关键字测试 package cn.galc.test;/*** 父类* @autho

java基础总结10-面向对象6(方法的重载)

1 方法的重载 方法名一样,但参数不一样,这就是重载(overload)。   所谓的参数不一样,主要有两点:第一是参数的个数不一样,第二是参数的类型不一样。只要这两方面有其中的一方面不一样就可以构成方法的重载了。 package cn.galc.test;public class TestOverLoad {void max(int a, int b) {System.out.prin