User Exit

User Exit examples and explanations
Mirjana's picture

User Exit - DB Interface

1.   Java Code for User Exit

 

package com.mirjana.userexit;

 

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

 

import com.sterlingcommerce.woodstock.translator.util.JDBCService;

 public class DBInterface {

     

      Connection con = null;
      PreparedStatement stm = null;
      ResultSet res = null;
      String result=null;

            public Connection getConnection(String poolName)

      {

            try

            {    
                  Connection con=JDBCService.getConnection(poolName);
                  return con;
            }

            catch (Exception e)

            {
                  e.printStackTrace();
                  return null;
            }

      }

      public PreparedStatement getPreparedStatement(String SQLstatement, Connection con) {

            try{

                  stm = con.prepareStatement(SQLstatement);

                  return stm;

            } catch (Exception e)

            {
                  e.printStackTrace();
                  return null;
            }

      }

      public ResultSet getResultSet(PreparedStatement stm) {

            try{
                  res = stm.executeQuery();
                  return res;

            } catch (Exception e)

            {
                  e.printStackTrace();
                  return null;
            }

      }

      public int getRowCount(ResultSet res, String SQLstatement)

          {

              int i = 0;
              try {
                 
                  stm = con.prepareStatement(SQLstatement);
                  res = stm.executeQuery();
                  res.next();
                  i = res.getInt("CNT");
                  return i;
                   

              } catch (Exception ex){

                  ex.printStackTrace();
                  return i;
              }

       }

      public void executeNext(ResultSet res)

      {

           
        try {
           
            res.next();
           
        } catch (Exception ex){ 
            ex.printStackTrace();
        }

     }

public String getData(ResultSet res, String columnName)

      {
      try {                        
                //res.next();       
                result = res.getString(columnName);            
        } catch (Exception ex){ 
            ex.printStackTrace();
        }

        return result;

      }

      public void closePreparedStatement(PreparedStatement stm) {

            try{
                  stm.close();
            } catch (Exception ex) {

                  ex.printStackTrace();

            }

      }

      public void closeResultSet(ResultSet res) {

           

            try{
                  res.close();
                 
            } catch (Exception ex) {
                  ex.printStackTrace();
            }

        }

            public void closeConnection(Connection con)

      {
            try
            {
                  if (con != null && !con.isClosed())
                  {
                        con.close();
                  }
            }
            catch (Exception e)
            {
                  e.printStackTrace();
            }
      }

            public void freeConn(String poolName,Connection con)

            {
                  try
                  {
                        if (con != null && !con.isClosed())
                        {
                              //con.close();
                              JDBCService.freeConnection(poolName,con);
                        }
                  }
                  catch (Exception e)
                  {
                        e.printStackTrace();
                  }
      }
}

 2.   Pre-Session extended rule

object jdbc_object, connection;

string[100] poolName, table, column, sql_string;

integer i;

object stm, res;

 

sql_string = "select BUILD_NUMBER, PRODUCT_LABEL from si_version where PRODUCT_LABEL = 'SI'";

// Conn object for obtaining connection

jdbc_object = new("com.mirjana.userexit.DBInterface");

connection = jdbc_object.getConnection("mysqlPool");

i = 1;

 3.   Extended rule in field level

string[255] sql_result_pl, sql_result_bn, pl, bn;

sql_result_pl = "a";

sql_result_bn = "b";

 

pl = "PRODUCT_LABEL";

bn = "BUILD_NUMBER";

 

stm = jdbc_object.getPreparedStatement(sql_string,connection);

res = jdbc_object.getResultSet(stm);

jdbc_object.executeNext(res);

sql_result_pl = jdbc_object.getData(res, pl);

sql_result_bn = jdbc_object.getData(res, bn);

 

#product_label = sql_result_pl;

#build_number = sql_result_bn;

 4.   Post-Session extended rule

jdbc_connection.freeConnection(connection) ;

Mirjana's picture

Get and Set Properties on SI Server - User Exit

Get/Set Property on SI through the Map

 
Get/Set property User Exit steps:
 
  1. User Exit code for get/set SI/nonSI properties
  2. Create the class from java code
  3. Create jar package from class
  4. Install SIProperty.jar
  5. Input data - example
  6. Property files used for test – example
  7. Translation Map and extended rule used for test – example
  8. Result - explanation
 
  1. Java code (User Exit) for getting and setting properties in SI property file as well as any other file.
 
  • This java code contains 4 methods:
 
