Added owner filter in lsiting and deleteAll

This commit is contained in:
Luca Frosini 2021-12-03 11:25:32 +01:00
parent e8e29532fa
commit abc354ee1b
3 changed files with 211 additions and 27 deletions

View File

@ -331,7 +331,7 @@ public class CKANPackage extends CKAN implements Moderated {
}
}
protected Map<String,String> getListCountParameters(int limit, int offset) {
protected Map<String,String> getListingParameters(int limit, int offset) {
Map<String,String> parameters = new HashMap<>();
if(limit <= 0) {
// According to CKAN documentation
@ -346,21 +346,72 @@ public class CKANPackage extends CKAN implements Moderated {
}
parameters.put(START_KEY, String.valueOf(offset * limit));
if(uriInfo != null) {
MultivaluedMap<String,String> queryParameters = uriInfo.getQueryParameters();
parameters = checkListParameters(queryParameters, parameters);
}
if(!parameters.containsKey(GCatConstants.Q_KEY)) {
String filter = getFilterForOrganizations();
parameters.put(GCatConstants.Q_KEY, filter);
}
parameters = addModerationStatusFilter(parameters);
return parameters;
}
/**
* @param purge indicate if the item
* @return the name list of deleted items
*/
public String deleteAll(boolean purge){
MultivaluedMap<String,String> queryParameters = uriInfo.getQueryParameters();
if(queryParameters.containsKey(GCatConstants.OWN_ONLY_QUERY_PARAMETER)) {
if(ckanUser.getRole() != Role.ADMIN) {
queryParameters.remove(GCatConstants.OWN_ONLY_QUERY_PARAMETER);
queryParameters.add(GCatConstants.OWN_ONLY_QUERY_PARAMETER, Boolean.TRUE.toString());
}
}else {
queryParameters.add(GCatConstants.OWN_ONLY_QUERY_PARAMETER, Boolean.TRUE.toString());
}
Map<String,String> parameters = getListingParameters(0,0);
ObjectNode objectNode = mapper.createObjectNode();
ArrayNode deleted = mapper.createArrayNode();
objectNode.set("deleted", deleted);
ArrayNode notDeleted = mapper.createArrayNode();
objectNode.set("failed", notDeleted);
sendGetRequest(LIST, parameters);
ArrayNode results = (ArrayNode) result.get(RESULTS_KEY);
while(results.size()>0) {
for(JsonNode node : results) {
try {
this.name = null;
this.result = node;
this.name = node.get(NAME_KEY).asText();
this.itemID = node.get(ID_KEY).asText();
delete(purge);
deleted.add(name);
} catch(Exception e) {
try {
if(name!=null) {
notDeleted.add(name);
logger.error("Error while trying to delete item with name {}", name);
}else {
logger.error("Unable to get the name of {}.",mapper.writeValueAsString(node));
}
} catch(Exception ex) {
logger.error("", ex);
}
}
}
sendGetRequest(LIST, parameters);
results = (ArrayNode) result.get(RESULTS_KEY);
}
return getAsString(objectNode);
}
public String list(Map<String,String> parameters) {
sendGetRequest(LIST, parameters);
@ -373,7 +424,7 @@ public class CKANPackage extends CKAN implements Moderated {
arrayNode.add(name);
} catch(Exception e) {
try {
logger.error("Unable to get the ID of {}. the result will not be included in the result",
logger.error("Unable to get the name of {}. The Item will not be included in the result",
mapper.writeValueAsString(node));
} catch(Exception ex) {
logger.error("", ex);
@ -386,12 +437,12 @@ public class CKANPackage extends CKAN implements Moderated {
@Override
public String list(int limit, int offset) {
Map<String,String> parameters = getListCountParameters(limit, offset);
Map<String,String> parameters = getListingParameters(limit, offset);
return list(parameters);
}
public int count() {
Map<String,String> parameters = getListCountParameters(1, 0);
Map<String,String> parameters = getListingParameters(1, 0);
sendGetRequest(LIST, parameters);
@ -410,7 +461,7 @@ public class CKANPackage extends CKAN implements Moderated {
return matches;
}
protected static String[] allowedListQueryParameters = new String[] {"q", "fq", "fq_list", "sort",
protected static String[] allowedListQueryParameters = new String[] {"fq", "fq_list", "sort",
/* "facet", "facet.mincount", "facet.limit", "facet.field", */
"include_drafts", "include_private", "ext_bbox"};
@ -443,7 +494,7 @@ public class CKANPackage extends CKAN implements Moderated {
if(organizations.size()==0) {
// Adding organization filter to q
String filter = getFilterForOrganizations();
parameters.put(GCatConstants.Q_KEY, String.format("%s AND %s", q, filter));
q = String.format("%s AND %s", q, filter);
}else {
organizations.removeAll(this.supportedOrganizations);
if(organizations.size()>0) {
@ -454,9 +505,17 @@ public class CKANPackage extends CKAN implements Moderated {
} else {
String filter = getFilterForOrganizations();
parameters.put(GCatConstants.Q_KEY, filter);
q = filter;
}
if(queryParameters.containsKey(GCatConstants.OWN_ONLY_QUERY_PARAMETER)) {
if(!queryParameters.get(GCatConstants.OWN_ONLY_QUERY_PARAMETER).isEmpty() && Boolean.parseBoolean(queryParameters.get(GCatConstants.OWN_ONLY_QUERY_PARAMETER).get(0))) {
String filter = String.format("%s:%s", AUTHOR_EMAIL_KEY, ckanUser.getPortalUser().getEMail());
q = String.format("%s AND %s", q, filter);
}
}
parameters.put(GCatConstants.Q_KEY, q);
for(String key : allowedListQueryParameters) {
if(queryParameters.containsKey(key)) {
@ -587,10 +646,14 @@ public class CKANPackage extends CKAN implements Moderated {
}
protected void readItem() throws Exception {
if(this.result == null) {
String ret = super.read();
result = mapper.readTree(ret);
this.result = mapper.readTree(ret);
}
if(this.itemID == null) {
this.itemID = result.get(ID_KEY).asText();
}
}
@Override

View File

@ -68,6 +68,22 @@ public class Item extends REST<CKANPackage> implements org.gcube.gcat.api.interf
}
}
@DELETE
@Produces(ResourceInitializer.APPLICATION_JSON_CHARSET_UTF_8)
public String deleteAll(@QueryParam(GCatConstants.PURGE_QUERY_PARAMETER) @DefaultValue("false") Boolean purge) {
CKANPackage ckan = getInstance();
return ckan.deleteAll(purge);
}
@PURGE
@Produces(ResourceInitializer.APPLICATION_JSON_CHARSET_UTF_8)
public String purgeAll() {
CKANPackage ckan = getInstance();
return ckan.deleteAll(true);
}
/*
* Not used as REST method, implemented to respect {@link org.gcube.gcat.api.interfaces.Item} interface
*/

View File

@ -174,6 +174,108 @@ public class CKANPackageTest extends ContextTest {
logger.debug("List:\n{}", mapper.writeValueAsString(gotList));
}
protected UriInfo getListingUriInfo(final MultivaluedMap<String, String> queryParameters) {
UriInfo uriInfo = new UriInfo() {
@Override
public URI resolve(URI uri) {
return null;
}
@Override
public URI relativize(URI uri) {
return null;
}
@Override
public UriBuilder getRequestUriBuilder() {
return null;
}
@Override
public URI getRequestUri() {
return null;
}
@Override
public MultivaluedMap<String, String> getQueryParameters(boolean decode) {
return null;
}
@Override
public MultivaluedMap<String, String> getQueryParameters() {
return queryParameters;
}
@Override
public List<PathSegment> getPathSegments(boolean decode) {
return null;
}
@Override
public List<PathSegment> getPathSegments() {
return null;
}
@Override
public MultivaluedMap<String, String> getPathParameters(boolean decode) {
return null;
}
@Override
public MultivaluedMap<String, String> getPathParameters() {
return null;
}
@Override
public String getPath(boolean decode) {
return null;
}
@Override
public String getPath() {
return null;
}
@Override
public List<String> getMatchedURIs(boolean decode) {
return null;
}
@Override
public List<String> getMatchedURIs() {
return null;
}
@Override
public List<Object> getMatchedResources() {
return null;
}
@Override
public UriBuilder getBaseUriBuilder() {
return null;
}
@Override
public URI getBaseUri() {
return null;
}
@Override
public UriBuilder getAbsolutePathBuilder() {
return null;
}
@Override
public URI getAbsolutePath() {
return null;
}
};
return uriInfo;
}
@Test
public void listWithParameters() throws Exception {
String contextName = "/gcube/devNext/NextNext";
@ -183,15 +285,13 @@ public class CKANPackageTest extends ContextTest {
CKANPackage ckanPackage = new CKANPackage();
ObjectMapper mapper = new ObjectMapper();
Map<String,String> parameters = new HashMap<>();
parameters.put(CKANPackage.ROWS_KEY, String.valueOf(10));
parameters.put(CKANPackage.START_KEY, String.valueOf(0));
boolean[] values = new boolean[]{true, false};
for(boolean includeValidOrganization : values) {
for(boolean includeFakeOrganization : values) {
MultivaluedMap<String,String> queryParameters = new MultivaluedHashMap<>();
StringWriter stringWriter = new StringWriter();
boolean addOr = false;
@ -213,16 +313,21 @@ public class CKANPackageTest extends ContextTest {
queryParameters.add(GCatConstants.Q_KEY, filter);
}
queryParameters.add(GCatConstants.OWN_ONLY_QUERY_PARAMETER, Boolean.TRUE.toString());
/*
parameters.put("fl","[\"name\"]");
queryParameters.add("fl","[\"name\"]");
*/
/*
parameters.put("facet.field","[\"name\"]");
parameters.put("sort","name asc");
queryParameters.add("facet.field","[\"name\"]");
queryParameters.add("sort","name asc");
*/
Map<String,String> parameters = null;
try {
parameters = ckanPackage.checkListParameters(queryParameters, parameters);
ckanPackage.setUriInfo(getListingUriInfo(queryParameters));
parameters = ckanPackage.getListingParameters(10, 0);
}catch (ForbiddenException e) {
if(includeFakeOrganization) {
// This is the expected behaviour