Skip to main content

Notice: this Wiki will be going read only early in 2024 and edits will no longer be possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.

Jump to: navigation, search

Difference between revisions of "Stardust/Knowledge Base/Integration/Camel/Json File To Structured Data"

(New page: == Requirements == #Need to write a process which should be triggered when a file arrives at a particular folder. #It should pick the file content (which happens to contain JSON Strings...)
 
Line 22: Line 22:
 
{"Name":"T2","Relation":"TR2","Age":43}]}</source>  
 
{"Name":"T2","Relation":"TR2","Age":43}]}</source>  
  
 +
<br>
  
 +
To achieve this we will be using IPP's Camel Trigger to monitor the arrival of the file.
  
To achieve this we will be using IPP's Camel Trigger to monitor the arrival of the file. We will also use Camel Application Type to route the request to the required beans (in this case DataToJSON). The aim to use Camel Application Type is to enable extendibility of the solution to be routed to other Camel/IPP supported endpoints. The model:
+
We will also use Camel Application Type to route the request to the required beans (in this case DataToJSON).  
  
 +
The aim to use Camel Application Type is to enable the solution to be routed to other Camel/IPP supported endpoints as per need.
  
 +
The model - Define Receive Incoming File Camel Trigger as shown in the image below.
 +
 +
<br>
 +
 +
[[Image:ModelJSON.JPG]]<br>
 +
 +
 +
Define 2 Camel Producer Application (FileTOObject and convertToJSON in this case) using that&nbsp;we will convert JSON to Object and back. Associate these to activities <u>Convert File into IPP Data</u> and <u>Convert Data To JSON.</u>
 +
 +
From Applications, choose Camel Producer Application and in its General Tab define the bean like:
 +
 +
<source lang="java"><bean id="dataToJSON" class="com.sungard.cm.invo.ipp.training.DataToJSON"/>
 +
</source>
 +
 +
and it is Producer Route Tab define route like (for&nbsp;Convert File into IPP Data)
 +
 +
<source lang="java"><to uri="bean:dataToJSON?method=readBack"/>
 +
</source>
 +
 +
This will call the readBack() method of the bean dataToJSON, which is reading from JSON string and converting it into an IPP data type (Map) called Employee. and for (Convert Data To JSON)
 +
 +
<source lang="java"><to uri="bean:dataToJSON?method=enrich"/>
 +
</source>
 +
 +
This will call the enrich() method of the bean dataToJSON, which is reading from IPP Data called Employee and converting it to JSON string.
 +
 +
DataToJSON class Code: <source lang="java">
 +
package com.sungard.cm.invo.ipp.training;
 +
import java.io.IOException;
 +
import java.util.ArrayList;
 +
import java.util.HashMap;
 +
import java.util.List;
 +
import java.util.ListIterator;
 +
import java.util.Map;
 +
 +
import org.codehaus.jackson.JsonGenerationException;
 +
import org.codehaus.jackson.map.JsonMappingException;
 +
import org.codehaus.jackson.map.ObjectMapper;
 +
import org.codehaus.jackson.type.TypeReference;
 +
 +
import com.google.gson.Gson;
 +
 +
//import com.google.gson.Gson;
 +
public class DataToJSON<E> {
 +
 +
public String enrich(Map data) {
 +
System.out.println("Incoming Data value: "+data);
 +
//Gson gson = new Gson();
 +
ObjectMapper objectMapper = new ObjectMapper();
 +
 +
String returnData = null;
 +
try {
 +
returnData = objectMapper.writeValueAsString(data);
 +
} catch (JsonGenerationException e) {
 +
// TODO Auto-generated catch block
 +
e.printStackTrace();
 +
} catch (JsonMappingException e) {
 +
// TODO Auto-generated catch block
 +
e.printStackTrace();
 +
} catch (IOException e) {
 +
// TODO Auto-generated catch block
 +
e.printStackTrace();
 +
}
 +
System.out.println("JSON value: "+returnData );
 +
return returnData;
 +
}
 +
 +
public Map<String, Object> readBack(String data) {
 +
 +
            System.out.println("Incoming JSON Data value: "+data);
 +
            //Gson gson = new Gson();
 +
            ObjectMapper objectMapper = new ObjectMapper();
 +
           
 +
            Map <String, Object>employee = new HashMap<String, Object>();
 +
            try {
 +
                  employee = objectMapper.readValue(data, new TypeReference<Map<String, Object>>() {});
 +
            } catch (JsonGenerationException e) {
 +
                  // TODO Auto-generated catch block
 +
                  e.printStackTrace();
 +
            } catch (JsonMappingException e) {
 +
                  // TODO Auto-generated catch block
 +
                  e.printStackTrace();
 +
            } catch (IOException e) {
 +
                  // TODO Auto-generated catch block
 +
                  e.printStackTrace();
 +
            }
 +
            System.out.println("MAP value: "+employee );
 +
            return employee;
 +
    }
 +
}
 +
