The Embed Java Files

The Embed Java Input

The input file of this workflow is the same as in the previous one: the tutorial_EmbedScript.json. Here you can find all the information about the document and the pricing data. The document part contains a "script" field. This script is called by the workflow.

The Embed Java Class

To use Java classes in the editor, you first need to create the package where your classes are located as.

To create a Java package
  1. Browse to /src/main/java/.
  2. Right-click java in the file tree.
  3. Select New > Java Package.
  4. Enter the name of the package: tutorial_package, and then click OK.
To create a Java class
  1. Browse the the Java package where you want to add the class. For example: src/main/java/tutorial_package
  2. Right-click the package name in the file tree.
  3. Select New > Java class.
  4. Enter the name of the class, and then click OK.
Fig. 85: Java class location.

Fig. 85: Java class location.

The content of the Tutorial_class.java file is displayed below:

package tutorial_package;
 
import java.io.IOException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.trmsys.fpp.api.result.*;
import com.trmsys.fpp.api.document.*;
import com.trmsys.fpp.api.rest.ProcessingData;
import java.util.Map;
import java.util.List;
 
 
public class Tutorial_class {
    
    public static ObjectMapper mapper = new ObjectMapper();
    public static ArrayNode tradeNode;
    public static ObjectNode pricingDataNode;
    public static ProcessingData pricingData;    
    
    public static void init(String value) throws IOException {
        // Read value, put it in an valueNode node
        final ObjectNode valueNode = (ObjectNode) mapper.readTree(value);
        // Extract the document and pricingData part from node
        tradeNode = (ArrayNode) valueNode.get("documents");
        pricingDataNode = (ObjectNode) valueNode.get("pricingData");
        // JSON converted to ProcessingData Java object
        pricingData = mapper.readValue(pricingDataNode.toString(), ProcessingData.class);    
    }    
         
    public static String outputResults(Map<String, Result> res, List<JsonDocument> doc) throws IOException {
        final ArrayNode results = mapper.createArrayNode();
 
        for (final JsonDocument it : doc) {
            ObjectNode docresults = mapper.createObjectNode();
            docresults.put("id", it.getId());
            final Result result = res.get(it.getId());
            docresults.put("type", result.type().name());
            docresults.set("values", mapper.readTree(result.toString()));
            results.add(docresults);
        }
        return mapper.writeValueAsString(results);
    }
    
}

The Embed Script Java workflow and box

In the workflow, the tutorial_EmbedJava box is called with the request input and the results output.

Fig. 86: The tutorial_EmbedJava Workflow.

Fig. 86: The tutorial_EmbedJava Workflow.

The content of the tutorial_EmbedJava box is displayed below:

import tutorial_package.Tutorial_class;
 
// Inputs management

def request(value) {
    Tutorial_class.init(value)
    }
 
// Create the dataManager from the pricingData
DataManager dataManager = DataManagerRestUtils.create(Tutorial_class.pricingData)  
 
// Register the documents assigning them a script
documents = []
Tutorial_class.tradeNode.each {
    def id = it.get("dealStamp").asText()
    documents.add(new JsonDocument(it.get("script").asText(), it, id)) // Select the script
}
 
// Run the execution (returns a non ordered map of results)
resultById = engine().buildSession(dataManager)
        .process(documents)
        .collect()  
        
// output results
output("results",Tutorial_class.outputResults(resultById, documents))
close("results") 

Several functions from the FPP Javadoc are leveraged.