diff --git a/src/main/java/org/gcube/application/geoportalcommon/ConvertToDataValueObjectModel.java b/src/main/java/org/gcube/application/geoportalcommon/ConvertToDataValueObjectModel.java index 6607667..869b426 100644 --- a/src/main/java/org/gcube/application/geoportalcommon/ConvertToDataValueObjectModel.java +++ b/src/main/java/org/gcube/application/geoportalcommon/ConvertToDataValueObjectModel.java @@ -7,6 +7,7 @@ import java.time.format.DateTimeFormatter; import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; +import java.util.HashSet; import java.util.Iterator; import java.util.LinkedHashMap; import java.util.List; @@ -23,6 +24,7 @@ import org.gcube.application.geoportal.common.model.document.lifecycle.Lifecycle import org.gcube.application.geoportal.common.model.document.relationships.Relationship; import org.gcube.application.geoportal.common.model.useCaseDescriptor.HandlerDeclaration; import org.gcube.application.geoportal.common.model.useCaseDescriptor.UseCaseDescriptor; +import org.gcube.application.geoportalcommon.geoportal.GeoportalConfigUtil; import org.gcube.application.geoportalcommon.geoportal.config.ActionDefinition; import org.gcube.application.geoportalcommon.geoportal.config.FilePath; import org.gcube.application.geoportalcommon.geoportal.config.GcubeProfile; @@ -106,7 +108,7 @@ public class ConvertToDataValueObjectModel { List listHandlersDV = new ArrayList(); if (handlersIds != null) { - LOG.info("List of input handlersIds is "+handlersIds+", converting it"); + LOG.info("List of input handlersIds is " + handlersIds + ", converting it"); Map> handlersMapByID = ucd.getHandlersMapByID(); if (handlersMapByID != null) { @@ -114,7 +116,7 @@ public class ConvertToDataValueObjectModel { List listHandlerDeclaration = handlersMapByID.get(handlersId); for (HandlerDeclaration handlerDeclaration : listHandlerDeclaration) { LOG.debug("converting handler {}", handlerDeclaration); - HandlerDeclarationDV hdDV = toHandlerDeclarationDV(handlerDeclaration, ucd.getName()); + HandlerDeclarationDV hdDV = toHandlerDeclarationDV(handlerDeclaration, ucd); if (hdDV != null) { LOG.debug("handler converted to {}", hdDV); listHandlersDV.add(hdDV); @@ -131,7 +133,7 @@ public class ConvertToDataValueObjectModel { List listHandlerDeclaration = ucd.getHandlers(); for (HandlerDeclaration handlerDeclaration : listHandlerDeclaration) { LOG.debug("converting handler {}", handlerDeclaration); - HandlerDeclarationDV hdDV = toHandlerDeclarationDV(handlerDeclaration, ucd.getName()); + HandlerDeclarationDV hdDV = toHandlerDeclarationDV(handlerDeclaration, ucd); if (hdDV != null) { LOG.debug("handler converted to {}", hdDV); listHandlersDV.add(hdDV); @@ -177,10 +179,12 @@ public class ConvertToDataValueObjectModel { * @return the handler declaration DV * @throws Exception the exception */ - public static HandlerDeclarationDV toHandlerDeclarationDV(HandlerDeclaration handlerDeclaration, String profileName, - GEOPORTAL_CONFIGURATION_TYPE... readConfigs) throws Exception { - LOG.debug("toHandlerDeclarationDV called [handlerDeclaration: "+handlerDeclaration+"], [profileName: "+profileName+"], [readConfigs: "+readConfigs+"]"); - + public static HandlerDeclarationDV toHandlerDeclarationDV(HandlerDeclaration handlerDeclaration, + UseCaseDescriptor ucd, GEOPORTAL_CONFIGURATION_TYPE... readConfigs) throws Exception { + String profileName = ucd.getName(); + LOG.debug("toHandlerDeclarationDV called [handlerDeclaration: " + handlerDeclaration + "], [profileName: " + + profileName + "], [readConfigs: " + readConfigs + "]"); + if (handlerDeclaration == null) { LOG.warn(HandlerDeclaration.class.getSimpleName() + " is null"); return null; @@ -252,7 +256,6 @@ public class ConvertToDataValueObjectModel { } } - if (jsonConfigurations == null) { String error = "No managed configurations as '" + Arrays.asList(GEOPORTAL_CONFIGURATION_TYPE.values()).toString() + "' found in the handler " @@ -260,9 +263,9 @@ public class ConvertToDataValueObjectModel { LOG.warn(error); return null; } - + for (GEOPORTAL_CONFIGURATION_TYPE geoportalConfigType : listGeoportalConfigType) { - + switch (geoportalConfigType) { case gcube_profiles: { @@ -271,8 +274,8 @@ public class ConvertToDataValueObjectModel { int i = 0; for (String asJSONString : jsonConfigurations) { LOG.debug(++i + ") the gCubeProfile is: " + asJSONString); - GcubeProfile profile = org.gcube.application.geoportal.client.utils.Serialization.read(asJSONString, - GcubeProfile.class); + GcubeProfile profile = org.gcube.application.geoportal.client.utils.Serialization + .read(asJSONString, GcubeProfile.class); listGcubeProfiles.add(toGcubeProfileDV(profile)); } @@ -291,8 +294,8 @@ public class ConvertToDataValueObjectModel { int i = 0; for (String asJSONString : jsonConfigurations) { LOG.debug(++i + ") the itemField is: " + asJSONString); - ItemField itemField = org.gcube.application.geoportal.client.utils.Serialization.read(asJSONString, - ItemField.class); + ItemField itemField = org.gcube.application.geoportal.client.utils.Serialization + .read(asJSONString, ItemField.class); listItemFields.add(toItemFieldDV(itemField)); } @@ -306,17 +309,39 @@ public class ConvertToDataValueObjectModel { case actions_definition: { - List listActionsDef = new ArrayList(jsonConfigurations.size()); + List listActionsDef = new ArrayList( + jsonConfigurations.size()); + + LOG.trace("Into " + GEOPORTAL_CONFIGURATION_TYPE.actions_definition + " handlers are: {}", ucd.getHandlers()); + List listHandlersLC = ucd + .getHandlersByType(GEOPORTAL_DATA_HANDLER.gna_concessioni_lc.getType()); + + HandlerDeclaration handlerLC = null; + if (listHandlersLC != null && listHandlersLC.size() > 0) { + handlerLC = listHandlersLC.get(0); + } + LOG.debug("Into " + GEOPORTAL_CONFIGURATION_TYPE.actions_definition + " the handler " + + GEOPORTAL_DATA_HANDLER.gna_concessioni_lc + " is: {}", handlerLC); + int i = 0; for (String asJSONString : jsonConfigurations) { LOG.debug(++i + ") the ActionDefinition is: " + asJSONString); ActionDefinition actionDef = org.gcube.application.geoportal.client.utils.Serialization .read(asJSONString, ActionDefinition.class); - listActionsDef.add(toActionDefinition(actionDef)); + + Set roles = new HashSet(); + if (handlerLC != null) { + for (String stepID : actionDef.getCall_STEPS()) { + List listRoles = GeoportalConfigUtil.readRolesForSTEPId(handlerLC, stepID); + roles.addAll(listRoles); + } + } + listActionsDef.add(toActionDefinition(actionDef, roles)); } - ConfigurationDV> dDV = new ConfigurationDV>(listActionsDef); + ConfigurationDV> dDV = new ConfigurationDV>( + listActionsDef); dDV.setConfiguration(listActionsDef); dDV.setConfigurationType(geoportalConfigType); // -> GEOPORTAL_CONFIGURATION_TYPE.actions_definition hdDV.setConfiguration(dDV); @@ -406,9 +431,10 @@ public class ConvertToDataValueObjectModel { * To action definition. * * @param actionDefinition the action definition + * @param roles the roles required to perform the callable STEPS * @return the action definition DV */ - public static ActionDefinitionDV toActionDefinition(ActionDefinition actionDefinition) { + public static ActionDefinitionDV toActionDefinition(ActionDefinition actionDefinition, Set roles) { LOG.trace("toActionDefinition called"); if (actionDefinition == null) { @@ -422,6 +448,7 @@ public class ConvertToDataValueObjectModel { actDef.setDescription(actionDefinition.getDescription()); actDef.setDisplayOnPhase(actionDefinition.getDisplay_on_phase()); actDef.setTitle(actionDefinition.getTitle()); + actDef.setRoles(roles); LOG.info("returning: " + actionDefinition); return actDef; diff --git a/src/main/java/org/gcube/application/geoportalcommon/geoportal/GeoportalConfigUtil.java b/src/main/java/org/gcube/application/geoportalcommon/geoportal/GeoportalConfigUtil.java new file mode 100644 index 0000000..c0de725 --- /dev/null +++ b/src/main/java/org/gcube/application/geoportalcommon/geoportal/GeoportalConfigUtil.java @@ -0,0 +1,80 @@ +package org.gcube.application.geoportalcommon.geoportal; + +import java.util.ArrayList; +import java.util.HashSet; +import java.util.List; +import java.util.stream.Collectors; + +import org.gcube.application.geoportal.common.model.useCaseDescriptor.HandlerDeclaration; +import org.json.JSONArray; +import org.json.JSONObject; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.jayway.jsonpath.JsonPath; +import com.jayway.jsonpath.spi.json.JsonOrgJsonProvider; + +/** + * The Class GeoportalConfigUtil. + * + * @author Francesco Mangiacrapa at ISTI-CNR francesco.mangiacrapa@isti.cnr.it + * + * Sep 15, 2022 + */ +public class GeoportalConfigUtil { + private static Logger LOG = LoggerFactory.getLogger(GeoportalConfigUtil.class); + + /** + * Read roles for STEP id. + * + * @param handler the handler + * @param stepID the step ID + * @return the list + * @throws Exception the exception + */ + public static List readRolesForSTEPId(HandlerDeclaration handler, String stepID) throws Exception { + LOG.debug("readRolesForSTEPId called for stepID {}, handler {}", stepID, handler); + + List roles = new ArrayList(); + try { + + com.jayway.jsonpath.Configuration configuration = com.jayway.jsonpath.Configuration.builder() + .jsonProvider(new JsonOrgJsonProvider()).build(); + + String toJSON = handler.getConfiguration().toJson(); + LOG.debug("Handler to JSON: " + toJSON); + JSONObject jObject = new JSONObject(toJSON); + JsonPath jsonPath = JsonPath.compile("$.step_access[?(@.STEP == '" + stepID + "' )].roles"); + LOG.trace("jsonPath: " + jsonPath.getPath()); + // $..book[?(@.price <= $['expensive'])] + JSONArray data = jsonPath.read(jObject, configuration); + LOG.debug("jsonPath matching data lenght: " + data.length()); + HashSet listdata = new HashSet(data.length()); + + if(data.length()>0) { + LOG.debug("stepID found"); + try { + JSONArray arrayRoles = new JSONArray(data.get(0).toString()); + for (int i = 0; i < arrayRoles.length(); i++) { + String role = arrayRoles.get(i).toString(); + LOG.info("for STEP_ID {} read role {}", stepID, role); + listdata.add(role); + } + }catch (Exception e) { + LOG.warn("Error on reading array roles for STEP_ID: "+stepID, e); + } + } + + roles = listdata.stream().collect(Collectors.toList()); + LOG.info("for STEP_ID {} returning Roles {} ", stepID, roles); + + } catch (Exception e) { + LOG.warn("Error occurred on reading roles into handler {} returning no role", handler); + + } + + return roles; + + } + +} diff --git a/src/main/java/org/gcube/application/geoportalcommon/shared/geoportal/config/ActionDefinitionDV.java b/src/main/java/org/gcube/application/geoportalcommon/shared/geoportal/config/ActionDefinitionDV.java index 06a8a63..9669aa0 100644 --- a/src/main/java/org/gcube/application/geoportalcommon/shared/geoportal/config/ActionDefinitionDV.java +++ b/src/main/java/org/gcube/application/geoportalcommon/shared/geoportal/config/ActionDefinitionDV.java @@ -2,6 +2,7 @@ package org.gcube.application.geoportalcommon.shared.geoportal.config; import java.io.Serializable; import java.util.Arrays; +import java.util.Set; public class ActionDefinitionDV implements GeoportalConfigurationID, Serializable { @@ -17,6 +18,8 @@ public class ActionDefinitionDV implements GeoportalConfigurationID, Serializabl private String configID; + public Set roles; + public ActionDefinitionDV() { } @@ -81,6 +84,14 @@ public class ActionDefinitionDV implements GeoportalConfigurationID, Serializabl this.configID = configID; } + public Set getRoles() { + return roles; + } + + public void setRoles(Set roles) { + this.roles = roles; + } + @Override public String toString() { StringBuilder builder = new StringBuilder(); @@ -96,6 +107,8 @@ public class ActionDefinitionDV implements GeoportalConfigurationID, Serializabl builder.append(Arrays.toString(displayOnPhase)); builder.append(", configID="); builder.append(configID); + builder.append(", roles="); + builder.append(roles); builder.append("]"); return builder.toString(); } diff --git a/src/main/java/org/gcube/application/geoportalcommon/shared/geoportal/ucd/GEOPORTAL_DATA_HANDLER.java b/src/main/java/org/gcube/application/geoportalcommon/shared/geoportal/ucd/GEOPORTAL_DATA_HANDLER.java index 35af563..e1a425d 100644 --- a/src/main/java/org/gcube/application/geoportalcommon/shared/geoportal/ucd/GEOPORTAL_DATA_HANDLER.java +++ b/src/main/java/org/gcube/application/geoportalcommon/shared/geoportal/ucd/GEOPORTAL_DATA_HANDLER.java @@ -5,7 +5,7 @@ public enum GEOPORTAL_DATA_HANDLER { geoportal_data_list("org.gcube.portlets.user.geoportal-data-list", "DATA_LIST_GUI"), geoportal_data_entry("org.gcube.portlets.user.geoportal-data-entry-app", "DATA_ENTRY_GUI"), geoportal_workflow_action_list("org.gcube.portlets.user.geoportal-workflow-action-list","WORKFLOW_ACTION_LIST_GUI"), - gna_concessionio_lc("GNA-CONCESSIONI-LC", "LifecycleManagement"); + gna_concessioni_lc("GNA-CONCESSIONI-LC", "LifecycleManagement"); String id; String type; diff --git a/src/test/java/org/gcube/application/UCD_Tests.java b/src/test/java/org/gcube/application/UCD_Tests.java index 37215e3..0e454e2 100644 --- a/src/test/java/org/gcube/application/UCD_Tests.java +++ b/src/test/java/org/gcube/application/UCD_Tests.java @@ -6,6 +6,7 @@ import java.util.HashSet; import java.util.List; import java.util.stream.Collectors; +import org.gcube.application.geoportal.client.utils.Serialization; import org.gcube.application.geoportal.common.model.useCaseDescriptor.HandlerDeclaration; import org.gcube.application.geoportal.common.model.useCaseDescriptor.UseCaseDescriptor; import org.gcube.application.geoportalcommon.ConvertToDataValueObjectModel; @@ -43,7 +44,7 @@ public class UCD_Tests { // private static String TOKEN = ""; //preVRE private static String CONTEXT = "/gcube/devsec/devVRE"; - private static String TOKEN = ""; // devVRE + private static String TOKEN = "c41a00c0-7897-48d2-a67a-05190d6ce5e6-98187548"; // devVRE private static String PROFILE_ID = "profiledConcessioni"; @@ -117,7 +118,7 @@ public class UCD_Tests { * @return the UCD for data entry handler ids * @throws Exception the exception */ - // @Test + //@Test public void getUCDFor_DataEntry_HandlerIds() throws Exception { ScopeProvider.instance.set(CONTEXT); SecurityTokenProvider.instance.set(TOKEN); @@ -207,7 +208,7 @@ public class UCD_Tests { try { UseCaseDescriptorCaller client = GeoportalClientCaller.useCaseDescriptors(); ucd = client.getUCDForId(PROFILE_ID); - handlers = client.getHandlersByType(PROFILE_ID, GEOPORTAL_DATA_HANDLER.gna_concessionio_lc.getType()); + handlers = client.getHandlersByType(PROFILE_ID, GEOPORTAL_DATA_HANDLER.gna_concessioni_lc.getType()); } catch (Exception e) { e.printStackTrace(); @@ -226,7 +227,7 @@ public class UCD_Tests { * @return the roles for STEP id * @throws Exception the exception */ - @Test + //@Test public void getRolesForSTEPId() throws Exception { ScopeProvider.instance.set(CONTEXT); SecurityTokenProvider.instance.set(TOKEN); @@ -238,8 +239,8 @@ public class UCD_Tests { UseCaseDescriptorCaller client = GeoportalClientCaller.useCaseDescriptors(); ucd = client.getUCDForId(PROFILE_ID); - handlers = client.getHandlersByType(PROFILE_ID, GEOPORTAL_DATA_HANDLER.gna_concessionio_lc.getType()); - + handlers = client.getHandlersByType(PROFILE_ID, GEOPORTAL_DATA_HANDLER.gna_concessioni_lc.getType()); + com.jayway.jsonpath.Configuration configuration = com.jayway.jsonpath.Configuration.builder() .jsonProvider(new JsonOrgJsonProvider()).build(); @@ -275,7 +276,7 @@ public class UCD_Tests { * @return the configurations for UC D data handlers * @throws Exception the exception */ - // @Test + @Test public void getConfigurations_ForUCD_Data_Handlers() throws Exception { ScopeProvider.instance.set(CONTEXT); SecurityTokenProvider.instance.set(TOKEN);