geoportal-data-common/src/main/java/org/gcube/application/geoportalcommon/geoportal/UseCaseDescriptorCaller.java

121 lines
4.0 KiB
Java
Raw Normal View History

2022-03-11 12:01:27 +01:00
package org.gcube.application.geoportalcommon.geoportal;
import static org.gcube.application.geoportal.client.plugins.GeoportalAbstractPlugin.useCaseDescriptors;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.bson.Document;
import org.gcube.application.geoportal.common.model.rest.QueryRequest;
import org.gcube.application.geoportal.common.model.useCaseDescriptor.UseCaseDescriptor;
import org.gcube.application.geoportal.common.rest.UseCaseDescriptorsI;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
2022-03-16 18:10:17 +01:00
import com.mongodb.BasicDBList;
2022-03-11 12:01:27 +01:00
import com.mongodb.BasicDBObject;
public class UseCaseDescriptorCaller {
private static Logger LOG = LoggerFactory.getLogger(UseCaseDescriptorCaller.class);
public UseCaseDescriptorsI useCaseDescriptorsClient() {
LOG.info("useCaseDescriptorsClient called");
return useCaseDescriptors().build();
}
public List<UseCaseDescriptor> getList() throws Exception {
2022-03-16 18:10:17 +01:00
LOG.info("getList called");
2022-03-11 12:01:27 +01:00
UseCaseDescriptorsI client = useCaseDescriptorsClient();
List<UseCaseDescriptor> listUCD = new ArrayList<UseCaseDescriptor>();
Iterator<UseCaseDescriptor> useCaseDescrs = client.query(new QueryRequest());
for (Iterator<UseCaseDescriptor> iterator = useCaseDescrs; useCaseDescrs.hasNext();) {
UseCaseDescriptor prg = (UseCaseDescriptor) iterator.next();
listUCD.add(prg);
}
2022-08-09 16:36:49 +02:00
2022-03-17 16:03:32 +01:00
LOG.info("returning {} {}", listUCD.size(), UseCaseDescriptor.class.getName());
2022-03-11 12:01:27 +01:00
return listUCD;
}
2022-03-16 18:10:17 +01:00
public List<UseCaseDescriptor> getListForHandlerIds(List<String> listHandlersIds) throws Exception {
2022-03-17 16:03:32 +01:00
LOG.info("getListForHandlerIds called");
2022-03-16 18:10:17 +01:00
return getListForJSONPath("_handlers._id", listHandlersIds);
}
public List<UseCaseDescriptor> getListForJSONPath(String jsonPath, List<String> listValues) throws Exception {
LOG.info("getListForJSONPath called for jsonPath: {}, with listValues: {}", jsonPath, listValues);
2022-03-11 12:01:27 +01:00
UseCaseDescriptorsI client = useCaseDescriptorsClient();
2022-03-16 18:10:17 +01:00
List<UseCaseDescriptor> listUCD = new ArrayList<UseCaseDescriptor>();
// BasicDBObjectBuilder builder = BasicDBObjectBuilder.start();
// for (String key : listHandlersIds) {
// // using regex and case-insensitive
// BasicDBObject bs = new BasicDBObject();
// bs.append(jsonPath, key);
// builder.append(key, bs);
//
// }
// Building list of Document in OR clause
BasicDBList list = new BasicDBList();
for (String value : listValues) {
Document doc = new Document(jsonPath, value);
list.add(doc);
}
QueryRequest queryRequest = new QueryRequest();
Document queryDoc = new Document();
2022-03-17 16:03:32 +01:00
queryDoc.put("$and", list);
2022-03-16 18:10:17 +01:00
LOG.debug("Performing query: {}", queryDoc.toJson());
queryRequest.setFilter(queryDoc);
// QueryRequest queryRequest = new QueryRequest();
// Document queryDoc = new Document();
// BasicDBObject bs = new BasicDBObject();
// bs.append("$eq", hasValue);
// queryDoc.put(jsonPath, bs);
// LOG.debug("Performing query: {}", queryDoc.toJson());
// queryRequest.setFilter(queryDoc);
2022-03-21 10:45:43 +01:00
Iterator<UseCaseDescriptor> useCaseDescrsIt = client.query(queryRequest);
if(useCaseDescrsIt!=null) {
while(useCaseDescrsIt.hasNext()) {
UseCaseDescriptor prg = (UseCaseDescriptor) useCaseDescrsIt.next();
listUCD.add(prg);
}
2022-03-16 18:10:17 +01:00
}
2022-03-21 10:45:43 +01:00
LOG.info("getListForJSONPath returning {} {}", listUCD.size(), UseCaseDescriptor.class.getName());
2022-03-16 18:10:17 +01:00
return listUCD;
}
public UseCaseDescriptor getUCDForId(String profileID) throws Exception {
LOG.info("getUCDForId called for profileID: {}", profileID);
UseCaseDescriptorsI client = useCaseDescriptorsClient();
2022-03-11 12:01:27 +01:00
QueryRequest queryRequest = new QueryRequest();
Document queryDoc = new Document();
BasicDBObject bs = new BasicDBObject();
bs.append("$eq", profileID);
queryDoc.put("_id", bs);
LOG.debug("Performing query: {}", queryDoc.toJson());
queryRequest.setFilter(queryDoc);
2022-03-16 18:10:17 +01:00
2022-03-11 12:01:27 +01:00
Iterator<UseCaseDescriptor> useCaseDescrs = client.query(queryRequest);
UseCaseDescriptor ucd = null;
if (useCaseDescrs.hasNext()) {
ucd = useCaseDescrs.next();
}
LOG.info("for profileID: {}, returning: {}", profileID, ucd);
return ucd;
}
}