product patch is available and allows to edit all the custom fields
git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/data-catalogue/ckan-util-library@135095 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
parent
6f1812ded6
commit
ce1c7c5f63
|
@ -42,7 +42,7 @@ public interface DataCatalogue {
|
|||
* @return a list of organizations
|
||||
*/
|
||||
List<CkanOrganization> getOrganizationsByUser(String username);
|
||||
|
||||
|
||||
/**
|
||||
* Returns the list of groups to whom the user belongs (with any role)
|
||||
* @param username
|
||||
|
@ -71,7 +71,7 @@ public interface DataCatalogue {
|
|||
* @return the catalogue url
|
||||
*/
|
||||
String getCatalogueUrl();
|
||||
|
||||
|
||||
/**
|
||||
* Return the manage product property
|
||||
* @return the manage product property
|
||||
|
@ -353,13 +353,13 @@ public interface DataCatalogue {
|
|||
* @return true if the field is set, false otherwise
|
||||
*/
|
||||
boolean setSearchableField(String datasetId, boolean searchable);
|
||||
|
||||
|
||||
/**
|
||||
* Retrieve the list of groups in this instance
|
||||
* @return a list of groups
|
||||
*/
|
||||
List<CkanGroup> getGroups();
|
||||
|
||||
|
||||
/**
|
||||
* Upload a file to ckan and attach it as resource.
|
||||
* @param file
|
||||
|
@ -370,7 +370,7 @@ public interface DataCatalogue {
|
|||
* @return
|
||||
*/
|
||||
CkanResource uploadResourceFile(File file, String packageId, String apiKey, String fileName, String description);
|
||||
|
||||
|
||||
/**
|
||||
* Allows to change the url, the name and the description of a resource.
|
||||
* @param resourceId
|
||||
|
@ -382,4 +382,14 @@ public interface DataCatalogue {
|
|||
* @return
|
||||
*/
|
||||
boolean patchResource(String resourceId, String url, String name, String description, String urlType, String apiKey);
|
||||
|
||||
/**
|
||||
* Patch a product with product id productId by using the couples in toChange.
|
||||
* NOTE: only the specified custom fields will be changed..
|
||||
* @param productId
|
||||
* @param apiKey
|
||||
* @param toChange
|
||||
* @return true on success, false otherwise
|
||||
*/
|
||||
boolean patchProductCustomFields(String productId, String apiKey, Map<String, List<String>> customFieldsToChange);
|
||||
}
|
||||
|
|
|
@ -145,7 +145,7 @@ public class DataCatalogueImpl implements DataCatalogue{
|
|||
return connection;
|
||||
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Check if the manage product is enabled
|
||||
* @return
|
||||
|
@ -1697,7 +1697,7 @@ public class DataCatalogueImpl implements DataCatalogue{
|
|||
checkNotNull(packageId);
|
||||
checkNotNull(token);
|
||||
checkNotNull(name);
|
||||
|
||||
|
||||
String returnedId = null;
|
||||
|
||||
try (CloseableHttpClient httpClient = HttpClientBuilder.create().build()) {
|
||||
|
@ -1724,9 +1724,9 @@ public class DataCatalogueImpl implements DataCatalogue{
|
|||
throw new RuntimeException("failed to add the file to CKAN storage. response status line from "
|
||||
+ apiRequestUrl + " was: " + response.getStatusLine());
|
||||
}
|
||||
|
||||
|
||||
logger.info("Returned message is " + response.getStatusLine());
|
||||
|
||||
|
||||
String json = EntityUtils.toString(response.getEntity());
|
||||
Object obj = JSONValue.parse(json);
|
||||
JSONObject finalResult=(JSONObject)obj;
|
||||
|
@ -1792,4 +1792,105 @@ public class DataCatalogueImpl implements DataCatalogue{
|
|||
return false;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
public boolean patchProductCustomFields(String productId, String apiKey,
|
||||
Map<String, List<String>> customFieldsToChange) {
|
||||
|
||||
// checks
|
||||
checkNotNull(productId);
|
||||
checkNotNull(apiKey);
|
||||
|
||||
if(customFieldsToChange == null || customFieldsToChange.isEmpty()) // TODO.. remove all custom fields maybe?!
|
||||
return true;
|
||||
|
||||
logger.info("Going to change product with id " + productId +"."
|
||||
+ " Request comes from user with key " + apiKey.substring(0, 5) + "****************");
|
||||
|
||||
logger.info("The new values are " + customFieldsToChange);
|
||||
|
||||
// Get already available custom fields
|
||||
Map<String, List<String>> fromCKANCustomFields = new HashMap<String, List<String>>();
|
||||
CheckedCkanClient client = new CheckedCkanClient(CKAN_CATALOGUE_URL, apiKey);
|
||||
List<CkanPair> extras = client.getDataset(productId).getExtras();
|
||||
|
||||
// fill the above map with these values
|
||||
for (CkanPair ckanPair : extras) {
|
||||
List<String> forThisValue = null;
|
||||
String key = ckanPair.getKey();
|
||||
if(fromCKANCustomFields.containsKey(key))
|
||||
forThisValue = fromCKANCustomFields.get(key);
|
||||
else
|
||||
forThisValue = new ArrayList<String>();
|
||||
forThisValue.add(ckanPair.getValue());
|
||||
fromCKANCustomFields.put(key, forThisValue);
|
||||
}
|
||||
|
||||
logger.info("The generated map from jackan looks like " + fromCKANCustomFields + ". Going to merge them");
|
||||
|
||||
// merge them with the new values
|
||||
Iterator<Entry<String, List<String>>> iteratorUserMap = customFieldsToChange.entrySet().iterator();
|
||||
while (iteratorUserMap.hasNext()) {
|
||||
Map.Entry<java.lang.String, java.util.List<java.lang.String>> entry = (Map.Entry<java.lang.String, java.util.List<java.lang.String>>) iteratorUserMap
|
||||
.next();
|
||||
|
||||
String key = entry.getKey();
|
||||
List<String> newValues = entry.getValue();
|
||||
fromCKANCustomFields.put(key, newValues);
|
||||
}
|
||||
|
||||
logger.info("After merging it is " + fromCKANCustomFields);
|
||||
|
||||
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();
|
||||
|
||||
// build the json array for the "extras" field.. each object looks like {"key": ..., "value": ...}
|
||||
JSONArray extrasObject = new JSONArray();
|
||||
|
||||
Iterator<Entry<String, List<String>>> iteratorNewFields = fromCKANCustomFields.entrySet().iterator();
|
||||
while (iteratorNewFields.hasNext()) {
|
||||
Map.Entry<java.lang.String, java.util.List<java.lang.String>> entry = (Map.Entry<java.lang.String, java.util.List<java.lang.String>>) iteratorNewFields
|
||||
.next();
|
||||
|
||||
String key = entry.getKey();
|
||||
List<String> values = entry.getValue();
|
||||
|
||||
for (String value : values) {
|
||||
JSONObject obj = new JSONObject();
|
||||
obj.put("value", value);
|
||||
obj.put("key", key);
|
||||
extrasObject.add(obj);
|
||||
}
|
||||
}
|
||||
|
||||
// perform the request
|
||||
jsonRequest.put("id", productId);
|
||||
jsonRequest.put("extras", extrasObject);
|
||||
|
||||
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 patch the product ", e);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -2,6 +2,7 @@ package org.gcube.datacatalogue.ckanutillibrary;
|
|||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
@ -14,8 +15,6 @@ import org.gcube.datacatalogue.ckanutillibrary.models.CkanDatasetRelationship;
|
|||
import org.gcube.datacatalogue.ckanutillibrary.models.DatasetRelationships;
|
||||
import org.gcube.datacatalogue.ckanutillibrary.models.RolesCkanGroupOrOrg;
|
||||
import org.gcube.datacatalogue.ckanutillibrary.utils.UtilMethods;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import eu.trentorise.opendata.jackan.CheckedCkanClient;
|
||||
|
@ -408,7 +407,14 @@ public class TestDataCatalogueLib {
|
|||
DataCatalogueImpl instance = factory.getUtilsPerScope(scope);
|
||||
String id = "858f5e77-80c2-4cb2-bcfc-77529693dc9a";
|
||||
instance.patchResource(id, "http://ftp.d4science.org/previews/69cc0769-de6f-45eb-a842-7be2807e8887.jpg", "new_name_for_testing_patch.csv", "description test", "", instance.getApiKeyFromUsername("costantino_perciante"));
|
||||
|
||||
}
|
||||
|
||||
//@Test
|
||||
public void testPatchProduct() throws Exception{
|
||||
DataCatalogueImpl instance = factory.getUtilsPerScope(scope);
|
||||
Map<String, List<String>> customFieldsToChange = new HashMap<String, List<String>>();
|
||||
customFieldsToChange.put("Status", Arrays.asList("Pending"));
|
||||
instance.patchProductCustomFields("a-test-to-ignore", instance.getApiKeyFromUsername("costantino_perciante"), customFieldsToChange);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue