Google Translator - Custom Service

Note: Prerequisite is to have MESA Developer Studio installed!!!
In MESA Developer Studio SDK, you have access to certain java classes for use in creating custom services and adapters. You can find detailed information about each class in the MESA Studio javadocs,
which are located in the install_dir/install/studiodocs folder of your application installation. The classes are grouped by functional area:
• ASI (Application Server Independence)
• IFC (Integration Framework Collection)
• AFC (Application Framework Collection)
The MESA Developer Studio SDK is designed as an Eclipse plugin and is installed locally on your computer. Use the SDK to create a service, build and export a service package within the Eclipse development environment, then install and test it with the application.
Start MESA Developer Studio SDK
Launch Eclipse >> Window menu >> Open Perspective >> Other >> MESA Developer Studio SDK
If this is the first time you have launched SDK, you are asked to enter licensing information.
• Hostname – the application server name
• Webdav Port - WebDAV port number for your application server
• Name - descriptive name for this instance
• Username - application username
• Password - password for your username
>> Click Finish
Create a new SDK project
New >> Project … >> MESA Developer Studio >> MESA Developer Studio SDK Project

Write the name of the project:




Here is the package and structure created in eclipse:

Add a business logic (2 ways)
- Show Window menu >> Show View >> Other >> General >> Tasks

TODO will appear in the right side in Tasks and you can double click on it and start implementing your code

- In the Package Explorer, under src > com.mypackage, find the folder called yourservicenameImpl.java, and doubleclick it.
You can go to method processData

The code for the project will be displayed in the Eclipse editor. Find the line “// TODO: Start here to implement the service logic.”

We will add a custom code here a little bit later.
First we will add google translator package (google-api-translate-java-0.92.jar) into the path.
Right click in the package >> Build Path >> Configure build path ... >> Libraries tab >> Add External JARs ... add google-api-translate-java-0.92.jar (or any newer version you can find at http://code.google.com/p/google-api-translate-java/ )

Available languages in google translator:

Define parameters:
Package Explorer >> project (e.g. googleTranslator) >> servicedefs >> [service name].xml >> right click … Open With … Service Definition Editor
Creating parameters:
Right click on any and/or every parameter group type and select New Group
Creating variable in any of parameters group:
Right click on a group title and select New Vardef
We chose select HTML type for every language and languauges options, and added 3 languages in Options as you can see below:

Change service label name
Package Explorer >> project (e.g. googleTranslator) >> ui >> properties >> lang >> en [project]_en.properties (e.g. googleTranslator_en.properties)
#
# googleTranslator Language File
#
# SERVICESDK_GUID=MIRJANA.F4bd107b8.12ebe36c477.F8000
googleTranslator.label.MIRJANA.F4bd107b8.12ebe36c477.F8000=googleTranslator
googleTranslator.description.MIRJANA.F4bd107b8.12ebe36c477.F8000=Theserviceimplements...
Import google translator packages into code:
package com.mypackage;
import java.text.MessageFormat;
import com.sterlingcommerce.woodstock.services.IService;
import com.sterlingcommerce.woodstock.services.XLogger;
import com.sterlingcommerce.woodstock.workflow.WorkFlowContext;
import com.sterlingcommerce.woodstock.workflow.WorkFlowException;
import com.google.api.GoogleAPI;
import com.google.api.translate.Language;
import com.google.api.translate.Translate;
Variable declaration:
private String srcDocString = null;
private String sourceLanguage = null;
private String targetLanguage = null;
private String translatedText = null;
private Document document;
private InputStream inputStream;
private OutputStream outputStream;
private BufferedInputStream bis;
private BufferedOutputStream bos;
String chunk = null;
Service code:
// TODO: Start here to implement the service logic
sourceLanguage = wfc.getParm("Source Language").toString();
targetLanguage = wfc.getParm("Target Language").toString();
if (sourceLanguage == null) {
wfc.setBasicStatus(1);
wfc.setAdvancedStatus("Source Language was not chosen. It is a required parameter!");
log.logError("Missing sourceLanguage parameter ...");
thrownew WorkFlowException("Missing parameter");
}
if (targetLanguage == null) {
wfc.setBasicStatus(2);
wfc.setAdvancedStatus("Target Language was not chosen. It is a required parameter!");
log.logError("Missing targetLanguage parameter ...");
thrownew WorkFlowException("Missing parameter");
}
Document srcDoc = wfc.getPrimaryDocument();
if (srcDoc == null) {
wfc.setBasicStatus(3);
wfc.setAdvancedStatus("Primary Document is missing. This service requires the primary Document!");
log.logError("Missing PrimaryDocument ...");
thrownew WorkFlowException("Missing PrimaryDocument");
}
inputStream = srcDoc.getInputStream();
bis = new BufferedInputStream(inputStream);
byte[] buffer = newbyte[1024];
int bytesRead = 0;
//Keep reading from the file while there is any content
//when the end of the stream has been reached, -1 is returned
while ((bytesRead = bis.read(buffer)) != -1) {
//Process the chunk of bytes read
//in this case we just construct a String and print it out
chunk = new String(buffer, 0, bytesRead);
}
System.out.println("MIRJANA GOOGLE TRANSLATION ... chunk --> " + chunk);
GoogleAPI.setHttpReferrer("http://code.google.com/p/google-api-translate-java/");
try {
translatedText = Translate.execute(chunk, Language.valueOf(sourceLanguage), Language.valueOf(targetLanguage));
} catch (Exception ex) {
ex.printStackTrace();
}
document = wfc.newDocument();
outputStream = document.getOutputStream();
bos = new BufferedOutputStream(outputStream);
bos.write(translatedText.getBytes());
System.out.println("MIRJANA GOOGLE TRANSLATION ... translatedText --> " + translatedText);
wfc.setAdvancedStatus("Translated!");
wfc.setBasicStatus(000);
bos.close();
wfc.putPrimaryDocument(document);
Build a service package
>> select the project you want to export in the Package Explorer
>> Right-click and select Export
>> In the Export window, select Mesa Studio > Service Packages as the export destination and
click Next
>> Browse to select the destination directory
>> Click Finish. The service package service name_version.jar is built and placed in the selected package folder/service name/dist/service name folder
The service is now ready to be installed into the application.
Install and run the service in an application test instance
>> MESA Studio perspective >> choose the application instance where the service will be Installed >> Right-click and choose Install Service Package
>> Browse and select the package file. Click Open
>> Click Finish to begin the installation of the service on the application instance
>> After the service package is installed, restart the application instance
>> Log in to the application and ensure that the service definition can be viewed and configured by using the options available from the Deployment > Services > Configuration screen
Service in a BP
- This is what the find command found for all “Google files” in SI system:
[si@lin2 install]$ find . -name *oogle*
./properties/services/googleTranslator.xml
./properties/lang/en/googleTranslator_en.properties
./noapp/deploy/dashboard/webapp/search/images/google.gif
./noapp/deploy/dashboard_OLD/webapp/search/images/google.gif
./installed_data/patch/components/gis/components/neo/exploded_wars/dash
./installed_data/gis/components/neo/exploded_wars/dashboard/search/imag
./installed_data/GoogleTranslator_CustomService
./installed_data/GoogleTranslator_CustomService/ui/properties/lang/en/g
./installed_data/GoogleTranslator_CustomService/servicedefs/googleTrans
./installed_data/GoogleTranslator_CustomService/jars/GoogleTranslator_C
./installed_data/GoogleTranslator_CustomService/jars/GoogleTranslator_C stomService_3000.0.0.jar
./packages/GoogleTranslator_CustomService_3000.0.0.jar
- Removing the services that we do not need any more:
1. Delete all configurations for the adapter you want to delete from the application.
2. Stop GIS.
3. A Directory structure with sub directories will have been created under the [SI_Install_Folder]/installed_data directory.
The directory name is the same as the name of the *.jar file installed. Remove the entire directory structure created.
Example:
rm -r GoogleTranslator_CustomService/
4. Remove the *.jar file from [SI_Install_Folder]/jar/si/version ?!?!
Could not find ‘si’ folder in UNIX/Linux system, but only ‘packages’!!!
Note 1:
- Windows system contains package for custom service in [SI_Install_Folder]/installed_data,
[SI_Install_Folder]/jar/si/[Service_Version] and
[SI_Install_Folder]/packages
- UNIX system contains custom service package in [SI_Install_Folder]/installed_data,
[SI_Install_Folder]/jar/[Service_Name]/[Service_Version] and [SI_Install_Folder]/packages
UNIX: Found the package in jar directly, not under jar/si!!!
/si/si50-2/install/jar/GoogleTranslator_CustomService
Example:
rm GoogleTranslator_CustomService_3000.0.0.jar
Note 2: To be sure where the package is, you can make a note of the path referring to the *.jar file in dynamicclasspth.cfg and dynamicclasspth.cfg.in and remove the *.jar from the specified file system location.
Remove the *.jar file from the directory [SI_Install_Folder]/packages.
5. Remove the entries referring to the *.jar file in dynamicclasspth.cfg and dynamicclasspth.cfg.in
6. Remove the corresponding entry from the GIS database table service_def to avoid future issues with another custom service being imported with the same name.
7. Start GIS again.