Download (all) Scripts from Maximo / ICD to your Eclipse Project

In one of my last blogs I showed a very nice way how to upload Jython scripts from Eclipse to your Maximo / ICD environment. Now what about the situation that you already have a Maximo / ICD environment with tons of scripts in it? In this blog I will show you a simple way how you can include all these scripts to your Eclipse project within 15 minutes. This will also include the first comment lines needed for the later upload.

To achieve our goal the following steps are needed:

  • Setup an application export in the Autoscript application
  • Download a XML File including all scripts
  • Run a Jython script to extract all the unique scripts

Setup an application export in the Autoscript application

The application export will be configured using the “Object Structures” application. Launch:

Go To > Integration > Object Structures

In the Object Structures application search for the “MXSCRIPT” Object Structure and open it.

Selection_018

After opening select an action:

Select Action > Add/Modify Application Export Support

 

Menu_020

In the dialog box create a new row in the table using the values shown here:

Selection_021

Press OK. The export support now has been created. Next step is to grant the security rights to the user group which should use the tool. In my example I will grant the rights to the maxadmin group. Goto the following Menu:

Go To > Security > Security Groups

Now perform the following steps:

  1. Search and open the security group you want to grant the rights to.
  2. Change to the “Applications” Tab.
  3. Filter for the Application “Automation Scripts” and select the record.
  4. In the Options for Automation Scripts section enable the option “Application Export”.Selection_022
  5. Save the Security Group.
  6. Logoff and Logon again to Maximo / ICD. Be sure that all your user sessions are successfully logged of – otherwise the settings will not be activated.

Download a XML File including all scripts

To download a XML File which includes all scripts in your system you have to go to the Automation Scripts application.

Go To > System Configuration > Plattform Configuration > Automation Scripts

If the previous step worked correctly you should now see a new Icon in the iconbar Selection_023for the application export. Before you press this button you have to search for the scripts to export. If all scripts should be exported just press enter in the search field. Now press the icon.

In the “Application Export” Dialog you just can take all settings and press “OK”.

Selection_024

The XML File now will be downloaded to the download directory of your browser on your local PC.

Run a Jython script to extract all the unique scripts

First of all you have to have a project setup in Eclipse where you would like to store your downloaded scripts. This chapter requires a complete project-setup using Eclipse, Jython and PyDev like described in this blog. Copy the downloaded XML file to this directory and name it scripts.xml.

Now create a Jython script with the following code:

#AUTOSCRIPT:EXPORTSCRIPTS
#DESCRIPTION:Script to export all Jython Scripts on a Server
#LOGLEVEL:ERROR

from java.io import File
from org.jdom.input import SAXBuilder

xmlFile = File("scripts.xml")
builder = SAXBuilder()

document = builder.build(xmlFile)
wrap = document.getRootElement()
ns = wrap.getNamespace()
rootNode = wrap.getChild("MXSCRIPTSet", ns)

liste = rootNode.getChildren("AUTOSCRIPT",ns)
for i in range(0,liste.size()):
    node = liste.get(i)
    scriptName = node.getChildText("AUTOSCRIPT",ns)
    scriptDesc = node.getChildText("DESCRIPTION",ns)
    scriptVersion = node.getChildText("VERSION",ns)
    scriptDebug = node.getChildText("LOGLEVEL",ns)
    scriptCode = node.getChildText("SOURCE",ns).encode('utf-8')
    
    scriptFile = open(scriptName + ".py","w")
    scriptFile.write("#AUTOSCRIPT: " + scriptName + "\n")
    scriptFile.write("#DESCRIPTION:" + scriptDesc + "\n")
    scriptFile.write("#VERSION:    " + scriptVersion + "\n")
    scriptFile.write("#LOGLEVEL:   " + scriptDebug + "\n")
    scriptFile.write(scriptCode)
    scriptFile.close()

This scripts requires the Java JDOM Class. Please ensure that the jdom.jar file is included in your PYTHONPATH. You can check this by right-click on your Eclipse project and select the “Properties” Menu. Check under “PyDev – PYTHONPATH” if jdom.jar is existent. If not use the “Add zip/jar/egg” Button to add it.