Ø       getProperty() – similar to sci-get-property() function that takes a property value from SI property based on key/name
Ø       setProperty() – similar to sci-set-property() function that sets a property value to SI property based on key/name
Ø       readPropertyFromFile() – read property value from a specific file and for a specific property name
Ø       addPropertyToFile() – set name-value pair into a file
 
·         User Exit java code
 
package com.tis.custom.userexit;
 
import com.sterlingcommerce.woodstock.util.frame.Manager;
import java.util.Properties;
import java.io.*;
 
publicclass SIProperty {
 
     
      public String getProperty(String vendorName, String propName)
      {
            String propValue = Manager.getProperty(vendorName, propName, true);
            System.out.println("SI Custom Property log ... gotten property is: " + propValue);
            return propValue;
      }
     
      public void setProperty(String vendorName, String propName, String propValue)
      {
            Properties props = Manager.getProperties(vendorName);
          if (props == null) {
            props = new Properties();
          }
          props.setProperty(propName, propValue);
          Manager.setVendorProperties(vendorName, props);
          System.out.println("SI Custom Property log ... set property is: " + propValue);
      }
     
      public String readPropertyFromFile(String vendorFileName, String propName)
      {
           
            String propValue = null;
            try {
                  FileInputStream fis = new FileInputStream(vendorFileName);
                  Properties props = new Properties();
            props.load(fis);
                  propValue = props.getProperty(propName);
                  fis.close();
            } catch(IOException e) {
                  e.printStackTrace();
            }
            System.out.println("SI Custom Property log ... gotten property from file: " + propValue);
            return propValue;
      }
     
      public void addPropertyToFile(String vendorFileName, String propName, String propValue)
      {
            try {
                  Properties pro = new Properties();
                  FileOutputStream fos = new FileOutputStream(vendorFileName);
                  pro.setProperty(propName, propValue);
                  pro.store(fos,null);
                  fos.close();
                  } catch (IOException e) {
                        e.printStackTrace();   
                  }
                  System.out.println("SI Custom Property log ... set property to file: " + propValue);
      }
}
 
  1. Compile java code 
·         As java class is created in the following packagecom.tis.custom.userexit, we can save the code in the following folder
 
C:\temp\com\tis\custom\userexit
 
And create the file with the name SIProperty.java
 
·         Command for compiling java code into class is:
 
C:\SterlingCommerce\SI50\install\jdk\bin>javac -classpath pathToPackage\Studio-API.jar c:\temp\com\tis\custom\userexit\SIProperty.java
 
Note:
·         Go to GIS/SI install folder, jdk/bin and use the same java, GIS/SI is using, for compiling java code. In that way we will avoid problems that can happen because of different java versions between compilation and running versions.
·         classpath setting in the command can be different depending on GIS/SI version. I presume, it can be woodstock.jar in GIS 4.x (in SIinstall/jar folder).
 
  1. Create jar package from SIProperty.class 
·         Delete SIProperty.java from C:\temp\com\tis\custom\userexit
·         Change directory up to c:\temp and run the following command to create SIProperty.jar:
 
C:\temp>C:\SterlingCommerce\SI50\install\jdk\bin\jar cvf SIProperty.jar com
 
·   Output of jar command is as follows:
 
added manifest
adding: com/tis/(in = 0) (out= 0)(stored 0%)
adding: com/tis/custom/(in = 0) (out= 0)(stored 0%)
adding: com/tis/custom/userexit/(in = 0) (out= 0)(stored 0%)
adding: com/tis/custom/userexit/SIProperty.class(in = 2108) (out= 982)(deflated
53%)
 
  1. Install SIProperty.jar 
Install SIProperty.jar with install3rdParty.cmd/sh on SI
 
  1. Input Data 
·         Input data is:
 
propName1
 
It can be anything, input data is as simple as possible, just to have anything to test the map.
 
  1. Property Files 
  • There is mirjana.properties file created in \install\properties folder and its content is:
 
propName1=propValu1
propName2=propValu2
keyHome = NikaDino
 
  • BP that will refresh that property so we can access the value:
 
