Topic
  • 3 replies
  • Latest Post - ‏2014-06-10T07:59:54Z by Babin_AG
wpc_user
wpc_user
2 Posts

Pinned topic Passing item object between script and JAVAAPI.

‏2014-01-02T06:55:09Z |

Hi All,

   We have a requirement to pass "item" object from Report script to Report Extension point and use the item object to do some modifications.

To achieve this, we have used "setScriptContextValue" to set custom item object in script and retrive it in JavaAPI using getCustomParameter but unfortunately we are getting NULL in JavaAPI code. Below is the sample code:

 

Report Script :
===================================================

var logger = getLogger("TestLog");    
var ctg = getCtgByName("Test Catalog");
var itm = ctg.getCtgItemByPrimaryKey("1234");
itm.setEntryAttrib("Test Primary Spec/Attribute1","TestValue1");

//setting item object below
setScriptContextValue("zaItem",itm);
 

//setting a String variable too.
var tempid = "wpc";
setScriptContextValue("zatempid",tempid);
runJavaApiExtensionPoint("japi:///uploaded_java_classes:com.test.reports.testReportExtension.class");

 

// fetching a value set in JavaAPI
var value = getScriptContextValue("javaVariable");
logger.loggerInfo(" value = "+value);
var hmSaveErrors = itm.saveCtgItem();
logger.loggerInfo("hmSaveErrors = "+hmSaveErrors );

 

Report Extension point Code:
=============================================================

public class testReportExtension implements ReportGenerateFunction{

    @Override
    public void reportGenerate(ReportGenerateFunctionArguments arg0) {
        
        try {
        
            Context ctx = PIMContextFactory.getCurrentContext();

            //now lets invoke the script ctx : item object
            Item zaItem = (Item) arg0.getCustomParameter("zaItem");
            =======Above line gives NullPointer Exception==============
            
            System.out.println(" Item pk = "+zaItem.getPrimaryKey());
            zaItem.setAttributeValue("Test Primary Spec/Attribute2", "TestValue2");


            //now lets invoke the script ctx :String variable
            String zatempid = (String) arg0.getCustomParameter("zatempid");
            =======Above line gives NullPointer Exception==============
            zatempid = zatempid+"Value added in java";
            System.out.println(" java : zatempid = "+zatempid);

            //now defining a variable here to invoke in script ctx
            arg0.setCustomParameter("javaVariable", "javaVariableValue");
            
        }
        catch (Exception e) {
            e.printStackTrace();
            System.out.println("   Exception"+ e.getMessage());
        }
        
    }
}
 

Pls let me know if anyone tried this and was able to do it succesfully. Thanks!!

 

 

  • wpc_user
    wpc_user
    2 Posts
    ACCEPTED ANSWER

    Re: Passing item object between script and JAVAAPI.

    ‏2014-01-25T05:53:47Z  

    Hi,

    You might better to implement a pure java code with simple java object as parameter and result.

    functionhighlightText(name)

    {

    var errMsg = null;

    var newName = name;

    catchError(errMsg)

    {

    var filterStr = createJavaMethod("ru.tvelinvest.syntax.Syntax","highlightText", "java.lang.String");

    newName = runJavaMethod(null, filterStr, name);

    }

    if(errMsg != null)

    {

    var oLogError = getLogger("computel");

    var errMsg = "CommonTools trigger library. 'highlightText' script error: " + errMsg;

    oLogError.loggerError(errMsg);

    }

    return newName;

    }

    or using static method of a class

    ....

    var getDB2XmlString = createJavaMethod("com.ibm.db2.jcc.am.oe","getDB2XmlString");

    var xmlData = resultSet.getColumnAt(10); // XML type object is returned by SQL query

    var xmlString = runJavaMethod(xmlData, getDB2XmlString ); // converting XML object into a string containing XML

    ....

     

     

    Thanks Alex !!

     

    Yeah, we finally had to use Reflection concept to do this. Also checked with IBM and found that very few extension point like PostPreProcessing, sandboxImpl supports passing of item objects seamlessly between Script and JAVAAPI.

  • Alexander_Zinovin
    Alexander_Zinovin
    113 Posts

    Re: Passing item object between script and JAVAAPI.

    ‏2014-01-23T05:15:55Z  

    Hi,

    You might better to implement a pure java code with simple java object as parameter and result.

    functionhighlightText(name)

    {

    var errMsg = null;

    var newName = name;

    catchError(errMsg)

    {

    var filterStr = createJavaMethod("ru.tvelinvest.syntax.Syntax","highlightText", "java.lang.String");

    newName = runJavaMethod(null, filterStr, name);

    }

    if(errMsg != null)

    {

    var oLogError = getLogger("computel");

    var errMsg = "CommonTools trigger library. 'highlightText' script error: " + errMsg;

    oLogError.loggerError(errMsg);

    }

    return newName;

    }

    or using static method of a class

    ....

    var getDB2XmlString = createJavaMethod("com.ibm.db2.jcc.am.oe","getDB2XmlString");

    var xmlData = resultSet.getColumnAt(10); // XML type object is returned by SQL query

    var xmlString = runJavaMethod(xmlData, getDB2XmlString ); // converting XML object into a string containing XML

    ....

     

     

  • wpc_user
    wpc_user
    2 Posts

    Re: Passing item object between script and JAVAAPI.

    ‏2014-01-25T05:53:47Z  

    Hi,

    You might better to implement a pure java code with simple java object as parameter and result.

    functionhighlightText(name)

    {

    var errMsg = null;

    var newName = name;

    catchError(errMsg)

    {

    var filterStr = createJavaMethod("ru.tvelinvest.syntax.Syntax","highlightText", "java.lang.String");

    newName = runJavaMethod(null, filterStr, name);

    }

    if(errMsg != null)

    {

    var oLogError = getLogger("computel");

    var errMsg = "CommonTools trigger library. 'highlightText' script error: " + errMsg;

    oLogError.loggerError(errMsg);

    }

    return newName;

    }

    or using static method of a class

    ....

    var getDB2XmlString = createJavaMethod("com.ibm.db2.jcc.am.oe","getDB2XmlString");

    var xmlData = resultSet.getColumnAt(10); // XML type object is returned by SQL query

    var xmlString = runJavaMethod(xmlData, getDB2XmlString ); // converting XML object into a string containing XML

    ....

     

     

    Thanks Alex !!

     

    Yeah, we finally had to use Reflection concept to do this. Also checked with IBM and found that very few extension point like PostPreProcessing, sandboxImpl supports passing of item objects seamlessly between Script and JAVAAPI.

  • Babin_AG
    Babin_AG
    10 Posts

    Re: Passing item object between script and JAVAAPI.

    ‏2014-06-10T07:59:54Z  

    There exists a way to pass some custom object through execution thread.

    For example, at import extensionpoint I want to say something to postSave extensionpoint of item being saved by that import.

    That means, a custom object must be attached to execution thread, that remains the same within execution chain (import->entrybuild->preprocess->rules->postprocess->postsave)

    java.lang.ThreadLocal to the resque!

    1. Create custom message with private static ThreadLocal

    2. Put it on appsvr classpath

    3. Use it! One can set value at import and read value at postsave.