Selection_025

Remark: If you don’t have a local version of jdom.jar you can find one on your Maximo / ICD Server in the following directory: %WEBSPHERE _DIR%\AppServer\profiles\ctgAppSrv01\installedApps\ctgCell01\MAXIMO.ear\lib

Everything is prepared now. You can run the Script now from Eclipse using the Run Icon Arrow  Selection_009 and then the following menu:

Menu_026

Hopefully after a refresh of your project/folder (F5) you will see a bunch of new scripts! Congratulations!

Deleting Mbo’s

So far we know how to insert, update and view our Mbo records, so the next step is to delete a record.

You delete Mbo’s by calling the delete() method. This method removes the current Mbo from an MboSet. Basically the Mbo is not delete when you call the delete method it is more over marked for deletion. The real delete operation is executed when the transaction is commit to the database. In general this is the save operation on the MboSet.

Here comes an example which deletes a specifc Workorder from the Workorder MboSet:

    # Example: Delete a specific Mbo
    woset = session.getMboSet('WORKORDER')
    woset.setWhere("WONUM = '2009'")
    woset.reset()
    wo = woset.moveFirst()
    
    if wo is not None:   
        # Mark Mbo for deletion
        wo.delete()

        # Real delete is done at this point
        woset.save()

Using field Flags to set attribute content

Each attribute in Maximo / ICD can have several Validation Java classes and Jython scripts associated which will trigger when a new value for an attribute is set. Field flags can control some of the conditions under which the attributes can be modified and prevent execution of that classes / scripts.

The following field Flags are commonly used:

  • NOACCESSCHECK is used to update the value even if the attribute is marked readonly
  • NOVALIDATION supressess checking of the value. Be sure you know what you do, because you will prevent the business logic from checking for a valid value.
  • DELAYVALIDATION does not perform any validation when the attribute is modified. The validation takes place on the save action. This can be useful if the validity of an attribute depends on other attributes.
  • NOACTION is used to bypass the execution of all business rules. Use it carefully!
  • NOVALIDATION_AND_NOACTION is a combination of the NOVALIDATION and NOACTION flag.

Now how do we use these flags in Jython? They are used in the setValue() Method as a third parameter. So sometimes you will see scripts like this:

