Added WhereClause into SearchingFilter

This commit is contained in:
Francesco Mangiacrapa 2021-12-15 15:42:39 +01:00
parent cca77a169f
commit 07bf557525
4 changed files with 206 additions and 55 deletions

View File

@ -18,7 +18,9 @@ import org.gcube.application.geoportal.common.rest.MongoConcessioni;
import org.gcube.application.geoportalcommon.shared.ItemField;
import org.gcube.application.geoportalcommon.shared.ResultSetPaginatedData;
import org.gcube.application.geoportalcommon.shared.SearchingFilter;
import org.gcube.application.geoportalcommon.shared.SearchingFilter.LOGICAL_OP;
import org.gcube.application.geoportalcommon.shared.SearchingFilter.ORDER;
import org.gcube.application.geoportalcommon.shared.WhereClause;
import org.gcube.application.geoportalcommon.shared.products.ConcessioneDV;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -168,38 +170,55 @@ public class MongoServiceCommon {
ordering.setFields(orderingFields);
request.setOrdering(ordering);
Document query = null;
if (filter.getSearchInto() != null) {
Map<String, Object> searchFields = filter.getSearchInto();
BasicDBObjectBuilder builder = BasicDBObjectBuilder.start();
for (String key : searchFields.keySet()) {
// using regex and case-insensitive
BasicDBObject bs = new BasicDBObject();
bs.append("$regex", searchFields.get(key));
bs.append("$options", "i");
builder.append(key, bs);
Document query = new Document();
if(filter.getConditions()!=null) {
for (WhereClause whereClause : filter.getConditions()) {
LOGICAL_OP searchWithOperator = whereClause.getOperator();
if(searchWithOperator==null) {
searchWithOperator = LOGICAL_OP.OR;
}
if (whereClause.getSearchInto() != null) {
Map<String, Object> searchFields = whereClause.getSearchInto();
BasicDBObjectBuilder builder = BasicDBObjectBuilder.start();
for (String key : searchFields.keySet()) {
// using regex and case-insensitive
BasicDBObject bs = new BasicDBObject();
bs.append("$regex", searchFields.get(key));
bs.append("$options", "i");
builder.append(key, bs);
}
//Building list of Document in OR clause
BasicDBList list = new BasicDBList();
Map map = builder.get().toMap();
for (Object key : map.keySet()) {
BasicDBObject value = (BasicDBObject) map.get(key);
Document doc = new Document((String) key, value);
list.add(doc);
}
//query = new Document();
query.put(searchWithOperator.getOperator(), list);
// BasicDBObject bs = new BasicDBObject();
// bs.append("$eq", "PASSED");
// query.put("report.status", bs);
}
}
//Building list of Document in OR clause
BasicDBList list = new BasicDBList();
Map map = builder.get().toMap();
for (Object key : map.keySet()) {
BasicDBObject value = (BasicDBObject) map.get(key);
Document doc = new Document((String) key, value);
list.add(doc);
}
query = new Document();
query.put("$or", list);
request.setFilter(query);
}
request.setFilter(query);
LOG.info("Paging offset: " + offsetIndex + ", limit: " + limitIndex);
LOG.info("Direction: " + sDirection);
LOG.info("Order by Fields: " + orderingFields);
LOG.info("Search for: " + filter.getSearchInto());
LOG.info("Search for conditions: " + filter.getConditions());
if (query != null) {
LOG.info("Search query to JSON: " + query.toJson());
}
@ -211,7 +230,7 @@ public class MongoServiceCommon {
ConcessioneDV concessioneDV = ConvertToDataViewModel.toMetadataConcessione(concessione, true);
toReturnList.add(concessioneDV);
i++;
LOG.trace("converted: " + concessioneDV);
LOG.trace(i+") converted: " + concessioneDV);
}
LOG.debug("read " + toReturnList + " project/s");
@ -219,10 +238,11 @@ public class MongoServiceCommon {
// TODO WORKAROUND MUST BE REMOVE AFTER THE QUERY COUNT
// AND LIST.SIZE WILL BE AVAILABLE IN THE SERVICE
if (filter.getSearchInto() != null) {
if (filter.getConditions() != null) {
searchedData.setTotalItems(toReturnList.size());
totalItems = toReturnList.size();
}
if (totalItems == limit || totalItems == 0) {
LOG.debug("Page completed returning " + totalItems + " items");
int newOffset = offsetIndex + limitIndex;

View File

@ -2,7 +2,6 @@ package org.gcube.application.geoportalcommon.shared;
import java.io.Serializable;
import java.util.List;
import java.util.Map;
/**
* The Class SearchingFilter.
@ -44,6 +43,37 @@ public class SearchingFilter implements Serializable {
}
}
/**
* The Enum LOGICAL_OP.
*
* @author Francesco Mangiacrapa at ISTI-CNR francesco.mangiacrapa@isti.cnr.it
*
* Dec 15, 2021
*/
public static enum LOGICAL_OP {
AND("$and"), OR("$or");
String operator;
/**
* Instantiates a new order.
*
* @param operator the operator
*/
LOGICAL_OP(String operator) {
this.operator = operator;
}
/**
* Gets the operator.
*
* @return the operator
*/
public String getOperator() {
return operator;
}
}
/**
*
*/
@ -51,9 +81,9 @@ public class SearchingFilter implements Serializable {
private List<ItemField> orderByFields;
private ORDER order;
private ORDER order = ORDER.ASC;
private Map<String, Object> searchInto;
private List<WhereClause> conditions;
/**
* Instantiates a new sort filter.
@ -80,19 +110,18 @@ public class SearchingFilter implements Serializable {
* @param order the order
* @param searchInto the search into
*/
public SearchingFilter(List<ItemField> orderByFields, ORDER order, Map<String, Object> searchInto) {
public SearchingFilter(List<ItemField> orderByFields, ORDER order, List<WhereClause> conditions) {
this.orderByFields = orderByFields;
this.order = order;
this.searchInto = searchInto;
this.conditions = conditions;
}
/**
* Sets the search into.
*
* @param searchInto the search into
*/
public void setSearchInto(Map<String, Object> searchInto) {
this.searchInto = searchInto;
public List<WhereClause> getConditions() {
return conditions;
}
public void setConditions(List<WhereClause> conditions) {
this.conditions = conditions;
}
/**
@ -131,15 +160,6 @@ public class SearchingFilter implements Serializable {
this.order = order;
}
/**
* Gets the search into.
*
* @return the search into
*/
public Map<String, Object> getSearchInto() {
return searchInto;
}
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
@ -147,8 +167,8 @@ public class SearchingFilter implements Serializable {
builder.append(orderByFields);
builder.append(", order=");
builder.append(order);
builder.append(", searchInto=");
builder.append(searchInto);
builder.append(", conditions=");
builder.append(conditions);
builder.append("]");
return builder.toString();
}

View File

@ -0,0 +1,97 @@
package org.gcube.application.geoportalcommon.shared;
import java.io.Serializable;
import java.util.Map;
import org.gcube.application.geoportalcommon.shared.SearchingFilter.LOGICAL_OP;
/**
* The Class WhereClause.
*
* @author Francesco Mangiacrapa at ISTI-CNR francesco.mangiacrapa@isti.cnr.it
*
* Dec 15, 2021
*/
public class WhereClause implements Serializable {
/**
*
*/
private static final long serialVersionUID = -8577885822669878296L;
private LOGICAL_OP operator = LOGICAL_OP.OR;
private Map<String, Object> searchInto;
/**
* Instantiates a new where clause.
*/
public WhereClause() {
}
/**
* Instantiates a new where clause.
*
* @param operator the operator
* @param searchInto the search into
*/
public WhereClause(LOGICAL_OP operator, Map<String, Object> searchInto) {
super();
this.operator = operator;
this.searchInto = searchInto;
}
/**
* Gets the operator.
*
* @return the operator
*/
public LOGICAL_OP getOperator() {
return operator;
}
/**
* Gets the search into.
*
* @return the search into
*/
public Map<String, Object> getSearchInto() {
return searchInto;
}
/**
* Sets the operator.
*
* @param operator the new operator
*/
public void setOperator(LOGICAL_OP operator) {
this.operator = operator;
}
/**
* Sets the search into.
*
* @param searchInto the search into
*/
public void setSearchInto(Map<String, Object> searchInto) {
this.searchInto = searchInto;
}
/**
* To string.
*
* @return the string
*/
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
builder.append("WhereClause [operator=");
builder.append(operator);
builder.append(", searchInto=");
builder.append(searchInto);
builder.append("]");
return builder.toString();
}
}

View File

@ -1,5 +1,6 @@
package org.gcube.application;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
@ -10,6 +11,8 @@ import org.gcube.application.geoportalcommon.shared.GeoNaDataViewerProfile;
import org.gcube.application.geoportalcommon.shared.GeoNaItemRef;
import org.gcube.application.geoportalcommon.shared.ResultSetPaginatedData;
import org.gcube.application.geoportalcommon.shared.SearchingFilter;
import org.gcube.application.geoportalcommon.shared.WhereClause;
import org.gcube.application.geoportalcommon.shared.SearchingFilter.LOGICAL_OP;
import org.gcube.application.geoportalcommon.shared.products.ConcessioneDV;
import org.gcube.common.authorization.library.provider.SecurityTokenProvider;
import org.gcube.common.scope.api.ScopeProvider;
@ -59,9 +62,20 @@ public class TestGNACommon {
SearchingFilter filter = new SearchingFilter();
Map<String, Object> searchInto = new HashMap<String, Object>();
//searchInto.put("nome", "mock");
searchInto.put("authors", "annalisa");
filter.setSearchInto(searchInto);
searchInto.put("nome", "san");
searchInto.put("authors", "silvia");
//searchInto.put("report.status", "PASSED");
WhereClause where1 = new WhereClause(LOGICAL_OP.OR, searchInto);
Map<String, Object> searchInto2 = new HashMap<String, Object>();
searchInto2.put("report.status", "PASSED");
WhereClause where2 = new WhereClause(LOGICAL_OP.AND, searchInto2);
ArrayList<WhereClause> list = new ArrayList<WhereClause>();
list.add(where1);
//list.add(where2);
filter.setConditions(list);
ResultSetPaginatedData result = msc.queryOnMongo(30, 0, 30, filter, "concessione");
int i = 0;