diff --git a/src/test/java/org/gcube/portlets/user/geoportaldataentry/Complex_Tests.java b/src/test/java/org/gcube/portlets/user/geoportaldataentry/Complex_Tests.java index 650e92a..b7ad298 100644 --- a/src/test/java/org/gcube/portlets/user/geoportaldataentry/Complex_Tests.java +++ b/src/test/java/org/gcube/portlets/user/geoportaldataentry/Complex_Tests.java @@ -5,6 +5,7 @@ import java.util.LinkedHashMap; import java.util.List; import org.bson.Document; +import org.gcube.application.geoportal.client.utils.Serialization; 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; @@ -13,12 +14,16 @@ 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.geoportal.access.GeportalCheckAccessPolicy; import org.gcube.application.geoportalcommon.shared.geoportal.ConfigurationDV; +import org.gcube.application.geoportalcommon.shared.geoportal.config.FilePathDV; 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.FilesetDV; +import org.gcube.application.geoportalcommon.shared.geoportal.view.PayloadDV; 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; @@ -27,12 +32,16 @@ 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.json.JSONArray; import org.junit.Before; import org.junit.Test; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; +import com.google.gson.JsonObject; +import com.google.gson.JsonParser; import com.jayway.jsonpath.JsonPath; import com.jayway.jsonpath.spi.json.JsonOrgJsonProvider; @@ -45,11 +54,13 @@ public class Complex_Tests { private static String PROFILE_ID = "profiledConcessioni"; private static String PROJECT_ID = "632c633155e2947b0278c999"; - + + private static String MY_LOGIN = "francesco.mangiacrapa"; + 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); @@ -57,13 +68,13 @@ public class Complex_Tests { clientUCD = GeoportalClientCaller.useCaseDescriptors(); } - //@Test + @Test public void testReadProjectForUCDDataEntry() { ScopeProvider.instance.set(CONTEXT); SecurityTokenProvider.instance.set(TOKEN); try { - System.out.println("Loading UCD for PROFILE_ID: "+PROFILE_ID); + LOG.debug("Loading UCD for PROFILE_ID: " + PROFILE_ID); UseCaseDescriptor ucd = clientUCD.getUCDForId(PROFILE_ID); GEOPORTAL_DATA_HANDLER theHandler = GEOPORTAL_DATA_HANDLER.geoportal_data_entry; List handlers = ucd.getHandlersByType(theHandler.getType()); @@ -71,19 +82,20 @@ public class Complex_Tests { if (handlers.size() == 0) throw new Exception("No handler " + theHandler + "found"); - //Loading Handler gcube_profiles + // 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); + + LOG.debug("Handler " + GEOPORTAL_CONFIGURATION_TYPE.gcube_profiles + " for PROFILE_ID: " + PROFILE_ID); + LOG.debug("" + handlerGcubeProfiles); ConfigurationDV config = handlerGcubeProfiles.getConfiguration(); + // List of gCube Profiles defined in the UCD List listGcubeProfiles = toListGcubeProfiles(config); - System.out.println("List of GcubeProfileDV are: "+listGcubeProfiles); - - //Loading Metadata Profile from IS + LOG.debug("List of GcubeProfileDV are: " + listGcubeProfiles); + + // Loading Metadata Profile from IS MetadataProfileFormBuilderServiceImpl metaProfileBUilder = new MetadataProfileFormBuilderServiceImpl(); LinkedHashMap> linkedHashProfiles = new LinkedHashMap>(); for (GcubeProfileDV gcubeProfileDV : listGcubeProfiles) { @@ -91,125 +103,170 @@ public class Complex_Tests { SecurityTokenProvider.instance.set(TOKEN); List 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); + + String key = gcubeProfileDV.getGcubeSecondaryType() + gcubeProfileDV.getGcubeName(); + LOG.debug("for key: " + key + " readi profiles: " + listGcubeProfiles); + linkedHashProfiles.put(gcubeProfileDV.getGcubeSecondaryType() + gcubeProfileDV.getGcubeName(), + listProfiles); } - + + for (String key : linkedHashProfiles.keySet()) { + LOG.debug("For key: " + key + " got profiles: " + linkedHashProfiles.get(key)); + } + 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()); - + + LOG.debug("theProjectDV as JSON: " + theWholeProjectAsJSON); + LOG.debug("theProjectDV as MAP: " + theProjectDV.getTheDocument().getDocumentAsMap()); + ProjectView projectView = new ProjectView(); projectView.setTheProjectDV(theProjectDV); + // NO UCD defined, applyong default + if (listGcubeProfiles.size() == 0) { + LOG.warn("No " + GEOPORTAL_CONFIGURATION_TYPE.gcube_profiles + " found in the UCD"); + LOG.info("Applying default business logic to display the project"); + SectionView sectionView = new SectionView(); + sectionView.setSectionTitle("Document"); + SubDocumentView subDocumentView = new SubDocumentView(); + subDocumentView.setMetadataAsJSON(theProjectDV.getTheDocument().getDocumentAsJSON()); + sectionView.addSubDocument(subDocumentView); + projectView.addSectionView(sectionView); + } + 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); + // Reading the Project according to list of Profile defined in the UCD + 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 parentPathFromProfile = gcubeProfileDV.getParentName() == null ? "" + : gcubeProfileDV.getParentName(); String theSectionName = gcubeProfileDV.getSectionName(); - if (theSectionName.compareTo(JSON_$_POINTER) == 0 || theSectionName.compareTo(JSON_$_POINTER + ".") == 0) { + 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); + 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()); - + JsonPath theSectionJsonPath = JsonPath.compile(sectionJSONPath); + Object data = theSectionJsonPath.read(theWholeProjectAsJSON, configuration); + + LOG.debug("Data is instace of: " + data.getClass()); + LOG.debug("data to string: " + data.toString()); + + // Splitting the General Document in bson.Document according to list of + // GcubeProfiles List listBSONDocument = new ArrayList(); - if(data instanceof org.json.JSONObject) { + 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) { + LOG.debug("the JSON to string: " + jsonString); + Document sectionDoc = Document.parse(jsonString); + boolean isAccessibleSection = isAccessibleSectionAccordingToPolicy(sectionDoc, sectionJSONPath); + if (isAccessibleSection) { + listBSONDocument.add(sectionDoc); + } + + } else if (data instanceof org.json.JSONArray) { org.json.JSONArray dataArray = (org.json.JSONArray) data; - for (int i=0; i listSubDocuments = new ArrayList(); - List theProfileBeans = linkedHashProfiles.get(gcubeProfileDV.getGcubeSecondaryType()+gcubeProfileDV.getGcubeName()); + + LOG.debug("Result for " + gcubeProfileDV.getSectionName() + " is: " + listBSONDocument); + List 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); + // For each bson.Document creating the SubDocumentView + for (int i = 0; i < listBSONDocument.size(); i++) { + Document fromSectionDoc = listBSONDocument.get(i); + SubDocumentView subDocumentView = new SubDocumentView(); + Document toSectionDoc = new Document(); + // Filling the SubDocumentView metadata with the metadataField.getFieldName() + // read from the Profile + for (MetadataFieldWrapper metadataField : theProfileBean.getMetadataFields()) { + + String theFieldName = metadataField.getFieldId() != null ? metadataField.getFieldId() + : metadataField.getFieldName(); + LOG.debug("reading theFieldName: " + theFieldName); + Object theOBJFieldValue = fromSectionDoc.get(theFieldName); + + // NB: Using ALWAYS THE metadataField.getFieldName() as LABEL + toSectionDoc = sanitizeDocumentValue(toSectionDoc, metadataField.getFieldName(), + theOBJFieldValue); } - String subToJSON = toDoc.toJson(); + String subToJSON = toSectionDoc.toJson(); LOG.debug("theSubSetionDoc is: " + subToJSON); - subDocumentView.setMetadataAsJSON(toDoc.toJson()); + subDocumentView.setMetadataAsJSON(toSectionDoc.toJson()); + + // Reading filePaths + List filePaths = gcubeProfileDV.getFilePaths(); + + // READING fileset* field ACCORDING TO filePaths OF THE 'gcubeProfiles' CONFIG + if (filePaths != null) { + String fromSectionDocJSON = fromSectionDoc.toJson(); + List listFileSet = new ArrayList(); + for (FilePathDV filePath : filePaths) { + + String filesetJSONPath = String.format("%s.%s", JSON_$_POINTER, filePath.getFieldName()); + List listPayloads = readPayloadsForFileset(filesetJSONPath, fromSectionDocJSON); + FilesetDV filesetDV = new FilesetDV(); + filesetDV.setName(filePath.getGcubeProfileFieldName()); + filesetDV.addListPayloadsDV(listPayloads); + listFileSet.add(filesetDV); + } + subDocumentView.setListFiles(listFileSet); + } + 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; + System.out.println("\n\n###### Section Title: " + section.getSectionTitle() + " ######"); + int i = 1; for (SubDocumentView subDocument : section.getListSubDocuments()) { - System.out.println("\t " + ++i + ") SubDocumentView meta: " - + new JSONObject(subDocument.getMetadataAsJSON()).toString(2)); + System.out.println("## "+SubDocumentView.class.getSimpleName()+" n."+i); + System.out.println("***** Metadata"); + System.out.println(prettyPrintJSON(subDocument.getMetadataAsJSON())); + System.out.println("***** Files"); + if (subDocument.getListFiles() != null) { + for (FilesetDV filesetDV : subDocument.getListFiles()) { + System.out.println("******* Fileset name: " + filesetDV.getName()); + for (PayloadDV payload : filesetDV.getListPayload()) { + System.out.println("********* Payload: " + payload); + } + } + } + i++; } } @@ -217,33 +274,110 @@ public class Complex_Tests { 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); + + private List readPayloadsForFileset(String filesetJSONPath, String sectionJSONDocument) { + LOG.debug("readPayloadsForFileset called"); + + List listPayloads = new ArrayList(); + String _payloadsJSONPath = String.format("%s.%s", filesetJSONPath, "_payloads"); + try { + com.jayway.jsonpath.Configuration configuration = com.jayway.jsonpath.Configuration.builder() + .jsonProvider(new JsonOrgJsonProvider()).build(); + + LOG.info("Reading sectionPath at {} into section document {}", _payloadsJSONPath, sectionJSONDocument); + JsonPath theSectionPolycJsonPath = JsonPath.compile(_payloadsJSONPath); + Object _payloads = theSectionPolycJsonPath.read(sectionJSONDocument, configuration).toString(); + + if (_payloads instanceof String) { + String toStringPayloads = (String) _payloads; + LOG.trace("The _payloads is a String {}", toStringPayloads); + JSONArray jsonArray = new JSONArray(toStringPayloads); + for (int i = 0; i < jsonArray.length(); i++) { + PayloadDV payloadDV = Serialization.read(jsonArray.getJSONObject(i).toString(), PayloadDV.class); + listPayloads.add(payloadDV); } } - - toDoc.append(fieldLabel, theObjectField); + + LOG.info("returning list of payloads {}", listPayloads); + } catch (Exception e) { + LOG.error("Error on reading the JSON Path {} in the doc {} ", _payloadsJSONPath, sectionJSONDocument); + e.printStackTrace(); } - + + return listPayloads; + + } + + private boolean isAccessibleSectionAccordingToPolicy(Document section, String sectionJSONPath) { + LOG.debug("isAccessibleSectionAccordingToPolicy called"); + boolean isAccessible = true; + + // Skipping the root, going to check the access_policy of subsections + if (sectionJSONPath.compareTo(JSON_$_POINTER) != 0) { + isAccessible = checkAccessPolicy(section.toJson(), MY_LOGIN); + } + + return isAccessible; + } + + private boolean checkAccessPolicy(String sectionDocumentJSON, String myLogin) { + LOG.debug("checkAccessPolicy called"); + // CHECKING THE POLICY + String accessPolicyPath = JSON_$_POINTER + "._access._policy"; + boolean isAccessible = true; + try { + com.jayway.jsonpath.Configuration configuration = com.jayway.jsonpath.Configuration.builder() + .jsonProvider(new JsonOrgJsonProvider()).build(); + + LOG.info("Reading access policy at {} into section document {}", accessPolicyPath, sectionDocumentJSON); + JsonPath theSectionPolycJsonPath = JsonPath.compile(accessPolicyPath); + String _policy = theSectionPolycJsonPath.read(sectionDocumentJSON, configuration).toString(); + LOG.info("The section {} has policy {}", accessPolicyPath, _policy); + isAccessible = GeportalCheckAccessPolicy.isAccessible(_policy, myLogin); + } catch (Exception e) { + LOG.error(accessPolicyPath + " not found. Check OK"); + } + LOG.info("Is the section {} accessible? {}", sectionDocumentJSON, isAccessible); + return isAccessible; + } + + private String prettyPrintJSON(String jsonString) { + + Gson gson = new GsonBuilder().setPrettyPrinting().create(); + JsonObject jsonObject = new JsonParser().parse(jsonString).getAsJsonObject(); + return gson.toJson(jsonObject); + } + + private Document sanitizeDocumentValue(Document toDoc, String fieldLabel, Object theObjectFieldValue) { + + if (theObjectFieldValue != null) { + if (theObjectFieldValue instanceof String) { + String toString = (String) theObjectFieldValue; + if (toString != null && !toString.isEmpty()) { + toDoc.append(fieldLabel, theObjectFieldValue); + } else { + LOG.debug("Skipping String field " + fieldLabel + " its value is null or empty"); + } + + } else if (theObjectFieldValue instanceof ArrayList) { + ArrayList toArrayList = (ArrayList) theObjectFieldValue; + if (toArrayList != null && !toArrayList.isEmpty()) { + toDoc.append(fieldLabel, theObjectFieldValue); + } else { + LOG.debug("Skipping ArrayList field " + fieldLabel + " its value is null or empty"); + } + } else { + toDoc.append(fieldLabel, theObjectFieldValue); + } + } else { + LOG.debug("Skipping field " + fieldLabel + " its value is null or empty"); + } + return toDoc; } - /** * To list gcube profiles.