From e54a81eec2df1c0dc9d02fd566f57b4e908c979f Mon Sep 17 00:00:00 2001 From: Costantino Perciante Date: Tue, 11 Oct 2016 08:05:12 +0000 Subject: [PATCH] Assign dataset to group no longer uses the jackan client, but the ckan apis directly git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/data-catalogue/ckan-util-library@133014 82a268e6-3cf1-43bd-a215-b396298e98cf --- .../ckanutillibrary/DataCatalogueImpl.java | 106 ++++++++++++++++-- 1 file changed, 95 insertions(+), 11 deletions(-) diff --git a/src/main/java/org/gcube/datacatalogue/ckanutillibrary/DataCatalogueImpl.java b/src/main/java/org/gcube/datacatalogue/ckanutillibrary/DataCatalogueImpl.java index 9f144bd..2edb421 100644 --- a/src/main/java/org/gcube/datacatalogue/ckanutillibrary/DataCatalogueImpl.java +++ b/src/main/java/org/gcube/datacatalogue/ckanutillibrary/DataCatalogueImpl.java @@ -39,6 +39,7 @@ import eu.trentorise.opendata.jackan.CkanClient; import eu.trentorise.opendata.jackan.exceptions.JackanException; import eu.trentorise.opendata.jackan.internal.org.apache.http.HttpResponse; import eu.trentorise.opendata.jackan.internal.org.apache.http.HttpStatus; +import eu.trentorise.opendata.jackan.internal.org.apache.http.client.methods.HttpGet; import eu.trentorise.opendata.jackan.internal.org.apache.http.client.methods.HttpPost; import eu.trentorise.opendata.jackan.internal.org.apache.http.entity.StringEntity; import eu.trentorise.opendata.jackan.internal.org.apache.http.impl.client.CloseableHttpClient; @@ -1184,6 +1185,7 @@ public class DataCatalogueImpl implements DataCatalogue{ return toReturn; } + @SuppressWarnings("unchecked") @Override public boolean assignDatasetToGroup(String groupNameOrId, String datasetNameOrId, String apiKey) { @@ -1194,24 +1196,106 @@ public class DataCatalogueImpl implements DataCatalogue{ checkArgument(!datasetNameOrId.isEmpty()); checkNotNull(apiKey); checkArgument(!apiKey.isEmpty()); - + String groupNameToCheck = UtilMethods.fromGroupTitleToName(groupNameOrId); - try{ + try(CloseableHttpClient httpClient = HttpClientBuilder.create().build();){ - // the association is performed by using the sysadmi api key, since from apis only group admin can perform this operation... + // TODO unfortunately it doesn't work properly.. We use the APIs + // // the association is performed by using the sysadmi api key, since from apis only group admin can perform this operation... CheckedCkanClient client = new CheckedCkanClient(CKAN_CATALOGUE_URL, CKAN_TOKEN_SYS); + // + // // retrieve the dataset + // CkanDataset dataset = client.getDataset(datasetNameOrId); + // List datasetGroups = dataset.getGroups(); + // CkanGroup destinationGroup = client.getGroup(groupNameToCheck); + // datasetGroups.add(destinationGroup); + // + // // patch the dataset + // client.patchUpdateDataset(dataset); - // retrieve the dataset - CkanDataset dataset = client.getDataset(datasetNameOrId); - List datasetGroups = dataset.getGroups(); - CkanGroup destinationGroup = client.getGroup(groupNameToCheck); - datasetGroups.add(destinationGroup); + // check the group exists + CkanGroup group = client.getGroup(groupNameToCheck); - // patch the dataset - client.patchUpdateDataset(dataset); + // we need to use the apis to make it + String pathPackageShow = CKAN_CATALOGUE_URL + "/api/3/action/package_show?id=" + datasetNameOrId; + HttpGet getRequest = new HttpGet(pathPackageShow); + getRequest.addHeader("Authorization", CKAN_TOKEN_SYS); + HttpResponse response = httpClient.execute(getRequest); + List fetchedGroups = new ArrayList(); - return true; + logger.debug("Response is " + response.getStatusLine().getStatusCode() + " and message is " + response.getStatusLine().getReasonPhrase()); + + // read the json dataset and fetch the groups and fetch the groups' names, if any + if(response.getStatusLine().getStatusCode() == HttpStatus.SC_OK){ + + // parse the json and convert to java beans + BufferedReader br = new BufferedReader( + new InputStreamReader((response.getEntity().getContent()))); + + String output; + String res = ""; + while ((output = br.readLine()) != null) { + res += output; + } + + if(res == "") + throw new Exception("Unable to retrieve the package!"); + + JSONParser parser = new JSONParser(); + JSONObject json = (JSONObject) parser.parse(res); + JSONObject resultJson = (JSONObject) json.get("result"); + JSONArray groupsJson = (JSONArray)resultJson.get("groups"); + Iterator it = groupsJson.iterator(); + + while (it.hasNext()) { + JSONObject object = (JSONObject) it.next(); + try{ + if(object.containsKey("name")) + fetchedGroups.add((String)object.get("name")); + }catch(Exception e){ + logger.error("Error while building CkanRelationship bean from object " + object, e); + } + } + + // add the new one + if(!fetchedGroups.contains(group.getName())) + fetchedGroups.add(group.getName()); + + logger.debug("Groups to be added are " + fetchedGroups); + + // now we patch the dataset with the new group + String pathUpdatePatch = CKAN_CATALOGUE_URL + "/api/3/action/package_patch"; + String parameterPostPatch = "{\"id\":\"PACKAGE_ID\", \"groups\":[GROUPS]}"; + parameterPostPatch = parameterPostPatch.replace("PACKAGE_ID", datasetNameOrId); + String singleGroup = "{\"name\":\"GROUP_ID\"}"; + + // evaluate parameterPostPatch + String replaceGROUPS = ""; + for (int i = 0; i < fetchedGroups.size(); i++) { + replaceGROUPS += singleGroup.replace("GROUP_ID", fetchedGroups.get(i)); + if(i != fetchedGroups.size() - 1) + replaceGROUPS += ","; + } + + // replace this into parameterPostPatch + parameterPostPatch = parameterPostPatch.replace("GROUPS", replaceGROUPS); + + logger.debug("Request for patch is going to be " + parameterPostPatch); + + HttpPost request = new HttpPost(pathUpdatePatch); + request.addHeader("Authorization", CKAN_TOKEN_SYS); + StringEntity params = new StringEntity(parameterPostPatch); + request.setEntity(params); + HttpResponse responsePatch = httpClient.execute(request); + logger.debug("Response code is " + responsePatch.getStatusLine().getStatusCode() + " and response message is " + responsePatch.getStatusLine().getReasonPhrase()); + + if(responsePatch.getStatusLine().getStatusCode() == HttpStatus.SC_OK){ + logger.info("Dataset Added to the group!!"); + return true; + } + + } }catch(Exception e){ logger.error("Unable to make this association", e);