task_24166 #11

Merged
francesco.mangiacrapa merged 37 commits from task_24166 into master 2023-05-12 10:57:15 +02:00
6 changed files with 452 additions and 61 deletions
Showing only changes of commit 5c031270a0 - Show all commits

View File

@ -74,6 +74,9 @@
@ -171,6 +174,9 @@
@ -268,6 +274,9 @@
@ -365,6 +374,9 @@
@ -414,6 +426,9 @@
@ -445,9 +460,9 @@
<dependent-module archiveName="geoportal-data-common-2.1.0-SNAPSHOT.jar" deploy-path="/WEB-INF/lib" handle="module:/resource/geoportal-data-common/geoportal-data-common"> <dependent-module archiveName="geoportal-data-common-2.1.0-SNAPSHOT.jar" deploy-path="/WEB-INF/lib" handle="module:/resource/geoportal-data-common/geoportal-data-common">
<dependency-type>uses</dependency-type> <dependency-type>uses</dependency-type>
</dependent-module> </dependent-module>
<dependent-module archiveName="geoportal-client-1.1.2.jar" deploy-path="/WEB-INF/lib" handle="module:/resource/geoportal-client/geoportal-client">
<dependency-type>uses</dependency-type>
</dependent-module>
@ -620,6 +635,9 @@
@ -717,6 +735,9 @@
@ -814,6 +835,9 @@

View File

@ -47,7 +47,7 @@ public class FormDataObjectToJSON {
// the root, instancing new json document // the root, instancing new json document
if (tree_Node.isRoot()) { if (tree_Node.isRoot()) {
theRootDocument = JSONObjecOrdered.instance(); theRootDocument = JSONObjectOrdered.instance();
} }
Configuration configuration = Configuration.builder().jsonProvider(new JsonOrgJsonProvider()).build(); Configuration configuration = Configuration.builder().jsonProvider(new JsonOrgJsonProvider()).build();
@ -163,7 +163,7 @@ public class FormDataObjectToJSON {
*/ */
protected JSONObject genericDatasetBeanToJSON(GenericDatasetBean gdb) throws JSONException { protected JSONObject genericDatasetBeanToJSON(GenericDatasetBean gdb) throws JSONException {
JSONObject sectJSONObject = JSONObjecOrdered.instance(); JSONObject sectJSONObject = JSONObjectOrdered.instance();
LinkedHashMap<String, List<String>> mapFields = gdb.getFormDataEntryFields(); LinkedHashMap<String, List<String>> mapFields = gdb.getFormDataEntryFields();
LOG.debug("Map ordered: " + mapFields); LOG.debug("Map ordered: " + mapFields);
@ -246,7 +246,7 @@ public class FormDataObjectToJSON {
* *
* Mar 10, 2022 * Mar 10, 2022
*/ */
public static class JSONObjecOrdered { public static class JSONObjectOrdered {
/** /**
* Instance. * Instance.

View File

@ -5,7 +5,6 @@ import java.io.FileInputStream;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.InputStream; import java.io.InputStream;
import java.lang.reflect.Type;
import java.net.URL; import java.net.URL;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
@ -18,7 +17,6 @@ import java.util.Iterator;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import java.util.stream.Stream; import java.util.stream.Stream;
import java.util.stream.StreamSupport; import java.util.stream.StreamSupport;
@ -67,7 +65,7 @@ import org.gcube.common.portal.PortalContext;
import org.gcube.contentmanagement.blobstorage.transport.backend.RemoteBackendException; import org.gcube.contentmanagement.blobstorage.transport.backend.RemoteBackendException;
import org.gcube.portlets.user.geoportaldataentry.client.GeoportalDataEntryService; import org.gcube.portlets.user.geoportaldataentry.client.GeoportalDataEntryService;
import org.gcube.portlets.user.geoportaldataentry.client.ProjectFormCard; import org.gcube.portlets.user.geoportaldataentry.client.ProjectFormCard;
import org.gcube.portlets.user.geoportaldataentry.server.FormDataObjectToJSON.JSONObjecOrdered; import org.gcube.portlets.user.geoportaldataentry.server.json.JsonMerge;
import org.gcube.portlets.user.geoportaldataentry.shared.CommitReport; import org.gcube.portlets.user.geoportaldataentry.shared.CommitReport;
import org.gcube.portlets.user.geoportaldataentry.shared.GNADataEntryExtendedConfigProfile; import org.gcube.portlets.user.geoportaldataentry.shared.GNADataEntryExtendedConfigProfile;
import org.gcube.portlets.user.geoportaldataentry.shared.GeoNaFormDataObject; import org.gcube.portlets.user.geoportaldataentry.shared.GeoNaFormDataObject;
@ -87,13 +85,13 @@ import org.json.JSONObject;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import com.google.gson.internal.LinkedTreeMap; import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken; import com.google.gson.JsonObject;
import com.google.gwt.user.server.rpc.RemoteServiceServlet; import com.google.gwt.user.server.rpc.RemoteServiceServlet;
import com.jayway.jsonpath.Configuration; import com.jayway.jsonpath.Configuration;
import com.jayway.jsonpath.DocumentContext;
import com.jayway.jsonpath.JsonPath; import com.jayway.jsonpath.JsonPath;
import com.jayway.jsonpath.spi.json.GsonJsonProvider; import com.jayway.jsonpath.spi.json.GsonJsonProvider;
import com.jayway.jsonpath.spi.json.JsonOrgJsonProvider;
/** /**
* The server side implementation of the RPC service. * The server side implementation of the RPC service.
@ -248,47 +246,58 @@ public class GeoportalDataEntryServiceImpl extends RemoteServiceServlet implemen
// Converter // Converter
FormDataObjectToJSON metadataConverter = new FormDataObjectToJSON(); FormDataObjectToJSON metadataConverter = new FormDataObjectToJSON();
// JSON Section to update converted as JSONObject // JSON Section to update converted as JSONObject
JSONObject jsonSectionObject = metadataConverter.genericDatasetBeanToJSON(sectionBean); JSONObject updatedSectionObject = metadataConverter.genericDatasetBeanToJSON(sectionBean);
LOG.info("Json Section Object: {}", jsonSectionObject.toString()); LOG.info("Input Json Section (to update): {}", updatedSectionObject.toString());
Configuration configuration = Configuration.builder().jsonProvider(new GsonJsonProvider()).build(); Configuration configurationGson = Configuration.builder().jsonProvider(new GsonJsonProvider()).build();
// Type type = new TypeToken<Set<LinkedTreeMap<String, Object>>>() {}.getType(); // Type type = new TypeToken<Set<LinkedTreeMap<String, Object>>>() {}.getType();
// Set<LinkedTreeMap<String, Object>> myMap = gson.fromJson(json, type); // Set<LinkedTreeMap<String, Object>> myMap = gson.fromJson(json, type);
// Source Project // Source Project
String jsonSourceProject = getJSONDocumentInTheProject(profileID, projectID); String theDocumentJson = getJSONDocumentInTheProject(profileID, projectID);
LOG.info("Json source Object: {}", jsonSourceProject); LOG.info("Source document: {}", theDocumentJson);
//If the section path is the Root document, passed as "$.", fixing as "$" // If the section path is the Root document, passed as "$.", fixing as "$"
if(sectionPath.compareTo(FormDataObjectToJSON.JSON_$_POINTER+".")==0) if (sectionPath.compareTo(FormDataObjectToJSON.JSON_$_POINTER + ".") == 0)
sectionPath = FormDataObjectToJSON.JSON_$_POINTER; sectionPath = FormDataObjectToJSON.JSON_$_POINTER;
ProjectsCaller client = GeoportalClientCaller.projects(); ProjectsCaller client = GeoportalClientCaller.projects();
com.google.gson.JsonObject targetSectionJObject = JsonPath.parse(theDocumentJson, configurationGson)
.read(sectionPath);
LOG.info("Current Section path {} in the Document is {}", sectionPath, targetSectionJObject.toString());
String srcJ = updatedSectionObject.toString();
String trgJ = targetSectionJObject.toString();
LOG.info("Merging src {} in the target: {}", srcJ, trgJ);
String mergedDoc = JsonMerge.merge(srcJ, trgJ);
LOG.info("mergedDoc: {}", mergedDoc);
JSONObject currentSectionJObject = JsonPath.parse(jsonSourceProject, configuration).read(sectionPath); String newDocJson;
LOG.info("currentSectionJObject: {}", currentSectionJObject.toString()); // If Updating path is first level of the root
if (sectionPath.equals(FormDataObjectToJSON.JSON_$_POINTER)) {
// Merged section between updated and current // The merged DOC is the root Document, no action required
JSONObject mergedSection = JSONObjecOrdered.instance(); newDocJson = mergedDoc;
mergedSection = FormDataObjectToJSON.deepMerge(jsonSectionObject, } else {
currentSectionJObject); // If the merged DOC is a child of the root Document, setting it as child of the
// Document in the proper section
LOG.info("Merged section: {}", mergedSection.toString()); Gson gson = new Gson();
JsonObject gsonOject = gson.fromJson(mergedDoc, JsonObject.class);
// Putting the merged section into Document // Putting the merged section into Document
Object newDocument = JsonPath.parse(jsonSourceProject, configuration).set(sectionPath, mergedSection) DocumentContext newContextDocJson = JsonPath.parse(theDocumentJson, configurationGson).set(sectionPath,
.json(); gsonOject);
newDocJson = newContextDocJson.json().toString();
String newDocumentString = newDocument.toString(); }
LOG.debug("Going to call updateProject with document: {}", newDocumentString);
Document updatedDocument = Serialization.read(newDocJson.toString(), Document.class);
LOG.info("New document is: {}", updatedDocument.toJson());
Document updatedDocument = Serialization.read(newDocumentString, Document.class);
Project project = client.updateProject(profileID, projectID, updatedDocument); Project project = client.updateProject(profileID, projectID, updatedDocument);
LOG.debug("Medatata Updated with document: {}", project.getTheDocument()); LOG.debug("Medatata Updated with document: {}", project.getTheDocument());
SessionUtil.getCurrentContext(getThreadLocalRequest(), true); SessionUtil.getCurrentContext(getThreadLocalRequest(), true);
//Cleaning all the fileset path of the section (defined in the UCD) //Cleaning all the fileset path of the section (defined in the UCD)
for (FilePathDV filePath: listFilePaths) { for (FilePathDV filePath: listFilePaths) {
@ -308,7 +317,7 @@ public class GeoportalDataEntryServiceImpl extends RemoteServiceServlet implemen
LOG.info("Project with id " + project.getId() + " updated correclty"); LOG.info("Project with id " + project.getId() + " updated correclty");
ProjectDVBuilder projectBuilder = ProjectDVBuilder.newBuilder().fullDocumentMap(true); ProjectDVBuilder projectBuilder = ProjectDVBuilder.newBuilder().fullDocumentMap(true);
ProjectDV toProjectDV = ConvertToDataValueObjectModel.toProjectDV(project, projectBuilder); ProjectDV toProjectDV = ConvertToDataValueObjectModel.toProjectDV(project, projectBuilder);
newDocumentString = toProjectDV.getTheDocument().getDocumentAsJSON(); String newDocumentString = toProjectDV.getTheDocument().getDocumentAsJSON();
LOG.info("Got Document: {} ", newDocumentString); LOG.info("Got Document: {} ", newDocumentString);
return new CommitReport(projectID, profileID, newDocumentString, null); return new CommitReport(projectID, profileID, newDocumentString, null);
} catch (Exception e) { } catch (Exception e) {
@ -526,7 +535,8 @@ public class GeoportalDataEntryServiceImpl extends RemoteServiceServlet implemen
input = new File(file.getTempSystemPath()); input = new File(file.getTempSystemPath());
output = new File(tmpDirPath, file.getFileName()); output = new File(tmpDirPath, file.getFileName());
copyContent(input, output); copyContent(input, output);
LOG.info("Temp file: " + file.getTempSystemPath() + ", copied to new file: " + output.getName()); LOG.info(
"Temp file: " + file.getTempSystemPath() + ", copied to new file: " + output.getName());
} }
collFieldDef.addFile(output); collFieldDef.addFile(output);
@ -552,9 +562,9 @@ public class GeoportalDataEntryServiceImpl extends RemoteServiceServlet implemen
FilePathDV filePath = uploadedFileset.getFilePathDV(); FilePathDV filePath = uploadedFileset.getFilePathDV();
Access access = ConvertToDataServiceModel.getAccessFromDocumentSection(theJSONDocument, sectionJSONPath); Access access = ConvertToDataServiceModel.getAccessFromDocumentSection(theJSONDocument, sectionJSONPath);
LOG.info("Going to registrer files: "+Arrays.asList(fileset).toString()); LOG.info("Going to registrer files: " + Arrays.asList(fileset).toString());
mongoService.registerFileSet(theProject.getProfileID(), theProject, sectionJSONPath, // mongoService.registerFileSet(theProject.getProfileID(), theProject, sectionJSONPath,
filePath.getFieldName(), filePath.getFieldDefinition(), access, fileset); // filePath.getFieldName(), filePath.getFieldDefinition(), access, fileset);
} }
} }

View File

@ -0,0 +1,188 @@
package org.gcube.portlets.user.geoportaldataentry.server.json;
import java.io.IOException;
import java.util.Iterator;
import java.util.Map;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
/**
* This class provides methods to merge two json of any nested level into a single json.
*
* copied from: https://github.com/hemantsonu20/json-merge
*/
public class JsonMerge {
private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
/**
* Method to merge two json objects into single json object.
*
* <p>It merges two json of any nested level into a single json following below logic.</p>
* <ul>
* <li>When keys are different, both keys with there values will be copied at same level.</li>
* <li>
* <p>When keys are same at some level, following table denotes what value will be used.</p>
* <table border="1" summary="">
* <thead>
* <tr>
* <th align="left">Src / Target</th>
* <th align="left">JSON Value</th>
* <th align="left">JSON Array</th>
* <th align="left">JSON Object</th>
* </tr>
* </thead>
* <tbody>
* <tr>
* <td align="left">JSON Value<sup>1</sup></td>
* <td align="left">Src</td>
* <td align="left">Src</td>
* <td align="left">Src</td>
* </tr>
* <tr>
* <td align="left">JSON Array</td>
* <td align="left">Src<sup>2</sup></td>
* <td align="left">Merge</td>
* <td align="left">Src</td>
* </tr>
* <tr>
* <td align="left">JSON Object</td>
* <td align="left">Src</td>
* <td align="left">Src</td>
* <td align="left">Merge<sup>3</sup></td>
* </tr>
* </tbody>
* </table>
* <ul>
* <li><sup><strong>1</strong></sup> Json Value denotes boolean, number or string value in json.</li>
* <li><sup><strong>2</strong></sup> Src denotes <code>Src</code> value will be copied.</li>
* <li><sup><strong>3</strong></sup> Merge denotes both <code>Src</code> and <code>Target</code> values will be merged.</li>
* </ul>
* </li>
* </ul>
*
* <h2>Examples</h2>
* <h3>Example 1</h3>
* <p><strong>Source Json</strong></p>
* <pre>{@code
* {
* "name": "json-merge-src"
* }
* }</pre>
* <p><strong>Target Json</strong></p>
* <pre>{@code
* {
* "name": "json-merge-target"
* }
* }</pre>
* <p><strong>Output</strong></p>
* <pre>{@code
* {
* "name": "json-merge-src"
* }
* }</pre>
* <h3>Example 2</h3>
* <p><strong>Source Json</strong></p>
* <pre>{@code
* {
* "level1": {
* "key1": "SrcValue1"
* }
* }
* }</pre>
* <p><strong>Target Json</strong></p>
* <pre>{@code
* {
* "level1": {
* "key1": "targetValue1",
* "level2": {
* "key2": "value2"
* }
* }
* }
* }</pre>
* <p><strong>Output</strong></p>
* <pre>{@code
* {
* "level1": {
* "key1": "SrcValue1",
* "level2": {
* "key2": "value2"
* }
* }
* }
* }</pre>
*
* @param srcJsonStr source json string
* @param targetJsonStr target json string
* @return merged json as a string
*/
public static String merge(String srcJsonStr, String targetJsonStr) {
try {
JsonNode srcNode = OBJECT_MAPPER.readTree(srcJsonStr);
JsonNode targetNode = OBJECT_MAPPER.readTree(targetJsonStr);
JsonNode result = merge(srcNode, targetNode);
return OBJECT_MAPPER.writeValueAsString(result);
} catch (IOException e) {
throw new JsonMergeException("Unable to merge json", e);
}
}
public static JsonNode merge(JsonNode srcNode, JsonNode targetNode) {
// if both nodes are object node, merged object node is returned
if (srcNode.isObject() && targetNode.isObject()) {
return merge((ObjectNode) srcNode, (ObjectNode) targetNode);
}
// if both nodes are array node, merged array node is returned
if (srcNode.isArray() && targetNode.isArray()) {
return merge((ArrayNode) srcNode, (ArrayNode) targetNode);
}
// special case when src node is null
if (srcNode.isNull()) {
return targetNode;
}
return srcNode;
}
public static ObjectNode merge(ObjectNode srcNode, ObjectNode targetNode) {
ObjectNode result = OBJECT_MAPPER.createObjectNode();
Iterator<Map.Entry<String, JsonNode>> srcItr = srcNode.fields();
while (srcItr.hasNext()) {
Map.Entry<String, JsonNode> entry = srcItr.next();
// check key in src json exists in target json or not at same level
if (targetNode.has(entry.getKey())) {
result.set(entry.getKey(), merge(entry.getValue(), targetNode.get(entry.getKey())));
} else {
// if key in src json doesn't exist in target json, just copy the same in result
result.set(entry.getKey(), entry.getValue());
}
}
// copy fields from target json into result which were missing in src json
Iterator<Map.Entry<String, JsonNode>> targetItr = targetNode.fields();
while (targetItr.hasNext()) {
Map.Entry<String, JsonNode> entry = targetItr.next();
if (!result.has(entry.getKey())) {
result.set(entry.getKey(), entry.getValue());
}
}
return result;
}
public static ArrayNode merge(ArrayNode srcNode, ArrayNode targetNode) {
ArrayNode result = OBJECT_MAPPER.createArrayNode();
return result.addAll(srcNode).addAll(targetNode);
}
}

View File

@ -0,0 +1,20 @@
package org.gcube.portlets.user.geoportaldataentry.server.json;
/**
* Exception to be thrown in case of any error occured while merging two json.
*
*/
public class JsonMergeException extends RuntimeException {
public JsonMergeException() {
super();
}
public JsonMergeException(String msg) {
super(msg);
}
public JsonMergeException(String msg, Throwable th) {
super(msg, th);
}
}

View File

@ -1,9 +1,16 @@
package org.gcube.portlets.user.geoportaldataentry; package org.gcube.portlets.user.geoportaldataentry;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Iterator;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
import java.util.List; import java.util.List;
import java.util.Map.Entry;
import java.util.Properties;
import java.util.Set;
import org.bson.Document; import org.bson.Document;
import org.gcube.application.geoportal.client.utils.Serialization; import org.gcube.application.geoportal.client.utils.Serialization;
@ -31,10 +38,15 @@ import org.gcube.application.geoportalcommon.shared.geoportal.view.SectionView;
import org.gcube.application.geoportalcommon.shared.geoportal.view.SubDocumentView; import org.gcube.application.geoportalcommon.shared.geoportal.view.SubDocumentView;
import org.gcube.common.authorization.library.provider.SecurityTokenProvider; import org.gcube.common.authorization.library.provider.SecurityTokenProvider;
import org.gcube.common.scope.api.ScopeProvider; import org.gcube.common.scope.api.ScopeProvider;
import org.gcube.portlets.user.geoportaldataentry.server.FormDataObjectToJSON;
import org.gcube.portlets.user.geoportaldataentry.server.FormDataObjectToJSON.JSONObjectOrdered;
import org.gcube.portlets.user.geoportaldataentry.server.json.JsonMerge;
import org.gcube.portlets.widgets.mpformbuilder.server.MetadataProfileFormBuilderServiceImpl; import org.gcube.portlets.widgets.mpformbuilder.server.MetadataProfileFormBuilderServiceImpl;
import org.gcube.portlets.widgets.mpformbuilder.shared.metadata.MetaDataProfileBean; import org.gcube.portlets.widgets.mpformbuilder.shared.metadata.MetaDataProfileBean;
import org.gcube.portlets.widgets.mpformbuilder.shared.metadata.MetadataFieldWrapper; import org.gcube.portlets.widgets.mpformbuilder.shared.metadata.MetadataFieldWrapper;
import org.json.JSONArray; import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.slf4j.Logger; import org.slf4j.Logger;
@ -42,19 +54,29 @@ import org.slf4j.LoggerFactory;
import com.google.gson.Gson; import com.google.gson.Gson;
import com.google.gson.GsonBuilder; import com.google.gson.GsonBuilder;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject; import com.google.gson.JsonObject;
import com.google.gson.JsonParser; import com.google.gson.JsonParser;
import com.jayway.jsonpath.Configuration;
import com.jayway.jsonpath.DocumentContext;
import com.jayway.jsonpath.JsonPath; import com.jayway.jsonpath.JsonPath;
import com.jayway.jsonpath.spi.json.GsonJsonProvider;
import com.jayway.jsonpath.spi.json.JsonOrgJsonProvider; import com.jayway.jsonpath.spi.json.JsonOrgJsonProvider;
public class Complex_Tests { public class Complex_Tests {
private static final String GCUBE_CONFIG_PROPERTIES_FILENAME = "gcube_config.properties";
// APP Working Directory + /src/test/resources must be the location of
// gcube_config.properties
private static String gcube_config_path = String.format("%s/%s",
System.getProperty("user.dir") + "/src/test/resources", GCUBE_CONFIG_PROPERTIES_FILENAME);
private static String CONTEXT;
private static String TOKEN;
private UseCaseDescriptorCaller clientUCD = null; private UseCaseDescriptorCaller clientUCD = null;
private ProjectsCaller clientPrj = null; private ProjectsCaller clientPrj = null;
private static String CONTEXT = "/gcube/devsec/devVRE";
private static String TOKEN = ""; // devVRE
private static String PROFILE_ID = "profiledConcessioni";
private static String PROFILE_ID = "profiledConcessioni";
private static String PROJECT_ID = "632c633155e2947b0278c999"; private static String PROJECT_ID = "632c633155e2947b0278c999";
private static String MY_LOGIN = "francesco.mangiacrapa"; private static String MY_LOGIN = "francesco.mangiacrapa";
@ -94,14 +116,141 @@ public class Complex_Tests {
} }
//@Before /**
* Read context settings.
*/
public static void readContextSettings() {
try (InputStream input = new FileInputStream(gcube_config_path)) {
Properties prop = new Properties();
// load a properties file
prop.load(input);
CONTEXT = prop.getProperty("CONTEXT");
TOKEN = prop.getProperty("TOKEN");
// get the property value and print it out
System.out.println("CONTEXT: " + CONTEXT);
System.out.println("TOKEN: " + TOKEN);
} catch (IOException ex) {
ex.printStackTrace();
}
}
@Before
public void init() { public void init() {
readContextSettings();
ScopeProvider.instance.set(CONTEXT); ScopeProvider.instance.set(CONTEXT);
SecurityTokenProvider.instance.set(TOKEN); SecurityTokenProvider.instance.set(TOKEN);
clientPrj = GeoportalClientCaller.projects(); clientPrj = GeoportalClientCaller.projects();
clientUCD = GeoportalClientCaller.useCaseDescriptors(); clientUCD = GeoportalClientCaller.useCaseDescriptors();
} }
//@Test
public void testSectionPathInnerParse() throws Exception {
String sectionPath = "$.abstractRelazione";
String profileID = "profiledConcessioni";
String projectID = "6425598a8593b215a1281e1c";
Configuration configurationGSON = Configuration.builder().jsonProvider(new GsonJsonProvider()).build();
ProjectsCaller client = GeoportalClientCaller.projects();
Project proejct = client.getProjectByID(profileID, projectID);
String jsonSourceProject = proejct.getTheDocument().toJson();
//If the section path is the Root document, passed as "$.", fixing as "$"
if(sectionPath.compareTo(FormDataObjectToJSON.JSON_$_POINTER+".")==0)
sectionPath = FormDataObjectToJSON.JSON_$_POINTER;
LOG.info("theString: {}", jsonSourceProject);
com.google.gson.JsonObject currentSectionJObject = JsonPath.parse(jsonSourceProject, configurationGSON).read(sectionPath);
LOG.info("currentSectionJObject: {}", currentSectionJObject.toString());
LOG.info("sourceSectionObject: {}", currentSectionJObject.toString());
JSONObject targetSectionJObject = new JSONObjectOrdered().instance();
targetSectionJObject.put("abstractIta", "Prova REJECT 1");
targetSectionJObject.put("titolo", "Prova REJECT abstract relazione di scavo 1");
LOG.info("targetSectionJObject: {}", targetSectionJObject.toString());
String mergedDoc = JsonMerge.merge(targetSectionJObject.toString(), currentSectionJObject.toString());
LOG.info("output: {}", mergedDoc);
String newDocJson;
//Updating path is first level of the root
if(sectionPath.equals(FormDataObjectToJSON.JSON_$_POINTER)) {
//The merged DOC is the root Document
newDocJson = mergedDoc;
}else {
//The merged DOC is a child of the root Document
Gson gson = new Gson();
JsonObject gsonOject = gson.fromJson(mergedDoc, JsonObject.class);
// Putting the merged section into Document
DocumentContext newContextDocJson = JsonPath.parse(jsonSourceProject, configurationGSON).set(sectionPath, gsonOject);
newDocJson = newContextDocJson.json().toString();
}
Document updatedDocument = Serialization.read(newDocJson.toString(), Document.class);
LOG.info("New document is: {}", updatedDocument.toJson());
}
@Test
public void testSectionPathRootParse() throws Exception {
String sectionPath = "$.";
String profileID = "profiledConcessioni";
String projectID = "6425598a8593b215a1281e1c";
Configuration configurationGSON = Configuration.builder().jsonProvider(new GsonJsonProvider()).build();
ProjectsCaller client = GeoportalClientCaller.projects();
Project proejct = client.getProjectByID(profileID, projectID);
String jsonSourceProject = proejct.getTheDocument().toJson();
//If the section path is the Root document, passed as "$.", fixing as "$"
if(sectionPath.compareTo(FormDataObjectToJSON.JSON_$_POINTER+".")==0)
sectionPath = FormDataObjectToJSON.JSON_$_POINTER;
LOG.info("theString: {}", jsonSourceProject);
com.google.gson.JsonObject currentSectionJObject = JsonPath.parse(jsonSourceProject, configurationGSON).read(sectionPath);
LOG.info("currentSectionJObject: {}", currentSectionJObject.toString());
LOG.info("sourceSectionObject: {}", currentSectionJObject.toString());
JSONObject targetSectionJObject = new JSONObjectOrdered().instance();
targetSectionJObject.put("nome", proejct.getTheDocument().get("nome")+" 1");
targetSectionJObject.put("titolo", proejct.getTheDocument().get("introduzione")+ " 1");
LOG.info("targetSectionJObject: {}", targetSectionJObject.toString());
// JSONObject mergedSection = FormDataObjectToJSON.deepMerge(sourceSectionObject,
// targetSectionJObject);
String output = JsonMerge.merge(targetSectionJObject.toString(), currentSectionJObject.toString());
LOG.info("output: {}", output);
Gson gson = new Gson();
JsonObject gsonOject = gson.fromJson(output, JsonObject.class);
// Putting the merged section into Document
DocumentContext newContextDocJson = JsonPath.parse(jsonSourceProject, configurationGSON).set(sectionPath, gsonOject);
//DocumentContext newDocument = JsonPath.parse(jsonSourceProject, configurationGSON).set(sectionPath, new JSONObject(output));
//String newDocJson = JsonPath.parse(jsonSourceProject).set(sectionPath, new JSONObject(output)).jsonString();
String newDocJson = newContextDocJson.json().toString();
LOG.info("Going to call updateProject with document: {}", newDocJson);
Document updatedDocument = Serialization.read(newDocJson.toString(), Document.class);
LOG.info("New document is: {}", updatedDocument.toJson());
}
//@Before //@Before
public void preloadgCubeProfilesForUCDs() { public void preloadgCubeProfilesForUCDs() {
LOG.debug("preloadgCubeProfilesForUCDs called"); LOG.debug("preloadgCubeProfilesForUCDs called");
@ -295,7 +444,7 @@ public class Complex_Tests {
String filesetJSONPath = String.format("%s.%s", JSON_$_POINTER, filePath.getFieldName()); String filesetJSONPath = String.format("%s.%s", JSON_$_POINTER, filePath.getFieldName());
List<Payload> listPayloads = readPayloadsForFileset(filesetJSONPath, fromSectionDocJSON); List<Payload> listPayloads = readPayloadsForFileset(filesetJSONPath, fromSectionDocJSON);
FilesetDV filesetDV = new FilesetDV(); FilesetDV filesetDV = new FilesetDV();
filesetDV.setName(filePath.getGcubeProfileFieldName()); filesetDV.setGcubeProfileFieldName(filePath.getGcubeProfileFieldName());
for (Payload payload : listPayloads) { for (Payload payload : listPayloads) {
PayloadDV payloadDV = ConvertToDataValueObjectModel.toPayloadDV(payload); PayloadDV payloadDV = ConvertToDataValueObjectModel.toPayloadDV(payload);
filesetDV.addPayloadDV(payloadDV); filesetDV.addPayloadDV(payloadDV);
@ -330,7 +479,7 @@ public class Complex_Tests {
System.out.println("***** Files"); System.out.println("***** Files");
if (subDocument.getListFiles() != null) { if (subDocument.getListFiles() != null) {
for (FilesetDV filesetDV : subDocument.getListFiles()) { for (FilesetDV filesetDV : subDocument.getListFiles()) {
System.out.println("******* File Fileset name: " + filesetDV.getName()); System.out.println("******* File Fileset name: " + filesetDV.getGcubeProfileFieldName());
for (PayloadDV payload : filesetDV.getListPayload()) { for (PayloadDV payload : filesetDV.getListPayload()) {
System.out.println("********* Payload: " + payload); System.out.println("********* Payload: " + payload);
} }
@ -339,7 +488,7 @@ public class Complex_Tests {
System.out.println("***** Images"); System.out.println("***** Images");
if (subDocument.getListImages() != null) { if (subDocument.getListImages() != null) {
for (FilesetDV filesetDV : subDocument.getListImages()) { for (FilesetDV filesetDV : subDocument.getListImages()) {
System.out.println("******* Image Fileset name: " + filesetDV.getName()); System.out.println("******* Image Fileset name: " + filesetDV.getGcubeProfileFieldName());
for (PayloadDV payload : filesetDV.getListPayload()) { for (PayloadDV payload : filesetDV.getListPayload()) {
System.out.println("********* Payload: " + payload); System.out.println("********* Payload: " + payload);
} }