Added method patchProductWithJSON to patch a product in one shot

git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/data-catalogue/ckan-util-library@139869 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
Costantino Perciante 2016-12-10 09:48:33 +00:00
parent aec8452e7b
commit ec5c34ecb2
2 changed files with 48 additions and 4 deletions

View File

@ -9,6 +9,7 @@ import org.gcube.datacatalogue.ckanutillibrary.models.CkanDatasetRelationship;
import org.gcube.datacatalogue.ckanutillibrary.models.DatasetRelationships;
import org.gcube.datacatalogue.ckanutillibrary.models.ResourceBean;
import org.gcube.datacatalogue.ckanutillibrary.models.RolesCkanGroupOrOrg;
import org.json.simple.JSONObject;
import eu.trentorise.opendata.jackan.model.CkanDataset;
import eu.trentorise.opendata.jackan.model.CkanGroup;
@ -461,4 +462,13 @@ public interface DataCatalogue {
* @return
*/
String getUriResolverUrl();
/**
* Require to patch a product according to the content of the parameter jsonRequest
* @param productId
* @param jsonRequest
* @param apiKey
* @return error message if any, null otherwise
*/
String patchProductWithJSON(String productId, JSONObject jsonRequest, String apiKey);
}

View File

@ -1417,7 +1417,7 @@ public class DataCatalogueImpl implements DataCatalogue{
String groupNameToCheck = UtilMethods.fromGroupTitleToName(groupNameOrId);
try(CloseableHttpClient httpClient = HttpClientBuilder.create().build();){
CheckedCkanClient client = new CheckedCkanClient(CKAN_CATALOGUE_URL, CKAN_TOKEN_SYS);
// check the group exists
@ -1954,13 +1954,13 @@ public class DataCatalogueImpl implements DataCatalogue{
String key = entry.getKey();
List<String> newValues = entry.getValue();
// get the unique set of values
Set<String> uniqueValues = new HashSet<String>();
if(fromCKANCustomFields.containsKey(key))
uniqueValues.addAll(fromCKANCustomFields.get(key));
uniqueValues.addAll(newValues);
fromCKANCustomFields.put(key, new ArrayList<String>(uniqueValues));
}
@ -2340,4 +2340,38 @@ public class DataCatalogueImpl implements DataCatalogue{
}
@Override
public String patchProductWithJSON(String productId, JSONObject jsonRequest,
String apiKey) {
checkNotNull(productId);
checkNotNull(jsonRequest);
checkNotNull(apiKey);
logger.info("Request of patching product " + productId + " with json " + jsonRequest);
try(CloseableHttpClient httpClient = HttpClientBuilder.create().build();){
String pathUpdatePatch = getCatalogueUrl() + "/api/3/action/package_patch";
HttpPost httpPostRequest = new HttpPost(pathUpdatePatch);
httpPostRequest.setHeader("Authorization", CKAN_TOKEN_SYS);
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 "
+ pathUpdatePatch + " was: " + response.getStatusLine());
}
return null;
}catch(Exception e){
logger.error("Error while trying to patch grsf record " + e.getMessage());
return e.getMessage();
}
}
}