</source>
  
 
== Results:  ==
 
== Results:  ==

Revision as of 06:20, 16 August 2013

Requirements

  1. Need to write a process which should be triggered when a file arrives at a particular folder.
  2. It should pick the file content (which happens to contain JSON Strings).
  3. Need to convert the JSON string into IPP Data Structure and present it to users for editing the IPP data.
  4. Once edited, need to convert the IPP data back to JSON string.

Steps:

  1. Create a RAD project and copy the xpdl into it.
  2. Copy all the source files as per its package structure.
  3. Download jackson-core-asl-1.9.10.jar and jackson-mapper-asl-1.9.10.jar and put it in WEB-INF/lib of the RAD project.
  4. Build the project, deploy the model.
  5. Place a file with itxt as extension in the designated folder which is being monitored by Camel Trigger (in this case its C:\temp).

File Content is:

{"Name":"Tanmoy Roy","EmpID":"1002345","DoJ":1376332200000,"Salary":54433.0,
"Phones":[{"Home":"11111111","Mobile":"111111111","Office":"33333333333"},
{"Home":"2222222","Mobile":"2222222","Office":"34444444"}],
"dependents":[{"Name":"T1","Relation":"TR1","Age":21},
{"Name":"T2","Relation":"TR2","Age":43}]}


To achieve this we will be using IPP's Camel Trigger to monitor the arrival of the file.

We will also use Camel Application Type to route the request to the required beans (in this case DataToJSON).

The aim to use Camel Application Type is to enable the solution to be routed to other Camel/IPP supported endpoints as per need.

The model - Define Receive Incoming File Camel Trigger as shown in the image below.


ModelJSON.JPG


Define 2 Camel Producer Application (FileTOObject and convertToJSON in this case) using that we will convert JSON to Object and back. Associate these to activities Convert File into IPP Data and Convert Data To JSON.

From Applications, choose Camel Producer Application and in its General Tab define the bean like:

<bean id="dataToJSON" class="com.sungard.cm.invo.ipp.training.DataToJSON"/>

and it is Producer Route Tab define route like (for Convert File into IPP Data)

<to uri="bean:dataToJSON?method=readBack"/>

This will call the readBack() method of the bean dataToJSON, which is reading from JSON string and converting it into an IPP data type (Map) called Employee. and for (Convert Data To JSON)

<to uri="bean:dataToJSON?method=enrich"/>

This will call the enrich() method of the bean dataToJSON, which is reading from IPP Data called Employee and converting it to JSON string.

DataToJSON class Code:
package com.sungard.cm.invo.ipp.training;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.ListIterator;
import java.util.Map;
 
import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.type.TypeReference;
 
import com.google.gson.Gson;
 
//import com.google.gson.Gson;
public class DataToJSON<E> {
 
	public String enrich(Map data) {
		System.out.println("Incoming Data value: "+data);
		//Gson gson = new Gson();
		ObjectMapper objectMapper = new ObjectMapper();
 
		String returnData = null;
		try {
			returnData = objectMapper.writeValueAsString(data);
		} catch (JsonGenerationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (JsonMappingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println("JSON value: "+returnData );
		return returnData;
	}
 
	public Map<String, Object> readBack(String data) {
 
            System.out.println("Incoming JSON Data value: "+data);
            //Gson gson = new Gson();
            ObjectMapper objectMapper = new ObjectMapper();
 
            Map <String, Object>employee = new HashMap<String, Object>();
            try {
                  employee = objectMapper.readValue(data, new TypeReference<Map<String, Object>>() {});
            } catch (JsonGenerationException e) {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
            } catch (JsonMappingException e) {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
            } catch (IOException e) {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
            }
            System.out.println("MAP value: "+employee );
            return employee;
    }
}

Results:

As soon as the file is placed the process gets started and You will see it as below after completing Display File Contents activity:

JSONToObject.JPG Next activity screen will present the data for read/write, make changes to it and complete the activity.

EditObject.JPG You will get JSON String out of edited data as shown below:

{"Phones":[{"Home":"11111111","Mobile":"111111111","Office":"33333333333"},
{"Home":"2222222","Mobile":"2222222","Office":"34444444"},
{"Home":"6666666","Mobile":"6666666","Office":"66666666"}],
"Name":"Tanmoy Roy","dependents":[{"Name":"T1","Relation":"TR1","Age":21},
{"Name":"T2","Relation":"TR2","Age":43},
{"Name":"T3","Relation":"TR3","Age":56}],
"EmpID":"1002345","DoJ":1376332200000,"Salary":54433.0}

ObjectToJSON.JPG

Artifacts:

Please get the required artifacts from here

Back to the top