197 lines
6.3 KiB
Java
197 lines
6.3 KiB
Java
package org.gcube.portlets.user.geoportaldataentry;
|
|
|
|
import java.lang.reflect.Field;
|
|
import java.util.ArrayList;
|
|
import java.util.Arrays;
|
|
import java.util.LinkedHashMap;
|
|
import java.util.List;
|
|
import java.util.Map;
|
|
|
|
import org.gcube.application.geoportalcommon.MockDocumentConfigurationReader;
|
|
import org.gcube.application.geoportalcommon.shared.geoportalconfig.GcubeProfileDV;
|
|
import org.gcube.common.scope.api.ScopeProvider;
|
|
import org.gcube.portlets.user.geoportaldataentry.shared.GeoNaFormDataObject;
|
|
import org.gcube.portlets.widgets.mpformbuilder.shared.GenericDatasetBean;
|
|
import org.json.JSONArray;
|
|
import org.json.JSONException;
|
|
import org.json.JSONObject;
|
|
import org.junit.Test;
|
|
|
|
import com.google.gwt.dev.util.collect.HashMap;
|
|
|
|
public class WriteDocument {
|
|
|
|
private static String TOKEN = "";
|
|
private static String CONTEXT = "/gcube/devsec/devVRE";
|
|
private static String USERNAME = "francesco.mangiacrapa";
|
|
|
|
// @Before
|
|
public void init() {
|
|
ScopeProvider.instance.set(CONTEXT);
|
|
}
|
|
|
|
//@Test
|
|
public void writeMockJson() throws Exception {
|
|
|
|
List<GeoNaFormDataObject> listGeonaFormObjects = new ArrayList<GeoNaFormDataObject>();
|
|
MockDocumentConfigurationReader mock = new MockDocumentConfigurationReader();
|
|
System.out.println(mock.getListDocumentConfig());
|
|
|
|
LinkedHashMap<String, List<String>> formDataEntryFields = new LinkedHashMap<String, List<String>>();
|
|
formDataEntryFields.put("Title", Arrays.asList("My Title"));
|
|
formDataEntryFields.put("My Desr", Arrays.asList("My Descr Value"));
|
|
formDataEntryFields.put("Field1", Arrays.asList("Field1 Value"));
|
|
|
|
List<GenericDatasetBean> listGDC = new ArrayList<GenericDatasetBean>();
|
|
GenericDatasetBean genericDatasetBean = new GenericDatasetBean();
|
|
genericDatasetBean.setFormDataEntryFields(formDataEntryFields);
|
|
listGDC.add(genericDatasetBean);
|
|
// GenericDatasetBean gdb = new GenericDatasetBean(null, formDataEntryFields, null);
|
|
//
|
|
// for (GenericDatasetBean genericDatasetBean : listGDC) {
|
|
// genericDatasetBean
|
|
// }
|
|
|
|
GeoNaFormDataObject gnform = new GeoNaFormDataObject(listGDC,
|
|
mock.getListDocumentConfig().get(0).getConfiguration().getGcubeProfiles().get(1));
|
|
System.out.println(gnform);
|
|
|
|
listGeonaFormObjects.add(gnform);
|
|
|
|
JSONObject rootDocument = JSONObjecOrdered.instance();
|
|
for (GeoNaFormDataObject geoNaFormDataObject : listGeonaFormObjects) {
|
|
|
|
List<GenericDatasetBean> listGDB = geoNaFormDataObject.getListGDB();
|
|
|
|
GcubeProfileDV profile = geoNaFormDataObject.getGcubeProfileDV();
|
|
System.out.println("The profile is: " + profile);
|
|
|
|
String jsonPathExp = String.format("%s%s", profile.getParentName(), profile.getSectionName());
|
|
//jsonPathExp = "$.chidl1.child2.child3";
|
|
|
|
jsonPathExp = jsonPathExp.replaceFirst("\\$", "");
|
|
System.out.println("The json path to build: " + jsonPathExp);
|
|
|
|
JSONObject sectRootObject = JSONObjecOrdered.instance();
|
|
|
|
for (GenericDatasetBean gdb : listGDB) {
|
|
|
|
JSONObject jsonObject = genericDatasetBeanToJSON(gdb);
|
|
String sectionToString = jsonObject.toString();
|
|
System.out.println("Adding section : " + sectionToString);
|
|
|
|
String[] jsonPathDeep = jsonPathExp.split("\\.");
|
|
|
|
System.out.println("jsonPathDeep: " + Arrays.asList(jsonPathDeep) + " size: " + jsonPathDeep.length);
|
|
|
|
// ROOT $.
|
|
if (jsonPathDeep.length == 0) {
|
|
sectRootObject = deepMerge(jsonObject, sectRootObject);
|
|
System.out.println("JSON:"+sectRootObject);
|
|
//rootDocument.put(jsonPathDeep[0], sectRootObject);
|
|
rootDocument = deepMerge(sectRootObject, rootDocument);
|
|
}
|
|
|
|
// DEPTH >= 1
|
|
if (jsonPathDeep.length > 1) {
|
|
sectRootObject.put(jsonPathDeep[jsonPathDeep.length - 1], jsonObject);
|
|
JSONObject deepJSON = sectRootObject;
|
|
System.out.println("sectRootObject: " + sectRootObject);
|
|
for (int i = jsonPathDeep.length - 2; i > 0; i--) {
|
|
JSONObject newOne = JSONObjecOrdered.instance();
|
|
newOne.put(jsonPathDeep[i], deepJSON);
|
|
deepJSON = newOne;
|
|
}
|
|
System.out.println("ext deepJSON: " + deepJSON);
|
|
rootDocument = deepMerge(deepJSON, rootDocument);
|
|
}
|
|
|
|
}
|
|
System.out.println("Document: " + rootDocument); // Print it with specified indentation
|
|
|
|
}
|
|
}
|
|
|
|
|
|
public static class JSONObjecOrdered {
|
|
|
|
public static JSONObject instance() {
|
|
JSONObject jsonObject = new JSONObject();
|
|
try {
|
|
Field changeMap = jsonObject.getClass().getDeclaredField("map");
|
|
changeMap.setAccessible(true);
|
|
changeMap.set(jsonObject, new LinkedHashMap<>());
|
|
changeMap.setAccessible(false);
|
|
} catch (IllegalAccessException | NoSuchFieldException e) {
|
|
}
|
|
return jsonObject;
|
|
}
|
|
}
|
|
|
|
private JSONObject innerJSONBuilder(JSONObject source, String key, JSONObject jsonValue) throws JSONException {
|
|
|
|
source.put(key, jsonValue);
|
|
System.out.println("innerJSONBuilder: " + source.toString(4));
|
|
return source.getJSONObject(key);
|
|
}
|
|
|
|
private JSONObject genericDatasetBeanToJSON(GenericDatasetBean gdb) throws JSONException {
|
|
|
|
JSONObject sectJSONObject = JSONObjecOrdered.instance();
|
|
|
|
Map<String, List<String>> mapFields = gdb.getFormDataEntryFields();
|
|
System.out.println("Map is: "+mapFields);
|
|
for (String key : mapFields.keySet()) {
|
|
List<String> listValues = mapFields.get(key);
|
|
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) {
|
|
array.put(value);
|
|
}
|
|
|
|
sectJSONObject.put(key, array);
|
|
}
|
|
|
|
System.out.println("sectJSONObject: "+sectJSONObject);
|
|
return sectJSONObject;
|
|
|
|
}
|
|
|
|
/**
|
|
* Merge "source" into "target". If fields have equal name, merge them
|
|
* recursively.
|
|
*
|
|
* @return the merged object (target).
|
|
*/
|
|
public static JSONObject deepMerge(JSONObject source, JSONObject target) throws JSONException {
|
|
for (String key : JSONObject.getNames(source)) {
|
|
System.out.println("key: "+key);
|
|
Object value = source.get(key);
|
|
if (!target.has(key)) {
|
|
// new value for "key":
|
|
target.put(key, value);
|
|
} else {
|
|
// existing value for "key" - recursively deep merge:
|
|
if (value instanceof JSONObject) {
|
|
JSONObject valueJson = (JSONObject) value;
|
|
deepMerge(valueJson, target.getJSONObject(key));
|
|
} else {
|
|
target.put(key, value);
|
|
}
|
|
}
|
|
}
|
|
return target;
|
|
}
|
|
|
|
}
|