[Major] Now using a nice query parser for resource querying in planning
This commit is contained in:
parent
5f71427d33
commit
8a5c4468c5
|
@ -15,12 +15,12 @@
|
|||
*/
|
||||
package li.strolch.model.query;
|
||||
|
||||
import ch.eitchnet.utils.dbc.DBC;
|
||||
import li.strolch.model.ActivityVisitor;
|
||||
import li.strolch.model.activity.Action;
|
||||
import li.strolch.model.activity.Activity;
|
||||
import li.strolch.model.query.ordering.StrolchQueryOrdering;
|
||||
import li.strolch.model.visitor.NoStrategyActivityVisitor;
|
||||
import ch.eitchnet.utils.dbc.DBC;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
|
@ -48,6 +48,10 @@ public class ActivityQuery<U> extends StrolchElementQuery<ActivityQueryVisitor>
|
|||
protected ActivityVisitor<U> activityVisitor;
|
||||
protected StrolchQueryOrdering ordering;
|
||||
|
||||
public ActivityQuery() {
|
||||
super();
|
||||
}
|
||||
|
||||
public ActivityQuery(Navigation navigation) {
|
||||
super(navigation);
|
||||
}
|
||||
|
@ -102,8 +106,8 @@ public class ActivityQuery<U> extends StrolchElementQuery<ActivityQueryVisitor>
|
|||
}
|
||||
|
||||
public static ActivityQuery<Activity> query(String type, StrolchQueryOrdering ordering) {
|
||||
return new ActivityQuery<Activity>(new StrolchTypeNavigation(type)).setActivityVisitor(
|
||||
new NoStrategyActivityVisitor()).setOrdering(ordering);
|
||||
return new ActivityQuery<Activity>(new StrolchTypeNavigation(type))
|
||||
.setActivityVisitor(new NoStrategyActivityVisitor()).setOrdering(ordering);
|
||||
}
|
||||
|
||||
public static <U> ActivityQuery<U> query(String type, ActivityVisitor<U> activityVisitor) {
|
||||
|
@ -112,7 +116,7 @@ public class ActivityQuery<U> extends StrolchElementQuery<ActivityQueryVisitor>
|
|||
|
||||
public static <U> ActivityQuery<U> query(String type, ActivityVisitor<U> activityVisitor,
|
||||
StrolchQueryOrdering ordering) {
|
||||
return new ActivityQuery<U>(new StrolchTypeNavigation(type)).setActivityVisitor(activityVisitor).setOrdering(
|
||||
ordering);
|
||||
return new ActivityQuery<U>(new StrolchTypeNavigation(type)).setActivityVisitor(activityVisitor)
|
||||
.setOrdering(ordering);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -15,12 +15,12 @@
|
|||
*/
|
||||
package li.strolch.model.query;
|
||||
|
||||
import ch.eitchnet.utils.dbc.DBC;
|
||||
import li.strolch.model.Order;
|
||||
import li.strolch.model.OrderVisitor;
|
||||
import li.strolch.model.parameter.Parameter;
|
||||
import li.strolch.model.query.ordering.StrolchQueryOrdering;
|
||||
import li.strolch.model.visitor.NoStrategyOrderVisitor;
|
||||
import ch.eitchnet.utils.dbc.DBC;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
|
@ -48,6 +48,10 @@ public class OrderQuery<U> extends StrolchElementQuery<OrderQueryVisitor> {
|
|||
protected OrderVisitor<U> orderVisitor;
|
||||
protected StrolchQueryOrdering ordering;
|
||||
|
||||
public OrderQuery() {
|
||||
super();
|
||||
}
|
||||
|
||||
public OrderQuery(Navigation navigation) {
|
||||
super(navigation);
|
||||
}
|
||||
|
|
|
@ -15,12 +15,12 @@
|
|||
*/
|
||||
package li.strolch.model.query;
|
||||
|
||||
import ch.eitchnet.utils.dbc.DBC;
|
||||
import li.strolch.model.Resource;
|
||||
import li.strolch.model.ResourceVisitor;
|
||||
import li.strolch.model.parameter.Parameter;
|
||||
import li.strolch.model.query.ordering.StrolchQueryOrdering;
|
||||
import li.strolch.model.visitor.NoStrategyResourceVisitor;
|
||||
import ch.eitchnet.utils.dbc.DBC;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
|
@ -48,6 +48,10 @@ public class ResourceQuery<U> extends StrolchElementQuery<ResourceQueryVisitor>
|
|||
protected ResourceVisitor<U> resourceVisitor;
|
||||
protected StrolchQueryOrdering ordering;
|
||||
|
||||
public ResourceQuery() {
|
||||
super();
|
||||
}
|
||||
|
||||
public ResourceQuery(Navigation navigation) {
|
||||
super(navigation);
|
||||
}
|
||||
|
@ -103,8 +107,8 @@ public class ResourceQuery<U> extends StrolchElementQuery<ResourceQueryVisitor>
|
|||
}
|
||||
|
||||
public static ResourceQuery<Resource> query(String type, StrolchQueryOrdering ordering) {
|
||||
return new ResourceQuery<Resource>(new StrolchTypeNavigation(type)).setResourceVisitor(
|
||||
new NoStrategyResourceVisitor()).setOrdering(ordering);
|
||||
return new ResourceQuery<Resource>(new StrolchTypeNavigation(type))
|
||||
.setResourceVisitor(new NoStrategyResourceVisitor()).setOrdering(ordering);
|
||||
}
|
||||
|
||||
public static <U> ResourceQuery<U> query(String type, ResourceVisitor<U> resourceVisitor) {
|
||||
|
@ -113,7 +117,7 @@ public class ResourceQuery<U> extends StrolchElementQuery<ResourceQueryVisitor>
|
|||
|
||||
public static <U> ResourceQuery<U> query(String type, ResourceVisitor<U> resourceVisitor,
|
||||
StrolchQueryOrdering ordering) {
|
||||
return new ResourceQuery<U>(new StrolchTypeNavigation(type)).setResourceVisitor(resourceVisitor).setOrdering(
|
||||
ordering);
|
||||
return new ResourceQuery<U>(new StrolchTypeNavigation(type)).setResourceVisitor(resourceVisitor)
|
||||
.setOrdering(ordering);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -25,14 +25,26 @@ public abstract class StrolchElementQuery<T extends QueryVisitor> implements Str
|
|||
protected Navigation navigation;
|
||||
protected Selection selection;
|
||||
|
||||
public StrolchElementQuery() {
|
||||
// empty
|
||||
}
|
||||
|
||||
public StrolchElementQuery(Navigation navigation) {
|
||||
this.navigation = navigation;
|
||||
}
|
||||
|
||||
public void setNavigation(Navigation navigation) {
|
||||
this.navigation = navigation;
|
||||
}
|
||||
|
||||
public boolean hasNavigation() {
|
||||
return this.navigation != null;
|
||||
}
|
||||
|
||||
public Navigation getNavigation() {
|
||||
return this.navigation;
|
||||
}
|
||||
|
||||
public boolean hasSelection() {
|
||||
return this.selection != null && this.selection.hasSelection();
|
||||
}
|
||||
|
|
|
@ -32,6 +32,9 @@ import javax.xml.parsers.SAXParser;
|
|||
import javax.xml.parsers.SAXParserFactory;
|
||||
import javax.xml.transform.sax.SAXResult;
|
||||
|
||||
import org.xml.sax.ContentHandler;
|
||||
import org.xml.sax.SAXException;
|
||||
|
||||
import li.strolch.model.Order;
|
||||
import li.strolch.model.Tags;
|
||||
import li.strolch.model.query.OrderQuery;
|
||||
|
@ -41,9 +44,6 @@ import li.strolch.model.xml.XmlModelSaxReader;
|
|||
import li.strolch.persistence.api.OrderDao;
|
||||
import li.strolch.persistence.api.StrolchPersistenceException;
|
||||
|
||||
import org.xml.sax.ContentHandler;
|
||||
import org.xml.sax.SAXException;
|
||||
|
||||
@SuppressWarnings("nls")
|
||||
public class PostgreSqlOrderDao extends PostgresqlDao<Order> implements OrderDao {
|
||||
|
||||
|
|
|
@ -5,7 +5,8 @@
|
|||
/*
|
||||
* Import different parts
|
||||
*/
|
||||
@import url("utils.css");
|
||||
@import url("loader.css");
|
||||
@import url("parts/main.css");
|
||||
@import url("parts/nav.css");
|
||||
@import url('utils.css');
|
||||
@import url('loader.css');
|
||||
@import url('bootstrap-override.css');
|
||||
@import url('parts/main.css');
|
||||
@import url('parts/nav.css');
|
||||
|
|
|
@ -0,0 +1,4 @@
|
|||
|
||||
.tab-pane {
|
||||
margin: 10px;
|
||||
}
|
|
@ -283,22 +283,26 @@ strolch.fn.initDataTable = function (tableId, columns, url, queryData) {
|
|||
columns: columns,
|
||||
lengthMenu: [[5, 10, 20, -1], [5, 10, 20, 'All']],
|
||||
processing: true,
|
||||
searching: false,
|
||||
serverSide: true,
|
||||
ajax: function (data, callback, settings) {
|
||||
|
||||
queryData.draw = data.draw;
|
||||
queryData.pageSize = data.length;
|
||||
queryData.page = data.start / data.length + 1;
|
||||
queryData.query = data.search.value;
|
||||
queryData.sortBy = data.columns[data.order[0].column].data;
|
||||
queryData.ascending = data.order[0].dir == 'asc';
|
||||
var payload = {
|
||||
realmName: queryData.realmName,
|
||||
draw: data.draw,
|
||||
pageSize: data.length,
|
||||
page: data.start / data.length + 1,
|
||||
query: $('#' + queryData.queryFieldId).val(),
|
||||
orderBy: data.columns[data.order[0].column].data,
|
||||
ascending: data.order[0].dir == 'asc'
|
||||
};
|
||||
|
||||
console.log('Searching for ' + queryData.query);
|
||||
console.log('Searching for ' + payload.query);
|
||||
|
||||
$.ajax({
|
||||
dataType: 'json',
|
||||
url: url,
|
||||
data: queryData,
|
||||
data: payload,
|
||||
success: function (data) {
|
||||
data.recordsTotal = data.dataSetSize;
|
||||
data.recordsFiltered = data.nrOfElements;
|
||||
|
|
|
@ -38,7 +38,6 @@ strolch.parts.model.hide = function () {
|
|||
strolch.parts.model.prepareResourceTable = function () {
|
||||
|
||||
// query => the search criteria
|
||||
// queryBy => the fields to use in searching, default is null/all
|
||||
// sortBy => a single column to sort by
|
||||
// ascending => true|false
|
||||
// pageSize => integer, max number of elements to return per page,
|
||||
|
@ -50,10 +49,9 @@ strolch.parts.model.prepareResourceTable = function () {
|
|||
pageSize: 1,
|
||||
page: 0,
|
||||
query: '',
|
||||
queryBy: 'Id, Name',
|
||||
types: '',
|
||||
orderBy: '',
|
||||
ascending: true
|
||||
ascending: true,
|
||||
queryFieldId: 'modelResourceTableSearch'
|
||||
};
|
||||
|
||||
var url = strolch.fn.url(strolch.const.urls.resources);
|
||||
|
|
|
@ -1,28 +1,50 @@
|
|||
<div id="part-model" class="hidden">
|
||||
<div id="part-model" class="container-fluid hidden">
|
||||
<div class="row">
|
||||
<div class="col-sm-offset-2 col-sm-8">
|
||||
|
||||
<!-- Nav tabs -->
|
||||
<ul class="nav nav-tabs" role="tablist">
|
||||
<li class="nav-item"><a class="nav-link" data-toggle="tab" href="#resources" role="tab">Resources</a></li>
|
||||
<li class="nav-item"><a class="nav-link" data-toggle="tab" href="#orders" role="tab">Orders</a></li>
|
||||
</ul>
|
||||
<!-- Nav tabs -->
|
||||
<ul class="nav nav-tabs" role="tablist">
|
||||
<li class="nav-item"><a class="nav-link" data-toggle="tab" href="#resources" role="tab">Resources</a>
|
||||
</li>
|
||||
<li class="nav-item"><a class="nav-link" data-toggle="tab" href="#orders" role="tab">Orders</a></li>
|
||||
</ul>
|
||||
|
||||
<!-- Tab panes -->
|
||||
<div class="tab-content">
|
||||
<div class="tab-pane active" id="resources" role="tabpanel">
|
||||
<table id="modelResourceTable" class="table table-striped table-bordered" cellspacing="0" width="100%">
|
||||
<!-- Tab panes -->
|
||||
<div class="tab-content">
|
||||
<div class="tab-pane active" id="resources" role="tabpanel">
|
||||
<div class="row">
|
||||
<div class="col-sm-12">
|
||||
<form>
|
||||
<div class="form-group row">
|
||||
<div class="col-sm-offset-2 col-sm-8">
|
||||
<input type="text" class="form-control" id="modelResourceTableSearch"
|
||||
placeholder="[id:<value>] [name:<value>] [type:<value>] [param:<bagId>:<paramId>] [value]">
|
||||
</div>
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
<div class="row">
|
||||
<div class="col-sm-12">
|
||||
<table id="modelResourceTable" class="table table-striped table-bordered hover" cellspacing="0"
|
||||
width="100%">
|
||||
|
||||
<caption>The resources for the current user's realm</caption>
|
||||
<caption>The resources for the current user's realm</caption>
|
||||
|
||||
</table>
|
||||
</table>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="tab-pane" id="orders" role="tabpanel">
|
||||
<div class="row">
|
||||
<table id="modelOrderTable" class="table table-striped">
|
||||
|
||||
</div>
|
||||
<div class="tab-pane" id="orders" role="tabpanel">
|
||||
<table id="modelOrderTable" class="table table-striped">
|
||||
<caption>The orders for the current user's realm</caption>
|
||||
|
||||
<caption>The orders for the current user's realm</caption>
|
||||
|
||||
</table>
|
||||
</table>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
</div>
|
|
@ -13,48 +13,50 @@ import javax.ws.rs.core.Context;
|
|||
import javax.ws.rs.core.MediaType;
|
||||
import javax.ws.rs.core.Response;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.GsonBuilder;
|
||||
import com.google.gson.JsonArray;
|
||||
import com.google.gson.JsonObject;
|
||||
|
||||
import ch.eitchnet.privilege.model.Certificate;
|
||||
import ch.eitchnet.utils.StringMatchMode;
|
||||
import ch.eitchnet.utils.collections.Paging;
|
||||
import ch.eitchnet.utils.helper.StringHelper;
|
||||
import li.strolch.agent.api.OrderMap;
|
||||
import li.strolch.agent.api.ResourceMap;
|
||||
import li.strolch.model.Order;
|
||||
import li.strolch.model.Resource;
|
||||
import li.strolch.model.StrolchRootElement;
|
||||
import li.strolch.model.Tags;
|
||||
import li.strolch.model.json.OrderToJsonVisitor;
|
||||
import li.strolch.model.json.ResourceToJsonVisitor;
|
||||
import li.strolch.model.parameter.Parameter;
|
||||
import li.strolch.model.query.IdSelection;
|
||||
import li.strolch.model.query.NameSelection;
|
||||
import li.strolch.model.query.OrSelection;
|
||||
import li.strolch.model.query.ParameterSelection;
|
||||
import li.strolch.model.query.OrderQuery;
|
||||
import li.strolch.model.query.ResourceQuery;
|
||||
import li.strolch.model.query.StrolchTypeNavigation;
|
||||
import li.strolch.model.query.parser.QueryParser;
|
||||
import li.strolch.model.visitor.StrolchElementVisitor;
|
||||
import li.strolch.persistence.api.StrolchTransaction;
|
||||
import li.strolch.rest.RestfulStrolchComponent;
|
||||
import li.strolch.rest.StrolchRestfulConstants;
|
||||
import li.strolch.rest.model.StrolchElementOverview;
|
||||
import li.strolch.rest.model.TypeDetail;
|
||||
import li.strolch.runtime.StrolchConstants;
|
||||
|
||||
@Path("strolch/model")
|
||||
public class ModelQuery {
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(ModelQuery.class);
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* Query Resources
|
||||
* </p>
|
||||
* Query {@link Resource Resources} by parsing the query string in {@link QueryData#getQuery()} using
|
||||
* {@link QueryParser}
|
||||
*
|
||||
* @param realm
|
||||
* the realm for which the resource type overview is to be returned
|
||||
* @param type
|
||||
* @param queryData
|
||||
* the data from the client
|
||||
* @param request
|
||||
* the {@link HttpServletRequest} on which to get the {@link Certificate}
|
||||
*
|
||||
* @return an overview of the {@link Resource Resources} with the given type. This is a list of overviews of the
|
||||
* resources
|
||||
*
|
||||
* @see TypeDetail
|
||||
* @see StrolchElementOverview
|
||||
* @return {@link Response} containing the JSONified {@link Resource Resources} queried
|
||||
*/
|
||||
@GET
|
||||
@Produces(MediaType.APPLICATION_JSON)
|
||||
|
@ -62,122 +64,219 @@ public class ModelQuery {
|
|||
public Response queryResources(@BeanParam QueryData queryData, @Context HttpServletRequest request) {
|
||||
Certificate cert = (Certificate) request.getAttribute(StrolchRestfulConstants.STROLCH_CERTIFICATE);
|
||||
|
||||
String realmName = queryData.getRealmName();
|
||||
if (StringHelper.isEmpty(realmName))
|
||||
realmName = StrolchConstants.DEFAULT_REALM;
|
||||
// see if a special realm was requested
|
||||
String realmName = getRealmName(queryData);
|
||||
|
||||
List<Resource> resources = new ArrayList<>();
|
||||
|
||||
Set<String> queryBy = queryData.getQueryByNames();
|
||||
Set<String> queryTypes = queryData.getTypesAsSet();
|
||||
// parse the query string
|
||||
ResourceQuery<Resource> query = QueryParser.parseToResourceQuery(queryData.getQuery(), true);
|
||||
|
||||
// query the data
|
||||
long dataSetSize = 0L;
|
||||
try (StrolchTransaction tx = openTx(cert, realmName)) {
|
||||
ResourceMap resourceMap = tx.getResourceMap();
|
||||
|
||||
if (queryTypes.isEmpty()) {
|
||||
if (query.hasNavigation()) {
|
||||
String type = ((StrolchTypeNavigation) query.getNavigation()).getType();
|
||||
dataSetSize = resourceMap.querySize(tx, type);
|
||||
resources.addAll(tx.doQuery(query));
|
||||
} else {
|
||||
Set<String> types = resourceMap.getTypes(tx);
|
||||
dataSetSize = resourceMap.querySize(tx);
|
||||
for (String type : types) {
|
||||
query(queryData, resources, queryBy, tx, type);
|
||||
}
|
||||
} else {
|
||||
for (String type : queryTypes) {
|
||||
dataSetSize = resourceMap.querySize(tx, type);
|
||||
query(queryData, resources, queryBy, tx, type);
|
||||
query.setNavigation(new StrolchTypeNavigation(type));
|
||||
resources.addAll(tx.doQuery(query));
|
||||
}
|
||||
}
|
||||
|
||||
tx.doNothingOnClose();
|
||||
}
|
||||
|
||||
String orderBy = queryData.getOrderBy();
|
||||
boolean asc = queryData.isAscending();
|
||||
if (StringHelper.isNotEmpty(orderBy)) {
|
||||
if (orderBy.equals(QueryData.ID)) {
|
||||
resources.sort((r1, r2) -> asc ? r1.getId().compareTo(r2.getId()) : r2.getId().compareTo(r1.getId()));
|
||||
} else if (orderBy.equals(QueryData.NAME)) {
|
||||
resources.sort(
|
||||
(r1, r2) -> asc ? r1.getName().compareTo(r2.getName()) : r2.getName().compareTo(r1.getName()));
|
||||
} else if (orderBy.equals(QueryData.TYPE)) {
|
||||
resources.sort(
|
||||
(r1, r2) -> asc ? r1.getType().compareTo(r2.getType()) : r2.getType().compareTo(r1.getType()));
|
||||
} else {
|
||||
String[] parts = orderBy.split(":");
|
||||
String bagKey = parts[0];
|
||||
String paramKey = parts[1];
|
||||
resources.sort((r1, r2) -> {
|
||||
Parameter<?> param1 = r1.getParameter(bagKey, paramKey);
|
||||
Parameter<?> param2 = r2.getParameter(bagKey, paramKey);
|
||||
if (asc)
|
||||
return param1.getValueAsString().compareTo(param2.getValueAsString());
|
||||
return param2.getValueAsString().compareTo(param1.getValueAsString());
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
// paging
|
||||
Paging<Resource> paging = Paging.asPage(resources, queryData.getPageSize(), queryData.getPage());
|
||||
|
||||
// get page
|
||||
List<Resource> page = paging.getPage();
|
||||
// do ordering
|
||||
doOrdering(queryData, resources);
|
||||
|
||||
// build JSON response
|
||||
ResourceToJsonVisitor toJsonVisitor = new ResourceToJsonVisitor();
|
||||
JsonObject root = marshall(queryData, dataSetSize, resources, toJsonVisitor);
|
||||
|
||||
// marshall result
|
||||
Gson gson = new GsonBuilder().setPrettyPrinting().create();
|
||||
String entity = gson.toJson(root);
|
||||
return Response.ok(entity).build();
|
||||
}
|
||||
|
||||
/**
|
||||
* Query {@link Order Orders} by parsing the query string in {@link QueryData#getQuery()} using {@link QueryParser}
|
||||
*
|
||||
* @param queryData
|
||||
* the data from the client
|
||||
* @param request
|
||||
* the {@link HttpServletRequest} on which to get the {@link Certificate}
|
||||
*
|
||||
* @return {@link Response} containing the JSONified {@link Order Orders} queried
|
||||
*/
|
||||
@GET
|
||||
@Produces(MediaType.APPLICATION_JSON)
|
||||
@Path("orders")
|
||||
public Response queryOrders(@BeanParam QueryData queryData, @Context HttpServletRequest request) {
|
||||
Certificate cert = (Certificate) request.getAttribute(StrolchRestfulConstants.STROLCH_CERTIFICATE);
|
||||
|
||||
// see if a special realm was requested
|
||||
String realmName = getRealmName(queryData);
|
||||
|
||||
List<Order> orders = new ArrayList<>();
|
||||
|
||||
// parse the query string
|
||||
OrderQuery<Order> query = QueryParser.parseToOrderQuery(queryData.getQuery(), true);
|
||||
|
||||
// query the data
|
||||
long dataSetSize = 0L;
|
||||
try (StrolchTransaction tx = openTx(cert, realmName)) {
|
||||
OrderMap orderMap = tx.getOrderMap();
|
||||
|
||||
if (query.hasNavigation()) {
|
||||
String type = ((StrolchTypeNavigation) query.getNavigation()).getType();
|
||||
dataSetSize = orderMap.querySize(tx, type);
|
||||
orders.addAll(tx.doQuery(query));
|
||||
} else {
|
||||
Set<String> types = orderMap.getTypes(tx);
|
||||
dataSetSize = orderMap.querySize(tx);
|
||||
for (String type : types) {
|
||||
query.setNavigation(new StrolchTypeNavigation(type));
|
||||
orders.addAll(tx.doQuery(query));
|
||||
}
|
||||
}
|
||||
|
||||
tx.doNothingOnClose();
|
||||
}
|
||||
|
||||
// do ordering
|
||||
doOrdering(queryData, orders);
|
||||
|
||||
// build JSON response
|
||||
OrderToJsonVisitor toJsonVisitor = new OrderToJsonVisitor();
|
||||
JsonObject root = marshall(queryData, dataSetSize, orders, toJsonVisitor);
|
||||
|
||||
// marshall result
|
||||
Gson gson = new GsonBuilder().setPrettyPrinting().create();
|
||||
String entity = gson.toJson(root);
|
||||
return Response.ok(entity).build();
|
||||
}
|
||||
|
||||
// /**
|
||||
// * Query {@link Activity Activities} by parsing the query string in {@link QueryData#getQuery()} using
|
||||
// * {@link QueryParser}
|
||||
// *
|
||||
// * @param queryData
|
||||
// * the data from the client
|
||||
// * @param request
|
||||
// * the {@link HttpServletRequest} on which to get the {@link Certificate}
|
||||
// *
|
||||
// * @return {@link Response} containing the JSONified {@link Activity Activities} queried
|
||||
// */
|
||||
// @GET
|
||||
// @Produces(MediaType.APPLICATION_JSON)
|
||||
// @Path("orders")
|
||||
// public Response queryActivities(@BeanParam QueryData queryData, @Context HttpServletRequest request) {
|
||||
// Certificate cert = (Certificate) request.getAttribute(StrolchRestfulConstants.STROLCH_CERTIFICATE);
|
||||
//
|
||||
// // see if a special realm was requested
|
||||
// String realmName = getRealmName(queryData);
|
||||
//
|
||||
// List<Activity> activities = new ArrayList<>();
|
||||
//
|
||||
// // parse the query string
|
||||
// ActivityQuery<Activity> query = QueryParser.parseToActivityQuery(queryData.getQuery(), true);
|
||||
//
|
||||
// // query the data
|
||||
// long dataSetSize = 0L;
|
||||
// try (StrolchTransaction tx = openTx(cert, realmName)) {
|
||||
// ActivityMap activityMap = tx.getActivityMap();
|
||||
//
|
||||
// if (query.hasNavigation()) {
|
||||
// String type = ((StrolchTypeNavigation) query.getNavigation()).getType();
|
||||
// dataSetSize = activityMap.querySize(tx, type);
|
||||
// activities.addAll(tx.doQuery(query));
|
||||
// } else {
|
||||
// Set<String> types = activityMap.getTypes(tx);
|
||||
// dataSetSize = activityMap.querySize(tx);
|
||||
// for (String type : types) {
|
||||
// query.setNavigation(new StrolchTypeNavigation(type));
|
||||
// activities.addAll(tx.doQuery(query));
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// tx.doNothingOnClose();
|
||||
// }
|
||||
//
|
||||
// // do ordering
|
||||
// doOrdering(queryData, activities);
|
||||
//
|
||||
// // build JSON response
|
||||
// ActivityToJsonVisitor toJsonVisitor = new ActivityToJsonVisitor();
|
||||
// JsonObject root = marshall(queryData, dataSetSize, activities, toJsonVisitor);
|
||||
//
|
||||
// // marshall result
|
||||
// Gson gson = new GsonBuilder().setPrettyPrinting().create();
|
||||
// String entity = gson.toJson(root);
|
||||
// return Response.ok(entity).build();
|
||||
// }
|
||||
|
||||
private <T extends StrolchRootElement> JsonObject marshall(QueryData queryData, long dataSetSize, List<T> elements,
|
||||
StrolchElementVisitor<T, JsonObject> toJsonVisitor) {
|
||||
|
||||
// paging
|
||||
Paging<T> paging = Paging.asPage(elements, queryData.getPageSize(), queryData.getPage());
|
||||
|
||||
// get page
|
||||
List<T> page = paging.getPage();
|
||||
|
||||
JsonObject root = new JsonObject();
|
||||
root.addProperty("msg", "-");
|
||||
root.addProperty("draw", queryData.getDraw());
|
||||
root.addProperty("dataSetSize", dataSetSize);
|
||||
root.addProperty("nrOfElements", paging.getNrOfElements());
|
||||
|
||||
if (StringHelper.isNotEmpty(orderBy))
|
||||
root.addProperty("sortBy", orderBy);
|
||||
root.addProperty("ascending", asc);
|
||||
if (StringHelper.isNotEmpty(queryData.getOrderBy()))
|
||||
root.addProperty("sortBy", queryData.getOrderBy());
|
||||
root.addProperty("ascending", queryData.isAscending());
|
||||
root.addProperty("nrOfPages", paging.getNrOfPages());
|
||||
root.addProperty("pageSize", paging.getPageSize());
|
||||
root.addProperty("page", paging.getPageToReturn());
|
||||
|
||||
// add items
|
||||
JsonArray data = new JsonArray();
|
||||
for (Resource resource : page) {
|
||||
JsonObject element = new ResourceToJsonVisitor().visit(resource);
|
||||
for (T t : page) {
|
||||
JsonObject element = toJsonVisitor.visit(t);
|
||||
data.add(element);
|
||||
}
|
||||
root.add("data", data);
|
||||
|
||||
Gson gson = new GsonBuilder().setPrettyPrinting().create();
|
||||
String entity = gson.toJson(root);
|
||||
return Response.ok(entity).build();
|
||||
return root;
|
||||
}
|
||||
|
||||
private void query(QueryData queryData, List<Resource> resources, Set<String> queryBy, StrolchTransaction tx,
|
||||
String type) {
|
||||
ResourceQuery<Resource> query = ResourceQuery.query(type);
|
||||
|
||||
OrSelection or = new OrSelection();
|
||||
|
||||
for (String queryB : queryBy) {
|
||||
if (queryB.equals(QueryData.TYPE))
|
||||
continue;
|
||||
|
||||
if (queryB.equals(QueryData.ID)) {
|
||||
or.with(new IdSelection(queryData.getQuery(), StringMatchMode.ci()));
|
||||
} else if (queryB.equals(QueryData.NAME)) {
|
||||
or.with(new NameSelection(queryData.getQuery(), StringMatchMode.ci()));
|
||||
private <T extends StrolchRootElement> void doOrdering(QueryData queryData, List<T> resources) {
|
||||
if (StringHelper.isNotEmpty(queryData.getOrderBy())) {
|
||||
if (queryData.getOrderBy().equals(Tags.ID)) {
|
||||
resources.sort((r1, r2) -> queryData.isAscending() ? r1.getId().compareTo(r2.getId())
|
||||
: r2.getId().compareTo(r1.getId()));
|
||||
} else if (queryData.getOrderBy().equals(Tags.ID)) {
|
||||
resources.sort((r1, r2) -> queryData.isAscending() ? r1.getName().compareTo(r2.getName())
|
||||
: r2.getName().compareTo(r1.getName()));
|
||||
} else if (queryData.getOrderBy().equals(Tags.ID)) {
|
||||
resources.sort((r1, r2) -> queryData.isAscending() ? r1.getType().compareTo(r2.getType())
|
||||
: r2.getType().compareTo(r1.getType()));
|
||||
} else {
|
||||
String[] parts = queryB.split(":");
|
||||
String bagKey = parts[0];
|
||||
String paramKey = parts[1];
|
||||
or.with(ParameterSelection.anyTypeSelection(bagKey, paramKey, queryData.getQuery(),
|
||||
StringMatchMode.ci()));
|
||||
logger.warn("Unhandled ordering " + queryData.getOrderBy());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (or.hasSelection())
|
||||
query.with(or);
|
||||
else
|
||||
query.withAny();
|
||||
|
||||
resources.addAll(tx.doQuery(query));
|
||||
private String getRealmName(QueryData queryData) {
|
||||
String realmName = queryData.getRealmName();
|
||||
if (StringHelper.isEmpty(realmName))
|
||||
realmName = StrolchConstants.DEFAULT_REALM;
|
||||
return realmName;
|
||||
}
|
||||
|
||||
private StrolchTransaction openTx(Certificate certificate, String realm) {
|
||||
|
|
|
@ -1,20 +1,9 @@
|
|||
package li.strolch.rest.endpoint;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import javax.ws.rs.QueryParam;
|
||||
|
||||
import ch.eitchnet.utils.helper.StringHelper;
|
||||
|
||||
public class QueryData {
|
||||
|
||||
public static final String ID = "Id";
|
||||
public static final String NAME = "Name";
|
||||
public static final String TYPE = "Type";
|
||||
|
||||
@QueryParam("realmName")
|
||||
private String realmName;
|
||||
|
||||
|
@ -30,12 +19,6 @@ public class QueryData {
|
|||
@QueryParam("query")
|
||||
private String query;
|
||||
|
||||
@QueryParam("queryBy")
|
||||
private String queryBy;
|
||||
|
||||
@QueryParam("types")
|
||||
private String types;
|
||||
|
||||
@QueryParam("orderBy")
|
||||
private String orderBy;
|
||||
|
||||
|
@ -97,38 +80,4 @@ public class QueryData {
|
|||
public void setQuery(String query) {
|
||||
this.query = query;
|
||||
}
|
||||
|
||||
public String getQueryBy() {
|
||||
return this.queryBy;
|
||||
}
|
||||
|
||||
public void setQueryBy(String queryBy) {
|
||||
this.queryBy = queryBy;
|
||||
}
|
||||
|
||||
public String getTypes() {
|
||||
return this.types;
|
||||
}
|
||||
|
||||
public void setTypes(String types) {
|
||||
this.types = types;
|
||||
}
|
||||
|
||||
public Set<String> getQueryByNames() {
|
||||
return toSet(this.queryBy);
|
||||
}
|
||||
|
||||
public Set<String> getTypesAsSet() {
|
||||
return toSet(this.types);
|
||||
}
|
||||
|
||||
private Set<String> toSet(String value) {
|
||||
if (StringHelper.isEmpty(value))
|
||||
return Collections.emptySet();
|
||||
|
||||
if (!value.contains(","))
|
||||
return Collections.singleton(value.trim());
|
||||
|
||||
return Arrays.stream(value.split(",")).map(s -> s.trim()).collect(Collectors.toSet());
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue