Added method to purge groups

git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/data-catalogue/ckan-util-library@141607 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
Costantino Perciante 2017-01-17 14:14:28 +00:00
parent 89d9abc1b6
commit 8712b8ac54
4 changed files with 51 additions and 1 deletions

View File

@ -1,4 +1,8 @@
<ReleaseNotes>
<Changeset component="org.gcube.data-catalogue.ckan-util-library.2-1-1"
date="2017-02-01">
<Change>Added method to delete/purge group</Change>
</Changeset>
<Changeset component="org.gcube.data-catalogue.ckan-util-library.2-1-0"
date="2016-12-01">
<Change>Product creation methods improved</Change>

View File

@ -10,7 +10,7 @@
</parent>
<groupId>org.gcube.data-catalogue</groupId>
<artifactId>ckan-util-library</artifactId>
<version>2.1.0-SNAPSHOT</version>
<version>2.1.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>CKan utility library</name>

View File

@ -502,4 +502,12 @@ public interface DataCatalogue {
* @throws ClassNotFoundException
*/
List<String> getProductsIdsInGroupOrOrg(String orgOrGroupName, boolean isOrganization, int start, int rows) throws ClassNotFoundException, SQLException;
/**
* Delete and purge (if purge is set to true) a group
* @param groupName
* @param purge
* @return
*/
boolean deleteGroup(String groupName, boolean purge);
}

View File

@ -2474,4 +2474,42 @@ public class DataCatalogueImpl implements DataCatalogue{
}
@Override
public boolean deleteGroup(String groupName, boolean purge) {
checkNotNull(groupName);
logger.info("Request of deleting group " + groupName + ". Purge is " + Boolean.toString(purge));
try(CloseableHttpClient httpClient = HttpClientBuilder.create().build();){
String deletePath = getCatalogueUrl() + "/api/3/action/group_delete";
String purgePath = getCatalogueUrl() + "/api/3/action/group_purge";
String requestToPerform = purge ? purgePath : deletePath;
HttpPost httpPostRequest = new HttpPost(requestToPerform);
httpPostRequest.setHeader("Authorization", CKAN_TOKEN_SYS);
JSONObject obj = new JSONObject();
obj.put("id", groupName);
StringEntity params = new StringEntity(obj.toJSONString(), ContentType.APPLICATION_JSON);
httpPostRequest.setEntity(params);
HttpResponse response = httpClient.execute(httpPostRequest);
if (response.getStatusLine().getStatusCode() < 200 || response.getStatusLine().getStatusCode() >= 300) {
throw new RuntimeException("failed to delete/purge the group. response status line from "
+ requestToPerform + " was: " + response.getStatusLine());
}
return true;
}catch(Exception e){
logger.error("Error while trying to delete/purge the group " + e.getMessage());
return false;
}
}
}