Using Java Classes from Jython Scripts in FDMEE (formatting dates)

Hi all,

I recently got an inquiry regarding the use of the datetime module from a Jython import script in FDMEE.
In my last post I showed how import scripts and custom/event script use a different version of Jython. The datetime module was introduced in Jython 2.3 so our colleague was getting an issue when trying to import it from the import script (which actually runs 2.2.1 as we saw)

Besides any workaround to update sys.path with new path so we can import and use the module (I'm still testing it...), I would like to encourage you to use Java classes from your FDMEE scripts when needed.
Jython is the implementation of Python for Java so one of its main pros is that you can use Java classes from Jython scripts.

How can this help us? This is a very good thing for developers as you can reuse Java libraries to perform complex operations. Sometimes alternative solutions using Jython may not be available due to limitations in version used by FDMEE as it actually happens with our today's example: datetime

Let's say that I would like to import the current date in ISO 8601 format in my description field (YYYY-MM-DD)
If I have a look to what I can use in Jython, we find the datetime module. Our script would be simply:
But, as we already showed, our import script cannot find the module and therefore an error is raised when importing it:
We can test same function in Eclipse using Jython 2.5.1 (same as custom/event scripts) and we get the expected result:

Using Java Classes to get formatted date
Not working so let's try another approach...using Java Classes!
Which classes? just bit of googling...
  • java.text.SimpleDateFormat
  • java.util.Date
  • java.lang.Exception
How do we use them from our import script in Jython?

I like using aliases for classes imported as it simplifies my code. As Jython modules, we first need to import the Java classes, and then adjust the code to Jython syntax (it's not Java syntax but Java used from Jython).

For example, we don't use new operator to create object instance of our class like we do in Java or use types when declaring variables. To create a new object of SimpleDateFormat class using pattern "yyyy-MM-dd" we do:
df = java.text.SimpleDateFormat(pattern)
while in Java we would do:
SimpleDateFormat df = new SimpleDateFormat(pattern)

The import script to return current date formatted as "YYYY-MM-DD" could be implemented as follows:
We just need to assign our import script to Description field (Desc2 in my case):
And run the import! 

As you can see, using Java classes from our scripts (not only import but event and custom) can take our FDMEE application to the next level. If you have issues with using Jython modules available in other Jython versions rather than instance used by FDMEE, this approach can be a good option. It can even perform better.
Will it always the best approach? not really, you should take into consideration that FDMEE currently runs on Java Platform 1.6.0_37. I recently had a complex requirement for importing data from database with Chinese Collation which is not supported in Java 1.6 but in Java 1.7. But as I say, everything has solution but death :-)

Enjoy!

0 Response to "Using Java Classes from Jython Scripts in FDMEE (formatting dates)"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel