From a7c24dc91923a4da7f2e51cf77c380a4b5728721 Mon Sep 17 00:00:00 2001 From: Costantino Perciante Date: Thu, 6 Oct 2016 12:24:05 +0000 Subject: [PATCH] added hashmap which stores couple git-svn-id: http://svn.d4science-ii.research-infrastructures.eu/gcube/trunk/portlets/widgets/ckan-metadata-publisher-widget@132811 82a268e6-3cf1-43bd-a215-b396298e98cf --- .../server/CKANPublisherServicesImpl.java | 57 +++++++++++++++---- 1 file changed, 47 insertions(+), 10 deletions(-) diff --git a/src/main/java/org/gcube/portlets/widgets/ckandatapublisherwidget/server/CKANPublisherServicesImpl.java b/src/main/java/org/gcube/portlets/widgets/ckandatapublisherwidget/server/CKANPublisherServicesImpl.java index 1f6fa09..9c71f5e 100644 --- a/src/main/java/org/gcube/portlets/widgets/ckandatapublisherwidget/server/CKANPublisherServicesImpl.java +++ b/src/main/java/org/gcube/portlets/widgets/ckandatapublisherwidget/server/CKANPublisherServicesImpl.java @@ -4,6 +4,7 @@ import java.util.ArrayList; import java.util.Calendar; import java.util.List; import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; import javax.servlet.http.HttpSession; @@ -52,6 +53,9 @@ public class CKANPublisherServicesImpl extends RemoteServiceServlet implements C public static final String TEST_USER = "test.user"; private final static String TEST_SEC_TOKEN = "a1e19695-467f-42b8-966d-bf83dd2382ef"; + // map + private ConcurrentHashMap mapOrganizationScope = new ConcurrentHashMap(); + /** * Retrieve an instance of the library for the scope * @param scope if it is null it is evaluated from the session @@ -200,10 +204,10 @@ public class CKANPublisherServicesImpl extends RemoteServiceServlet implements C * @return */ private String findLicenseIdByLicense(String chosenLicense) { - + String scope = (String)getThreadLocalRequest().getSession().getAttribute(SessionCatalogueAttributes.SCOPE_CLIENT_PORTLET_URL); return getCatalogue(scope).findLicenseIdByLicenseTitle(chosenLicense); - + } @Override @@ -478,7 +482,7 @@ public class CKANPublisherServicesImpl extends RemoteServiceServlet implements C } logger.debug("The user wants to publish in organization with name " + organizationNameOrId); - String scope = Utils.retrieveScopeFromOrganizationName(organizationNameOrId); + String scope = getScopeFromOrgName(organizationNameOrId); DataCatalogue utils = getCatalogue(scope); String datasetId = utils.createCKanDataset(getUserCKanTokenFromSession(scope), title, organizationNameOrId, author, @@ -493,15 +497,15 @@ public class CKANPublisherServicesImpl extends RemoteServiceServlet implements C // retrieve the url String datasetUrl = utils.getPortletUrl() + "?path=" + utils.getUrlFromDatasetIdOrName(getUserCKanTokenFromSession(scope), datasetId, true); toCreate.setSource(datasetUrl); - + // start a thread that will associate this dataset with the group if(toCreate.getChosenProfile() != null){ - + AssociationToGroupThread thread = new AssociationToGroupThread(toCreate.getChosenProfile(), datasetId, userName, utils, organizationNameOrId); thread.start(); - + } - + return toCreate; }else{ @@ -552,7 +556,7 @@ public class CKANPublisherServicesImpl extends RemoteServiceServlet implements C null); // get the scope in which we should discover the ckan instance given the organization name in which the dataset was created - String scope = Utils.retrieveScopeFromOrganizationName(resource.getOrganizationNameDatasetParent()); + String scope = getScopeFromOrgName(resource.getOrganizationNameDatasetParent()); String resourceId = getCatalogue(scope).addResourceToDataset(resourceBean, getUserCKanTokenFromSession(scope)); if(resourceId != null){ @@ -590,7 +594,7 @@ public class CKANPublisherServicesImpl extends RemoteServiceServlet implements C }else{ try{ // get the scope in which we should discover the ckan instance given the organization name in which the dataset was created - String scope = Utils.retrieveScopeFromOrganizationName(resource.getOrganizationNameDatasetParent()); + String scope = getScopeFromOrgName(resource.getOrganizationNameDatasetParent()); deleted = getCatalogue(scope). deleteResourceFromDataset(resource.getId(), getUserCKanTokenFromSession(scope)); if(deleted){ @@ -612,7 +616,7 @@ public class CKANPublisherServicesImpl extends RemoteServiceServlet implements C List toReturn = new ArrayList(); try{ - String evaluatedScope = Utils.retrieveScopeFromOrganizationName(orgName); + String evaluatedScope = getScopeFromOrgName(orgName); logger.debug("Evaluated scope is " + evaluatedScope); toReturn = Utils.getMetadataProfilesList(evaluatedScope, getThreadLocalRequest().getSession(), getASLSession()); }catch(Exception e){ @@ -640,4 +644,37 @@ public class CKANPublisherServicesImpl extends RemoteServiceServlet implements C return false; } + + /** + * The method tries to retrieve the scope related to the organization using the map first, + * if no match is found, it retrieves such information by using liferay + */ + private String getScopeFromOrgName(String orgName){ + + logger.debug("Request for scope related to orgName " + orgName + "[ map that will be used is " + mapOrganizationScope.toString() + " ]"); + + if(orgName == null || orgName.isEmpty()) + throw new IllegalArgumentException("orgName cannot be empty or null!"); + + String toReturn = null; + + if(mapOrganizationScope.containsKey(orgName)) + toReturn = mapOrganizationScope.get(orgName); + else{ + try{ + + String evaluatedScope = Utils.retrieveScopeFromOrganizationName(orgName); + mapOrganizationScope.put(orgName, evaluatedScope); + toReturn = evaluatedScope; + + }catch(Exception e){ + logger.error("Failed to retrieve scope from OrgName for organization " + orgName, e); + } + } + + + logger.debug("Returning scope " + toReturn); + return toReturn; + + } } \ No newline at end of file