added hashmap which stores couple <organization name, scope>

git-svn-id: http://svn.d4science-ii.research-infrastructures.eu/gcube/trunk/portlets/widgets/ckan-metadata-publisher-widget@132811 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
Costantino Perciante 2016-10-06 12:24:05 +00:00
parent 169974e09b
commit a7c24dc919
1 changed files with 47 additions and 10 deletions

View File

@ -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 <orgName, scope>
private ConcurrentHashMap<String, String> mapOrganizationScope = new ConcurrentHashMap<String, String>();
/**
* 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<MetaDataProfileBean> toReturn = new ArrayList<MetaDataProfileBean>();
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;
}
}