<process name="default">
          <sequence>
                   <operation name="Cache Refresh Service">
                             <participant name="CacheRefreshService"/>
                             <output message="CacheRefreshServiceTypeInputMessage">
                                      <assign to="." from="*"/>
                                      <assign to="cache_name">mirjana</assign>
                                      <assign to="cache_type">properties</assign>
                             </output>
                             <input message="inmsg">
                                      <assign to="." from="*"/>
                             </input>
                   </operation>
          </sequence>
</process>
 
  • There is a file with properties that is not a part of SI installation, but simple file somewhere in the file system, e.g. c:\temp\a.properties, and its content is:
 
fileKey=fileVal
 
  1. Translation Map  
  • The map is simple, and 4 fields in the output will be populated by extended rule written in, that means property values before setting and after setting of SI property as well as non-SI property file
 

 
Extended rule in #filed, at the output side, is:
 
/////////////////////////////////////////////////////////////////////////////////////////////
 
// instantiate an object for SIProperty class
object SIProperty;
SIProperty = new("com.tis.custom.userexit.SIProperty");
 
// read/get property 'keyHome' from mirjana.properties
#propertyBeforeSetting = SIProperty.getProperty("mirjana.properties", "keyHome");
 
// set property 'keyHome' in mirjana.properties, from NikaDino to NEW_VALUE
SIProperty.setProperty("mirjana.properties", "keyHome", "NEW_VALUE");
 
// read/get property 'keyHome' from mirjana.properties after setting a new value
#propertyAfterSetting = SIProperty.getProperty("mirjana.properties", "keyHome");
 
 
 
// read/get property 'fileKey' from a.properties
#propertyFromFileBeforeSetting = SIProperty.readPropertyFromFile("c://temp//a.properties", "fileKey");
 
// set property name 'a' to value 'b'in a.properties
SIProperty.addPropertyToFile("c://temp//a.properties", "a", "b");
 
// read/get property 'a' from a.properties after setting a new value
#propertyFromFileAfterSetting = SIProperty.readPropertyFromFile("c://temp//a.properties", "a");
 
/////////////////////////////////////////////////////////////////////////////////////////////
 
  1. Result - explanation 
Map is run twice.
After running the map for the first and second time, the results are:
 
propValue1             NikaDino               NEW_VALUE              fileVal               b  
propValue1             NEW_VALUE          NEW_VALUE                                      b 
 
Property read from mirjana.property, for key/prop name (keyHome) is NikaDino, but another time it will be NEW_VALUE as property is set like that.
Also, a.properties contains fileKey=fileVal in the beginning but after setting it will contain a=b.
 
Note:
·         Keep in mind that setProperty() method set a value for a specific property name just in memory and after restarting SI, it will not be updated, but one from the property file will be loaded again. It will just live as long as the application (SI) lives.
·         After setting a new property in a file (a.properties), we do not create/append a new property in an existent file, but override it with new one. So this functionallity will always rewrite an old name-value pair with a new one.
 
Mirjana's picture

Google Translator - User Exit, used in Translation Map

Creating User Exit for Google Translator
 
Steps to do:
 
  • Download java api (package) for google translation
  • Install google translation package in SI system
  • Write user exit code
  • Compile and pack user exit code
  • Install user exit in SI system
  • Write the map that will use the user exit
 
1. Download google-api-translate-java-0.92.jar or newer that can be found at
 
 
Note: Be careful with google translator API version and java version used by SI. I think for GIS/SI versions that uses java 1.5 we should use Google Translator 0.92 and SI system that uses java 1.6, we should take Google Translator 0.95!
 
2. Install the jar with install3rdParty.sh/cmd script in SI system
 
SI_Install_Folder/bin/install3rdParty.cmd GoogleTranslator 0_92 -j /path_To_Jar/google-api-translate-java-0.92.jar
 
You will be able to find the jar file in
  • SI_Install_Folder/jar/GoogleTranslator/0_92/
  • Also, you will see the following line in the end of dynamicclasspath.cfg file:
 
 
3. Java code for user exit (GoogleTranslation.java)
 
package google.translation;
 
