In progress the procedure to transform the JSON document into

dispalyable sections
This commit is contained in:
Francesco Mangiacrapa 2022-10-06 17:31:10 +02:00
parent 267b1fd086
commit 848f7acb6a
5 changed files with 148 additions and 25 deletions

View File

@ -1,5 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?><project-modules id="moduleCoreId" project-version="1.5.0">
@ -50,7 +51,8 @@
<wb-module deploy-name="geoportal-data-entry-app-3.0.0-SNAPSHOT">
@ -101,7 +103,8 @@
<wb-resource deploy-path="/" source-path="/target/m2e-wtp/web-resources"/>
@ -152,7 +155,8 @@
<wb-resource deploy-path="/" source-path="/src/main/webapp" tag="defaultRootSource"/>
@ -203,13 +207,21 @@
<wb-resource deploy-path="/WEB-INF/classes" source-path="/src/main/java"/>
<wb-resource deploy-path="/WEB-INF/classes" source-path="/src/main/resources"/>
<dependent-module archiveName="metadata-profile-form-builder-widget-2.0.0-SNAPSHOT.jar" deploy-path="/WEB-INF/lib" handle="module:/resource/metadata-profile-form-builder-widget/metadata-profile-form-builder-widget">
<dependency-type>uses</dependency-type>
</dependent-module>
<dependent-module archiveName="metadata-profile-discovery-1.1.0-SNAPSHOT.jar" deploy-path="/WEB-INF/lib" handle="module:/resource/metadata-profile-discovery/metadata-profile-discovery">
<dependency-type>uses</dependency-type>
</dependent-module>
<dependent-module archiveName="geoportal-data-common-2.0.0-SNAPSHOT.jar" deploy-path="/WEB-INF/lib" handle="module:/resource/geoportal-data-common/geoportal-data-common">
<dependency-type>uses</dependency-type>
</dependent-module>
@ -260,7 +272,8 @@
<property name="context-root" value="geoportal-data-entry-app"/>
@ -311,7 +324,8 @@
<property name="java-output-path" value="/geoportal-data-entry-app/target/geoportal-data-entry-app-2.0.0-SNAPSHOT/WEB-INF/classes"/>
@ -362,7 +376,8 @@
</wb-module>

View File

@ -237,6 +237,12 @@
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>

View File

@ -91,9 +91,7 @@ import com.google.gwt.event.logical.shared.ResizeHandler;
import com.google.gwt.event.shared.HandlerManager;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HTMLPanel;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;

View File

