geoportal-data-entry-app/src/test/java/org/gcube/portlets/user/geoportaldataentry/Complex_Tests.java

267 lines
11 KiB
Java

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;
import org.gcube.application.geoportalcommon.ConvertToDataValueObjectModel;
import org.gcube.application.geoportalcommon.ProjectDVBuilder;
import org.gcube.application.geoportalcommon.geoportal.GeoportalClientCaller;
import org.gcube.application.geoportalcommon.geoportal.ProjectsCaller;
import org.gcube.application.geoportalcommon.geoportal.UseCaseDescriptorCaller;
import org.gcube.application.geoportalcommon.shared.geoportal.ConfigurationDV;
import org.gcube.application.geoportalcommon.shared.geoportal.config.GcubeProfileDV;
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 {
private UseCaseDescriptorCaller clientUCD = 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 PROJECT_ID = "632c633155e2947b0278c999";
public static final String JSON_$_POINTER = "$";
private static final Logger LOG = LoggerFactory.getLogger(Complex_Tests.class);
@Before
public void init() {
ScopeProvider.instance.set(CONTEXT);
SecurityTokenProvider.instance.set(TOKEN);
clientPrj = GeoportalClientCaller.projects();
clientUCD = GeoportalClientCaller.useCaseDescriptors();
}
@Test
public void testReadProjectForUCDDataEntry() {
ScopeProvider.instance.set(CONTEXT);
SecurityTokenProvider.instance.set(TOKEN);
try {
System.out.println("Loading UCD for PROFILE_ID: "+PROFILE_ID);
UseCaseDescriptor ucd = clientUCD.getUCDForId(PROFILE_ID);
GEOPORTAL_DATA_HANDLER theHandler = GEOPORTAL_DATA_HANDLER.geoportal_data_entry;
List<HandlerDeclaration> handlers = ucd.getHandlersByType(theHandler.getType());
if (handlers.size() == 0)
throw new Exception("No handler " + theHandler + "found");
//Loading Handler gcube_profiles
HandlerDeclaration dataEntryHandler = handlers.get(0);
HandlerDeclarationDV handlerGcubeProfiles = ConvertToDataValueObjectModel
.toHandlerDeclarationDV(dataEntryHandler, ucd, GEOPORTAL_CONFIGURATION_TYPE.gcube_profiles);
System.out.println("Handler "+GEOPORTAL_CONFIGURATION_TYPE.gcube_profiles+" for PROFILE_ID: "+PROFILE_ID);
System.out.println(handlerGcubeProfiles);
ConfigurationDV<?> config = handlerGcubeProfiles.getConfiguration();
List<GcubeProfileDV> listGcubeProfiles = toListGcubeProfiles(config);
System.out.println("List of GcubeProfileDV are: "+listGcubeProfiles);
//Loading Metadata Profile from IS
MetadataProfileFormBuilderServiceImpl metaProfileBUilder = new MetadataProfileFormBuilderServiceImpl();
LinkedHashMap<String, List<MetaDataProfileBean>> linkedHashProfiles = new LinkedHashMap<String, List<MetaDataProfileBean>>();
for (GcubeProfileDV gcubeProfileDV : listGcubeProfiles) {
ScopeProvider.instance.set(CONTEXT);
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);
}
Project theProject = clientPrj.getProjectByID(PROFILE_ID, PROJECT_ID);
ProjectDVBuilder projectBuilder = ProjectDVBuilder.newBuilder().fullDocumentMap(true);
ProjectDV theProjectDV = ConvertToDataValueObjectModel.toProjectDV(theProject, projectBuilder);
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) {
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 = "";
String parentPathFromProfile = gcubeProfileDV.getParentName() == null ? "" : gcubeProfileDV.getParentName();
String theSectionName = gcubeProfileDV.getSectionName();
if (theSectionName.compareTo(JSON_$_POINTER) == 0 || theSectionName.compareTo(JSON_$_POINTER + ".") == 0) {
sectionJSONPath = JSON_$_POINTER;
theSectionName = "";
} else {
sectionJSONPath = String.format("%s%s",
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);
System.out.println("Data is instace of: "+data.getClass());
System.out.println("data to string: "+data.toString());
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 (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("\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.
*
* @param config the config
* @return the list
*/
private List<GcubeProfileDV> toListGcubeProfiles(ConfigurationDV<?> config) {
try {
return (List<GcubeProfileDV>) config.getConfiguration();
} catch (Exception e) {
System.err.println("Error on casting " + ConfigurationDV.class.getName() + " to List of "
+ GcubeProfileDV.class.getName());
}
return null;
}
}