import com.google.api.GoogleAPI;
import com.google.api.translate.Language;
import com.google.api.translate.Translate;
 
         public class GoogleTranslation {
                   public String trans(String str, String sourceLanguage, String targetLanguage) {
                            String translatedText = new String();
                            GoogleAPI.setHttpReferrer("http://code.google.com/p/google-api-translate-java/");
                            try {
                                      translatedText = Translate.execute(str, Language.valueOf(sourceLanguage), Language.valueOf(targetLanguage));
                                      System.out.println(translatedText);
                                      } catch (Exception ex) {
                                      ex.printStackTrace();
                                      }
                                      return translatedText;
                  
         }
         }
4. Compile GoogleTranslation.java into GoogleTranslation.class
 
Compilation can be done with a proper version of java in eclipse or in the command line.
 
 
SI_Install_Folder/jdk/bin/javac /path_To_Java_Code/GoogleTranslation.java
 
… or …
 
C:\temp>c:\SterlingCommerce\MEFG_43\jdk\bin\javac -classpath c:\temp\google-api-translate-java-0.92.jar GoogleTranslation.java
 
GoogleTranslation.class should be created in the folder in which you have run the command.
 
5. Pack the class file into jar
 
  • Place the class file under package folder (google/translation)
  • Change directory to parent folder of ‘google’
  • SI_Install_Folder/install/jdk/bin/jar cvf GoogleTranslation.jar google
 
GoogleTranslation.class, in package (jar), must be under google\translation path, that means in the package defined in code.
 
 
 
6. Install GoogleTranslation.jar at SI system
 
 
SI_Install_Folder/bin/install3rdParty.cmd UserExit 1_0 -j /path_To_Jar/GoogleTranslation.jar
 
 
After installing google translate api (google-api-translate-java-0.92.jar) and GoogleTranslator.jar package as a User Exit, you can also find the following lines in dynamicclasspath.cfg (SI_Install_Folder/properties folder):
 
VENDOR_JAR=C:\SterlingCommerce\MEFG_43\jar\GoogleTranslator\0_92\google-api-translate-java-0.92.jar
VENDOR_JAR=C:\SterlingCommerce\MEFG_43\jar\UserExit\1_0\GoogleTranslation.jar
 
  • Restart GIS/SI system!!!
 
7. Google translation in Translation map
 
  • Extended rule in a #field:
 
///////////////////////////////////
 
object obj;
String[50] sourceLanguage;
String[50] targetLanguage;
 
obj = new("google.translation.GoogleTranslation");
 
sourceLanguage = "ENGLISH";
targetLanguage = "CROATIAN";
 
#word = obj.trans(#word,sourceLanguage,targetLanguage);
 
///////////////////////////////////
 
 
 
  • Input file:
 
Welcome to the World of Integration.
Wish you a nice day!
 
Different results for translation to CROATIAN, SPANISH and GERMAN languages:
 
  • Result of translation (from ENGLISH to CROATIAN langugae):



Dobro došli u svijet integracije.                                                                   
┼Żelimo vam lijep dan!
 
(For specific Croatian characters I added 0x00 – 0xFF range in X Syntax Token and also set Encodings at both sides of the map to Cp1250 - Windows Eastern European)
 
  • Result of translation (from ENGLISH to SPANISH language):
 
////////////////////////////////
...
sourceLanguage = "ENGLISH";
targetLanguage = "SPANISH";
...
////////////////////////////////
 
Bienvenido al mundo de la integración.                                                             
Deseamos un buen día!                                                                              
 
  • Result of translation (from ENGLISH to GERMAN language):
 
////////////////////////////////
...
sourceLanguage = "ENGLISH";
targetLanguage = "GERMAN";
...
////////////////////////////////
 
Willkommen in der Welt von Integration.                                                            
Wünsche dir einen schönen Tag!                                                                     
 
8. Uninstall/Remove User Exit
 
  • Remove GoogleTranslation.jar from SI_Install_Folder/jar/User Exit/1_0, or remove the whole UserExit folder if there is nothing you need from it any more
  • Remove google-api-translate-java-0.92.jar SI_Install_Folder/jar/GoogleTranslator/0_92 or remove the whole GoogleTranslator folder if you do not need it any more
  • Remove the lines from dynamicclasspath.cfg file that reference to these packages, mentioned in the previous steps
 
Syndicate content