improved some methods of the interface provided by CMS adding a map for

filtering
This commit is contained in:
Francesco Mangiacrapa 2022-04-07 15:19:50 +02:00
parent c9f64ab384
commit 0a686c7667
6 changed files with 225 additions and 123 deletions

View File

@ -4,7 +4,6 @@
package org.gcube.datacatalogue.utillibrary.gcat;
import java.net.MalformedURLException;
import java.util.HashMap;
import java.util.Map;
import javax.ws.rs.WebApplicationException;
@ -36,7 +35,7 @@ public class GCatCaller {
private static final Logger LOG = LoggerFactory.getLogger(GCatCaller.class);
public final static String MODERATOR_ITEM_STATUS_FIELD_NAME = Moderated.SYSTEM_CM_ITEM_STATUS.replaceAll(":", "");
public static final String MODERATOR_SYSTEM_CM_PREFIX = Moderated.SYSTEM_CM_PREFIX.replaceAll(":", "");
/**
@ -222,57 +221,41 @@ public class GCatCaller {
/**
* Gets the list items for CM status.
*
* @param status the status
* @param limit the limit
* @param offset the offset
* @param status the status
* @param limit the limit
* @param offset the offset
* @param filters the filters
* @return the list items for CM status
* @throws WebServiceException the web service exception
* @throws MalformedURLException the malformed URL exception
*/
public String getListItemsForCMStatus(CMItemStatus status, int limit, int offset)
public String getListItemsForCMStatus(CMItemStatus status, int limit, int offset, Map<String, String> filters)
throws WebServiceException, MalformedURLException {
LOG.trace("getListItemsForCMStatus called");
LOG.info("Calling list items for status: " + status);
LOG.info("called getListItemsForCMStatus called with [status: " + status + "], [limit: " + limit
+ "], [offset: " + offset + "], [filters: " + filters + "]");
Map<String, String> queryParams = new HashMap<String, String>();
// queryParams.put(Moderated.SYSTEM_CM_ITEM_STATUS, status.getValue());
String modStatusField = GCatCaller.MODERATOR_ITEM_STATUS_FIELD_NAME;
queryParams.put("q", "extras_" + modStatusField + ":" + status.getValue());
if (limit < 0)
limit = 10;
if (offset < 0)
offset = 0;
int pageNumber = offsetToPageNumber(limit, offset);
LOG.info("Using pageNumber {} for offset {} " + pageNumber, offset);
queryParams.put("limit", limit + "");
queryParams.put("offset", pageNumber + "");
Map<String, String> queryParams = GCatCallerUtil.genericQueryBuilderFor(status, limit, offset, false, filters);
return getListItemsForQuery(queryParams);
}
/**
* Count list items for CM status.
*
* @param status the status
* @return the int
* @param status the status
* @param filters
* @return the number of items
* @throws WebServiceException the web service exception
* @throws MalformedURLException the malformed URL exception
*/
public int countListItemsForCMStatus(CMItemStatus status) throws WebServiceException, MalformedURLException {
public int countListItemsForCMStatus(CMItemStatus status, Map<String, String> filters)
throws WebServiceException, MalformedURLException {
LOG.trace("countListItemsForCMStatus called");
LOG.info("Calling count list items for status: " + status);
LOG.info("Calling count list items for [status: " + status + "], [filters: " + filters + "]");
Map<String, String> queryParams = new HashMap<String, String>();
// queryParams.put(Moderated.SYSTEM_CM_ITEM_STATUS, status.getValue());
Map<String, String> queryParams = GCatCallerUtil.genericQueryBuilderFor(status, null, null, true, filters);
String modStatusField = GCatCaller.MODERATOR_ITEM_STATUS_FIELD_NAME;
queryParams.put("q", "extras_" + modStatusField + ":" + status.getValue());
queryParams.put("count", "true");
String theCount = getListItemsForQuery(queryParams);
int count = 0;
try {
@ -347,8 +330,7 @@ public class GCatCaller {
LOG.info("returning gCatConfig: " + catalogueConfiguration);
return catalogueConfiguration;
}
private static int offsetToPageNumber(int limit, int offset) {
int pageNumber = offset;

View File

@ -0,0 +1,69 @@
package org.gcube.datacatalogue.utillibrary.gcat;
import java.util.HashMap;
import java.util.Map;
import org.gcube.gcat.api.moderation.CMItemStatus;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* The Class GCatCallerUtil.
*
* @author Francesco Mangiacrapa at ISTI-CNR francesco.mangiacrapa@isti.cnr.it
*
* Apr 7, 2022
*/
public class GCatCallerUtil {
private static final Logger LOG = LoggerFactory.getLogger(GCatCallerUtil.class);
/**
* Generic query builder for.
*
* @param status the status
* @param limit the limit
* @param offset the offset
* @param count the count
* @param filters the filters
* @return the map
*/
public static Map<String, String> genericQueryBuilderFor(CMItemStatus status, Integer limit, Integer offset,
Boolean count, Map<String, String> filters) {
LOG.info("genericQueryBuilderFor called with [status: " + status + "], [limit: " + limit + "], [offset: "
+ offset + "], [count: " + count + "], [filters: " + filters + "]");
Map<String, String> queryParams = new HashMap<String, String>();
String modStatusField = GCatCaller.MODERATOR_ITEM_STATUS_FIELD_NAME;
StringBuilder queryBuilder = new StringBuilder();
queryBuilder.append("extras_" + modStatusField + ":" + status.getValue());
if (filters != null) {
for (String fieldName : filters.keySet()) {
queryBuilder.append(" AND " + fieldName + ":" + filters.get(fieldName));
}
}
queryParams.put("q", queryBuilder.toString());
if (limit != null) {
limit = limit < 0 ? 10 : limit;
queryParams.put("limit", limit + "");
}
if (offset != null) {
offset = offset < 0 ? 0 : offset;
queryParams.put("offset", offset + "");
}
if (count != null) {
if (count) {
queryParams.put("count", "true");
}
}
return queryParams;
}
}

View File

@ -3,6 +3,7 @@ package org.gcube.datacatalogue.utillibrary.server.cms;
import java.io.InvalidObjectException;
import java.net.MalformedURLException;
import java.util.List;
import java.util.Map;
import javax.ws.rs.WebApplicationException;
import javax.xml.ws.WebServiceException;
@ -24,7 +25,7 @@ public interface CatalogueContentModeratorSystem {
*
* @param reloadConfig the reload config
* @return true, if is moderation enabled
* @throws MalformedURLException
* @throws MalformedURLException
*/
boolean isModerationEnabled(boolean reloadConfig) throws MalformedURLException;
@ -34,13 +35,14 @@ public interface CatalogueContentModeratorSystem {
* @param theStatus the the status
* @param limit the limit
* @param offset the offset
* @param filters add the input filters to query on CKAN
* @return the list items for status
* @throws WebApplicationException the web application exception
* @throws MalformedURLException the malformed URL exception
* @throws InvalidObjectException the invalid object exception
*/
public List<CkanDataset> getListItemsForStatus(ItemStatus theStatus, int limit, int offset)
throws WebApplicationException, MalformedURLException, InvalidObjectException;
public List<CkanDataset> getListItemsForStatus(ItemStatus theStatus, int limit, int offset,
Map<String, String> filters) throws WebApplicationException, MalformedURLException, InvalidObjectException;
/**
* Reject item.
@ -67,9 +69,10 @@ public interface CatalogueContentModeratorSystem {
* Count list items for status.
*
* @param theStatus the the status
* @param filters add the input filters to query on CKAN
* @return the long
*/
long countListItemsForStatus(ItemStatus theStatus);
long countListItemsForStatus(ItemStatus theStatus, Map<String, String> filters);
/**
* Approve item.

View File

@ -7,6 +7,7 @@ import java.io.InvalidObjectException;
import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.ws.rs.WebApplicationException;
import javax.xml.ws.WebServiceException;
@ -51,7 +52,7 @@ public class DataCatalogueCMSImpl implements CatalogueContentModeratorSystem {
* Checks if is Moderation is enabled in the working scope
*
* @return true, if is content moderator enabled
* @throws MalformedURLException
* @throws MalformedURLException
*/
@Override
public boolean isModerationEnabled(boolean reloadConfig) throws MalformedURLException {
@ -117,19 +118,20 @@ public class DataCatalogueCMSImpl implements CatalogueContentModeratorSystem {
* @param theStatus the the status
* @param limit the limit
* @param offset the offset
* @param filters add the input filters to query on CKAN
* @return the list items for status
* @throws WebApplicationException the web application exception
* @throws MalformedURLException the malformed URL exception
* @throws InvalidObjectException the invalid object exception
*/
@Override
public List<CkanDataset> getListItemsForStatus(ItemStatus theStatus, int limit, int offset)
throws WebApplicationException, MalformedURLException, InvalidObjectException {
public List<CkanDataset> getListItemsForStatus(ItemStatus theStatus, int limit, int offset,
Map<String, String> filters) throws WebApplicationException, MalformedURLException, InvalidObjectException {
LOG.info("called getListItemsForStatus with [status: " + theStatus + "], [limit: " + limit + "], [offset: "
+ offset + "]");
+ offset + "], [filters: " + filters + "]");
List<CkanDataset> listDataset = null;
checkNotNull(theStatus);
JSONArray jsonArray = getSourceArrayOfItemsForStatus(theStatus, limit, offset);
JSONArray jsonArray = getSourceArrayOfItemsForStatus(theStatus, limit, offset, filters);
if (jsonArray != null) {
int size = jsonArray.size();
@ -164,16 +166,17 @@ public class DataCatalogueCMSImpl implements CatalogueContentModeratorSystem {
* Count list items for status.
*
* @param theStatus the the status
* @param filters add the input filters to query on CKAN
* @return the number of items with the input status
*/
@Override
public long countListItemsForStatus(ItemStatus theStatus) {
public long countListItemsForStatus(ItemStatus theStatus, Map<String, String> filters) {
LOG.info("called countListItemsForStatus with [status: " + theStatus + "]");
checkNotNull(theStatus);
int count = 0;
try {
CMItemStatus cmStatus = toCMStatus(theStatus);
count = gCatCaller.countListItemsForCMStatus(cmStatus);
count = gCatCaller.countListItemsForCMStatus(cmStatus, filters);
} catch (Exception e) {
LOG.error("Error on counting the items for status " + theStatus, e);
return -1;
@ -189,15 +192,16 @@ public class DataCatalogueCMSImpl implements CatalogueContentModeratorSystem {
* @param theStatus the the status
* @param limit the limit
* @param offset the offset
* @param filters the filters
* @return the source array of items for status
* @throws WebApplicationException the web application exception
* @throws MalformedURLException the malformed URL exception
* @throws InvalidObjectException the invalid object exception
*/
protected org.json.simple.JSONArray getSourceArrayOfItemsForStatus(ItemStatus theStatus, int limit, int offset)
throws WebApplicationException, MalformedURLException, InvalidObjectException {
protected org.json.simple.JSONArray getSourceArrayOfItemsForStatus(ItemStatus theStatus, int limit, int offset,
Map<String, String> filters) throws WebApplicationException, MalformedURLException, InvalidObjectException {
LOG.info("called getSourceArrayOfItemsForStatus with [status: " + theStatus + "], [limit: " + limit
+ "], [offset: " + offset + "]");
+ "], [offset: " + offset + "], [filters: " + filters + "]");
checkNotNull(theStatus);
// TODO MUST BE CHANGED FOR THE STATUS
org.json.simple.JSONArray jsonArray = null;
@ -210,7 +214,7 @@ public class DataCatalogueCMSImpl implements CatalogueContentModeratorSystem {
*/
CMItemStatus cmiStatus = toCMStatus(theStatus);
String datasetNames = gCatCaller.getListItemsForCMStatus(cmiStatus, limit, offset);
String datasetNames = gCatCaller.getListItemsForCMStatus(cmiStatus, limit, offset, filters);
if (datasetNames != null) {
LOG.debug("for status " + theStatus + " found dataset: " + datasetNames);

View File

@ -1,16 +1,18 @@
package org.gcube.datacatalogue.utillibrary.test;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.gcube.common.authorization.library.provider.SecurityTokenProvider;
import org.gcube.common.scope.api.ScopeProvider;
import org.gcube.datacatalogue.utillibrary.gcat.GCatCaller;
import org.gcube.datacatalogue.utillibrary.server.DataCatalogueFactory;
import org.gcube.datacatalogue.utillibrary.server.DataCatalogueImpl;
import org.gcube.datacatalogue.utillibrary.server.cms.CatalogueContentModeratorSystem;
import org.gcube.datacatalogue.utillibrary.shared.ItemStatus;
import org.gcube.datacatalogue.utillibrary.shared.jackan.model.CkanDataset;
import org.junit.Before;
import org.junit.Test;
import org.slf4j.LoggerFactory;
/**
@ -32,7 +34,7 @@ public class TestDataCatalogueCMS {
*
* @throws Exception the exception
*/
//@Before
// @Before
public void before() throws Exception {
factory = DataCatalogueFactory.getFactory();
}
@ -52,57 +54,133 @@ public class TestDataCatalogueCMS {
LOG.debug(CatalogueContentModeratorSystem.class.getName() + " instancied correclty");
}
// @Test
public void performQuery() throws Exception {
DataCatalogueImpl instance = factory.getUtilsPerScope(scope);
List<String> emailsAddresses = new ArrayList<String>(2);
emailsAddresses.add("luca.frosini@isti.cnr.it");
emailsAddresses.add("francesco.mangiacrapa@cnr.it");
emailsAddresses.add("pagano@cnr.it");
String theQuery = mockQueryForEmails(emailsAddresses, "OR");
String system_cm_prefix = GCatCaller.MODERATOR_ITEM_STATUS_FIELD_NAME;
StringBuilder queryValuesBuilder = new StringBuilder();
queryValuesBuilder.append("author_email:" + theQuery);
queryValuesBuilder.append(" AND ");
queryValuesBuilder.append("extras_" + system_cm_prefix + ":*");
Map<String, String> query = new HashMap<String, String>(1);
query.put("q", queryValuesBuilder.toString());
ScopeProvider.instance.set(scope);
SecurityTokenProvider.instance.set(authorizationToken);
String listItems = instance.performQuery(query);
System.out.println("item returned: " + listItems);
}
// @Test
public void listItemsForCMStatus() throws Exception {
ItemStatus theStatus = ItemStatus.PENDING;
ScopeProvider.instance.set(scope);
SecurityTokenProvider.instance.set(authorizationToken);
DataCatalogueImpl dImpl = factory.getUtilsPerScope(scope);
CatalogueContentModeratorSystem cCMS = dImpl.getCatalogueContentModerator();
LOG.debug(CatalogueContentModeratorSystem.class.getName() + " instancied correclty");
List<String> emailsAddresses = new ArrayList<String>();
// emailsAddresses.add("luca.frosini@isti.cnr.it");
emailsAddresses.add("francesco.mangiacrapa@isti.cnr.it");
// emailsAddresses.add("pagano@cnr.it");
String theQuery = mockQueryForEmails(emailsAddresses, "OR");
Map<String, String> filters = new HashMap<String, String>();
filters.put("author_email", theQuery);
List<CkanDataset> listItems = cCMS.getListItemsForStatus(theStatus, 10, 0, filters);
int i = 0;
for (CkanDataset ckanDataset : listItems) {
System.out.println(++i + ") item returned: " + ckanDataset);
}
}
/**
* Gets the scope per url.
*
* @return the scope per url
* @throws Exception
*/
//@Test
public void getListItemsForStatus() throws Exception {
// @Test
public void countListItemsForStatus() throws Exception {
try {
ItemStatus theStatus = ItemStatus.APPROVED;
ScopeProvider.instance.set(scope);
SecurityTokenProvider.instance.set(authorizationToken);
DataCatalogueFactory factory = DataCatalogueFactory.getFactory();
DataCatalogueImpl dImpl = factory.getUtilsPerScope(scope);
CatalogueContentModeratorSystem cCMS = dImpl.getCatalogueContentModerator();
LOG.debug(CatalogueContentModeratorSystem.class.getName() + " instancied correclty");
List<CkanDataset> listDatasets = cCMS.getListItemsForStatus(ItemStatus.REJECTED, 20, 0);
List<String> emailsAddresses = new ArrayList<String>();
emailsAddresses.add("luca.frosini@isti.cnr.it");
// emailsAddresses.add("francesco.mangiacrapa@isti.cnr.it");
// emailsAddresses.add("pagano@cnr.it");
for (CkanDataset ckanDataset : listDatasets) {
LOG.debug("CkanDataset read: " + ckanDataset);
String theQuery = mockQueryForEmails(emailsAddresses, "OR");
Map<String, String> filters = new HashMap<String, String>();
filters.put("author_email", theQuery);
long size = cCMS.countListItemsForStatus(theStatus, filters);
LOG.debug("Size of list of items for status " + theStatus + " is: " + size);
} catch (Exception e) {
e.printStackTrace();
}
}
public static String mockQueryForEmails(List<String> emailsAddresses, String queryOperator) throws Exception {
StringBuilder queryMails = new StringBuilder();
if (queryOperator == null)
queryOperator = "OR";
// BUILDING EMAILS QUERY
int numberOfEmails = emailsAddresses.size();
String theQuery = "";
// case 1 email address
if (numberOfEmails == 1) {
theQuery = "'" + emailsAddresses.get(0) + "'";
} else {
// case N > 1 email addresses
for (int i = 0; i < emailsAddresses.size() - 1; i++) {
String email = emailsAddresses.get(i);
if (i == 0) {
// opening the query and adding first email address
queryMails.append("('" + email + "'");
} else {
// adding the operator and the email address
queryMails.append(" " + queryOperator + " '" + email + "'");
}
}
} catch (Exception e) {
e.printStackTrace();
theQuery = queryMails.toString();
// to be sure that endsWith Operator
if (!theQuery.endsWith(queryOperator)) {
theQuery += " " + queryOperator + " ";
}
// adding last email address and closing the query
theQuery += "'" + emailsAddresses.get(numberOfEmails - 1) + "')";
}
return theQuery;
}
/**
* Gets the scope per url.
*
* @return the scope per url
* @throws Exception
*/
//@Test
public void getSizeOfListItemsForStatus() throws Exception {
try {
ItemStatus theStatus = ItemStatus.PENDING;
ScopeProvider.instance.set(scope);
SecurityTokenProvider.instance.set(authorizationToken);
DataCatalogueFactory factory = DataCatalogueFactory.getFactory();
DataCatalogueImpl dImpl = factory.getUtilsPerScope(scope);
CatalogueContentModeratorSystem cCMS = dImpl.getCatalogueContentModerator();
LOG.debug(CatalogueContentModeratorSystem.class.getName() + " instancied correclty");
long size = cCMS.countListItemsForStatus(theStatus);
LOG.debug("Size of list of items for status "+theStatus + " is: "+ size);
} catch (Exception e) {
e.printStackTrace();
}
}
}

View File

@ -21,6 +21,7 @@ import org.gcube.datacatalogue.utillibrary.shared.jackan.model.CkanGroup;
import org.gcube.datacatalogue.utillibrary.shared.jackan.model.CkanOrganization;
import org.gcube.datacatalogue.utillibrary.shared.jackan.model.CkanUser;
import org.junit.Before;
import org.junit.Test;
import org.slf4j.LoggerFactory;
/**
@ -52,7 +53,7 @@ public class TestDataCatalogueLib {
*
* @throws Exception the exception
*/
//@Before
// @Before
public void before() throws Exception {
factory = DataCatalogueFactory.getFactory();
}
@ -360,40 +361,5 @@ public class TestDataCatalogueLib {
}
//@Test
public void performQuery() throws Exception {
DataCatalogueImpl instance = factory.getUtilsPerScope(scope);
List<String> emails2 = new ArrayList<String>(2);
//emails2.add("luca.frosini@isti.cnr.it");
//emails2.add("francesco.mangiacrapa@cnr.it");
List<String> emails = new ArrayList<String>(2);
StringBuilder queryMails = new StringBuilder();
queryMails.append("(");
for (String email : emails2) {
emails.add(email);
queryMails.append(" '" + email + "'");
}
queryMails.append(")");
// queryParams.put(Moderated.SYSTEM_CM_ITEM_STATUS, status.getValue());
String system_cm_prefix = GCatCaller.MODERATOR_ITEM_STATUS_FIELD_NAME;
StringBuilder queryValuesBuilder = new StringBuilder();
queryValuesBuilder.append("author_email:" + queryMails.toString());
queryValuesBuilder.append(" AND ");
queryValuesBuilder.append("extras_" + system_cm_prefix + ":*");
Map<String, String> query = new HashMap<String, String>(1);
query.put("q", queryValuesBuilder.toString());
ScopeProvider.instance.set(scope);
SecurityTokenProvider.instance.set(authorizationToken);
String listItems = instance.performQuery(query);
System.out.println("item returned: " + listItems);
}
}