itemMbo.setValue(description", "Hello World!", 1L)

In this 1L stands for NOVALIDATION and it is bad practise to write it in a numeric format. How about this style?

from psdi.mbo import MboConstants
itemMbo.setValue("description", "Hello World!", MboConstants.NOVALIDATION)
companyMbo.setValue("company", "COM1", MboConstants.NOVALIDATION_AND_NOACTION)

If multiple field flags should be combined for a field they could be combined by a bitwise or operation. See this example:

from psdi.mbo import MboConstants
itemMbo.setValue("description", "Hello World!", MboConstants.NOVALIDATION | MboConstants.NOACCESSCHECK)

So far we always used the setValue method to directly manipulate the field flag while we are setting a value to a field. In some cases it is also useful to just set the field flag without applying a new value. This can be done with the setFieldFlag method. You can set and remove certain flags from a field. Mostly taken is this for the read only state:

from psdi.mbo import MboConstants
itemMbo.setFieldFlag("description", MboConstants.READONLY,False)
itemMbo.setValue("description", "Hello World!")
itemMbo.setFieldFlag("description", MboConstants.READONLY,True)

The setFieldFlag method can also take a list of fields as a parameter, if you would like to modify a bunch of fields:

from psdi.mbo import MboConstants
fields = ['DESCRIPTION','ITEMTYPE', 'ORDERUNIT']
itemMbo.setFieldFlag(fields, MboConstants.READONLY,False)

Upload Jython Scripts from Eclipse to Maximo/ICD

In this post I would like to introduce a cool feature to upload your Jython scripts developed in Eclipse directly to Maximo/ICD without the need to jump to the scripting application and doing some copy/paste actions. To Archive this objective we will utilize the Integration Framework and will write a small Jython script to be called from Eclipse and utilizing this interface. I will show you step by step the different configuration steps.

This is a quit complex configuration to do and there are a lot of places to introduce errors so that the complete system will not work. Please post me your experiences with the configuration as a comment so I can improve this article over time.

Prerequisites

What are the prerequisites for our project:

  1. An Eclipse Installation with PyDev integrated where you develop your Jython Scripts
  2. A local Jython 2.x installation (which should normally already be in place). Please consider that we need a full installation and it is not enough to just have a copy of the jython.jar file since we need some standard Jython libraries for our project.
  3. Access to a Maximo/ICD System

The solution currently only has been tested with local authorization. The Upload script might need slight modifications for LDAP authorization (comments welcome :-).

The solution currently only supports http protocol. If you are using https the upload script might need some modifications.

Preparations Integration Framework in Maximo/ICD

Create a new Object Structure

We need to create an own Object Structure based on the existing MXScript Structure.

  1. Go to menu Integration → Object Structure and search the MXScript Object Structure.
  2. Duplicate the Object Structure (Select Action → Duplicate Object Structure)
    jythoneclupl1
  3. The new Object Structure should be called JYSCRIPT unless you want to edit the upload script later on.
  4. Remove the Source Object „AUTOSCRIPT/VARSThe configuration for the duplicated Object Structure should now look like this:jyecli02
  5. Select Action → Exclude/Include Fields
    Exclude all fields except the following ones:
    • AUTOSCRIPT
    • DESCRIPTION
    • LOGLEVEL
    • SCRIPTLANGUAGE
    • SOURCE
    • VERSION

Create an Enterprise Service

A new Enterprise Service must be created.

  1. Go to menu Integration → Enterprise Services
  2. Create a new Record and fill in the following fields only:
    jyecli03
  3. Save the Enterprise Services

Create an External System

A new External System must be created by the following steps:

  1. Go to menu Integration → External Systems
  2. Create a new Record and fill in the System Tab with the following values:
    jyecli04
  3. Be sure all fields are filled in as shown
  4. Select the tab “Enterprise Services” and create a connection to the previously created Enterprise service. For this select the “New Row” button. Fill in the new line as follows:
    jyecli05
  5. Be sure that the enabled box is checked.
  6. Save the External System. The configuration on the Maximo/ICD side is now finished.

 Configuration of the Eclipse Side

The configuration of Eclipse consist of two steps:

  • Prepare Upload Script
  • Create a “Run entry” in the Run configuration.

Prepare Upload Script

Take the following script and store it in a place of a Eclipse project. The project should have a valid Jython Runtime configured. Please have a look to this article for proper Eclipse setup if you are unsure.

#-------------------------------------------------------------
# AUTOSCRIPT:         UPLOADSCRIPT
# DESCRIPTION:        Script to upload Jython Scripts from
#                     Eclipse to Maximo / ICD
# AUTHOR:             Matthias Stroske
# Created             25.11.15
# VERSION:            1.0
# COPYRIGHT:          (c) Matthias Stroske
# MAXIMO/ICD Version: 7.5 and higher
#-------------------------------------------------------------
# History of Changes
#
# Ver  Date      Name               Description
# 1.0  01.10.15  Matthias Stroske   Initial Version
# 1.1  25.11.15  Matthias Stroske   Improved Parameter Handling
#
#-------------------------------------------------------------
 
import sys
import httplib
import base64
import string
import re
from xml.sax.saxutils import escape
 
def main():
    if len(sys.argv) < 5:
        print "USAGE: UploadScript.py <inDatei> <host> <user> <password>"
        sys.exit()
 
    inDatei = sys.argv[1]
    host = sys.argv[2]
    user = sys.argv[3]
    password = sys.argv[4]
     
    # Set Defaults as far as possible
    scriptName = None
    scriptDescription = None
    logLevel = "ERROR"
    version = "1.0"
 
    httpUrl = "http://" + host + "/meaweb/es/JYSCRIPT/JYSCRIPT"
 
    try:
        f = open(inDatei)
        fileContent = f.read()
    except Exception,ex:
        print ("Error reading the input file!")
        print ("Exception:" + str(ex))
    else:
        f.close
 
    firstLines = fileContent.split("\n", 15)
    for line in firstLines:
        matchObj = re.match(r'^#\s*DESCRIPTION:(.*)',line, re.I)
        if matchObj:
            scriptDescription = matchObj.group(1)
        matchObj = re.match(r'^#\s*AUTOSCRIPT:(.*)', line, re.I)
        if matchObj:
            scriptName = matchObj.group(1)
        matchObj = re.match(r'^#\s*LOGLEVEL:(.*)', line, re.I)
        if matchObj:
            logLevel = matchObj.group(1)
        # Simple Style
        matchObj = re.match(r'^#\s*VERSION:(.*)', line, re.I)
        if matchObj:
            version = matchObj.group(1)
        # CVS Style
        matchObj = re.match(r'^#\s*Revision:(.*)', line, re.I)
        if matchObj:
            version = matchObj.group(1)
             
 
    if scriptName is None or scriptDescription is None:
        print "ScriptName and ScriptDescription have to be specified in first Script Lines!"
        sys.exit()
 
    print "Script Name       = " + scriptName.strip()
    print "DESCRIPTION       = " + scriptDescription.strip()
    print "LOGLEVEL          = " + logLevel.strip()
    print "VERSION           = " + version.strip()
          
    message = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
    message += "<SyncJYSCRIPT xmlns=\"http://www.ibm.com/maximo\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">\n"
    message += "<JYSCRIPTSet>\n"
    message += "<AUTOSCRIPT>\n"
    message += "<AUTOSCRIPT>" + scriptName.strip() + "</AUTOSCRIPT>\n"
    message += "<DESCRIPTION>" + scriptDescription.strip() + "</DESCRIPTION>\n"
    message += "<LOGLEVEL>" + logLevel.strip() + "</LOGLEVEL>\n"
    message += "<SCRIPTLANGUAGE>jython</SCRIPTLANGUAGE>\n"
    message += "<VERSION>" + version.strip() + "</VERSION>\n"
    message += "<SOURCE>\n"
    message += escape(fileContent)
    #message += "pass\n"
    message += "</SOURCE>\n"
    message += "</AUTOSCRIPT>\n"
    message += "</JYSCRIPTSet>\n"
    message += "</SyncJYSCRIPT>\n"
    message += "\n"
 
    contentType = "application/soap+xml;charset=UTF-8;action=\"urn:processDocument\""
    auth = 'Basic ' + string.strip(base64.encodestring( user + ':' + password))
     
    webservice = httplib.HTTPConnection(host)
    #webservice.set_debuglevel(5) 
    headers = {"Content-type": contentType, "Content-Length": len(message), "Authorization": auth}
    webservice.request("POST", httpUrl, message, headers)
             
    # get the response
    res = webservice.getresponse()
     
    if res.status == 200:
        print "Script upload successful!"
    else:
        print "Script upload completed with RC = " + str(res.status)
     
    webservice.close()
    return
      
if __name__ == "__main__":
    main()

Eclipse External Tools configuration

To run the script in context of Eclipse you need to do an additional configuration step in Eclipse.

Find the following Icon in the Eclipse iconbar:   Selection_004

Select the little arrow beside the icon to expand the menu and select “External Tools Configurations…”

Menu_016

In the following Window fill in the Head section with the Name of the configuration.

Continue to fill in the  Main tab with the following values:

  • Location: The location of your locally installed Jython interpreter.
  • Working Directory: should be ${workspace_loc:/<ProjectName}
  • Arguments: 5 Arguments for the Jython interpreter to run.
    1. Name of Jython Upload Script: e.G. ${workspace_log:/Blog/UloadScript.py}
    2. Name of the Script to be uploaded: Best we can specify here is always: ${workspace_loc}${selected_resource_path}
    3. Maximo / ICD Host: IP Adress or Hostname of the Maximo Server
    4. Username in Maximo / ICD
    5. Password in Maximo / ICD  *** yes I know it is not good to store the PW in clear-text. With little modifications you could store it base64 encoded here if you modify the Jython script slightly.

Selection_017

Preparing your script and using the new tool

Preparing your script

To use the new tool your script must follow a little convention. The first 15 lines must include relevant information which are required to synchronize the script with Maximo/ICD.

#AUTOSCRIPT: UPLOADSCRIPT
#DESCRIPTION: PYTHON UPLOAD SCRIPT 2
#LOGLEVEL: ERROR
#VERSION: 1.0

This are fields which map to the appropriate fields in Maximo/ICD. The AUTOSCRIPT field is the Name of the Autoscript in Maximo/ICD. With the upload you can create new automation scripts, but you can not create or modify any launchpoints. When you change the DESCRIPTION,VERSION or LOGLEVEL field later on these fields are modified in Maximo/ICD.

I have created a base template for your code, which can be found in this blog.

Using the new Tool

To use the tool we assume, that you are now editing your custom Maximo/ICD Jython script with the information lines included and some Jython Code. From this place you just need to run the Upload Script by Clicking the Run external Symbol in the iconbar again. Selection_004 A script with the name you have chosen during configuration should appear in the drop down list.

After some second you will hopefully get a response “Script upload successful”  in the Console View of Eclipse. Otherwise you’ll get an error with the http Return Code.

Selection_046

I hope the solution will work in your environment!

Implicit launchpoint Variables

Implicit launchpoint variables are predefined if a script runs in the context of ICD (not if you are using the RMI Interface).

Overview of Implicit Variables

The following list describes the most commonly used variables:

Input Variables (incoming to the script):

mbo – The current Mbo in the context of the script execution. This is an object.
mboname – The name of the current Mbo in context of the script as a string.
app – Name of the ICD application which initiated the script execution
user – Name of the user whose action initiated the script execution.
interactive – Is true if script is executed interactive via UI and false if executed in background (e.g. scheduler, integration framework).
scriptName – name of the script
launchPoint – name of the launch point
action – Only for action launchpoints the name of the action that executed the script.
onAdd – Boolean variable. Is True if the Mbo in the script is added and a new Mbo (not yet saved). Very useful for conditional launchpoints, but may also be useful for Object and Attribute launchpoints.
onUpdate – Boolean variable indicating that related Mbo is being updated.
onDelete – Boolean variable indicating that related Mbo is marked for deletion.
wfinstance – Object of type psdi.workflow.WFInstance that indicates the current workflow from which the action was started. This is only valid for action launchpoints if the action is launched from a workflow.

Output Variables (Script provides them back to ICD)

errorkey – For throwing MXExceptions from the script
errorgroup – For throwing MXExceptions from the script
params – For throwing MXExceptions from the script. This is a Array of Strings
evalresult – evaluation result of the Condition Launch point

Usage patterns

When using implicit variables in your script you should first care about the fact that the variables are nowhere defined and therefor eclipse will throw you an error on the first usage. Please have a look to the “Undefined Variables” Section in this articel to solve the issue.

Perform different actions based on the Launchpoint executing the script

A Jython script could have more than one launchpoint to start its execution. Sometimes the script needs to perform different kind of actions depending on the launchpoint. Another use case for this pattern is to assign different values to variables based on the launchpoint to omit the usage of launchpoint variables. A sample for this is shown in the article “Tips for using Eclipse with Jython” in the section “Prevent Using Script In-/Out Variables”

Execute script actions only if running from GUI

It is sometimes a requirement that a script behaves differently if it is called from a User GUI Interaction or from a system interaction like a crontask, escalation or an Integration Framework call. The following pattern can solve this issue:

interactive = interactive # @UndefinedVariable

if interactive == True:
# Things to do if script is running in user Context
else:
# Things to do if script is called by Crontask, MIF, ...
.

Execute different script actions depending on the ICD Application

Sometimes it is required that a script executes different paths depending on the Maximo application it is executed from. The code pattern is quit simple in that case:

app = app  # @UndefinedVariable

if app == "ASSET":
    # Do actions if called from asset Application
if app == "ITEM":
    # Do actions if called from item Application

Throwing an exception from a script

The usage of the errorkey/errorgroup/params variables is shown in this article.