added remove group method for product
git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/data-catalogue/ckan-util-library@135116 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
parent
22a849332b
commit
495c10958e
|
@ -328,6 +328,17 @@ public interface DataCatalogue {
|
|||
*/
|
||||
boolean assignDatasetToGroup(String groupNameOrId, String datasetNameOrId,
|
||||
String apiKey);
|
||||
|
||||
/**
|
||||
* Remove a dataset from a group.
|
||||
* @param groupNameOrId the id or the name of the group.
|
||||
* @param datasetNameOrId the id or the name of the dataset
|
||||
* @param apiKey (the apiKey should belong to someone that has the role of editor/admin of the organization in which
|
||||
* the dataset is placed, plus the admin role into the destination group.
|
||||
* @return true on success, false otherwise
|
||||
*/
|
||||
boolean removeDatasetFromGroup(String groupNameOrId, String datasetNameOrId,
|
||||
String apiKey);
|
||||
|
||||
/**
|
||||
* Delete the dataset with id datasetId. If purge is true, the product will be purged too.
|
||||
|
|
|
@ -1457,6 +1457,81 @@ public class DataCatalogueImpl implements DataCatalogue{
|
|||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean removeDatasetFromGroup(String groupNameOrId,
|
||||
String datasetNameOrId, String apiKey) {
|
||||
|
||||
// checks
|
||||
checkNotNull(groupNameOrId);
|
||||
checkNotNull(datasetNameOrId);
|
||||
checkNotNull(apiKey);
|
||||
|
||||
try (CloseableHttpClient httpClient = HttpClientBuilder.create().build()) {
|
||||
String apiRequestUrl = CKAN_CATALOGUE_URL + "/api/3/action/package_patch";
|
||||
HttpPost httpPostRequest = new HttpPost(apiRequestUrl);
|
||||
httpPostRequest.setHeader("Authorization", CKAN_TOKEN_SYS);
|
||||
|
||||
// Request parameters to be replaced
|
||||
JSONObject jsonRequest = new JSONObject();
|
||||
JSONArray groupsAsJson = new JSONArray();
|
||||
|
||||
CheckedCkanClient client = new CheckedCkanClient(CKAN_CATALOGUE_URL, CKAN_TOKEN_SYS);
|
||||
CkanDataset dataset = client.getDataset(datasetNameOrId);
|
||||
List<CkanGroup> currentGroups = dataset.getGroups();
|
||||
|
||||
// build the json array for the "groups" field.. each object looks like
|
||||
// {
|
||||
//
|
||||
// "display_name": "Fishery activity",
|
||||
// "description": "",
|
||||
// "image_display_url": "",
|
||||
// "title": "Fishery activity",
|
||||
// "id": "20b8d23c-d2dd-4613-9e1e-856311862e87",
|
||||
// "name": "fishery-activity"
|
||||
//
|
||||
// }
|
||||
|
||||
// get the id of the group to be removed
|
||||
String groupId = client.getGroup(groupNameOrId).getId();
|
||||
|
||||
for(CkanGroup ckanGroup : currentGroups){
|
||||
if(!ckanGroup.getId().equals(groupId)){
|
||||
JSONObject obj = new JSONObject();
|
||||
obj.put("display_name", ckanGroup.getDisplayName());
|
||||
obj.put("description", ckanGroup.getDescription());
|
||||
obj.put("image_display_url", ckanGroup.getImageDisplayUrl());
|
||||
obj.put("title", ckanGroup.getTitle());
|
||||
obj.put("id", ckanGroup.getId());
|
||||
obj.put("name", ckanGroup.getName());
|
||||
groupsAsJson.add(obj);
|
||||
}
|
||||
}
|
||||
|
||||
// perform the request
|
||||
jsonRequest.put("id", datasetNameOrId);
|
||||
jsonRequest.put("groups", groupsAsJson);
|
||||
|
||||
logger.debug("Request param is going to be " + jsonRequest);
|
||||
|
||||
StringEntity params = new StringEntity(jsonRequest.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 patch the product. response status line from "
|
||||
+ apiRequestUrl + " was: " + response.getStatusLine());
|
||||
}
|
||||
|
||||
return true;
|
||||
|
||||
}catch(Exception e){
|
||||
logger.error("Failed to remove the group " + groupNameOrId + " from product " + datasetNameOrId, e);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<String, List<String>> getRolesAndUsersOrganization(String organizationName) {
|
||||
|
||||
|
|
|
@ -417,4 +417,24 @@ public class TestDataCatalogueLib {
|
|||
instance.patchProductCustomFields("a-test-to-ignore", instance.getApiKeyFromUsername("costantino_perciante"), customFieldsToChange);
|
||||
}
|
||||
|
||||
//@Test
|
||||
public void addTag()throws Exception{
|
||||
DataCatalogueImpl instance = factory.getUtilsPerScope(scope);
|
||||
instance.addTag("test-after-tags-editing", instance.getApiKeyFromUsername("costantino_perciante"), "api add tag to test");
|
||||
}
|
||||
|
||||
//@Test
|
||||
public void removeTag()throws Exception{
|
||||
DataCatalogueImpl instance = factory.getUtilsPerScope(scope);
|
||||
instance.removeTag("test-after-tags-editing", instance.getApiKeyFromUsername("costantino_perciante"), "api add tag to test");
|
||||
|
||||
}
|
||||
|
||||
//@Test
|
||||
public void removeGroup()throws Exception{
|
||||
DataCatalogueImpl instance = factory.getUtilsPerScope(scope);
|
||||
instance.removeDatasetFromGroup("pending", "test-after-tags-editing", instance.getApiKeyFromUsername("costantino_perciante"));
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue