diff --git a/pom.xml b/pom.xml index 37f7751..a1d862d 100644 --- a/pom.xml +++ b/pom.xml @@ -167,6 +167,12 @@ [2.0.0-SNAPSHOT, 3.0.0-SNAPSHOT) compile + + com.jayway.jsonpath + json-path + 2.7.0 + compile + org.gcube.application geoportal-client diff --git a/src/main/java/org/gcube/portlets/user/geoportaldataentry/server/FormDataObjectToJSON.java b/src/main/java/org/gcube/portlets/user/geoportaldataentry/server/FormDataObjectToJSON.java index ba721d1..ab160a1 100644 --- a/src/main/java/org/gcube/portlets/user/geoportaldataentry/server/FormDataObjectToJSON.java +++ b/src/main/java/org/gcube/portlets/user/geoportaldataentry/server/FormDataObjectToJSON.java @@ -25,254 +25,135 @@ import com.jayway.jsonpath.spi.json.JsonOrgJsonProvider; * * @author Francesco Mangiacrapa at ISTI-CNR francesco.mangiacrapa@isti.cnr.it * - * Mar 10, 2022 + * Mar 10, 2022 */ 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 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 tree_Node, JSONObject theRootDocument) throws JSONException { + public JSONObject convert(Tree_Node 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 treeNodeChild : tree_Node.getChildren()) { - + GeoNaFormDataObject gnaFO = treeNodeChild.getData(); - //Building the JSON section - List listGDB = gnaFO.getListGDB(); + + // Reading data and profile + List 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(); - - if(profile.getSectionName().compareTo(JSON_$_POINTER)==0 || profile.getSectionName().compareTo(JSON_$_POINTER+".")==0) { + String parentFullName = profile.getParentName() == null ? "" : profile.getParentName(); + String theSectionName = profile.getSectionName(); + + 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); - - // jsonPathExp = "$.chidl1.child2.child3"; + LOG.info("Current document is: " + theRootDocument); -// String toJsonPathExp = fullJSONPath.replaceFirst("\\$", ""); -// String[] jsonPathDeep = toJsonPathExp.split("\\."); - //List listJSONObject = toListJonObject(listGDB, jsonPathDeep, theRootDocument, profile.getMinOccurs(), profile.getMaxOccurs()); - - List 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(".")); + // Building Parent PATH + 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 + LOG.info("Putting into parentJSONPath: " + parentJSONPath); + + List listJSONObject = toListJonObject(listGDB); + JSONObject jsonObject = listJSONObject.get(0); + + // 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); return theRootDocument; - + } - - /*public static 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 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; } - + LOG.debug("pathExists returning false"); return false; } - - - - /** - * Convert. - * - * @param tree_Node the tree node - * @return the JSON object - * @throws JSONException the JSON exception - */ - /*public JSONObject convert(Tree_Node tree_Node) throws JSONException { - - JSONObject theDocument = JSONObjecOrdered.instance(); - for (GeoNaFormDataObject geoNaFormDataObject : tree_Node) { - - List 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 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 toListJonObject(List listGDB) - throws JSONException { + private List toListJonObject(List listGDB) throws JSONException { List listJSONObject = new ArrayList(); 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 toListJonObject(List listGDB, String[] jsonPathDeep, JSONObject theRootDocument, int minOccurs, int maxOccurs) - throws JSONException { - - List listJSONObject = new ArrayList(); - - 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,13 +239,12 @@ public class FormDataObjectToJSON { return target; } - /** * The Class JSONObjecOrdered. * * @author Francesco Mangiacrapa at ISTI-CNR francesco.mangiacrapa@isti.cnr.it * - * Mar 10, 2022 + * Mar 10, 2022 */ public static class JSONObjecOrdered { @@ -448,14 +265,13 @@ public class FormDataObjectToJSON { return jsonObject; } } - - + /** * The Class JSONObjecOrdered. * * @author Francesco Mangiacrapa at ISTI-CNR francesco.mangiacrapa@isti.cnr.it * - * Mar 10, 2022 + * Mar 10, 2022 */ public static class JSONArrayOrdered {