JSON object built from TreeNode implemented
This commit is contained in:
parent
c2e4846e58
commit
3ab1f5c89c
6
pom.xml
6
pom.xml
|
@ -167,6 +167,12 @@
|
|||
<version>[2.0.0-SNAPSHOT, 3.0.0-SNAPSHOT)</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.jayway.jsonpath</groupId>
|
||||
<artifactId>json-path</artifactId>
|
||||
<version>2.7.0</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.gcube.application</groupId>
|
||||
<artifactId>geoportal-client</artifactId>
|
||||
|
|
|
@ -32,154 +32,96 @@ public class FormDataObjectToJSON {
|
|||
private static final String JSON_$_POINTER = "$";
|
||||
private static final Logger LOG = LoggerFactory.getLogger(FormDataObjectToJSON.class);
|
||||
|
||||
|
||||
/**
|
||||
* Convert.
|
||||
*
|
||||
* @param tree_Node the tree node
|
||||
* @param theRootDocument the the root document
|
||||
* @return the JSON object
|
||||
* @throws JSONException the JSON exception
|
||||
*/
|
||||
public JSONObject convert(Tree_Node<GeoNaFormDataObject> tree_Node, JSONObject theRootDocument) throws JSONException {
|
||||
public JSONObject convert(Tree_Node<GeoNaFormDataObject> tree_Node, JSONObject theRootDocument)
|
||||
throws JSONException {
|
||||
|
||||
//JSONObject theDocument = JSONObjecOrdered.instance();
|
||||
|
||||
if(tree_Node==null)
|
||||
if (tree_Node == null)
|
||||
return theRootDocument;
|
||||
|
||||
//the root, instancing new json document
|
||||
if(tree_Node.isRoot()) {
|
||||
// the root, instancing new json document
|
||||
if (tree_Node.isRoot()) {
|
||||
theRootDocument = JSONObjecOrdered.instance();
|
||||
}
|
||||
|
||||
Configuration configuration = Configuration.builder()
|
||||
.jsonProvider(new JsonOrgJsonProvider())
|
||||
.build();
|
||||
Configuration configuration = Configuration.builder().jsonProvider(new JsonOrgJsonProvider()).build();
|
||||
|
||||
for (Tree_Node<GeoNaFormDataObject> treeNodeChild : tree_Node.getChildren()) {
|
||||
|
||||
GeoNaFormDataObject gnaFO = treeNodeChild.getData();
|
||||
//Building the JSON section
|
||||
|
||||
// Reading data and profile
|
||||
List<GenericDatasetBean> listGDB = gnaFO.getListGDB();
|
||||
GcubeProfileDV profile = gnaFO.getGcubeProfileDV();
|
||||
|
||||
LOG.debug("The profile is: " + profile);
|
||||
|
||||
// Building JSON/section full PATH and section name
|
||||
String fullJSONPath = "";
|
||||
String parentFullName = profile.getParentName()==null?"":profile.getParentName();
|
||||
String parentFullName = profile.getParentName() == null ? "" : profile.getParentName();
|
||||
String theSectionName = profile.getSectionName();
|
||||
|
||||
if(profile.getSectionName().compareTo(JSON_$_POINTER)==0 || profile.getSectionName().compareTo(JSON_$_POINTER+".")==0) {
|
||||
if (theSectionName.compareTo(JSON_$_POINTER) == 0 || theSectionName.compareTo(JSON_$_POINTER + ".") == 0) {
|
||||
fullJSONPath = JSON_$_POINTER;
|
||||
}else {
|
||||
fullJSONPath = String.format("%s%s", parentFullName.endsWith(".")?parentFullName:parentFullName+".", profile.getSectionName());
|
||||
theSectionName = "";
|
||||
} else {
|
||||
fullJSONPath = String.format("%s%s",
|
||||
parentFullName.endsWith(".") ? parentFullName : parentFullName + ".", theSectionName);
|
||||
}
|
||||
LOG.debug("The json path to build: " + fullJSONPath);
|
||||
LOG.debug("Current document is: " + theRootDocument);
|
||||
LOG.info("Current document is: " + theRootDocument);
|
||||
|
||||
// jsonPathExp = "$.chidl1.child2.child3";
|
||||
|
||||
// String toJsonPathExp = fullJSONPath.replaceFirst("\\$", "");
|
||||
// String[] jsonPathDeep = toJsonPathExp.split("\\.");
|
||||
//List<JSONObject> listJSONObject = toListJonObject(listGDB, jsonPathDeep, theRootDocument, profile.getMinOccurs(), profile.getMaxOccurs());
|
||||
// Building Parent PATH
|
||||
String parentPath = fullJSONPath.compareTo(JSON_$_POINTER) == 0 ? JSON_$_POINTER
|
||||
: fullJSONPath.substring(0, fullJSONPath.lastIndexOf("."));
|
||||
JsonPath parentJSONPath = JsonPath.compile(parentPath);
|
||||
LOG.info("Putting into parentJSONPath: " + parentJSONPath);
|
||||
|
||||
List<JSONObject> listJSONObject = toListJonObject(listGDB);
|
||||
|
||||
JSONObject jsonObject = listJSONObject.get(0);
|
||||
|
||||
int maxOccurs = profile.getMaxOccurs();
|
||||
|
||||
String parentPath = fullJSONPath.compareTo(JSON_$_POINTER)==0?JSON_$_POINTER:fullJSONPath.substring(0,fullJSONPath.lastIndexOf("."));
|
||||
JsonPath parentJSONPath = JsonPath.compile(parentPath);
|
||||
LOG.debug("Putting into path: "+parentPath);
|
||||
//If the maxOccurs is not 1
|
||||
if(maxOccurs==0 || maxOccurs>1) {
|
||||
LOG.debug("maxOccurs is not 1");
|
||||
|
||||
//String pathExpAsArray = fullJSONPath+"[*]";
|
||||
//LOG.debug("pathExpAsArray: "+pathExpAsArray);
|
||||
|
||||
//Must be an array
|
||||
boolean pathExists = pathExists(theRootDocument,fullJSONPath+"[*]");
|
||||
|
||||
if(pathExists) {
|
||||
LOG.debug("pathExists is true");
|
||||
// Object object = JsonPath.read(theRootDocument, pathExpAsArray);
|
||||
// LOG.debug("Object instance is: " + object.getClass());
|
||||
// if(object instanceof JSONArray) {
|
||||
// JSONArray targetArray = (JSONArray) object;
|
||||
// targetArray.put(jsonObject);
|
||||
// }
|
||||
|
||||
theRootDocument = JsonPath.parse(theRootDocument,configuration).add(fullJSONPath, jsonObject).json();
|
||||
//JsonPath.parse(theRootDocument,configuration).put(parentJSONPath, profile.getSectionName(), targetArray).json();
|
||||
|
||||
}else {
|
||||
LOG.debug("pathExists is false");
|
||||
//Adding as array of object
|
||||
// If the maxOccurs is not 1
|
||||
if (profile.getMaxOccurs() == 0 || profile.getMaxOccurs() > 1) {
|
||||
LOG.debug("maxOccurs is NOT 1");
|
||||
// Must be an array
|
||||
boolean pathExists = pathExists(theRootDocument, fullJSONPath + "[*]");
|
||||
LOG.debug(fullJSONPath+ "exists? "+pathExists);
|
||||
if (pathExists) {
|
||||
theRootDocument = JsonPath.parse(theRootDocument, configuration).add(fullJSONPath, jsonObject)
|
||||
.json();
|
||||
} else {
|
||||
// Adding as array of object
|
||||
JSONArray targetArray = JSONArrayOrdered.instance();
|
||||
targetArray.put(jsonObject);
|
||||
LOG.debug("JSON array created: "+targetArray);
|
||||
theRootDocument = JsonPath.parse(theRootDocument,configuration).put(parentJSONPath, profile.getSectionName(), targetArray).json();
|
||||
//JsonPath.parse(theRootDocument.toString()).set(setIntoPath, targetArray);
|
||||
|
||||
// theRootDocument.put(profile.getSectionName(), targetArray);
|
||||
//
|
||||
// String setIntoPath = String.format("%s[%d]", fullJSONPath, targetArray.length()-1);
|
||||
// LOG.debug("setIntoPath is: " + setIntoPath);
|
||||
// JsonPath.parse(theRootDocument.toString()).set(setIntoPath, targetArray);
|
||||
|
||||
LOG.debug("JSON array created: " + targetArray);
|
||||
theRootDocument = JsonPath.parse(theRootDocument, configuration)
|
||||
.put(parentJSONPath, theSectionName, targetArray).json();
|
||||
}
|
||||
|
||||
LOG.debug("theRootDocument as array is: " + theRootDocument);
|
||||
|
||||
//theRootDocument.get(jsonPathDeep[jsonPathDeep.length - 1], targetArray);
|
||||
}else {
|
||||
//Adding as single object
|
||||
//theRootDocument.put(jsonPathDeep[jsonPathDeep.length - 1], jsonObject);
|
||||
|
||||
//theRootDocument.put(profile.getSectionName(), jsonObject);
|
||||
theRootDocument = JsonPath.parse(theRootDocument,configuration).put(parentJSONPath, profile.getSectionName(), jsonObject).json();
|
||||
// jsonObject.put(profile.getSectionName(), jsonObject);
|
||||
// parentPath = parentPath.endsWith(".")?parentPath.substring(0,parentPath.length()-1):parentPath;
|
||||
// JsonPath.parse(theRootDocument.toString()).set(parentPath, jsonObject);
|
||||
} else {
|
||||
LOG.debug("maxOccurs is 1");
|
||||
// Merging as direct properties of the JSON root document
|
||||
if (theSectionName == null || theSectionName.isEmpty()) {
|
||||
deepMerge(jsonObject, theRootDocument);
|
||||
} else {
|
||||
// Putting as child of the JSON document
|
||||
// theRootDocument.put(profile.getSectionName(), jsonObject);
|
||||
theRootDocument = JsonPath.parse(theRootDocument, configuration)
|
||||
.put(parentJSONPath, theSectionName, jsonObject).json();
|
||||
}
|
||||
|
||||
LOG.debug("theRootDocument as single object is: " + theRootDocument);
|
||||
}
|
||||
|
||||
/*
|
||||
// adding as JSONObject to theDocument at first deep level (under the root or to the section specified)
|
||||
if (listJSONObject.size() == 1) {
|
||||
|
||||
|
||||
int maxOccurs = profile.getMaxOccurs();
|
||||
//If the maxOccurs is not 1
|
||||
if(maxOccurs==0 || maxOccurs>1) {
|
||||
//Adding as array of object
|
||||
JSONArray targetArray = JSONArrayOrdered.instance();
|
||||
targetArray.put(jsonObject);
|
||||
sectRootObject.put(jsonPathDeep[jsonPathDeep.length - 1], targetArray);
|
||||
}else {
|
||||
//Adding as single object
|
||||
sectRootObject.put(jsonPathDeep[jsonPathDeep.length - 1], jsonObject);
|
||||
}
|
||||
|
||||
theRootDocument = deepMerge(listJSONObject.get(0), theRootDocument);
|
||||
//theDocument = theDocument.put(listJSONObject.get(0), jsonArray);
|
||||
} else {
|
||||
// adding as JSONArray to theDocument under the section specified
|
||||
// to jsonPathDeep[1]
|
||||
JSONArray jsonArray = new JSONArray();
|
||||
for (JSONObject jsonObject : listJSONObject) {
|
||||
// reading the i-mo JSONObject created in the list with the key as
|
||||
// jsonPathDeep[1]
|
||||
jsonArray.put(jsonObject.get(jsonPathDeep[1]));
|
||||
}
|
||||
theRootDocument.put(jsonPathDeep[1], jsonArray);
|
||||
|
||||
}*/
|
||||
|
||||
//recursive call...
|
||||
theRootDocument = convert(treeNodeChild,theRootDocument);
|
||||
// recursive call...
|
||||
theRootDocument = convert(treeNodeChild, theRootDocument);
|
||||
}
|
||||
|
||||
LOG.debug("Partial Root Document is: " + theRootDocument);
|
||||
|
@ -187,35 +129,24 @@ public class FormDataObjectToJSON {
|
|||
|
||||
}
|
||||
|
||||
/*public static <T> void appendInDocument(JSONObject node, String pathExp) {
|
||||
GWT.log("preOrderVisit called");
|
||||
|
||||
if (node == null)
|
||||
return;
|
||||
|
||||
|
||||
|
||||
GWT.log("preOrderVisit Node name: " + node + ", parent: " + parentName);
|
||||
|
||||
for (Tree_Node<T> child : node.getChildren()) {
|
||||
preOrderVisit(child);
|
||||
}
|
||||
|
||||
return;
|
||||
|
||||
}*/
|
||||
|
||||
public static boolean pathExists(JSONObject document, String pathExp){
|
||||
/**
|
||||
* Path exists.
|
||||
*
|
||||
* @param document the document
|
||||
* @param pathExp the path exp
|
||||
* @return true, if successful
|
||||
*/
|
||||
public static boolean pathExists(JSONObject document, String pathExp) {
|
||||
LOG.debug("pathExists called");
|
||||
|
||||
try {
|
||||
LOG.debug("pathExists finding: "+pathExp+" into node: "+document);
|
||||
LOG.debug("pathExists finding: " + pathExp + " into node: " + document);
|
||||
Object object = JsonPath.read(document.toString(), pathExp);
|
||||
if(object!=null) {
|
||||
if (object != null) {
|
||||
LOG.debug("pathExists returning true");
|
||||
return true;
|
||||
}
|
||||
}catch (Exception e) {
|
||||
} catch (Exception e) {
|
||||
LOG.error("pathExists error", e);
|
||||
return false;
|
||||
}
|
||||
|
@ -224,56 +155,6 @@ public class FormDataObjectToJSON {
|
|||
return false;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Convert.
|
||||
*
|
||||
* @param tree_Node the tree node
|
||||
* @return the JSON object
|
||||
* @throws JSONException the JSON exception
|
||||
*/
|
||||
/*public JSONObject convert(Tree_Node<GeoNaFormDataObject> tree_Node) throws JSONException {
|
||||
|
||||
JSONObject theDocument = JSONObjecOrdered.instance();
|
||||
for (GeoNaFormDataObject geoNaFormDataObject : tree_Node) {
|
||||
|
||||
List<GenericDatasetBean> listGDB = geoNaFormDataObject.getListGDB();
|
||||
|
||||
GcubeProfileDV profile = geoNaFormDataObject.getGcubeProfileDV();
|
||||
LOG.debug("The profile is: " + profile);
|
||||
|
||||
String jsonPathExp = String.format("%s%s", profile.getParentName(), profile.getSectionName());
|
||||
LOG.debug("The json path to build: " + jsonPathExp);
|
||||
// jsonPathExp = "$.chidl1.child2.child3";
|
||||
|
||||
String toJsonPathExp = jsonPathExp.replaceFirst("\\$", "");
|
||||
String[] jsonPathDeep = toJsonPathExp.split("\\.");
|
||||
List<JSONObject> listJSONObject = toListJonObject(listGDB, jsonPathDeep);
|
||||
|
||||
// adding as JSONObject to theDocument at first deep level (under the root or at section specified)
|
||||
if (listJSONObject.size() == 1) {
|
||||
theDocument = deepMerge(listJSONObject.get(0), theDocument);
|
||||
//theDocument = theDocument.put(listJSONObject.get(0), jsonArray);
|
||||
} else {
|
||||
// adding as JSONArray to theDocument under the section specified
|
||||
// to jsonPathDeep[1]
|
||||
JSONArray jsonArray = new JSONArray();
|
||||
for (JSONObject jsonObject : listJSONObject) {
|
||||
// reading the i-mo JSONObject created in the list with the key as
|
||||
// jsonPathDeep[1]
|
||||
jsonArray.put(jsonObject.get(jsonPathDeep[1]));
|
||||
}
|
||||
theDocument.put(jsonPathDeep[1], jsonArray);
|
||||
|
||||
}
|
||||
LOG.debug("Partial Root Document is: " + theDocument);
|
||||
}
|
||||
|
||||
LOG.debug("Final JSON Document is: " + theDocument);
|
||||
return theDocument;
|
||||
}*/
|
||||
|
||||
/**
|
||||
* Generic dataset bean to JSON.
|
||||
*
|
||||
|
@ -292,13 +173,11 @@ public class FormDataObjectToJSON {
|
|||
if (listValues == null || listValues.isEmpty()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// key/value as string
|
||||
if (listValues.size() == 1) {
|
||||
sectJSONObject.put(key, listValues.get(0));
|
||||
continue;
|
||||
}
|
||||
|
||||
// value is a list
|
||||
JSONArray array = new JSONArray();
|
||||
for (String value : listValues) {
|
||||
|
@ -309,26 +188,22 @@ public class FormDataObjectToJSON {
|
|||
}
|
||||
|
||||
return sectJSONObject;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* To list jon object.
|
||||
*
|
||||
* @param listGDB the list GDB
|
||||
* @param jsonPathDeep the json path deep
|
||||
* @return the list
|
||||
* @throws JSONException the JSON exception
|
||||
*/
|
||||
private List<JSONObject> toListJonObject(List<GenericDatasetBean> listGDB)
|
||||
throws JSONException {
|
||||
private List<JSONObject> toListJonObject(List<GenericDatasetBean> listGDB) throws JSONException {
|
||||
|
||||
List<JSONObject> listJSONObject = new ArrayList<JSONObject>();
|
||||
|
||||
for (GenericDatasetBean gdb : listGDB) {
|
||||
JSONObject jsonObject = genericDatasetBeanToJSON(gdb);
|
||||
listJSONObject.add(jsonObject);
|
||||
|
||||
}
|
||||
|
||||
LOG.info("returning : " + listJSONObject);
|
||||
|
@ -336,63 +211,6 @@ public class FormDataObjectToJSON {
|
|||
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* To list jon object.
|
||||
*
|
||||
* @param listGDB the list GDB
|
||||
* @param jsonPathDeep the json path deep
|
||||
* @return the list
|
||||
* @throws JSONException the JSON exception
|
||||
*/
|
||||
private List<JSONObject> toListJonObject(List<GenericDatasetBean> listGDB, String[] jsonPathDeep, JSONObject theRootDocument, int minOccurs, int maxOccurs)
|
||||
throws JSONException {
|
||||
|
||||
List<JSONObject> listJSONObject = new ArrayList<JSONObject>();
|
||||
|
||||
for (GenericDatasetBean gdb : listGDB) {
|
||||
JSONObject sectRootObject = JSONObjecOrdered.instance();
|
||||
JSONObject jsonObject = genericDatasetBeanToJSON(gdb);
|
||||
LOG.debug("Adding section : " + jsonObject);
|
||||
LOG.trace("jsonPathDeep: " + Arrays.asList(jsonPathDeep) + " size: " + jsonPathDeep.length);
|
||||
|
||||
// Adding JSONObject to ROOT DOCUMENT POSITION (using PLACEHOLDER_ROOT_POINTER_JSON_DOC)
|
||||
if (jsonPathDeep.length == 0) {
|
||||
sectRootObject = deepMerge(jsonObject, sectRootObject);
|
||||
} else {
|
||||
|
||||
//If the maxOccurs is not 1
|
||||
if(maxOccurs==0 || maxOccurs>1) {
|
||||
//Adding as array of object
|
||||
JSONArray targetArray = JSONArrayOrdered.instance();
|
||||
targetArray.put(jsonObject);
|
||||
sectRootObject.put(jsonPathDeep[jsonPathDeep.length - 1], targetArray);
|
||||
}else {
|
||||
//Adding as single object
|
||||
sectRootObject.put(jsonPathDeep[jsonPathDeep.length - 1], jsonObject);
|
||||
}
|
||||
|
||||
|
||||
JSONObject deepJSON = sectRootObject;
|
||||
System.out.println("toListJonObject sectRootObject: " + sectRootObject.toString());
|
||||
for (int i = jsonPathDeep.length - 2; i > 0; i--) {
|
||||
JSONObject newOne = JSONObjecOrdered.instance();
|
||||
newOne.put(jsonPathDeep[i], deepJSON);
|
||||
deepJSON = newOne;
|
||||
}
|
||||
sectRootObject = deepJSON;
|
||||
}
|
||||
|
||||
listJSONObject.add(sectRootObject);
|
||||
|
||||
}
|
||||
|
||||
LOG.info("For listGDB : " + listGDB);
|
||||
LOG.info("returning : " + listJSONObject);
|
||||
return listJSONObject;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Merge "source" into "target". If fields have equal name, merge them
|
||||
* recursively.
|
||||
|
@ -421,7 +239,6 @@ public class FormDataObjectToJSON {
|
|||
return target;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* The Class JSONObjecOrdered.
|
||||
*
|
||||
|
@ -449,7 +266,6 @@ public class FormDataObjectToJSON {
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* The Class JSONObjecOrdered.
|
||||
*
|
||||
|
|
Loading…
Reference in New Issue