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
This commit is contained in:
Costantino Perciante 2016-10-11 08:05:12 +00:00
parent 357d242cd6
commit e54a81eec2
1 changed files with 95 additions and 11 deletions

View File

@ -39,6 +39,7 @@ import eu.trentorise.opendata.jackan.CkanClient;
import eu.trentorise.opendata.jackan.exceptions.JackanException; 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.HttpResponse;
import eu.trentorise.opendata.jackan.internal.org.apache.http.HttpStatus; 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.client.methods.HttpPost;
import eu.trentorise.opendata.jackan.internal.org.apache.http.entity.StringEntity; import eu.trentorise.opendata.jackan.internal.org.apache.http.entity.StringEntity;
import eu.trentorise.opendata.jackan.internal.org.apache.http.impl.client.CloseableHttpClient; import eu.trentorise.opendata.jackan.internal.org.apache.http.impl.client.CloseableHttpClient;
@ -1184,6 +1185,7 @@ public class DataCatalogueImpl implements DataCatalogue{
return toReturn; return toReturn;
} }
@SuppressWarnings("unchecked")
@Override @Override
public boolean assignDatasetToGroup(String groupNameOrId, String datasetNameOrId, String apiKey) { public boolean assignDatasetToGroup(String groupNameOrId, String datasetNameOrId, String apiKey) {
@ -1197,21 +1199,103 @@ public class DataCatalogueImpl implements DataCatalogue{
String groupNameToCheck = UtilMethods.fromGroupTitleToName(groupNameOrId); 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); CheckedCkanClient client = new CheckedCkanClient(CKAN_CATALOGUE_URL, CKAN_TOKEN_SYS);
//
// // retrieve the dataset
// CkanDataset dataset = client.getDataset(datasetNameOrId);
// List<CkanGroup> datasetGroups = dataset.getGroups();
// CkanGroup destinationGroup = client.getGroup(groupNameToCheck);
// datasetGroups.add(destinationGroup);
//
// // patch the dataset
// client.patchUpdateDataset(dataset);
// retrieve the dataset // check the group exists
CkanDataset dataset = client.getDataset(datasetNameOrId); CkanGroup group = client.getGroup(groupNameToCheck);
List<CkanGroup> datasetGroups = dataset.getGroups();
CkanGroup destinationGroup = client.getGroup(groupNameToCheck);
datasetGroups.add(destinationGroup);
// patch the dataset // we need to use the apis to make it
client.patchUpdateDataset(dataset); 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<String> fetchedGroups = new ArrayList<String>();
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<JSONObject> 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; return true;
}
}
}catch(Exception e){ }catch(Exception e){
logger.error("Unable to make this association", e); logger.error("Unable to make this association", e);