added methods to add and remove tags to a product
git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/data-catalogue/ckan-util-library@135115 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
parent
c43c8e8307
commit
22a849332b
|
@ -392,4 +392,22 @@ public interface DataCatalogue {
|
|||
* @return true on success, false otherwise
|
||||
*/
|
||||
boolean patchProductCustomFields(String productId, String apiKey, Map<String, List<String>> customFieldsToChange);
|
||||
|
||||
/**
|
||||
* Remove a tag from a product
|
||||
* @param productId
|
||||
* @param apiKey
|
||||
* @param tagToRemove
|
||||
* @return true on success, false otherwise
|
||||
*/
|
||||
boolean removeTag(String productId, String apiKey, String tagToRemove);
|
||||
|
||||
/**
|
||||
* Add a tag from a product
|
||||
* @param productId
|
||||
* @param apiKey
|
||||
* @param tagToAdd
|
||||
* @return true on success, false otherwise
|
||||
*/
|
||||
boolean addTag(String productId, String apiKey, String tagToAdd);
|
||||
}
|
||||
|
|
|
@ -1893,4 +1893,115 @@ public class DataCatalogueImpl implements DataCatalogue{
|
|||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean removeTag(String productId, String apiKey, String tagToRemove) {
|
||||
|
||||
// checks
|
||||
checkNotNull(productId);
|
||||
checkNotNull(apiKey);
|
||||
checkNotNull(tagToRemove);
|
||||
|
||||
try (CloseableHttpClient httpClient = HttpClientBuilder.create().build()) {
|
||||
String apiRequestUrl = CKAN_CATALOGUE_URL + "/api/3/action/package_patch";
|
||||
HttpPost httpPostRequest = new HttpPost(apiRequestUrl);
|
||||
httpPostRequest.setHeader("Authorization", apiKey);
|
||||
|
||||
// Request parameters to be replaced
|
||||
JSONObject jsonRequest = new JSONObject();
|
||||
JSONArray tagsAsJson = new JSONArray();
|
||||
|
||||
CheckedCkanClient client = new CheckedCkanClient(CKAN_CATALOGUE_URL, apiKey);
|
||||
CkanDataset dataset = client.getDataset(productId);
|
||||
List<CkanTag> currentTags = dataset.getTags();
|
||||
|
||||
// build the json array for the "tags" field.. each object looks like
|
||||
// {
|
||||
//
|
||||
// "vocabulary_id": null,
|
||||
// "state": "active",
|
||||
// "display_name": "....",
|
||||
// "id": "....",
|
||||
// "name": "..."
|
||||
//
|
||||
// }
|
||||
|
||||
for(CkanTag ckanTag : currentTags){
|
||||
if(!ckanTag.getName().equals(tagToRemove)){
|
||||
JSONObject obj = new JSONObject();
|
||||
obj.put("vocabulary_id", ckanTag.getVocabularyId());
|
||||
obj.put("state", ckanTag.getState().toString());
|
||||
obj.put("display_name", ckanTag.getDisplayName());
|
||||
obj.put("id", ckanTag.getId());
|
||||
obj.put("name", ckanTag.getName());
|
||||
tagsAsJson.add(obj);
|
||||
}
|
||||
}
|
||||
|
||||
// perform the request
|
||||
jsonRequest.put("id", productId);
|
||||
jsonRequest.put("tags", tagsAsJson);
|
||||
|
||||
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 tag " + tagToRemove, e);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean addTag(String productId, String apiKey, String tagToAdd) {
|
||||
|
||||
// checks
|
||||
checkNotNull(productId);
|
||||
checkNotNull(apiKey);
|
||||
checkNotNull(tagToAdd);
|
||||
|
||||
try{
|
||||
|
||||
CheckedCkanClient client = new CheckedCkanClient(CKAN_CATALOGUE_URL, apiKey);
|
||||
CkanDataset dataset = client.getDataset(productId);
|
||||
List<CkanTag> currentTags = dataset.getTags();
|
||||
|
||||
Iterator<CkanTag> tagsIterator = currentTags.iterator();
|
||||
|
||||
boolean added = true;
|
||||
|
||||
// check if it is already there ...
|
||||
while (tagsIterator.hasNext()) {
|
||||
CkanTag ckanTag = (CkanTag) tagsIterator.next();
|
||||
if(ckanTag.getName().equals(tagToAdd)){
|
||||
added = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if(added){
|
||||
currentTags.add(new CkanTag(tagToAdd));
|
||||
dataset.setTags(currentTags);
|
||||
client.patchUpdateDataset(dataset);
|
||||
}
|
||||
|
||||
return true;
|
||||
}catch(Exception e){
|
||||
logger.error("Failed to add the tag " + tagToAdd, e);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue