Simulate events for custom objects in Lotus script

Source: Internet
Author: User

Preface

Events are a common mode supported and used by object-oriented languages. Events are not only widely used in systems that interact with users, but also use events to properly design objects to clearly and independently write data.CodeIt is also very helpful. LotusScript supports events. Events published by each UI object areProgramAre often used. However, among the three objects supported by Lotus Script: Notes objects, custom objects, and OLE objects, only notes objects support events. That is to say, we can only use events published by the notes class and cannot define events in the Custom class.

Can an event be simulated in LotusScript?

The core of event processing is that when a "state" changes, one program (event source event emitters) notifies the subscribe to process this event, and the other program (event consumer event consumers ), many languages implement this mechanism through callback functions. Lotus script does not support any form of "function pointer", so you can only find another method.

Next we will analyze a practical problem.

Back to Top

Implementation 1

Imagine that there is an asset management database that processes multiple processes, such as receipt, transfer, and cancellation. We put all the public code for processing the process in a process class simpleflow. For a specific workflow, you only need to create a simpleflow instance and then call its submit method. Each process has some specific business logic. For example, you need to check the value of a amount domain before submitting the asset cancellation process. If the amount is greater than 5000, a dialog box is displayed to allow users to enter more information; after the process is submitted, update the corresponding asset document. These business logic can be placed in many places, such as the button or operation for submitting an action on the form, before and after calling the submit method of simpleflow. If you consider using the event method, we want to raise the event querysubmit and postsubmit before and after the submit respectively, and add the specific code of the Process instance in their handler (Event Handlers.

To make the discussion concentrated, the sample code in this article only contains the theme-related sections, excluding the details of the processing process and error handling code.

Listing 1. simpleflow class (the code is included in a script library named simpleflowlib)

Public class simpleflow 'define public object variable 'process variable private strflow as string private strnode as string private straction as string 'notes object private s as notessession private ws as notesuiworkspace private uidoc as notesuidocument private Doc as notesdocument private dB as notesdatabase below are some public properties of simpleflow get flowname as string flowname = strflow end property public property get nodename as string nodename = strnode end property public property get actionname as string actionname = straction end property public property get mainuidoc as notesuidocument set mainuidoc = uidoc end property public property get maindoc as notesdocument set maindoc = Doc end property public property get maindb as notesdatabase set maindb = DB end property public sub new (flowname as string, nodename as string, actionname as string) strflow = flowname strnode = nodename straction = actionname' initialize the notes object set S = new notessession set DB = s. currentdatabase set Ws = new notesuiworkspace set uidoc = ws. currentdocument set Doc = uidoc. document end sub 'main method public sub submit for external calls' If querysubmit returns false, if not querysubmit (me) then exit sub end if 'code for processing the process call postsubmit (me) end sub end class

Simpleflowlib also defines the querysubmit and postsubmit methods called by the simpleflow class.

Listing 2. querysubmit code

Private function querysubmit (flowobj as simpleflow) as Boolean 'Run before process submission' return a Boolean value. If it is true, continue to submit; otherwise, cancel querysubmit = true dim ws as new notesuiworkspace 'and check the process name and status if flowobj. flowname = "asset" and _ flowobj. nodename = "draft" and _ flowobj. actionname = "Submit" then 'dlginfo is a form used to enter more information if not ws. dialogbox ("dlginfo") then' if the user cancels the task, querysubmit = false end if end ifend function will not be submitted.

Listing 3. postsubmit code

Private sub postsubmit (flowobj as simpleflow) 'After the process is submitted, run dim viewasset as notesview' to obtain the reference to the current database from the simpleflow attribute. the 'Assets view contains the asset document and the first column is sorted by assetid. Set viewasset = flowobj. maindb. getview ("assets") 'asset document dim docasset as notesdocument' checks the process name and status if flowobj. flowname = "asset" and _ flowobj. nodename = "5. manager approval "and _ flowobj. actionname = "approve" then set docasset = viewasset. getdocumentbykey (flowobj. maindoc. assetid (0), true) if not docasset is nothing then 'update the asset document docasset. approvalstatus = "approved by manager" Call docasset. save (true, false) end if end ifend sub

In this way, in the main form of asset cancellation, we only need to reference simpleflowlib, and then create a simpleflow object in a button or operation and submit it.

This simulation is quite different from the nature of the event. The simpleflow object, as the event source, calls a fixed method instead of adding an event handler to the consumer. The result is that the querysubmit and postsubmit methods can only be written in the same scriptlibrary as the simpleflow class. Simpleflowlib has lost some of its versatility. Every database using this script library may need to modify the querysubmit and postsubmit methods.

Back to Top

Another idea

In order to overcome the shortcomings above, we adopt another method closer to the event nature to achieve simulation. The following is a simple class for publishing a demo event.

Listing 4. eventobject class (the code is included in a script library named eventobjectlib)

Public class eventobject is used to add event handler public demoevent as string to the demo event. In this method, the demo event public function run call ondemoevent end function 'RUNS the event handler private function ondemoevent added by event consumer. execute demoevent end function end class

Create an eventobject object in another method and add the handler of the demo event.

Listing 5. Add an event handler (the code is included in a script library named testlib)

Public Function main dim EO as new eventobject set demoobj = new demo dim demohandler as string demohandler = {MessageBox ("Hello everybody! ")} Eo. demoevent = demohandler call eo. Run () end Function

Here, the demohandler can reference the notes object or custom object and method like the ordinary method. Generally, the event consumer and eventobject are not defined in the same module. At this time, the called object and method must be public, and the Use statement must be added when the demohandler is defined. In this way, the objects and Methods referenced by demohandler can be accessed only when the temporary load module is installed in the execute statement.

The following defines a class for testing in testlib.

Listing 6. Declarations in testlib

'A simple class for testing public class demo public function dosomething (LAN as string) msgbox "do something in" & LAN end function end class 'public demoobj as demo of a demo class

Then, you can reference the handler of the demo event when adding it.

Listing 7. Slightly complex event handlers

'Method for referencing a custom demo object demohandler = {use "test": demoobj. dosomething ("LotusScript ")}

Note that the above Code uses the colon (:), which is a method inherited by Lotus script from basic for connecting multiple statements in one row. This feature is rarely used (if multiple statements are separated by colons in the designer, the designer Automatically splits them into multiple rows and removes the colons ), however, the definition of demohandler is concise and readable, and it does not need to be written as a multi-line string.

Back to Top

Re-implement simpleflow events

Now we use the above method to re-implement the querysubmit and postsubmit events of simpleflow.

Listing 8. Modified simpleflowlib

'Add a public variable in (declarations) to save the results returned by the event handler. Public eventresult as Boolean 'add the following event simulation code in simpleflow. 'Define querysubmit and postsubmit events public querysubmit as string public postsubmit as string' run querysubmit event handler private function onquerysubmit = execute (querysubmit) end function 'event handler private function onpostsubmit = execute (postsubmit) end function' modify the submit method public sub submit 'If querysubmit returns false, the call onquerysubmit if not eventresult then exit sub end if 'Process Code msgbox ("oops") Call onpostsubmit end sub

At the same time, we can remove the code that originally contains the business logic from simpleflowlib, put it into testlib, and change its access modifier (access modifier) to public. In this way, simpleflowlib can be used as a standard script library by various processes.

Listing 9. Modified testlib

'In the newly added method, add the event handler of the simpleflow object and submit it. Public Function submitflow set flowobj = new simpleflow ("asset", "draft", "Submit") flowobj. querysubmit = {use "testlib": eventresult = querysubmit} flowobj. postsubmit = {use "testlib": postsubmit} call flowobj. submit () end Function

Back to Top

Further

The real event mechanism is often much more complex than the preceding example. For example, the event source can pass parameters to the handler, and the event consumer can add multiple handlers to an event, you can also remove existing handlers. The following code partially simulates these features. After a common event processing code is added to the simpleflow class, you can define the event freely. Programs that reference the instance of this class can also dynamically add and delete event processing programs.

Listing 10. Common event processing code in simpleflow

'Define the General event list private eventlist list as variant' Add the event handler public function addeventhandler (eventname as string, handler as string) dim handlerlist list as string dim V as variant if iselement (eventlist (eventname) then v = eventlist (eventname) V (handler) = handler eventlist (eventname) = V else handlerlist (handler) = handler eventlist (eventname) = handlerlist end if end function 'Remove the event handler public function removeeventhandler (eventname as string, handler as string) 'You need to add % include "lserr. LSS "on error errlistitemdoesnotexist goto exitfunction dim handlerlist as variant handlerlist = eventlist (eventname) Erase handlerlist (handler) eventlist (eventname) = handlerlist exitfunction: exit function end function 'run all the handlers of an event in eventlist: private sub onevent (eventname as string) If iselement (eventlist (eventname) then dim V as variant v = eventlist (eventname) forall handler in V execute handler end forall end if end sub 'The rewritten submit method public sub submit' If querysubmit returns false, the onevent ("querysubmit") will not be called again ") if not eventresult then exit sub end if 'code of the processing flow msgbox ("oops") Call onevent ("postsubmit") end sub

The simpleflow code referenced in testlib is also modified accordingly:

Listing 11. testlib modified again

'Use the common event public function submitflowex set flowobj = new simpleflow ("asset", "draft", "Submit") Call flowobj. addeventhandler ("querysubmit", {use "testlib": eventresult = querysubmit}) 'can add any number of event handler' call flowobj. addeventhandler ("querysubmit", {use "testlib": querysubmithandler2}) Call flowobj. addeventhandler ("postsubmit", {use "testlib": postsubmit}) 'removes event handler call flowobj. removeeventhandler ("querysubmit", {use "testlib": querysubmithandler2}) Call flowobj. submit () end Function

Back to Top

Conclusion

By simulating events, you can write custom classes that are easier to transplant. However, compared with the event mechanism of many languages, there are still many limitations. The event handler is only passed through a string, and the type and signature cannot be checked, which lacks security. Only public variables can be used to transmit event-related information between the event source and the consumer. The event handler must be defined in a script library before the event source can be referenced and accessed through the use statement. These restrictions make it inconvenient to simulate events.

References

Learning

    • "How to Process Groups in Lotus script" (developerworks, November March 2005): This article describes how to use the built-in notesadministrationprocess class and two custom Lotus script classes to Process Groups in Lotus script.

    • "Using Lotus script to automatically generate and Operate Excel reports" (developerworks, August 1, October 2006): This article describes, the principles, methods, and practical skills for implementing Excel report operations using LotusScript language.
    • "Using Lotus script and automation to integrate IBM Lotus Notes and Microsoft Office" (developerworks, November May 2009 ): the technology introduced in this article allows Microsoft Office applications to publish information to the IBM Lotus Notes database in different formats.
    • See: developerworks Lotus Notes and Domino product pages.

Obtain products and technologies

    • Download the trial version: Lotus Domino designer v8.5.1.

    • Download the trial version: Lotus Domino and Lotus iNotes.
    • Download the trial version: IBM Lotus Notes and Domino administrator 8.5 client software.

Discussion

    • Visit: IBM Lotus Notes/Domino 8.5 forum.

    • Visit: Lotus Notes and Domino wiki.

About the author

starrow pan, a free software developer, has 7 years of IT industry experience and rich experience in Notes development and project management.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.