Searchability field is now managed

git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/data-catalogue/ckan-util-library@133697 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
Costantino Perciante 2016-10-28 08:55:13 +00:00
parent e143607f13
commit e18c5ff89c
3 changed files with 59 additions and 1 deletions

View File

@ -327,4 +327,9 @@ public interface DataCatalogue {
* @return
*/
CkanDataset getDataset(String datasetId, String apiKey);
/**
* Set searchable field
*/
boolean setSearchableField(String datasetId, boolean searchable);
}

View File

@ -793,12 +793,18 @@ public class DataCatalogueImpl implements DataCatalogue{
res.getId(),
CKAN_TOKEN_SYS); // use sysadmin api key to be sure it will be set
logger.debug("Was visibility set to " + (setPublic ? "public" : "private") + "? " + visibilitySet);
logger.info("Was visibility set to " + (setPublic ? "public" : "private") + "? " + visibilitySet);
// set searchable to true
boolean searchableSet = setSearchableField(res.getId(), true);
logger.info("Was searchable set to True " + searchableSet);
return res.getId();
}
}catch(Exception e){
// try to update
@ -1563,4 +1569,43 @@ public class DataCatalogueImpl implements DataCatalogue{
return null;
}
@Override
public boolean setSearchableField(String datasetId, boolean searchable) {
// checks
checkNotNull(datasetId);
checkArgument(!datasetId.isEmpty());
String searchableAsString = searchable ? "True" : "False";
// Patch package path
String patchPackage = CKAN_CATALOGUE_URL + "/api/3/action/package_patch";
// Request parameters to be replaced
String parameter = "{"
+ "\"id\":\"DATASET_ID\","
+ "\"searchable\":\"SEARCHABLE\""
+ "}";
// replace with right data
parameter = parameter.replace("DATASET_ID", datasetId);
parameter = parameter.replace("SEARCHABLE", searchableAsString);
try(CloseableHttpClient httpClient = HttpClientBuilder.create().build();) {
HttpPost request = new HttpPost(patchPackage);
request.addHeader("Authorization", CKAN_TOKEN_SYS);
StringEntity params = new StringEntity(parameter);
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() == HttpStatus.SC_OK)
return true;
}catch (Exception ex) {
logger.error("Error while trying to set searchable the dataset ", ex);
}
return false;
}
}

View File

@ -370,4 +370,12 @@ public class TestDataCatalogueLib {
}
// @Test
public void testSearchableSet() throws Exception{
DataCatalogueImpl util = factory.getUtilsPerScope("/gcube/devNext/NextNext");
boolean setSearchability = util.setSearchableField("1b261d07-9f9c-414f-ad8d-c5aa429548fc", false);
logger.debug("Searchability set? " + setSearchability);
}
}