diff --git a/src/main/java/org/gcube/gcat/persistence/ckan/CKANPackage.java b/src/main/java/org/gcube/gcat/persistence/ckan/CKANPackage.java index d52b31f..5438f1d 100644 --- a/src/main/java/org/gcube/gcat/persistence/ckan/CKANPackage.java +++ b/src/main/java/org/gcube/gcat/persistence/ckan/CKANPackage.java @@ -37,9 +37,12 @@ import com.fasterxml.jackson.databind.node.ObjectNode; public class CKANPackage extends CKAN { private static final Logger logger = LoggerFactory.getLogger(CKANPackage.class); - + /* // see https://docs.ckan.org/en/latest/api/#ckan.logic.action.get.package_list public static final String ITEM_LIST = CKAN.CKAN_API_PATH + "package_list"; + */ + // see https://docs.ckan.org/en/latest/api/index.html#ckan.logic.action.get.package_search + public static final String ITEM_LIST = CKAN.CKAN_API_PATH + "package_search"; // see http://docs.ckan.org/en/latest/api/#ckan.logic.action.create.package_create public static final String ITEM_CREATE = CKAN.CKAN_API_PATH + "package_create"; // see http://docs.ckan.org/en/latest/api/#ckan.logic.action.get.package_show @@ -53,6 +56,14 @@ public class CKANPackage extends CKAN { // see http://docs.ckan.org/en/latest/api/#ckan.logic.action.delete.dataset_purge public static final String ITEM_PURGE = CKAN.CKAN_API_PATH + "dataset_purge"; + // limit in https://docs.ckan.org/en/latest/api/index.html#ckan.logic.action.get.package_search + protected static final String ROWS_KEY = "rows"; + // offset in https://docs.ckan.org/en/latest/api/index.html#ckan.logic.action.get.package_search + protected static final String START_KEY = "start"; + + protected static final String Q_KEY = "q"; + protected static final String ORGANIZATION_FILTER_TEMPLATE = "organization:%s"; + protected static final String LICENSE_KEY = "license_id"; protected static final String ITEM_URL_KEY = "item_url"; @@ -88,6 +99,19 @@ public class CKANPackage extends CKAN { managedResources = new ArrayList(); } + /* + * Return the CKAN organization name using the current context name + */ + protected String getOrganizationName(ScopeBean scopeBean) { + String contextName = scopeBean.name(); + return contextName.toLowerCase().replace(" ", "_"); + } + + protected String getOrganizationName() { + ScopeBean scopeBean = new ScopeBean(ContextUtility.getCurrentContext()); + return getOrganizationName(scopeBean); + } + public ObjectNode checkBaseInformation(String json) throws Exception { ObjectNode objectNode = (ObjectNode) mapper.readTree(json); @@ -114,7 +138,6 @@ public class CKANPackage extends CKAN { // owner organization must be specified if the token belongs to a VRE ScopeBean scopeBean = new ScopeBean(ContextUtility.getCurrentContext()); - String contextName = scopeBean.name(); String gotOrganization = null; if(objectNode.has(OWNER_ORG_KEY)) { @@ -122,7 +145,7 @@ public class CKANPackage extends CKAN { } if(scopeBean.is(Type.VRE)) { - String organizationFromContext = contextName.toLowerCase().replace(" ", "_"); + String organizationFromContext = getOrganizationName(scopeBean); if(gotOrganization != null) { if(gotOrganization.compareTo(organizationFromContext) != 0) { CKANOrganization ckanOrganization = new CKANOrganization(); @@ -194,10 +217,27 @@ public class CKANPackage extends CKAN { } } - // see https://docs.ckan.org/en/latest/api/#ckan.logic.action.get.package_list @Override public String list(int limit, int offset) { - return super.list(limit, offset); + Map parameters = new HashMap<>(); + if(limit <= 0) { + // According to CKAN documentation + // the number of matching rows to return. There is a hard limit of 1000 datasets per query. + // see https://docs.ckan.org/en/2.6/api/index.html#ckan.logic.action.get.package_search + limit = 1000; + } + parameters.put(ROWS_KEY, String.valueOf(limit)); + + if(offset < 0) { + offset = 0; + } + parameters.put(START_KEY, String.valueOf(offset)); + + String organizationName = getOrganizationName(); + String organizationQueryString = String.format(ORGANIZATION_FILTER_TEMPLATE, organizationName); + parameters.put(Q_KEY, organizationQueryString); + + return sendGetRequest(LIST, parameters); } protected void rollbackManagedResources() {