Documentum and Jython – part DeuxMay 16, 2008 at 8:22 pm | Posted in Jython | 1 Comment
Tags: documentum, jython
In my first post about the joys of Jython and Documentum I just showed some bare bones code to login and perform an action. One of the things I like is the jython interpreter where you can effectively run DFC line-by-line at the command prompt and see the effect. However all that typing can get a little tedious so it makes sense to wrap up some of the useful stuff into a module that can be imported into Jython.
First lets see how we can wrap the DFC connection code into a function that can be called multiple times. Fire up the jython interpreter and enter the following:
>>> from com.documentum.com import DfClientX >>> def connect(docbase, username, password): ... cx=DfClientX() ... c =cx.getLocalClient() ... li =cx.getLoginInfo() ... li.setUser(username) ... li.setPassword(password) ... s = c.newSession(docbase, li) ... return s ... >>>
The first line is the standard import of the DfClientX class from which we can dynamically create (directly or indirectly) most of the other DFC classes.
The second line is the way to create functions in Jython. The
def keyword starts a function definition. It is followed by the function name and then the parameters in brackets – Jython is not strictly typed so we don’t need to specify types in the function definition. The line ends with a colon. This is the standard python/jython way of indicating a multi-line statement block; unlike java there are no curly brackets to delimit statement blocks. Following the function definition line each line of the function must be indented. Every indented line up to and including the
return statement is part of the function (notice the interpreter changes the
...). The function definition is ended by a blank line – after this the interpreter returns to the
Now we can simply call the function like this:
>>> s1 = connect("mydocbase","dmadmin","dmadmin") >>> s2 = connect("mydocbase","user1","user1pass")
to give us 2 sessions, one for dmadmin and one for user1.
Now we could use the session to create an object:
doc = s1.newObject("dm_document") doc.setObjectName("my document") doc.link("/cabinet1/folder2") doc.save()
I think you get the idea!
But we don't want to have to create the connect function every time, so copy all the code into a separate file:
from com.documentum.com import DfClientX def connect(docbase, username, password): cx=DfClientX() c =cx.getLocalClient() li =cx.getLoginInfo() li.setUser(username) li.setPassword(password) s = c.newSession(docbase, li) return s
Name it dctm.py (py is the standard python/jython script extension) and save it in the main Jython directory (where you installed jython which in my case is c:\jython2.2.1). Now restart the the jython interpreter and enter the following:
>>> import dctm >>> s = dctm.connect("docbase2","dmadmin","dmadmin")
The import statement pulls in the code from the dctm.py script and makes the function available via the
dctm namespace. Once we have the IDfSession object s we can again do some work:
doc2 = s.newObject("dm_document") doc2.setObjectName("another document") doc2.save()