Added setDatasetPrivate function for visibility

git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/data-catalogue/ckan-util-library@129058 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
Costantino Perciante 2016-06-10 14:26:01 +00:00
parent 213d328070
commit 8b914ba9f1
2 changed files with 98 additions and 9 deletions

View File

@ -17,6 +17,12 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import eu.trentorise.opendata.jackan.CkanClient;
import eu.trentorise.opendata.jackan.internal.org.apache.http.HttpResponse;
import eu.trentorise.opendata.jackan.internal.org.apache.http.HttpStatus;
import eu.trentorise.opendata.jackan.internal.org.apache.http.client.methods.HttpPost;
import eu.trentorise.opendata.jackan.internal.org.apache.http.entity.StringEntity;
import eu.trentorise.opendata.jackan.internal.org.apache.http.impl.client.CloseableHttpClient;
import eu.trentorise.opendata.jackan.internal.org.apache.http.impl.client.HttpClientBuilder;
import eu.trentorise.opendata.jackan.model.CkanLicense;
import eu.trentorise.opendata.jackan.model.CkanOrganization;
@ -266,7 +272,7 @@ public class CKanUtilsImpl implements CKanUtilsInterface{
public String getCKANDBUrl() {
return CKAN_DB_URL;
}
@Override
public String getCatalogueUrl() {
return CKAN_CATALOGUE_URL;
@ -327,4 +333,76 @@ public class CKanUtilsImpl implements CKanUtilsInterface{
return result;
}
@Override
public boolean setDatasetPrivate(boolean priv, String organizationId,
String datasetId, String owner) {
String pathSetPrivate = "/api/3/action/bulk_update_private";
String pathSetPublic = "/api/3/action/bulk_update_public";
String token = null;
if(owner == null || owner.isEmpty()){
logger.error("The owner parameter is mandatory");
return false;
}else{
token = getApiKeyFromUser(owner);
if(token == null){
logger.error("Unable to retrieve user's token");
return false;
}
}
// Request parameters to be replaced
String parameter = "{"
+ "\"org_id\":\"ORGANIZATION_ID\","
+ "\"datasets\":[\"DATASET_ID\"]"
+ "}";
if(organizationId != null && !organizationId.isEmpty() && datasetId != null && !datasetId.isEmpty()){
// replace with right data
parameter = parameter.replace("ORGANIZATION_ID", organizationId);
parameter = parameter.replace("DATASET_ID", datasetId);
CloseableHttpClient httpClient = HttpClientBuilder.create().build();
if(priv){
try {
HttpPost request = new HttpPost(CKAN_CATALOGUE_URL + pathSetPrivate);
request.addHeader("Authorization", token);
StringEntity params = new StringEntity(parameter);
request.setEntity(params);
HttpResponse response = httpClient.execute(request);
logger.debug("[PRIVATE]Response code is " + response.getStatusLine().getStatusCode() + " and response message is " + response.getStatusLine().getReasonPhrase());
if(response.getStatusLine().getStatusCode() == HttpStatus.SC_OK)
return true;
}catch (Exception ex) {
logger.error("Error while trying to set private the dataset " + ex);
}
}else
{
try {
HttpPost request = new HttpPost(CKAN_CATALOGUE_URL + pathSetPublic);
StringEntity params =new StringEntity(parameter);
request.addHeader("Authorization", token);
request.setEntity(params);
HttpResponse response = httpClient.execute(request);
logger.debug("[PUBLIC]Response code is " + response.getStatusLine().getStatusCode() + " and response message is " + response.getStatusLine().getReasonPhrase());
if(response.getStatusLine().getStatusCode() == HttpStatus.SC_OK)
return true;
}catch (Exception ex) {
logger.error("Error while trying to set public the dataset " + ex);
}
}
}
return false;
}
}

View File

@ -13,35 +13,35 @@ import eu.trentorise.opendata.jackan.model.CkanOrganization;
* @author Costantino Perciante at ISTI-CNR (costantino.perciante@isti.cnr.it)
*/
public interface CKanUtilsInterface {
/**
* Retrieve the API_KEY given the username .
* @param username
* @return an API_KEY string
*/
public String getApiKeyFromUser(String username);
/**
* Retrieve the user given the API_KEY (the user is retrieved if it is active).
* @param username
* @return an API_KEY string
*/
public CKanUserWrapper getUserFromApiKey(String apiKey);
/**
* Returns the list of organizations to whom the user belongs.
* @param username
* @return a list of organizations
*/
public List<CkanOrganization> getOrganizationsByUser(String username);
/**
* Returns the list of organizations' names to whom the user belongs.
* @param username
* @return a list of organizations
*/
public List<String> getOrganizationsNamesByUser(String username);
/**
* Given a username and a list of roles to be matched, find the organizations to who the user
* belongs and the role(s) he has in them.
@ -56,22 +56,33 @@ public interface CKanUtilsInterface {
* @return the catalogue url or exception if not found
*/
public String getCatalogueUrl();
/**
* Return the url of the database
*/
public String getCKANDBUrl();
/**
* Get the list of licenses' titles.
* @return the list of licenses' titles
*/
public List<String> getLicenseTitles();
/**
* Finds the id associated to the chosen license
* @param chosenLicense
* @return the id on success, null otherwise
*/
public String findLicenseIdByLicense(String chosenLicense);
/**
* Set dataset private
* @param priv
* @param organizationId
* @param datasetId
* @param owner
* @return true on success, false otherwise
*/
public boolean setDatasetPrivate(boolean priv, String organizationId, String datasetId, String owner);
}