Added methods to delete/retrieve a product

git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/data-catalogue/ckan-util-library@133211 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
Costantino Perciante 2016-10-14 15:10:43 +00:00
parent 187730a87e
commit 9091c57696
3 changed files with 143 additions and 1 deletions

View File

@ -9,6 +9,7 @@ import org.gcube.datacatalogue.ckanutillibrary.models.DatasetRelationships;
import org.gcube.datacatalogue.ckanutillibrary.models.ResourceBean;
import org.gcube.datacatalogue.ckanutillibrary.models.RolesCkanGroupOrOrg;
import eu.trentorise.opendata.jackan.model.CkanDataset;
import eu.trentorise.opendata.jackan.model.CkanGroup;
import eu.trentorise.opendata.jackan.model.CkanLicense;
import eu.trentorise.opendata.jackan.model.CkanOrganization;
@ -287,4 +288,19 @@ public interface DataCatalogue {
boolean assignDatasetToGroup(String groupNameOrId, String datasetNameOrId,
String apiKey);
/**
* Delete the dataset with id datasetId. If purge is true, the product will be purged too.
* @param datasetId
* @param apiKey
* @param purge
* @return
*/
boolean deleteProduct(String datasetId, String apiKey, boolean purge);
/**
* Retrieve a ckan dataset given its id
* @param datasetId
* @return
*/
CkanDataset getDataset(String datasetId, String apiKey);
}

View File

@ -1410,4 +1410,72 @@ public class DataCatalogueImpl implements DataCatalogue{
return toReturn;
}
@Override
public boolean deleteProduct(String datasetId, String apiKey, boolean purge) {
// checks
checkNotNull(datasetId);
checkArgument(!datasetId.isEmpty());
checkNotNull(apiKey);
checkArgument(!apiKey.isEmpty());
logger.debug("Incoming request of deleting dataset with id " + datasetId);
try{
CheckedCkanClient client = new CheckedCkanClient(CKAN_CATALOGUE_URL, apiKey);
client.deleteDataset(datasetId);
logger.info("Dataset with id " + datasetId + " deleted!");
if(purge){
logger.debug("Purging also ....");
try(CloseableHttpClient httpClient = HttpClientBuilder.create().build();){
String path = CKAN_CATALOGUE_URL + "/api/3/action/dataset_purge";
HttpPost request = new HttpPost(path);
request.addHeader("Authorization", CKAN_TOKEN_SYS); // this must be a sys_admin key
String entityBody = "{\"id\": \"" + datasetId + "\"}";
StringEntity params = new StringEntity(entityBody);
request.setEntity(params);
HttpResponse response = httpClient.execute(request);
logger.debug("Response code is " + response.getStatusLine().getStatusCode() + " and response message is " + response.getStatusLine().getReasonPhrase());
if(response.getStatusLine().getStatusCode() == 200){
logger.info("Dataset with id " + datasetId + " delete and purged!");
}
}
return true;
}
return true;
}catch(Exception e){
logger.error("Unable to delete such dataset ", e);
}
return false;
}
@Override
public CkanDataset getDataset(String datasetId, String apiKey) {
logger.info("Request ckan dataset with id " + datasetId);
// checks
checkNotNull(datasetId);
checkArgument(!datasetId.isEmpty());
checkNotNull(apiKey);
checkArgument(!apiKey.isEmpty());
try{
CheckedCkanClient client = new CheckedCkanClient(CKAN_CATALOGUE_URL, apiKey);
return client.getDataset(datasetId);
}catch(Exception e){
logger.error("Unable to retrieve such dataset, returning null ...", e);
}
return null;
}
}

View File

@ -6,6 +6,7 @@ import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.UUID;
import org.gcube.common.scope.api.ScopeProvider;
import org.gcube.datacatalogue.ckanutillibrary.models.CKanUserWrapper;
@ -16,7 +17,9 @@ import org.gcube.datacatalogue.ckanutillibrary.utils.UtilMethods;
import org.slf4j.LoggerFactory;
import eu.trentorise.opendata.jackan.CheckedCkanClient;
import eu.trentorise.opendata.jackan.model.CkanDataset;
import eu.trentorise.opendata.jackan.model.CkanOrganization;
import eu.trentorise.opendata.jackan.model.CkanResource;
import eu.trentorise.opendata.jackan.model.CkanUser;
/**
@ -33,7 +36,7 @@ public class TestDataCatalogueLib {
String subjectId = "aa_father4";
String objectId = "bb_son4";
//@Before
// @Before
public void before(){
factory = DataCatalogueFactory.getFactory();
}
@ -312,4 +315,59 @@ public class TestDataCatalogueLib {
}
//@Test
public void testAddResource() throws Exception{
DataCatalogueImpl instance = factory.getUtilsPerScope(scope);
String datasetId = "test_publish_folder_15_44";
//instance.assignDatasetToGroup(groupName, datasetId, instance.getApiKeyFromUsername("costantino_perciante"));
String api = instance.getApiKeyFromUsername("costantino_perciante");
CheckedCkanClient client = new CheckedCkanClient(instance.getCatalogueUrl(), api);
List<String> randomName = Arrays.asList("FIRMS", "RAM", "FishSource");
for (int i = 0; i < 100; i++) {
CkanResource resource = new CkanResource("https://goo.gl/FH5AQ5", datasetId);
String name = randomName.get((int)Math.round(Math.ceil(Math.random() * 3)));
resource.setName(name);
client.createResource(resource);
}
}
//@Test
public void checkGroupRole() throws Exception{
DataCatalogueImpl instance = factory.getUtilsPerScope(scope);
String role = instance.getRoleOfUserInGroup("francesco.mangiacrapa", "test-by-francesco", instance.getApiKeyFromUsername("francesco.mangiacrapa"));
logger.debug("Role is " + role);
}
//@Test
public void getURL() throws Exception{
DataCatalogueImpl util = factory.getUtilsPerScope("/gcube/devNext/NextNext");
CkanDataset dataset = new CkanDataset();
CheckedCkanClient client = new CheckedCkanClient(util.getCatalogueUrl(), util.getApiKeyFromUsername("costantino_perciante"));
dataset.setName("random-test-" + UUID.randomUUID().toString().substring(0, 5));
dataset.setOwnerOrg(client.getOrganization("devvre").getId());
dataset.setOpen(true);
dataset.setPriv(true);
//CkanDataset id = client.createDataset(dataset);
// util.setDatasetPrivate(true, client.getOrganization("devvre").getId(), id.getId(), util.getApiKeyFromUsername("costantino_perciante"));
}
//@Test
public void deleteAndPurgeDataset() throws Exception{
DataCatalogueImpl util = factory.getUtilsPerScope("/gcube/devNext/NextNext");
boolean deleted = util.deleteProduct("random-test-56sadadsfsdf", util.getApiKeyFromUsername("user_admin_devvre"), true);
logger.debug("Deleted ? " + deleted);
}
}