@ -1,8 +1,10 @@
package org.gcube.portlets.user.geoportaldataentry;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import org.bson.Document;
import org.gcube.application.geoportal.common.model.document.Project;
import org.gcube.application.geoportal.common.model.useCaseDescriptor.HandlerDeclaration;
import org.gcube.application.geoportal.common.model.useCaseDescriptor.UseCaseDescriptor;
@ -17,17 +19,22 @@ import org.gcube.application.geoportalcommon.shared.geoportal.project.ProjectDV;
import org.gcube.application.geoportalcommon.shared.geoportal.ucd.GEOPORTAL_CONFIGURATION_TYPE;
import org.gcube.application.geoportalcommon.shared.geoportal.ucd.GEOPORTAL_DATA_HANDLER;
import org.gcube.application.geoportalcommon.shared.geoportal.ucd.HandlerDeclarationDV;
import org.gcube.application.geoportalcommon.shared.geoportal.view.ProjectView;
import org.gcube.application.geoportalcommon.shared.geoportal.view.SectionView;
import org.gcube.application.geoportalcommon.shared.geoportal.view.SubDocumentView;
import org.gcube.common.authorization.library.provider.SecurityTokenProvider;
import org.gcube.common.scope.api.ScopeProvider;
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.MetadataFieldWrapper;
import org.json.JSONObject;
import org.junit.Before;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.jayway.jsonpath.JsonPath;
import com.jayway.jsonpath.spi.json.JsonOrgJsonProvider;
public class Complex_Tests {
@ -37,12 +44,12 @@ public class Complex_Tests {
private static String TOKEN = ""; // devVRE
private static String PROFILE_ID = "profiledConcessioni";
private static String PROJECT_ID = "630f905855e2947b0278c1a8";
private static String PROJECT_ID = "632c633155e2947b0278c999";
public static final String JSON_$_POINTER = "$";
private static final Logger LOG = LoggerFactory.getLogger(Complex_Tests.class);
//@Before
@Before
public void init() {
ScopeProvider.instance.set(CONTEXT);
SecurityTokenProvider.instance.set(TOKEN);
@ -50,7 +57,7 @@ public class Complex_Tests {
clientUCD = GeoportalClientCaller.useCaseDescriptors();
}
//@Test
@Test
public void testReadProjectForUCDDataEntry() {
ScopeProvider.instance.set(CONTEXT);
SecurityTokenProvider.instance.set(TOKEN);
@ -84,6 +91,8 @@ public class Complex_Tests {
SecurityTokenProvider.instance.set(TOKEN);
List<MetaDataProfileBean> listProfiles = metaProfileBUilder.getProfilesInTheScopeForName(CONTEXT,
gcubeProfileDV.getGcubeSecondaryType(), gcubeProfileDV.getGcubeName());
String key = gcubeProfileDV.getGcubeSecondaryType()+gcubeProfileDV.getGcubeName();
System.out.println("for key: "+key+ " readi profiles: "+listGcubeProfiles);
linkedHashProfiles.put(gcubeProfileDV.getGcubeSecondaryType()+gcubeProfileDV.getGcubeName(), listProfiles);
@ -92,12 +101,25 @@ public class Complex_Tests {
Project theProject = clientPrj.getProjectByID(PROFILE_ID, PROJECT_ID);
ProjectDVBuilder projectBuilder = ProjectDVBuilder.newBuilder().fullDocumentMap(true);
ProjectDV theProjectDV = ConvertToDataValueObjectModel.toProjectDV(theProject, projectBuilder);
System.out.println("theProjectDV as JSON: "+theProject.getTheDocument().toJson());
String theWholeProjectAsJSON = theProject.getTheDocument().toJson();
System.out.println("theProjectDV as JSON: "+theWholeProjectAsJSON);
System.out.println("theProjectDV as MAP: "+theProjectDV.getTheDocument().getDocumentAsMap());
ProjectView projectView = new ProjectView();
projectView.setTheProjectDV(theProjectDV);
com.jayway.jsonpath.Configuration configuration = com.jayway.jsonpath.Configuration.builder()
.jsonProvider(new JsonOrgJsonProvider()).build();
for (GcubeProfileDV gcubeProfileDV : listGcubeProfiles) {
LOG.debug("The profile is: " + gcubeProfileDV);
SectionView sectionView = new SectionView();
sectionView.setSectionTitle(gcubeProfileDV.getSectionTitle());
LOG.debug("\n\nThe profile is: " + gcubeProfileDV);
// Building JSON/section full PATH and section name
String sectionJSONPath = "";
@ -112,33 +134,116 @@ public class Complex_Tests {
parentPathFromProfile.endsWith(".") ? parentPathFromProfile : parentPathFromProfile + ".", theSectionName);
}
LOG.debug("The sectionJSONPath is: " + sectionJSONPath);
//Object jsonFile = configuration.jsonProvider().parse(theWholeProjectAsJSON);
//Object data = JsonPath.using(configuration).parse(theWholeProjectAsJSON).read(sectionJSONPath);
JsonPath jsonPath = JsonPath.compile(sectionJSONPath);
Object data= jsonPath.read(theWholeProjectAsJSON, configuration);
// Object jsonFile = configuration.jsonProvider().parse(theWholeProjectAsJSON);
// Object data = JsonPath.read(jsonFile, sectionJSONPath);
// Building Parent PATH
String parentPath = sectionJSONPath.compareTo(JSON_$_POINTER) == 0 ? JSON_$_POINTER
: sectionJSONPath.substring(0, sectionJSONPath.lastIndexOf("."));
System.out.println("Data is instace of: "+data.getClass());
System.out.println("data to string: "+data.toString());
JsonPath parentJSONPath = JsonPath.compile(parentPath);
List<Document> listBSONDocument = new ArrayList<Document>();
if(data instanceof org.json.JSONObject) {
String jsonString = data.toString();
Document doc = Document.parse(jsonString);
System.out.println("the JSON to string: "+doc.toJson());
listBSONDocument.add(doc);
}else if(data instanceof org.json.JSONArray) {
org.json.JSONArray dataArray = (org.json.JSONArray) data;
for (int i=0; i<dataArray.length(); i++) {
String jsonString = dataArray.get(i).toString();
Document doc = Document.parse(jsonString);
System.out.println("the array "+i +" JSON to string: "+doc.toJson());
listBSONDocument.add(doc);
}
}
System.out.println("Result for "+gcubeProfileDV.getSectionName() + " is: "+listBSONDocument);
//List<SubDocumentView> listSubDocuments = new ArrayList<SubDocumentView>();
List<MetaDataProfileBean> theProfileBeans = linkedHashProfiles.get(gcubeProfileDV.getGcubeSecondaryType()+gcubeProfileDV.getGcubeName());
MetaDataProfileBean theProfileBean = theProfileBeans.get(0);
for (MetadataFieldWrapper metadataField : theProfileBean.getMetadataFields()) {
String theFieldName = metadataField.getFieldId()!=null?metadataField.getFieldId():metadataField.getFieldName();
theProjectDV.getTheDocument().getDocumentAsMap().get(linkedHashProfiles);
for (int i = 0; i < listBSONDocument.size(); i++) {
Document fromDoc = listBSONDocument.get(i);
SubDocumentView subDocumentView = new SubDocumentView();
// LOG.debug("subDocumentString is: " + subDocumentString);
// Document doc = Document.parse(subDocumentString);
// DocumentDV readTheDocument = ConvertToDataValueObjectModel.toGenericDocumentDV(doc, DocumentDV.class, null, true);
// LOG.debug("toDocumentDV is: " + readTheDocument);
Document toDoc = new Document();
for (MetadataFieldWrapper metadataField : theProfileBean.getMetadataFields()) {
String theFieldName = metadataField.getFieldId()!=null?metadataField.getFieldId():metadataField.getFieldName();
LOG.debug("reading theFieldName: " + theFieldName);
Object theOBJFieldIntoDocument = fromDoc.get(theFieldName);
toDoc = sanitizeDocumentValue(toDoc, metadataField.getFieldName(), theOBJFieldIntoDocument);
}
String subToJSON = toDoc.toJson();
LOG.debug("theSubSetionDoc is: " + subToJSON);
subDocumentView.setMetadataAsJSON(toDoc.toJson());
sectionView.addSubDocument(subDocumentView);
}
projectView.addSectionView(sectionView);
}
for (String key : linkedHashProfiles.keySet()) {
System.out.println("For key: "+key+" got profiles: "+linkedHashProfiles.get(key));
}
for (SectionView section : projectView.getListSections()) {
System.out.println("##### Section Title: " + section.getSectionTitle());
int i = 0;
for (SubDocumentView subDocument : section.getListSubDocuments()) {
System.out.println("\t " + ++i + ") SubDocumentView meta: "
+ new JSONObject(subDocument.getMetadataAsJSON()).toString(2));
}
}
} catch (Exception e) {
System.out.println("error");
e.printStackTrace();
}
System.out.println("termimated");
System.out.println("\n\n Procedure termimated!!!");
}
private Document sanitizeDocumentValue(Document toDoc, String fieldLabel, Object theObjectField) {
if(theObjectField != null) {
//LOG.debug("theOBJFieldIntoDocument instance of: " + theObjectField.getClass() + " value: "+theObjectField);
if(theObjectField instanceof String) {
String toString = (String) theObjectField;
if(toString!=null && !toString.isEmpty()) {
toDoc.append(fieldLabel, theObjectField);
}
}else if(theObjectField instanceof ArrayList) {
ArrayList toArrayList = (ArrayList) theObjectField;
if(toArrayList!=null && !toArrayList.isEmpty()) {
toDoc.append(fieldLabel, theObjectField);
}
}
toDoc.append(fieldLabel, theObjectField);
}
return toDoc;
}
/**
* To list gcube profiles.

View File

@ -13,7 +13,6 @@ import org.gcube.application.geoportalcommon.shared.geoportal.ucd.UseCaseDescrip
import org.gcube.common.authorization.library.provider.SecurityTokenProvider;
import org.gcube.common.scope.api.ScopeProvider;
import org.junit.Before;
import org.junit.Test;
public class UCD_Tests {