Customer Portal

CustomJavaTransformer Usage Example

Comments 1

  • Avatar
    Ladislav Szabo
    0
    Comment actions Permalink

    Hi Alan,

     

    When working with the Custom Java Components in CloverDX, besides studying the documentation, it is also good to check the example code within the component's algorithm property. 
    Anyway, let's take it from the top. As you already know, CustomJavaTransformer is a more specific CustomJavaComponent focused on transforming data. Meaning that we expect the component to read data from the input port(s), transform data, and write the data to the output port(s).
    Thus, there are four essential steps to create a simple transformation:

    1. prepare record variables for reading and writing from/to input/output ports
    2. reading data from the input port
    3. transforming data
    4. writing data to output port

     

    To start coding, open the component > algorithm > Switch to Java editor > set the name for the Java class and the package. We implement the transformation in the execute function of the Java class.

    1| Declare variables of DataRecerd class to store input/output records 

    /** Record prepared for reading from input port 0 */
    DataRecord inRecord = inRecords[0];
    /** Record prepared for writing to output port 0 */
    DataRecord outRecord = outRecords[0];

     

    2| We can read data from the input port with a simple while cycle. If we want each input record to create one output record, steps no.2 & no.3 have to be encapsulated within this cycle

    while ((inRecord = readRecordFromPort(0)) != null) {
    }

     

    3| Now, the transform part. To keep the example simple, let's say we are working with integer numbers, and want to increment each incoming number by one.

    // get the value from the record field "myNumber"
    Integer valueFromRecord = (Integer) inRecord.getField("myNumber").getValue();
    // incrementing the value
    valueFromRecord++;
    // set the new value of myNumber field for the output record
    outRecord.getField("myNumber").setValue(valueFromRecord);

     

     

    4| Lastly, we map the record to the output port

    writeRecordToPort(0, outRecord);

     

    Final:

    @Override
    public void execute() {
    	/** Record prepared for reading from input port 0 */
    	DataRecord inRecord = inRecords[0];
    	/** Record prepared for writing to output port 0 */
    	DataRecord outRecord = outRecords[0];
    	
    	while ((inRecord = readRecordFromPort(0)) != null) {
    		// get the value from the record field "myNumber"
    		Integer valueFromRecord = (Integer) inRecord.getField("myNumber").getValue();     
    		// incrementing the value
    		valueFromRecord++;
    		// set the new value of myNumber field for the output record
    		outRecord.getField("myNumber").setValue(valueFromRecord);
    		
    		writeRecordToPort(0, outRecord);
    	}
    }

     

    I hope this helps,
    Ladislav.

Please sign in to leave a comment.