[Major] Refactored query API

- Now added ordering
- added setters for visitors on query
-
This commit is contained in:
Robert von Burg 2015-08-10 22:21:45 +02:00
parent 4b96679588
commit 69eb5ccf38
76 changed files with 1015 additions and 532 deletions

@ -1 +1 @@
Subproject commit a3c2a2ed8f65c5463609f26c2835ce8f7baa5e69
Subproject commit f5cf3e3ad0a4865ec75ffb4e58b03557b4d1db2e

View File

@ -17,7 +17,6 @@ package li.strolch.agent.api;
import java.util.List;
import li.strolch.model.ActivityVisitor;
import li.strolch.model.activity.Activity;
import li.strolch.model.query.ActivityQuery;
import li.strolch.persistence.api.StrolchTransaction;
@ -27,5 +26,5 @@ import li.strolch.persistence.api.StrolchTransaction;
*/
public interface ActivityMap extends ElementMap<Activity> {
public <U> List<U> doQuery(StrolchTransaction tx, ActivityQuery query, ActivityVisitor<U> activityVisitor);
public <U> List<U> doQuery(StrolchTransaction tx, ActivityQuery<U> query);
}

View File

@ -20,7 +20,6 @@ import java.util.Set;
import li.strolch.model.audit.Audit;
import li.strolch.model.audit.AuditQuery;
import li.strolch.model.audit.AuditVisitor;
import li.strolch.persistence.api.StrolchTransaction;
import ch.eitchnet.utils.collections.DateRange;
@ -67,7 +66,5 @@ public interface AuditTrail {
public long removeAll(StrolchTransaction tx, String type, DateRange dateRange);
public List<Audit> doQuery(StrolchTransaction tx, AuditQuery query);
public <U> List<U> doQuery(StrolchTransaction tx, AuditQuery query, AuditVisitor<U> auditVisitor);
public <U> List<U> doQuery(StrolchTransaction tx, AuditQuery<U> query);
}

View File

@ -18,7 +18,6 @@ package li.strolch.agent.api;
import java.util.List;
import li.strolch.model.Order;
import li.strolch.model.OrderVisitor;
import li.strolch.model.query.OrderQuery;
import li.strolch.persistence.api.StrolchTransaction;
@ -27,5 +26,5 @@ import li.strolch.persistence.api.StrolchTransaction;
*/
public interface OrderMap extends ElementMap<Order> {
public <U> List<U> doQuery(StrolchTransaction tx, OrderQuery query, OrderVisitor<U> orderVisitor);
public <U> List<U> doQuery(StrolchTransaction tx, OrderQuery<U> query);
}

View File

@ -18,7 +18,6 @@ package li.strolch.agent.api;
import java.util.List;
import li.strolch.model.Resource;
import li.strolch.model.ResourceVisitor;
import li.strolch.model.query.ResourceQuery;
import li.strolch.persistence.api.StrolchTransaction;
@ -27,5 +26,5 @@ import li.strolch.persistence.api.StrolchTransaction;
*/
public interface ResourceMap extends ElementMap<Resource> {
public <U> List<U> doQuery(StrolchTransaction tx, ResourceQuery query, ResourceVisitor<U> resourceVisitor);
public <U> List<U> doQuery(StrolchTransaction tx, ResourceQuery<U> query);
}

View File

@ -17,6 +17,7 @@ package li.strolch.agent.impl;
import java.util.List;
import ch.eitchnet.utils.dbc.DBC;
import li.strolch.agent.api.ActivityMap;
import li.strolch.agent.api.AuditTrail;
import li.strolch.agent.api.ElementMap;
@ -47,11 +48,14 @@ public class AuditingActivityMap extends AuditingElementMapFacade<Activity> impl
}
@Override
public <U> List<U> doQuery(StrolchTransaction tx, ActivityQuery query, ActivityVisitor<U> activityVisitor) {
List<U> result = getElementMap().doQuery(tx, query, activity -> {
public <U> List<U> doQuery(StrolchTransaction tx, ActivityQuery<U> query) {
ActivityVisitor<U> activityVisitor = query.getActivityVisitor();
DBC.PRE.assertNotNull("activityVisitor on query", activityVisitor);
query.setActivityVisitor(activity -> {
this.read.add(activity);
return activityVisitor.visit(activity);
});
return result;
return getElementMap().doQuery(tx, query);
}
}

View File

@ -25,7 +25,6 @@ import li.strolch.agent.api.AuditTrail;
import li.strolch.model.audit.Audit;
import li.strolch.model.audit.AuditQuery;
import li.strolch.model.audit.AuditVisitor;
import li.strolch.model.audit.NoStrategyAuditVisitor;
import li.strolch.persistence.api.StrolchTransaction;
import ch.eitchnet.utils.collections.DateRange;
import ch.eitchnet.utils.dbc.DBC;
@ -203,18 +202,13 @@ public class AuditingAuditMapFacade implements AuditTrail {
}
@Override
public List<Audit> doQuery(StrolchTransaction tx, AuditQuery query) {
List<Audit> elements = this.auditTrail.doQuery(tx, query, new NoStrategyAuditVisitor());
this.read.addAll(elements);
return elements;
}
@Override
public <U> List<U> doQuery(StrolchTransaction tx, AuditQuery query, AuditVisitor<U> auditVisitor) {
List<U> elements = this.auditTrail.doQuery(tx, query, audit -> {
public <U> List<U> doQuery(StrolchTransaction tx, AuditQuery<U> query) {
AuditVisitor<U> auditVisitor = query.getAuditVisitor();
query.setAuditVisitor(audit -> {
this.read.add(audit);
return auditVisitor.visitAudit(audit);
});
return elements;
return this.auditTrail.doQuery(tx, query);
}
}

View File

@ -24,6 +24,7 @@ import li.strolch.model.Order;
import li.strolch.model.OrderVisitor;
import li.strolch.model.query.OrderQuery;
import li.strolch.persistence.api.StrolchTransaction;
import ch.eitchnet.utils.dbc.DBC;
/**
* This is the {@link AuditTrail} for {@link Order Orders}
@ -47,11 +48,14 @@ public class AuditingOrderMap extends AuditingElementMapFacade<Order> implements
}
@Override
public <U> List<U> doQuery(StrolchTransaction tx, OrderQuery query, OrderVisitor<U> orderVisitor) {
List<U> result = getElementMap().doQuery(tx, query, order -> {
public <U> List<U> doQuery(StrolchTransaction tx, OrderQuery<U> query) {
OrderVisitor<U> orderVisitor = query.getOrderVisitor();
DBC.PRE.assertNotNull("orderVisitor on query", orderVisitor);
query.setOrderVisitor(order -> {
this.read.add(order);
return orderVisitor.visit(order);
});
return result;
return getElementMap().doQuery(tx, query);
}
}

View File

@ -24,6 +24,7 @@ import li.strolch.model.Resource;
import li.strolch.model.ResourceVisitor;
import li.strolch.model.query.ResourceQuery;
import li.strolch.persistence.api.StrolchTransaction;
import ch.eitchnet.utils.dbc.DBC;
/**
* This is the {@link AuditTrail} for {@link Resource Resources}
@ -47,11 +48,14 @@ public class AuditingResourceMap extends AuditingElementMapFacade<Resource> impl
}
@Override
public <U> List<U> doQuery(StrolchTransaction tx, ResourceQuery query, ResourceVisitor<U> resourceVisitor) {
List<U> result = getElementMap().doQuery(tx, query, resource -> {
public <U> List<U> doQuery(StrolchTransaction tx, ResourceQuery<U> query) {
ResourceVisitor<U> resourceVisitor = query.getResourceVisitor();
DBC.PRE.assertNotNull("resourceVisitor on query", resourceVisitor);
query.setResourceVisitor(resource -> {
this.read.add(resource);
return resourceVisitor.visit(resource);
});
return result;
return getElementMap().doQuery(tx, query);
}
}

View File

@ -20,7 +20,6 @@ import static li.strolch.model.StrolchModelConstants.INTERPRETATION_ACTIVITY_REF
import java.util.List;
import li.strolch.agent.api.ActivityMap;
import li.strolch.model.ActivityVisitor;
import li.strolch.model.activity.Activity;
import li.strolch.model.parameter.Parameter;
import li.strolch.model.query.ActivityQuery;
@ -53,7 +52,7 @@ public class CachedActivityMap extends CachedElementMap<Activity> implements Act
}
@Override
public <U> List<U> doQuery(StrolchTransaction tx, ActivityQuery query, ActivityVisitor<U> activityVisitor) {
return getCachedDao().doQuery(query, activityVisitor);
public <U> List<U> doQuery(StrolchTransaction tx, ActivityQuery<U> query) {
return getCachedDao().doQuery(query);
}
}

View File

@ -22,8 +22,6 @@ import java.util.Set;
import li.strolch.agent.api.AuditTrail;
import li.strolch.model.audit.Audit;
import li.strolch.model.audit.AuditQuery;
import li.strolch.model.audit.AuditVisitor;
import li.strolch.model.audit.NoStrategyAuditVisitor;
import li.strolch.persistence.api.AuditDao;
import li.strolch.persistence.api.StrolchTransaction;
import li.strolch.persistence.inmemory.InMemoryAuditDao;
@ -156,12 +154,7 @@ public class CachedAuditTrail implements AuditTrail {
}
@Override
public List<Audit> doQuery(StrolchTransaction tx, AuditQuery query) {
return getDbDao(tx).doQuery(query, new NoStrategyAuditVisitor());
}
@Override
public <U> List<U> doQuery(StrolchTransaction tx, AuditQuery query, AuditVisitor<U> auditVisitor) {
return getDbDao(tx).doQuery(query, auditVisitor);
public <U> List<U> doQuery(StrolchTransaction tx, AuditQuery<U> query) {
return getDbDao(tx).doQuery(query);
}
}

View File

@ -21,7 +21,6 @@ import java.util.List;
import li.strolch.agent.api.OrderMap;
import li.strolch.model.Order;
import li.strolch.model.OrderVisitor;
import li.strolch.model.parameter.Parameter;
import li.strolch.model.query.OrderQuery;
import li.strolch.persistence.api.OrderDao;
@ -53,7 +52,7 @@ public class CachedOrderMap extends CachedElementMap<Order> implements OrderMap
}
@Override
public <U> List<U> doQuery(StrolchTransaction tx, OrderQuery query, OrderVisitor<U> orderVisitor) {
return getCachedDao().doQuery(query, orderVisitor);
public <U> List<U> doQuery(StrolchTransaction tx, OrderQuery<U> query) {
return getCachedDao().doQuery(query);
}
}

View File

@ -21,7 +21,6 @@ import java.util.List;
import li.strolch.agent.api.ResourceMap;
import li.strolch.model.Resource;
import li.strolch.model.ResourceVisitor;
import li.strolch.model.parameter.Parameter;
import li.strolch.model.query.ResourceQuery;
import li.strolch.persistence.api.ResourceDao;
@ -53,7 +52,7 @@ public class CachedResourceMap extends CachedElementMap<Resource> implements Res
}
@Override
public <U> List<U> doQuery(StrolchTransaction tx, ResourceQuery query, ResourceVisitor<U> resourceVisitor) {
return getCachedDao().doQuery(query, resourceVisitor);
public <U> List<U> doQuery(StrolchTransaction tx, ResourceQuery<U> query) {
return getCachedDao().doQuery(query);
}
}

View File

@ -21,7 +21,6 @@ import java.util.Set;
import li.strolch.agent.api.AuditTrail;
import li.strolch.model.audit.Audit;
import li.strolch.model.audit.AuditQuery;
import li.strolch.model.audit.AuditVisitor;
import li.strolch.persistence.api.StrolchTransaction;
import ch.eitchnet.utils.collections.DateRange;
@ -101,12 +100,7 @@ public class NoStrategyAuditTrail implements AuditTrail {
}
@Override
public List<Audit> doQuery(StrolchTransaction tx, AuditQuery query) {
return null;
}
@Override
public <U> List<U> doQuery(StrolchTransaction tx, AuditQuery query, AuditVisitor<U> auditVisitor) {
public <U> List<U> doQuery(StrolchTransaction tx, AuditQuery<U> query) {
return null;
}
}

View File

@ -20,7 +20,6 @@ import static li.strolch.model.StrolchModelConstants.INTERPRETATION_ACTIVITY_REF
import java.util.List;
import li.strolch.agent.api.ActivityMap;
import li.strolch.model.ActivityVisitor;
import li.strolch.model.activity.Activity;
import li.strolch.model.parameter.Parameter;
import li.strolch.model.query.ActivityQuery;
@ -40,7 +39,7 @@ public class TransactionalActivityMap extends TransactionalElementMap<Activity>
}
@Override
public <U> List<U> doQuery(StrolchTransaction tx, ActivityQuery query, ActivityVisitor<U> activityVisitor) {
return getDao(tx).doQuery(query, activityVisitor);
public <U> List<U> doQuery(StrolchTransaction tx, ActivityQuery<U> query) {
return getDao(tx).doQuery(query);
}
}

View File

@ -21,8 +21,6 @@ import java.util.Set;
import li.strolch.agent.api.AuditTrail;
import li.strolch.model.audit.Audit;
import li.strolch.model.audit.AuditQuery;
import li.strolch.model.audit.AuditVisitor;
import li.strolch.model.audit.NoStrategyAuditVisitor;
import li.strolch.persistence.api.AuditDao;
import li.strolch.persistence.api.StrolchTransaction;
import ch.eitchnet.utils.collections.DateRange;
@ -109,12 +107,7 @@ public class TransactionalAuditTrail implements AuditTrail {
}
@Override
public List<Audit> doQuery(StrolchTransaction tx, AuditQuery query) {
return getDao(tx).doQuery(query, new NoStrategyAuditVisitor());
}
@Override
public <U> List<U> doQuery(StrolchTransaction tx, AuditQuery query, AuditVisitor<U> auditVisitor) {
return getDao(tx).doQuery(query, auditVisitor);
public <U> List<U> doQuery(StrolchTransaction tx, AuditQuery<U> query) {
return getDao(tx).doQuery(query);
}
}

View File

@ -21,7 +21,6 @@ import java.util.List;
import li.strolch.agent.api.OrderMap;
import li.strolch.model.Order;
import li.strolch.model.OrderVisitor;
import li.strolch.model.parameter.Parameter;
import li.strolch.model.query.OrderQuery;
import li.strolch.persistence.api.OrderDao;
@ -40,7 +39,7 @@ public class TransactionalOrderMap extends TransactionalElementMap<Order> implem
}
@Override
public <U> List<U> doQuery(StrolchTransaction tx, OrderQuery query, OrderVisitor<U> orderVisitor) {
return getDao(tx).doQuery(query, orderVisitor);
public <U> List<U> doQuery(StrolchTransaction tx, OrderQuery<U> query) {
return getDao(tx).doQuery(query);
}
}

View File

@ -21,7 +21,6 @@ import java.util.List;
import li.strolch.agent.api.ResourceMap;
import li.strolch.model.Resource;
import li.strolch.model.ResourceVisitor;
import li.strolch.model.parameter.Parameter;
import li.strolch.model.query.ResourceQuery;
import li.strolch.persistence.api.ResourceDao;
@ -40,7 +39,7 @@ public class TransactionalResourceMap extends TransactionalElementMap<Resource>
}
@Override
public <U> List<U> doQuery(StrolchTransaction tx, ResourceQuery query, ResourceVisitor<U> resourceVisitor) {
return getDao(tx).doQuery(query, resourceVisitor);
public <U> List<U> doQuery(StrolchTransaction tx, ResourceQuery<U> query) {
return getDao(tx).doQuery(query);
}
}

View File

@ -38,14 +38,11 @@ import li.strolch.agent.impl.AuditingResourceMap;
import li.strolch.agent.impl.InternalStrolchRealm;
import li.strolch.exception.StrolchAccessDeniedException;
import li.strolch.exception.StrolchException;
import li.strolch.model.ActivityVisitor;
import li.strolch.model.GroupedParameterizedElement;
import li.strolch.model.Locator;
import li.strolch.model.Order;
import li.strolch.model.OrderVisitor;
import li.strolch.model.ParameterBag;
import li.strolch.model.Resource;
import li.strolch.model.ResourceVisitor;
import li.strolch.model.StrolchElement;
import li.strolch.model.StrolchRootElement;
import li.strolch.model.Tags;
@ -53,8 +50,6 @@ import li.strolch.model.activity.Activity;
import li.strolch.model.audit.AccessType;
import li.strolch.model.audit.Audit;
import li.strolch.model.audit.AuditQuery;
import li.strolch.model.audit.AuditVisitor;
import li.strolch.model.audit.NoStrategyAuditVisitor;
import li.strolch.model.parameter.Parameter;
import li.strolch.model.parameter.StringListParameter;
import li.strolch.model.parameter.StringParameter;
@ -65,9 +60,6 @@ import li.strolch.model.query.StrolchQuery;
import li.strolch.model.timedstate.StrolchTimedState;
import li.strolch.model.timevalue.IValue;
import li.strolch.model.visitor.ElementTypeVisitor;
import li.strolch.model.visitor.NoStrategyActivityVisitor;
import li.strolch.model.visitor.NoStrategyOrderVisitor;
import li.strolch.model.visitor.NoStrategyResourceVisitor;
import li.strolch.runtime.StrolchConstants;
import li.strolch.runtime.privilege.PrivilegeHandler;
import li.strolch.service.api.Command;
@ -292,51 +284,31 @@ public abstract class AbstractTransaction implements StrolchTransaction {
}
@Override
public List<Order> doQuery(OrderQuery query) {
public <U> List<U> doQuery(OrderQuery<U> query) {
assertQueryAllowed(query);
return getOrderMap().doQuery(this, query, new NoStrategyOrderVisitor());
DBC.PRE.assertNotNull("orderVisitor", query.getOrderVisitor());
return getOrderMap().doQuery(this, query);
}
@Override
public <U> List<U> doQuery(OrderQuery query, OrderVisitor<U> orderVisitor) {
public <U> List<U> doQuery(ResourceQuery<U> query) {
assertQueryAllowed(query);
return getOrderMap().doQuery(this, query, orderVisitor);
DBC.PRE.assertNotNull("resourceVisitor", query.getResourceVisitor());
return getResourceMap().doQuery(this, query);
}
@Override
public List<Resource> doQuery(ResourceQuery query) {
public <U> List<U> doQuery(ActivityQuery<U> query) {
assertQueryAllowed(query);
return getResourceMap().doQuery(this, query, new NoStrategyResourceVisitor());
DBC.PRE.assertNotNull("activityVisitor", query.getActivityVisitor());
return getActivityMap().doQuery(this, query);
}
@Override
public <U> List<U> doQuery(ResourceQuery query, ResourceVisitor<U> resourceVisitor) {
public <U> List<U> doQuery(AuditQuery<U> query) {
assertQueryAllowed(query);
return getResourceMap().doQuery(this, query, resourceVisitor);
}
@Override
public List<Activity> doQuery(ActivityQuery query) {
assertQueryAllowed(query);
return getActivityMap().doQuery(this, query, new NoStrategyActivityVisitor());
}
@Override
public <U> List<U> doQuery(ActivityQuery query, ActivityVisitor<U> activityVisitor) {
assertQueryAllowed(query);
return getActivityMap().doQuery(this, query, activityVisitor);
}
@Override
public List<Audit> doQuery(AuditQuery query) {
assertQueryAllowed(query);
return getAuditTrail().doQuery(this, query, new NoStrategyAuditVisitor());
}
@Override
public <U> List<U> doQuery(AuditQuery query, AuditVisitor<U> auditVisitor) {
assertQueryAllowed(query);
return getAuditTrail().doQuery(this, query, auditVisitor);
DBC.PRE.assertNotNull("auditVisitor", query.getAuditVisitor());
return getAuditTrail().doQuery(this, query);
}
@SuppressWarnings("unchecked")
@ -514,18 +486,6 @@ public abstract class AbstractTransaction implements StrolchTransaction {
DBC.PRE.assertNotNull("refP", refP);
return getResourceMap().getBy(this, refP, assertExists);
}
@Override
public Activity getActivityBy(String type, String id) {
@ -566,26 +526,6 @@ public abstract class AbstractTransaction implements StrolchTransaction {
return getActivityMap().getBy(this, refP, assertExists);
}
@Override
public void flush() {
try {

View File

@ -17,7 +17,6 @@ package li.strolch.persistence.api;
import java.util.List;
import li.strolch.model.ActivityVisitor;
import li.strolch.model.activity.Activity;
import li.strolch.model.query.ActivityQuery;
@ -26,5 +25,5 @@ import li.strolch.model.query.ActivityQuery;
*/
public interface ActivityDao extends StrolchDao<Activity> {
public <U> List<U> doQuery(ActivityQuery query, ActivityVisitor<U> activityVisitor);
public <U> List<U> doQuery(ActivityQuery<U> query);
}

View File

@ -20,7 +20,6 @@ import java.util.Set;
import li.strolch.model.audit.Audit;
import li.strolch.model.audit.AuditQuery;
import li.strolch.model.audit.AuditVisitor;
import ch.eitchnet.utils.collections.DateRange;
/**
@ -54,5 +53,5 @@ public interface AuditDao {
public long removeAll(String type, DateRange dateRange);
public <U> List<U> doQuery(AuditQuery query, AuditVisitor<U> auditVisitor);
public <U> List<U> doQuery(AuditQuery<U> query);
}

View File

@ -18,7 +18,6 @@ package li.strolch.persistence.api;
import java.util.List;
import li.strolch.model.Order;
import li.strolch.model.OrderVisitor;
import li.strolch.model.query.OrderQuery;
/**
@ -26,5 +25,5 @@ import li.strolch.model.query.OrderQuery;
*/
public interface OrderDao extends StrolchDao<Order> {
public <U> List<U> doQuery(OrderQuery query, OrderVisitor<U> orderVisitor);
public <U> List<U> doQuery(OrderQuery<U> query);
}

View File

@ -18,7 +18,6 @@ package li.strolch.persistence.api;
import java.util.List;
import li.strolch.model.Resource;
import li.strolch.model.ResourceVisitor;
import li.strolch.model.query.ResourceQuery;
/**
@ -26,5 +25,5 @@ import li.strolch.model.query.ResourceQuery;
*/
public interface ResourceDao extends StrolchDao<Resource> {
public <U> List<U> doQuery(ResourceQuery query, ResourceVisitor<U> resourceVisitor);
public <U> List<U> doQuery(ResourceQuery<U> query);
}

View File

@ -363,33 +363,10 @@ public interface StrolchTransaction extends AutoCloseable {
*/
public Audit auditFrom(AccessType accessType, StrolchRootElement element);
/**
* <p>
* Performs the given {@link OrderQuery} returning the resulting list of {@link Order Orders}.
* </p>
*
* <p>
* <b>Note:</b> Should the result be mapped to different objects, then use
* {@link #doQuery(OrderQuery, OrderVisitor)}
* </p>
*
* @param query
* the query to perform
*
* @return the result list, never null
*/
public List<Order> doQuery(OrderQuery query);
/**
* <p>
* Performs the given {@link OrderQuery} and each returned {@link Order} is passed through the {@link OrderVisitor}
* and the return value of the visitor is added to the return list
* </p>
*
* <p>
* This method is intended for situations where the query result should not be {@link Order} but some other object
* type. For instance in a restful API, the result might have to be mapped to a POJO, thus using this method can
* perform the mapping step for you
* attached to the {@link OrderQuery} and the return value of the visitor is added to the return list
* </p>
*
* @param query
@ -397,35 +374,13 @@ public interface StrolchTransaction extends AutoCloseable {
*
* @return the result list of elements as returned by the {@link OrderVisitor}, never null
*/
public <U> List<U> doQuery(OrderQuery query, OrderVisitor<U> orderVisitor);
/**
* <p>
* Performs the given {@link ResourceQuery} returning the resulting list of {@link Resource Resources}.
* </p>
*
* <p>
* <b>Note:</b> Should the result be mapped to different objects, then use
* {@link #doQuery(ResourceQuery, ResourceVisitor)}
* </p>
*
* @param query
* the query to perform
*
* @return the result list, never null
*/
public List<Resource> doQuery(ResourceQuery query);
public <U> List<U> doQuery(OrderQuery<U> query);
/**
* <p>
* Performs the given {@link ResourceQuery} and each returned {@link Resource} is passed through the
* {@link ResourceVisitor} and the return value of the visitor is added to the return list
* </p>
*
* <p>
* This method is intended for situations where the query result should not be {@link Resource} but some other
* object type. For instance in a restful API, the result might have to be mapped to a POJO, thus using this method
* can perform the mapping step for you
* {@link ResourceVisitor} attached to the {@link ResourceQuery} and the return value of the visitor is added to the
* return list
* </p>
*
* @param query
@ -433,35 +388,13 @@ public interface StrolchTransaction extends AutoCloseable {
*
* @return the result list of elements as returned by the {@link ResourceVisitor}, never null
*/
public <U> List<U> doQuery(ResourceQuery query, ResourceVisitor<U> resourceVisitor);
/**
* <p>
* Performs the given {@link ActivityQuery} returning the resulting list of {@link Activity Activities}.
* </p>
*
* <p>
* <b>Note:</b> Should the result be mapped to different objects, then use
* {@link #doQuery(ActivityQuery, ActivityVisitor)}
* </p>
*
* @param query
* the query to perform
*
* @return the result list, never null
*/
public List<Activity> doQuery(ActivityQuery query);
public <U> List<U> doQuery(ResourceQuery<U> query);
/**
* <p>
* Performs the given {@link ActivityQuery} and each returned {@link Activity} is passed through the
* {@link ActivityVisitor} and the return value of the visitor is added to the return list
* </p>
*
* <p>
* This method is intended for situations where the query result should not be {@link Activity} but some other
* object type. For instance in a restful API, the result might have to be mapped to a POJO, thus using this method
* can perform the mapping step for you
* {@link ActivityVisitor} attached to the {@link ActivityQuery} and the return value of the visitor is added to the
* return list
* </p>
*
* @param query
@ -469,35 +402,12 @@ public interface StrolchTransaction extends AutoCloseable {
*
* @return the result list of elements as returned by the {@link ActivityVisitor}, never null
*/
public <U> List<U> doQuery(ActivityQuery query, ActivityVisitor<U> activityVisitor);
/**
* <p>
* Performs the given {@link AuditQuery} returning the resulting list of {@link Audit Audits}.
* </p>
*
* <p>
* <b>Note:</b> Should the result be mapped to different objects, then use
* {@link #doQuery(AuditQuery, AuditVisitor)}
* </p>
*
* @param query
* the query to perform
*
* @return the result list, never null
*/
public List<Audit> doQuery(AuditQuery query);
public <U> List<U> doQuery(ActivityQuery<U> query);
/**
* <p>
* Performs the given {@link AuditQuery} and each returned {@link Audit} is passed through the {@link AuditVisitor}
* and the return value of the visitor is added to the return list
* </p>
*
* <p>
* This method is intended for situations where the query result should not be {@link Audit} but some other object
* type. For instance in a restful API, the result might have to be mapped to a POJO, thus using this method can
* perform the mapping step for you
* attached to the {@link AuditQuery} and the return value of the visitor is added to the return list
* </p>
*
* @param query
@ -505,7 +415,7 @@ public interface StrolchTransaction extends AutoCloseable {
*
* @return the result list of elements as returned by the {@link AuditVisitor}, never null
*/
public <U> List<U> doQuery(AuditQuery query, AuditVisitor<U> auditVisitor);
public <U> List<U> doQuery(AuditQuery<U> query);
/**
* <p>

View File

@ -17,7 +17,6 @@ package li.strolch.persistence.inmemory;
import java.util.List;
import li.strolch.model.ActivityVisitor;
import li.strolch.model.activity.Activity;
import li.strolch.model.query.ActivityQuery;
import li.strolch.persistence.api.ActivityDao;
@ -27,9 +26,9 @@ import li.strolch.runtime.query.inmemory.InMemoryQuery;
public class InMemoryActivityDao extends InMemoryDao<Activity> implements ActivityDao {
@Override
public <U> List<U> doQuery(ActivityQuery activityQuery, ActivityVisitor<U> activityVisitor) {
public <U> List<U> doQuery(ActivityQuery<U> activityQuery) {
InMemoryActivityQueryVisitor visitor = new InMemoryActivityQueryVisitor();
InMemoryQuery<Activity, U> query = visitor.visit(activityQuery, activityVisitor);
InMemoryQuery<Activity, U> query = visitor.visit(activityQuery);
return query.doQuery(this);
}
}

View File

@ -23,7 +23,6 @@ import java.util.Set;
import li.strolch.model.audit.Audit;
import li.strolch.model.audit.AuditQuery;
import li.strolch.model.audit.AuditVisitor;
import li.strolch.persistence.api.AuditDao;
import li.strolch.runtime.query.inmemory.InMemoryAuditQuery;
import li.strolch.runtime.query.inmemory.InMemoryAuditQueryVisitor;
@ -160,9 +159,9 @@ public class InMemoryAuditDao implements AuditDao {
}
@Override
public <U> List<U> doQuery(AuditQuery auditQuery, AuditVisitor<U> auditVisitor) {
InMemoryAuditQueryVisitor visitor = new InMemoryAuditQueryVisitor();
InMemoryAuditQuery<U> query = visitor.visit(auditQuery, auditVisitor);
public <U> List<U> doQuery(AuditQuery<U> auditQuery) {
InMemoryAuditQueryVisitor<U> visitor = new InMemoryAuditQueryVisitor<>();
InMemoryAuditQuery<U> query = visitor.toInMemory(auditQuery);
return query.doQuery(this);
}
}

View File

@ -18,7 +18,6 @@ package li.strolch.persistence.inmemory;
import java.util.List;
import li.strolch.model.Order;
import li.strolch.model.OrderVisitor;
import li.strolch.model.query.OrderQuery;
import li.strolch.persistence.api.OrderDao;
import li.strolch.runtime.query.inmemory.InMemoryOrderQueryVisitor;
@ -27,9 +26,9 @@ import li.strolch.runtime.query.inmemory.InMemoryQuery;
public class InMemoryOrderDao extends InMemoryDao<Order> implements OrderDao {
@Override
public <U> List<U> doQuery(OrderQuery orderQuery, OrderVisitor<U> orderVisitor) {
public <U> List<U> doQuery(OrderQuery<U> orderQuery) {
InMemoryOrderQueryVisitor visitor = new InMemoryOrderQueryVisitor();
InMemoryQuery<Order, U> query = visitor.visit(orderQuery, orderVisitor);
InMemoryQuery<Order, U> query = visitor.visit(orderQuery);
return query.doQuery(this);
}
}

View File

@ -18,7 +18,6 @@ package li.strolch.persistence.inmemory;
import java.util.List;
import li.strolch.model.Resource;
import li.strolch.model.ResourceVisitor;
import li.strolch.model.query.ResourceQuery;
import li.strolch.persistence.api.ResourceDao;
import li.strolch.runtime.query.inmemory.InMemoryQuery;
@ -27,9 +26,9 @@ import li.strolch.runtime.query.inmemory.InMemoryResourceQueryVisitor;
public class InMemoryResourceDao extends InMemoryDao<Resource> implements ResourceDao {
@Override
public <U> List<U> doQuery(ResourceQuery resourceQuery, ResourceVisitor<U> resourceVisitor) {
public <U> List<U> doQuery(ResourceQuery<U> resourceQuery) {
InMemoryResourceQueryVisitor visitor = new InMemoryResourceQueryVisitor();
InMemoryQuery<Resource, U> query = visitor.visit(resourceQuery, resourceVisitor);
InMemoryQuery<Resource, U> query = visitor.visit(resourceQuery);
return query.doQuery(this);
}
}

View File

@ -20,6 +20,7 @@ import java.util.List;
import li.strolch.model.audit.Audit;
import li.strolch.persistence.api.AuditDao;
import ch.eitchnet.utils.collections.DateRange;
import ch.eitchnet.utils.dbc.DBC;
/**
* @author Robert von Burg <eitch@eitchnet.ch>
@ -30,6 +31,8 @@ public class AuditTypeNavigator implements AuditNavigator {
private DateRange dateRange;
public AuditTypeNavigator(String type, DateRange dateRange) {
DBC.PRE.assertNotNull("type", type);
DBC.PRE.assertNotNull("dateRange", dateRange);
this.type = type;
this.dateRange = dateRange;
}

View File

@ -40,8 +40,10 @@ public class InMemoryActivityQueryVisitor extends InMemoryQueryVisitor<Activity,
return new InMemoryActivityQueryVisitor();
}
public <U> InMemoryQuery<Activity, U> visit(ActivityQuery activityQuery, ActivityVisitor<U> activityVisitor) {
public <U> InMemoryQuery<Activity, U> visit(ActivityQuery<U> activityQuery) {
ActivityVisitor<U> activityVisitor = activityQuery.getActivityVisitor();
DBC.PRE.assertNotNull("ActivityVisitor may not be null!", activityVisitor); //$NON-NLS-1$
activityQuery.accept(this);
Navigator<Activity> navigator = getNavigator();
@ -52,10 +54,10 @@ public class InMemoryActivityQueryVisitor extends InMemoryQueryVisitor<Activity,
List<Selector<Activity>> selectors = getSelectors();
if (selectors.isEmpty())
return new InMemoryQuery<>(navigator, null, activityVisitor);
return new InMemoryQuery<>(navigator, null, activityVisitor, getComparator());
DBC.PRE.assertTrue("Invalid query as it may only contain one selector!", selectors.size() == 1); //$NON-NLS-1$
return new InMemoryQuery<>(navigator, selectors.get(0), activityVisitor);
return new InMemoryQuery<>(navigator, selectors.get(0), activityVisitor, getComparator());
}
@Override

View File

@ -30,13 +30,15 @@ import ch.eitchnet.utils.dbc.DBC;
/**
* @author Robert von Burg <eitch@eitchnet.ch>
*/
public class InMemoryAuditQueryVisitor implements AuditQueryVisitor {
public class InMemoryAuditQueryVisitor<U> implements AuditQueryVisitor {
private AuditTypeNavigator navigator;
private List<AuditSelector> selectors;
public <U> InMemoryAuditQuery<U> visit(AuditQuery auditQuery, AuditVisitor<U> auditVisitor) {
DBC.PRE.assertNotNull("auditVisitor may not be null!", auditVisitor); //$NON-NLS-1$
public InMemoryAuditQuery<U> toInMemory(AuditQuery<U> auditQuery) {
AuditVisitor<U> auditVisitor = auditQuery.getAuditVisitor();
DBC.PRE.assertNotNull("auditVisitor", auditVisitor); //$NON-NLS-1$
this.selectors = new ArrayList<>();
auditQuery.accept(this);
@ -66,7 +68,7 @@ public class InMemoryAuditQueryVisitor implements AuditQueryVisitor {
}
@Override
public void visit(AuditQuery auditQuery) {
public void visit(AuditQuery<?> auditQuery) {
String type = auditQuery.getElementTypeSelection();
DateRange dateRange = auditQuery.getDateRange();
this.navigator = new AuditTypeNavigator(type, dateRange);

View File

@ -41,8 +41,10 @@ public class InMemoryOrderQueryVisitor extends InMemoryQueryVisitor<Order, Order
return new InMemoryOrderQueryVisitor();
}
public <U> InMemoryQuery<Order, U> visit(OrderQuery orderQuery, OrderVisitor<U> orderVisitor) {
public <U> InMemoryQuery<Order, U> visit(OrderQuery<U> orderQuery) {
OrderVisitor<U> orderVisitor = orderQuery.getOrderVisitor();
DBC.PRE.assertNotNull("OrderVisitor may not be null!", orderVisitor); //$NON-NLS-1$
orderQuery.accept(this);
Navigator<Order> navigator = getNavigator();
@ -53,10 +55,10 @@ public class InMemoryOrderQueryVisitor extends InMemoryQueryVisitor<Order, Order
List<Selector<Order>> selectors = getSelectors();
if (selectors.isEmpty())
return new InMemoryQuery<>(navigator, null, orderVisitor);
return new InMemoryQuery<>(navigator, null, orderVisitor, getComparator());
DBC.PRE.assertTrue("Invalid query as it may only contain one selector!", selectors.size() == 1); //$NON-NLS-1$
return new InMemoryQuery<>(navigator, selectors.get(0), orderVisitor);
return new InMemoryQuery<>(navigator, selectors.get(0), orderVisitor, getComparator());
}
@Override

View File

@ -17,6 +17,7 @@ package li.strolch.runtime.query.inmemory;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
@ -33,15 +34,18 @@ public class InMemoryQuery<T extends StrolchElement, U> {
private Navigator<T> navigator;
private Selector<T> selector;
private StrolchElementVisitor<T, U> elementVisitor;
private Comparator<T> comparator;
public InMemoryQuery() {
// empty constructor
}
public InMemoryQuery(Navigator<T> navigator, Selector<T> selector, StrolchElementVisitor<T, U> elementVisitor) {
public InMemoryQuery(Navigator<T> navigator, Selector<T> selector, StrolchElementVisitor<T, U> elementVisitor,
Comparator<T> comparator) {
this.navigator = navigator;
this.selector = selector;
this.elementVisitor = elementVisitor;
this.comparator = comparator;
}
/**
@ -73,8 +77,11 @@ public class InMemoryQuery<T extends StrolchElement, U> {
if (this.selector == null)
return Collections.emptyList();
List<U> result = new ArrayList<U>();
List<T> elements = this.navigator.navigate(dao);
if (this.comparator != null)
elements.sort(this.comparator);
List<U> result = new ArrayList<U>();
Iterator<T> iter = elements.iterator();
while (iter.hasNext()) {
T element = iter.next();

View File

@ -17,6 +17,7 @@ package li.strolch.runtime.query.inmemory;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import li.strolch.model.GroupedParameterizedElement;
@ -44,6 +45,10 @@ import li.strolch.model.query.ParameterSelection.StringParameterSelection;
import li.strolch.model.query.ParameterSelectionVisitor;
import li.strolch.model.query.Selection;
import li.strolch.model.query.StrolchRootElementSelectionVisitor;
import li.strolch.model.query.ordering.OrderById;
import li.strolch.model.query.ordering.OrderByName;
import li.strolch.model.query.ordering.OrderByParameter;
import li.strolch.model.query.ordering.StrolchQueryOrderingVisitor;
import li.strolch.persistence.api.StrolchDao;
import li.strolch.runtime.query.inmemory.ParameterBagSelector.NullParameterBagSelector;
import li.strolch.runtime.query.inmemory.ParameterSelector.StringParameterSelector;
@ -53,8 +58,9 @@ import ch.eitchnet.utils.dbc.DBC;
* @author Robert von Burg <eitch@eitchnet.ch>
*/
public abstract class InMemoryQueryVisitor<T extends GroupedParameterizedElement, S extends StrolchDao<?>> implements
StrolchRootElementSelectionVisitor, ParameterSelectionVisitor {
StrolchRootElementSelectionVisitor, ParameterSelectionVisitor, StrolchQueryOrderingVisitor {
private Comparator<T> comparator;
private Navigator<T> navigator;
private List<Selector<T>> selectors;
private boolean any;
@ -71,6 +77,10 @@ public abstract class InMemoryQueryVisitor<T extends GroupedParameterizedElement
return this.navigator;
}
public Comparator<T> getComparator() {
return this.comparator;
}
/**
* @param navigator
*/
@ -242,4 +252,22 @@ public abstract class InMemoryQueryVisitor<T extends GroupedParameterizedElement
public void visit(ParameterBagSelection selection) {
addSelector(new ParameterBagSelector<T>(selection.getBagKey()));
}
@Override
public InMemoryQueryVisitor<T, S> visit(OrderById ordering) {
this.comparator = new InMemoryStrolchQueryOrderingVisitor<T>().visit(ordering).getComparator();
return this;
}
@Override
public InMemoryQueryVisitor<T, S> visit(OrderByName ordering) {
this.comparator = new InMemoryStrolchQueryOrderingVisitor<T>().visit(ordering).getComparator();
return this;
}
@Override
public InMemoryQueryVisitor<T, S> visit(OrderByParameter ordering) {
this.comparator = new InMemoryStrolchQueryOrderingVisitor<T>().visit(ordering).getComparator();
return this;
}
}

View File

@ -40,8 +40,10 @@ public class InMemoryResourceQueryVisitor extends InMemoryQueryVisitor<Resource,
return new InMemoryResourceQueryVisitor();
}
public <U> InMemoryQuery<Resource, U> visit(ResourceQuery resourceQuery, ResourceVisitor<U> resourceVisitor) {
public <U> InMemoryQuery<Resource, U> visit(ResourceQuery<U> resourceQuery) {
ResourceVisitor<U> resourceVisitor = resourceQuery.getResourceVisitor();
DBC.PRE.assertNotNull("ResourceVisitor may not be null!", resourceVisitor); //$NON-NLS-1$
resourceQuery.accept(this);
Navigator<Resource> navigator = getNavigator();
@ -52,10 +54,10 @@ public class InMemoryResourceQueryVisitor extends InMemoryQueryVisitor<Resource,
List<Selector<Resource>> selectors = getSelectors();
if (selectors.isEmpty())
return new InMemoryQuery<>(navigator, null, resourceVisitor);
return new InMemoryQuery<>(navigator, null, resourceVisitor, getComparator());
DBC.INTERIM.assertTrue("Invalid query as it may only contain one selector!", selectors.size() == 1); //$NON-NLS-1$
return new InMemoryQuery<>(navigator, selectors.get(0), resourceVisitor);
return new InMemoryQuery<>(navigator, selectors.get(0), resourceVisitor, getComparator());
}
@Override

View File

@ -0,0 +1,58 @@
/*
* Copyright 2015 Robert von Burg <eitch@eitchnet.ch>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package li.strolch.runtime.query.inmemory;
import java.util.Comparator;
import li.strolch.model.GroupedParameterizedElement;
import li.strolch.model.query.ordering.ByIdComparator;
import li.strolch.model.query.ordering.ByNameComparator;
import li.strolch.model.query.ordering.ByParamComparator;
import li.strolch.model.query.ordering.OrderById;
import li.strolch.model.query.ordering.OrderByName;
import li.strolch.model.query.ordering.OrderByParameter;
import li.strolch.model.query.ordering.StrolchQueryOrderingVisitor;
/**
* @author Robert von Burg <eitch@eitchnet.ch>
*/
public class InMemoryStrolchQueryOrderingVisitor<T extends GroupedParameterizedElement> implements
StrolchQueryOrderingVisitor {
private Comparator<T> comparator;
public Comparator<T> getComparator() {
return this.comparator;
}
@Override
public InMemoryStrolchQueryOrderingVisitor<T> visit(OrderById ordering) {
this.comparator = new ByIdComparator<>(ordering.isAscending());
return this;
}
@Override
public InMemoryStrolchQueryOrderingVisitor<T> visit(OrderByName ordering) {
this.comparator = new ByNameComparator<>(ordering.isAscending());
return this;
}
@Override
public InMemoryStrolchQueryOrderingVisitor<T> visit(OrderByParameter ordering) {
this.comparator = new ByParamComparator<>(ordering.getBagKey(), ordering.getParamKey(), ordering.isAscending());
return this;
}
}

View File

@ -31,6 +31,7 @@ import li.strolch.model.Tags;
import li.strolch.model.audit.AccessType;
import li.strolch.model.audit.Audit;
import li.strolch.model.audit.AuditQuery;
import li.strolch.model.audit.AuditVisitor;
import li.strolch.model.audit.NoStrategyAuditVisitor;
import li.strolch.persistence.inmemory.InMemoryAuditDao;
@ -72,97 +73,104 @@ public class AuditQueryTest {
@Test
public void shouldQueryTypeAndDateRange() throws SQLException {
AuditQuery query = new AuditQuery(Tags.AUDIT, new DateRange().from(earlier, true).to(later, true));
AuditVisitor<Audit> visitor = new NoStrategyAuditVisitor();
AuditQuery<Audit> query = new AuditQuery<>(visitor, Tags.AUDIT, new DateRange().from(earlier, true).to(later,
true));
performQuery(query, Arrays.asList(0L, 1L, 2L, 3L, 4L));
query = new AuditQuery(Tags.AUDIT, new DateRange().from(current, true).to(current, true));
query = new AuditQuery<>(visitor, Tags.AUDIT, new DateRange().from(current, true).to(current, true));
performQuery(query, Arrays.asList(1L, 3L, 4L));
query = new AuditQuery(Tags.AUDIT, new DateRange().from(current, true));
query = new AuditQuery<>(visitor, Tags.AUDIT, new DateRange().from(current, true));
performQuery(query, Arrays.asList(1L, 2L, 3L, 4L));
query = new AuditQuery(Tags.AUDIT, new DateRange().to(current, true));
query = new AuditQuery<>(visitor, Tags.AUDIT, new DateRange().to(current, true));
performQuery(query, Arrays.asList(0L, 1L, 3L, 4L));
query = new AuditQuery(Tags.RESOURCE, new DateRange().from(past, true).to(future, true));
query = new AuditQuery<>(visitor, Tags.RESOURCE, new DateRange().from(past, true).to(future, true));
performQuery(query, Arrays.<Long> asList());
}
@Test
public void shouldQueryAudits() throws SQLException {
AuditQuery query = new AuditQuery(Tags.AUDIT, new DateRange().from(past, true).to(future, true));
AuditVisitor<Audit> visitor = new NoStrategyAuditVisitor();
AuditQuery<Audit> query = new AuditQuery<>(visitor, Tags.AUDIT, new DateRange().from(past, true).to(future,
true));
query.action().accessTypes(AccessType.CREATE, AccessType.READ);
performQuery(query, Arrays.asList(0L, 1L, 4L));
query = new AuditQuery(Tags.AUDIT, new DateRange().from(past, true).to(future, true));
query = new AuditQuery<>(visitor, Tags.AUDIT, new DateRange().from(past, true).to(future, true));
query.action().accessTypes(AccessType.CREATE);
performQuery(query, Arrays.asList(0L, 4L));
query = new AuditQuery(Tags.AUDIT, new DateRange().from(past, true).to(future, true));
query = new AuditQuery<>(visitor, Tags.AUDIT, new DateRange().from(past, true).to(future, true));
query.action().accessTypes(AccessType.CREATE, AccessType.READ)
.actions(StringMatchMode.EQUALS_CASE_SENSITIVE, "create", "read");
performQuery(query, Arrays.asList(0L, 1L, 4L));
query = new AuditQuery(Tags.AUDIT, new DateRange().from(past, true).to(future, true));
query = new AuditQuery<>(visitor, Tags.AUDIT, new DateRange().from(past, true).to(future, true));
query.action().accessTypes(AccessType.CREATE, AccessType.READ)
.actions(StringMatchMode.EQUALS_CASE_SENSITIVE, "read");
performQuery(query, Arrays.asList(1L));
query = new AuditQuery(Tags.AUDIT, new DateRange().from(past, true).to(future, true));
query = new AuditQuery<>(visitor, Tags.AUDIT, new DateRange().from(past, true).to(future, true));
query.element().elementAccessed(StringMatchMode.CONTAINS_CASE_INSENSITIVE, "crea");
performQuery(query, Arrays.asList(0L, 4L));
query = new AuditQuery(Tags.AUDIT, new DateRange().from(past, true).to(future, true));
query = new AuditQuery<>(visitor, Tags.AUDIT, new DateRange().from(past, true).to(future, true));
query.element().elementAccessed(StringMatchMode.CONTAINS_CASE_SENSITIVE, "crea");
performQuery(query, Arrays.<Long> asList());
query = new AuditQuery(Tags.AUDIT, new DateRange().from(past, true).to(future, true));
query = new AuditQuery<>(visitor, Tags.AUDIT, new DateRange().from(past, true).to(future, true));
query.element().elementAccessed(StringMatchMode.EQUALS_CASE_INSENSITIVE, "create");
performQuery(query, Arrays.asList(0L, 4L));
query = new AuditQuery(Tags.AUDIT, new DateRange().from(past, true).to(future, true));
query = new AuditQuery<>(visitor, Tags.AUDIT, new DateRange().from(past, true).to(future, true));
query.identity().usernames(StringMatchMode.EQUALS_CASE_INSENSITIVE, "earlier");
performQuery(query, Arrays.asList(0L));
query = new AuditQuery(Tags.AUDIT, new DateRange().from(past, true).to(future, true));
query = new AuditQuery<>(visitor, Tags.AUDIT, new DateRange().from(past, true).to(future, true));
query.identity().usernames(StringMatchMode.EQUALS_CASE_INSENSITIVE, "earlier", "later");
performQuery(query, Arrays.asList(0L, 2L));
query = new AuditQuery(Tags.AUDIT, new DateRange().from(past, true).to(future, true));
query = new AuditQuery<>(visitor, Tags.AUDIT, new DateRange().from(past, true).to(future, true));
query.identity().usernames(StringMatchMode.EQUALS_CASE_INSENSITIVE, "earlier")
.firstnames(StringMatchMode.CONTAINS_CASE_INSENSITIVE, "enn");
performQuery(query, Arrays.asList(0L));
query = new AuditQuery(Tags.AUDIT, new DateRange().from(past, true).to(future, true));
query = new AuditQuery<>(visitor, Tags.AUDIT, new DateRange().from(past, true).to(future, true));
query.identity().usernames(StringMatchMode.EQUALS_CASE_INSENSITIVE, "earlier")
.firstnames(StringMatchMode.CONTAINS_CASE_INSENSITIVE, "enn")
.lastnames(StringMatchMode.CONTAINS_CASE_INSENSITIVE, "kennedy");
performQuery(query, Arrays.asList(0L));
query = new AuditQuery(Tags.AUDIT, new DateRange().from(past, true).to(future, true));
query = new AuditQuery<>(visitor, Tags.AUDIT, new DateRange().from(past, true).to(future, true));
query.identity().firstnames(StringMatchMode.CONTAINS_CASE_INSENSITIVE, "enn")
.lastnames(StringMatchMode.CONTAINS_CASE_INSENSITIVE, "kennedy");
performQuery(query, Arrays.asList(0L, 1L, 2L, 3L, 4L));
query = new AuditQuery(Tags.AUDIT, new DateRange().from(past, true).to(future, true));
query = new AuditQuery<>(visitor, Tags.AUDIT, new DateRange().from(past, true).to(future, true));
query.element().elementSubTypes(StringMatchMode.EQUALS_CASE_SENSITIVE, "Foo");
performQuery(query, Arrays.asList(0L, 1L, 2L, 3L, 4L));
query = new AuditQuery(Tags.AUDIT, new DateRange().from(past, true).to(future, true));
query = new AuditQuery<>(visitor, Tags.AUDIT, new DateRange().from(past, true).to(future, true));
query.element().elementSubTypes(StringMatchMode.EQUALS_CASE_SENSITIVE, "Bar");
performQuery(query, Arrays.asList());
query = new AuditQuery(Tags.AUDIT, new DateRange().from(past, true).to(future, true));
query = new AuditQuery<>(visitor, Tags.AUDIT, new DateRange().from(past, true).to(future, true));
query.limit(1).element().elementSubTypes(StringMatchMode.EQUALS_CASE_SENSITIVE, "Foo");
performQuery(query, Arrays.asList(2L));
}
private void performQuery(AuditQuery query, List<Long> expected) throws SQLException {
private void performQuery(AuditQuery<Audit> query, List<Long> expected) throws SQLException {
InMemoryAuditDao dao = new InMemoryAuditDao();
dao.saveAll(getAudits());
List<Audit> result = dao.doQuery(query, new NoStrategyAuditVisitor());
List<Audit> result = dao.doQuery(query);
Set<Long> ids = new HashSet<>();
for (Audit audit : result) {
ids.add(audit.getId());

View File

@ -48,9 +48,6 @@ import li.strolch.model.query.NameSelection;
import li.strolch.model.query.OrderQuery;
import li.strolch.model.query.ParameterSelection;
import li.strolch.model.query.ResourceQuery;
import li.strolch.model.query.StrolchTypeNavigation;
import li.strolch.model.visitor.NoStrategyOrderVisitor;
import li.strolch.model.visitor.NoStrategyResourceVisitor;
import li.strolch.persistence.inmemory.InMemoryOrderDao;
import li.strolch.persistence.inmemory.InMemoryResourceDao;
@ -69,9 +66,9 @@ public class InMemoryQueryTest {
InMemoryOrderDao dao = new InMemoryOrderDao();
dao.saveAll(orders);
OrderQuery orderQuery = new OrderQuery(new StrolchTypeNavigation("MyType1"));
OrderQuery<Order> orderQuery = OrderQuery.query("MyType1");
orderQuery.with(new IdSelection("@1"));
List<Order> result = dao.doQuery(orderQuery, new NoStrategyOrderVisitor());
List<Order> result = dao.doQuery(orderQuery);
assertEquals(1, result.size());
assertEquals("@1", result.get(0).getId());
}
@ -83,10 +80,10 @@ public class InMemoryQueryTest {
InMemoryResourceDao dao = new InMemoryResourceDao();
dao.saveAll(resources);
ResourceQuery resourceQuery = new ResourceQuery(new StrolchTypeNavigation("MyType1"));
ResourceQuery<Resource> resourceQuery = ResourceQuery.query("MyType1");
resourceQuery.with(new IdSelection("@1"));
List<Resource> result = dao.doQuery(resourceQuery, new NoStrategyResourceVisitor());
List<Resource> result = dao.doQuery(resourceQuery);
assertEquals(1, result.size());
assertEquals("@1", result.get(0).getId());
}
@ -98,10 +95,10 @@ public class InMemoryQueryTest {
InMemoryResourceDao dao = new InMemoryResourceDao();
dao.saveAll(resources);
ResourceQuery resourceQuery = new ResourceQuery(new StrolchTypeNavigation("MyType2"));
ResourceQuery<Resource> resourceQuery = ResourceQuery.query("MyType2");
resourceQuery.or().with(new IdSelection("@3"), new IdSelection("@4"));
List<Resource> result = dao.doQuery(resourceQuery, new NoStrategyResourceVisitor());
List<Resource> result = dao.doQuery(resourceQuery);
assertEquals(2, result.size());
assertEquals("@3", result.get(0).getId());
assertEquals("@4", result.get(1).getId());
@ -114,10 +111,10 @@ public class InMemoryQueryTest {
InMemoryResourceDao dao = new InMemoryResourceDao();
dao.saveAll(resources);
ResourceQuery resourceQuery = new ResourceQuery(new StrolchTypeNavigation("MyType2"));
ResourceQuery<Resource> resourceQuery = ResourceQuery.query("MyType2");
resourceQuery.and().with(new IdSelection("@3"), new NameSelection("Res 3", es()));
List<Resource> result = dao.doQuery(resourceQuery, new NoStrategyResourceVisitor());
List<Resource> result = dao.doQuery(resourceQuery);
assertEquals(1, result.size());
assertEquals("@3", result.get(0).getId());
}
@ -129,10 +126,10 @@ public class InMemoryQueryTest {
InMemoryResourceDao dao = new InMemoryResourceDao();
dao.saveAll(resources);
ResourceQuery resourceQuery = new ResourceQuery(new StrolchTypeNavigation("MyType1"));
ResourceQuery<Resource> resourceQuery = ResourceQuery.query("MyType1");
resourceQuery.and().with(new IdSelection("@3"), new NameSelection("@4", es()));
List<Resource> result = dao.doQuery(resourceQuery, new NoStrategyResourceVisitor());
List<Resource> result = dao.doQuery(resourceQuery);
assertEquals(0, result.size());
}
@ -144,13 +141,13 @@ public class InMemoryQueryTest {
InMemoryResourceDao dao = new InMemoryResourceDao();
dao.saveAll(resources);
ResourceQuery ballQuery = new ResourceQuery(new StrolchTypeNavigation("Ball"));
ResourceQuery<Resource> ballQuery = ResourceQuery.query("Ball");
ballQuery.and().with(
//
stringSelection("parameters", "color", "red", es()),
booleanSelection("parameters", "forChildren", true), floatSelection("parameters", "diameter", 22.0));
List<Resource> result = dao.doQuery(ballQuery, new NoStrategyResourceVisitor());
List<Resource> result = dao.doQuery(ballQuery);
assertEquals(1, result.size());
}
@ -162,78 +159,78 @@ public class InMemoryQueryTest {
InMemoryResourceDao dao = new InMemoryResourceDao();
dao.saveAll(resources);
ResourceQuery ballQuery;
ResourceQuery<Resource> ballQuery;
List<Resource> result;
// string list
{
ballQuery = new ResourceQuery(new StrolchTypeNavigation("Ball"));
ballQuery = ResourceQuery.query("Ball");
ballQuery.and().with(stringListSelection("parameters", "stringListValues", Arrays.asList("a", "z")));
result = dao.doQuery(ballQuery, new NoStrategyResourceVisitor());
result = dao.doQuery(ballQuery);
assertEquals(0, result.size());
ballQuery = new ResourceQuery(new StrolchTypeNavigation("Ball"));
ballQuery = ResourceQuery.query("Ball");
ballQuery.and().with(stringListSelection("parameters", "stringListValues", Arrays.asList("a")));
result = dao.doQuery(ballQuery, new NoStrategyResourceVisitor());
result = dao.doQuery(ballQuery);
assertEquals(1, result.size());
ballQuery = new ResourceQuery(new StrolchTypeNavigation("Ball"));
ballQuery = ResourceQuery.query("Ball");
ballQuery.and().with(stringListSelection("parameters", "stringListValues", Arrays.asList("c", "b", "a")));
result = dao.doQuery(ballQuery, new NoStrategyResourceVisitor());
result = dao.doQuery(ballQuery);
assertEquals(1, result.size());
}
// integer list
{
ballQuery = new ResourceQuery(new StrolchTypeNavigation("Ball"));
ballQuery = ResourceQuery.query("Ball");
ballQuery.and().with(integerListSelection("parameters", "intListValues", Arrays.asList(1, 5)));
result = dao.doQuery(ballQuery, new NoStrategyResourceVisitor());
result = dao.doQuery(ballQuery);
assertEquals(0, result.size());
ballQuery = new ResourceQuery(new StrolchTypeNavigation("Ball"));
ballQuery = ResourceQuery.query("Ball");
ballQuery.and().with(integerListSelection("parameters", "intListValues", Arrays.asList(1)));
result = dao.doQuery(ballQuery, new NoStrategyResourceVisitor());
result = dao.doQuery(ballQuery);
assertEquals(1, result.size());
ballQuery = new ResourceQuery(new StrolchTypeNavigation("Ball"));
ballQuery = ResourceQuery.query("Ball");
ballQuery.and().with(integerListSelection("parameters", "intListValues", Arrays.asList(3, 2, 1)));
result = dao.doQuery(ballQuery, new NoStrategyResourceVisitor());
result = dao.doQuery(ballQuery);
assertEquals(1, result.size());
}
// float list
{
ballQuery = new ResourceQuery(new StrolchTypeNavigation("Ball"));
ballQuery = ResourceQuery.query("Ball");
ballQuery.and().with(floatListSelection("parameters", "floatListValues", Arrays.asList(4.0, 8.0)));
result = dao.doQuery(ballQuery, new NoStrategyResourceVisitor());
result = dao.doQuery(ballQuery);
assertEquals(0, result.size());
ballQuery = new ResourceQuery(new StrolchTypeNavigation("Ball"));
ballQuery = ResourceQuery.query("Ball");
ballQuery.and().with(floatListSelection("parameters", "floatListValues", Arrays.asList(4.0)));
result = dao.doQuery(ballQuery, new NoStrategyResourceVisitor());
result = dao.doQuery(ballQuery);
assertEquals(1, result.size());
ballQuery = new ResourceQuery(new StrolchTypeNavigation("Ball"));
ballQuery = ResourceQuery.query("Ball");
ballQuery.and().with(floatListSelection("parameters", "floatListValues", Arrays.asList(6.2, 5.1, 4.0)));
result = dao.doQuery(ballQuery, new NoStrategyResourceVisitor());
result = dao.doQuery(ballQuery);
assertEquals(1, result.size());
}
// long list
{
ballQuery = new ResourceQuery(new StrolchTypeNavigation("Ball"));
ballQuery = ResourceQuery.query("Ball");
ballQuery.and().with(longListSelection("parameters", "longListValues", Arrays.asList(8L, 11L)));
result = dao.doQuery(ballQuery, new NoStrategyResourceVisitor());
result = dao.doQuery(ballQuery);
assertEquals(0, result.size());
ballQuery = new ResourceQuery(new StrolchTypeNavigation("Ball"));
ballQuery = ResourceQuery.query("Ball");
ballQuery.and().with(longListSelection("parameters", "longListValues", Arrays.asList(8L)));
result = dao.doQuery(ballQuery, new NoStrategyResourceVisitor());
result = dao.doQuery(ballQuery);
assertEquals(1, result.size());
ballQuery = new ResourceQuery(new StrolchTypeNavigation("Ball"));
ballQuery = ResourceQuery.query("Ball");
ballQuery.and().with(longListSelection("parameters", "longListValues", Arrays.asList(10L, 9L, 8L)));
result = dao.doQuery(ballQuery, new NoStrategyResourceVisitor());
result = dao.doQuery(ballQuery);
assertEquals(1, result.size());
}
}
@ -245,11 +242,11 @@ public class InMemoryQueryTest {
InMemoryResourceDao dao = new InMemoryResourceDao();
dao.saveAll(resources);
ResourceQuery ballQuery = new ResourceQuery(new StrolchTypeNavigation("Ball"));
ResourceQuery<Resource> ballQuery = ResourceQuery.query("Ball");
ballQuery.and().with( //
ParameterSelection.nullSelection("parameters", "color"));
List<Resource> result = dao.doQuery(ballQuery, new NoStrategyResourceVisitor());
List<Resource> result = dao.doQuery(ballQuery);
assertEquals(0, result.size());
}
@ -260,11 +257,11 @@ public class InMemoryQueryTest {
InMemoryResourceDao dao = new InMemoryResourceDao();
dao.saveAll(resources);
ResourceQuery ballQuery = new ResourceQuery(new StrolchTypeNavigation("Ball"));
ResourceQuery<Resource> ballQuery = ResourceQuery.query("Ball");
ballQuery.and().with( //
ParameterSelection.nullSelection("parameters", "weight"));
List<Resource> result = dao.doQuery(ballQuery, new NoStrategyResourceVisitor());
List<Resource> result = dao.doQuery(ballQuery);
assertEquals(1, result.size());
}
@ -275,11 +272,11 @@ public class InMemoryQueryTest {
InMemoryResourceDao dao = new InMemoryResourceDao();
dao.saveAll(resources);
ResourceQuery ballQuery = new ResourceQuery(new StrolchTypeNavigation("Ball"));
ResourceQuery<Resource> ballQuery = ResourceQuery.query("Ball");
ballQuery.and().with( //
ParameterSelection.nullSelection("parameters", "weight"));
List<Resource> result = dao.doQuery(ballQuery, new NoStrategyResourceVisitor());
List<Resource> result = dao.doQuery(ballQuery);
assertEquals(1, result.size());
}
@ -291,10 +288,10 @@ public class InMemoryQueryTest {
InMemoryResourceDao dao = new InMemoryResourceDao();
dao.saveAll(resources);
ResourceQuery ballQuery = new ResourceQuery(new StrolchTypeNavigation("Ball"));
ResourceQuery<Resource> ballQuery = ResourceQuery.query("Ball");
ballQuery.with(new NameSelection("ball ", ci()));
List<Resource> result = dao.doQuery(ballQuery, new NoStrategyResourceVisitor());
List<Resource> result = dao.doQuery(ballQuery);
assertEquals(1, result.size());
}

View File

@ -35,8 +35,6 @@ import li.strolch.model.query.OrderQuery;
import li.strolch.model.query.ParameterSelection;
import li.strolch.model.query.ResourceQuery;
import li.strolch.model.query.Selection;
import li.strolch.model.visitor.NoStrategyOrderVisitor;
import li.strolch.model.visitor.NoStrategyResourceVisitor;
import li.strolch.persistence.api.StrolchTransaction;
import li.strolch.runtime.StrolchConstants;
@ -75,14 +73,14 @@ public class QueryTest {
tx.commitOnClose();
}
ResourceQuery query = ResourceQuery.query("MyType");
ResourceQuery<Resource> query = ResourceQuery.query("MyType");
List<Selection> elementAndSelections = new ArrayList<>();
elementAndSelections.add(new IdSelection("@1"));
elementAndSelections.add(ParameterSelection.integerSelection(BAG_ID, "nbOfBooks", 33));
query.and().with(elementAndSelections);
InMemoryResourceQueryVisitor resourceQuery = new InMemoryResourceQueryVisitor();
InMemoryQuery<Resource, Resource> inMemoryQuery = resourceQuery.visit(query, new NoStrategyResourceVisitor());
InMemoryQuery<Resource, Resource> inMemoryQuery = resourceQuery.visit(query);
List<Resource> result;
try (StrolchTransaction tx = container.getRealm(StrolchConstants.DEFAULT_REALM).openTx(certificate, "test")) {
result = inMemoryQuery.doQuery(tx.getPersistenceHandler().getResourceDao(tx));
@ -108,14 +106,14 @@ public class QueryTest {
tx.commitOnClose();
}
OrderQuery query = OrderQuery.query("MyType");
OrderQuery<Order> query = OrderQuery.query("MyType");
List<Selection> elementAndSelections = new ArrayList<>();
elementAndSelections.add(new IdSelection("@1"));
elementAndSelections.add(ParameterSelection.integerSelection(BAG_ID, "nbOfBooks", 33));
query.and().with(elementAndSelections);
InMemoryOrderQueryVisitor orderQuery = new InMemoryOrderQueryVisitor();
InMemoryQuery<Order, Order> inMemoryQuery = orderQuery.visit(query, new NoStrategyOrderVisitor());
InMemoryQuery<Order, Order> inMemoryQuery = orderQuery.visit(query);
List<Order> result;
try (StrolchTransaction tx = container.getRealm(StrolchConstants.DEFAULT_REALM).openTx(certificate, "test")) {
result = inMemoryQuery.doQuery(tx.getPersistenceHandler().getOrderDao(tx));
@ -139,7 +137,7 @@ public class QueryTest {
tx.commitOnClose();
}
ResourceQuery query = ResourceQuery.query("MyType");
ResourceQuery<Resource> query = ResourceQuery.query("MyType");
query.and().with(
ParameterSelection.stringSelection(BAG_ID, PARAM_STRING_ID, "olch",
StringMatchMode.CONTAINS_CASE_SENSITIVE));
@ -166,7 +164,7 @@ public class QueryTest {
tx.commitOnClose();
}
ResourceQuery query = ResourceQuery.query("MyType");
ResourceQuery<Resource> query = ResourceQuery.query("MyType");
query.and().with(
ParameterSelection.stringSelection(BAG_ID, PARAM_STRING_ID, "str",
StringMatchMode.CONTAINS_CASE_SENSITIVE));
@ -192,7 +190,7 @@ public class QueryTest {
tx.commitOnClose();
}
ResourceQuery query = ResourceQuery.query("MyType");
ResourceQuery<Resource> query = ResourceQuery.query("MyType");
query.and().with(
ParameterSelection.stringSelection(BAG_ID, PARAM_STRING_ID, "strolch",
StringMatchMode.EQUALS_CASE_INSENSITIVE));
@ -219,7 +217,7 @@ public class QueryTest {
tx.commitOnClose();
}
ResourceQuery query = ResourceQuery.query("MyType");
ResourceQuery<Resource> query = ResourceQuery.query("MyType");
query.and().with(
ParameterSelection.stringSelection(BAG_ID, PARAM_STRING_ID, "strolch",
StringMatchMode.EQUALS_CASE_SENSITIVE));
@ -248,7 +246,7 @@ public class QueryTest {
}
{
ResourceQuery query = ResourceQuery.query("MyType");
ResourceQuery<Resource> query = ResourceQuery.query("MyType");
query.not(new IdSelection("@1"));
List<Resource> result;
try (StrolchTransaction tx = container.getRealm(StrolchConstants.DEFAULT_REALM).openTx(certificate, "test")) {
@ -259,7 +257,7 @@ public class QueryTest {
}
{
ResourceQuery query = ResourceQuery.query("MyType");
ResourceQuery<Resource> query = ResourceQuery.query("MyType");
query.not(new IdSelection("@2"));
List<Resource> result;
try (StrolchTransaction tx = container.getRealm(StrolchConstants.DEFAULT_REALM).openTx(certificate, "test")) {
@ -270,7 +268,7 @@ public class QueryTest {
}
{
ResourceQuery query = ResourceQuery.query("MyType");
ResourceQuery<Resource> query = ResourceQuery.query("MyType");
query.not(new IdSelection("@1", "@2"));
List<Resource> result;
try (StrolchTransaction tx = container.getRealm(StrolchConstants.DEFAULT_REALM).openTx(certificate, "test")) {

View File

@ -26,7 +26,7 @@ public class ActionSelection extends AuditSelection {
private StringSelection actionSelection;
private AccessType[] accessTypes;
public ActionSelection(AuditQuery query) {
public ActionSelection(AuditQuery<?> query) {
super(query);
}

View File

@ -23,23 +23,43 @@ import ch.eitchnet.utils.collections.DateRange;
import ch.eitchnet.utils.dbc.DBC;
/**
*
* <p>
* The {@link AuditVisitor} is intended for situations where the query result should not be {@link Audit} but some other
* object type. For instance in a restful API, the result might have to be mapped to a POJO, thus using this method can
* perform the mapping step for you
* </p>
*
* @author Robert von Burg <eitch@eitchnet.ch>
*/
public class AuditQuery implements StrolchQuery {
public class AuditQuery<U> implements StrolchQuery {
private String elementTypeSelection;
private List<AuditSelection> selections;
private DateRange dateRange;
private long limit;
protected String elementTypeSelection;
protected List<AuditSelection> selections;
protected DateRange dateRange;
protected long limit;
protected AuditVisitor<U> auditVisitor;
public AuditQuery(String elementTypeSelection, DateRange dateRange) {
public AuditQuery(AuditVisitor<U> auditVisitor, String elementTypeSelection, DateRange dateRange) {
DBC.PRE.assertNotNull("auditVisitor", auditVisitor);
DBC.PRE.assertNotEmpty("No elementTypeSelection (navigation) set!", elementTypeSelection); //$NON-NLS-1$
DBC.PRE.assertFalse("dateRange may not be unbounded!", dateRange.isUnbounded());
this.auditVisitor = auditVisitor;
this.elementTypeSelection = elementTypeSelection;
this.dateRange = dateRange;
this.selections = new ArrayList<>();
}
public AuditVisitor<U> getAuditVisitor() {
return this.auditVisitor;
}
public AuditQuery<U> setAuditVisitor(AuditVisitor<U> auditVisitor) {
DBC.PRE.assertNotNull("auditVisitor", auditVisitor);
this.auditVisitor = auditVisitor;
return this;
}
public String getElementTypeSelection() {
return this.elementTypeSelection;
}
@ -52,7 +72,7 @@ public class AuditQuery implements StrolchQuery {
return limit;
}
public AuditQuery limit(long limit) {
public AuditQuery<U> limit(long limit) {
this.limit = limit;
return this;
}
@ -97,4 +117,12 @@ public class AuditQuery implements StrolchQuery {
public Object getPrivilegeValue() {
return getClass().getName();
}
public static AuditQuery<Audit> query(String elementTypeSelection, DateRange dateRange) {
return new AuditQuery<Audit>(new NoStrategyAuditVisitor(), elementTypeSelection, dateRange);
}
public static <U> AuditQuery<U> query(String elementTypeSelection, DateRange dateRange, AuditVisitor<U> orderVisitor) {
return new AuditQuery<U>(orderVisitor, elementTypeSelection, dateRange);
}
}

View File

@ -26,5 +26,5 @@ public interface AuditQueryVisitor {
public void visit(ActionSelection selection);
public void visit(AuditQuery auditQuery);
public void visit(AuditQuery<?> auditQuery);
}

View File

@ -20,14 +20,14 @@ package li.strolch.model.audit;
*/
public abstract class AuditSelection {
private AuditQuery query;
private AuditQuery<?> query;
public AuditSelection(AuditQuery query) {
public AuditSelection(AuditQuery<?> query) {
super();
this.query = query;
}
public AuditQuery query() {
public AuditQuery<?> query() {
return this.query;
}

View File

@ -26,7 +26,7 @@ public class ElementSelection extends AuditSelection {
private StringSelection elementSubTypeSelection;
private StringSelection elementAccessedSelection;
public ElementSelection(AuditQuery query) {
public ElementSelection(AuditQuery<?> query) {
super(query);
}

View File

@ -27,7 +27,7 @@ public class IdentitySelection extends AuditSelection {
private StringSelection firstnameSelection;
private StringSelection lastnameSelection;
public IdentitySelection(AuditQuery query) {
public IdentitySelection(AuditQuery<?> query) {
super(query);
}

View File

@ -18,49 +18,101 @@ package li.strolch.model.query;
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>
* {@link ActivityQuery} is the user API to query {@link Activity Activities} in Strolch. The {@link Navigation} is used
* to navigate to a type of activity on which any further {@link Selection Selections} will be performed. The
* {@link ActivityVisitor} is used to transform the returned object into a domain specific object (if required). This
* mechanism allows you to query e.g. a specific {@link Action} instead of having to return all the elements and then
* performing this transformation.
* </p>
*
* <p>
* The {@link ActivityVisitor} is intended for situations where the query result should not be {@link Activity} but some
* other object type. For instance in a restful API, the result might have to be mapped to a POJO, thus using this
* method can perform the mapping step for you
* </p>
*
* @param <U>
* defines the return type of this query. Depending on the user {@link ActivityVisitor} this query can return
* an {@link Activity}, or any type of object to which the visitor mapped the activity
*
* @author Robert von Burg <eitch@eitchnet.ch>
*/
public class ActivityQuery extends StrolchElementQuery<ActivityQueryVisitor> {
public class ActivityQuery<U> extends StrolchElementQuery<ActivityQueryVisitor> {
protected ActivityVisitor<U> activityVisitor;
protected StrolchQueryOrdering ordering;
/**
* @param navigation
* @param elementVisitor
*/
public ActivityQuery(Navigation navigation) {
super(navigation);
}
public ActivityVisitor<U> getActivityVisitor() {
return this.activityVisitor;
}
public ActivityQuery<U> setActivityVisitor(ActivityVisitor<U> activityVisitor) {
DBC.PRE.assertNotNull("activityVisitor", activityVisitor);
this.activityVisitor = activityVisitor;
return this;
}
public StrolchQueryOrdering getOrdering() {
return this.ordering;
}
public ActivityQuery<U> setOrdering(StrolchQueryOrdering ordering) {
this.ordering = ordering;
return this;
}
@Override
public ActivityQuery with(Selection selection) {
public ActivityQuery<U> with(Selection selection) {
super.with(selection);
return this;
}
@Override
public ActivityQuery not(Selection selection) {
public ActivityQuery<U> not(Selection selection) {
super.not(selection);
return this;
}
@Override
public ActivityQuery withAny() {
public ActivityQuery<U> withAny() {
super.withAny();
return this;
}
public static ActivityQuery query(Navigation navigation) {
return new ActivityQuery(navigation);
@Override
public void accept(ActivityQueryVisitor visitor) {
super.accept(visitor);
if (this.ordering != null)
this.ordering.accept(visitor);
}
public static ActivityQuery query(String type) {
return new ActivityQuery(new StrolchTypeNavigation(type));
public static ActivityQuery<Activity> query(String type) {
return new ActivityQuery<Activity>(new StrolchTypeNavigation(type))
.setActivityVisitor(new NoStrategyActivityVisitor());
}
public static ActivityQuery<Activity> query(String type, StrolchQueryOrdering ordering) {
return new ActivityQuery<Activity>(new StrolchTypeNavigation(type)).setActivityVisitor(
new NoStrategyActivityVisitor()).setOrdering(ordering);
}
public static <U> ActivityQuery<U> query(String type, ActivityVisitor<U> activityVisitor) {
return new ActivityQuery<U>(new StrolchTypeNavigation(type)).setActivityVisitor(activityVisitor);
}
public static <U> ActivityQuery<U> query(String type, ActivityVisitor<U> activityVisitor,
StrolchQueryOrdering ordering) {
return new ActivityQuery<U>(new StrolchTypeNavigation(type)).setActivityVisitor(activityVisitor).setOrdering(
ordering);
}
}

View File

@ -15,10 +15,13 @@
*/
package li.strolch.model.query;
import li.strolch.model.query.ordering.StrolchQueryOrderingVisitor;
/**
* @author Robert von Burg <eitch@eitchnet.ch>
*/
public interface ActivityQueryVisitor extends StrolchRootElementSelectionVisitor, ParameterSelectionVisitor {
public interface ActivityQueryVisitor extends StrolchRootElementSelectionVisitor, ParameterSelectionVisitor,
StrolchQueryOrderingVisitor {
// marker interface
}

View File

@ -18,45 +18,99 @@ package li.strolch.model.query;
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>
* {@link OrderQuery} is the user API to query {@link Order Orders} in Strolch. The {@link Navigation} is used to
* navigate to a type of order on which any further {@link Selection Selections} will be performed. The
* {@link OrderVisitor} is used to transform the returned object into a domain specific object (if required). This
* mechanism allows you to query only the values of a {@link Parameter} instead of having to return all the elements and
* then performing this transformation.
* </p>
*
* <p>
* The {@link OrderVisitor} is intended for situations where the query result should not be {@link Order} but some other
* object type. For instance in a restful API, the result might have to be mapped to a POJO, thus using this method can
* perform the mapping step for you
* </p>
*
* @param <U>
* defines the return type of this query. Depending on the user {@link OrderVisitor} this query can return an
* {@link Order}, or any type of object to which the visitor mapped the order
*
* @author Robert von Burg <eitch@eitchnet.ch>
*/
public class OrderQuery extends StrolchElementQuery<OrderQueryVisitor> {
public class OrderQuery<U> extends StrolchElementQuery<OrderQueryVisitor> {
protected OrderVisitor<U> orderVisitor;
protected StrolchQueryOrdering ordering;
public OrderQuery(Navigation navigation) {
super(navigation);
}
public OrderVisitor<U> getOrderVisitor() {
return this.orderVisitor;
}
public OrderQuery<U> setOrderVisitor(OrderVisitor<U> orderVisitor) {
DBC.PRE.assertNotNull("orderVisitor", orderVisitor);
this.orderVisitor = orderVisitor;
return this;
}
public StrolchQueryOrdering getOrdering() {
return this.ordering;
}
public OrderQuery<U> setOrdering(StrolchQueryOrdering ordering) {
this.ordering = ordering;
return this;
}
@Override
public OrderQuery with(Selection selection) {
public OrderQuery<U> with(Selection selection) {
super.with(selection);
return this;
}
@Override
public OrderQuery not(Selection selection) {
public OrderQuery<U> not(Selection selection) {
super.not(selection);
return this;
}
@Override
public OrderQuery withAny() {
public OrderQuery<U> withAny() {
super.withAny();
return this;
}
public static OrderQuery query(Navigation navigation) {
return new OrderQuery(navigation);
@Override
public void accept(OrderQueryVisitor visitor) {
DBC.PRE.assertNotNull("orderVisitor", this.orderVisitor);
super.accept(visitor);
if (this.ordering != null)
this.ordering.accept(visitor);
}
public static OrderQuery query(String type) {
return new OrderQuery(new StrolchTypeNavigation(type));
public static OrderQuery<Order> query(String type) {
return new OrderQuery<Order>(new StrolchTypeNavigation(type)).setOrderVisitor(new NoStrategyOrderVisitor());
}
public static OrderQuery<Order> query(String type, StrolchQueryOrdering ordering) {
return new OrderQuery<Order>(new StrolchTypeNavigation(type)).setOrderVisitor(new NoStrategyOrderVisitor())
.setOrdering(ordering);
}
public static <U> OrderQuery<U> query(String type, OrderVisitor<U> orderVisitor) {
return new OrderQuery<U>(new StrolchTypeNavigation(type)).setOrderVisitor(orderVisitor);
}
public static <U> OrderQuery<U> query(String type, OrderVisitor<U> orderVisitor, StrolchQueryOrdering ordering) {
return new OrderQuery<U>(new StrolchTypeNavigation(type)).setOrdering(ordering).setOrderVisitor(orderVisitor);
}
}

View File

@ -15,11 +15,13 @@
*/
package li.strolch.model.query;
import li.strolch.model.query.ordering.StrolchQueryOrderingVisitor;
/**
* @author Robert von Burg <eitch@eitchnet.ch>
*
*/
public interface OrderQueryVisitor extends OrderSelectionVisitor, ParameterSelectionVisitor {
public interface OrderQueryVisitor extends OrderSelectionVisitor, ParameterSelectionVisitor,
StrolchQueryOrderingVisitor {
// marker interface
}

View File

@ -18,49 +18,102 @@ package li.strolch.model.query;
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>
* {@link ResourceQuery} is the user API to query {@link Resource Resources} in Strolch. The {@link Navigation} is used
* to navigate to a type of resource on which any further {@link Selection Selections} will be performed. The
* {@link ResourceVisitor} is used to transform the returned object into a domain specific object (if required). This
* mechanism allows you to query only the values of a {@link Parameter} instead of having to return all the elements and
* then performing this transformation.
* </p>
*
* <p>
* The {@link ResourceVisitor} is intended for situations where the query result should not be {@link Resource} but some
* other object type. For instance in a restful API, the result might have to be mapped to a POJO, thus using this
* method can perform the mapping step for you
* </p>
*
* @param <U>
* defines the return type of this query. Depending on the user {@link ResourceVisitor} this query can return
* a {@link Resource}, or any type of object to which the visitor mapped the resource
*
* @author Robert von Burg <eitch@eitchnet.ch>
*/
public class ResourceQuery extends StrolchElementQuery<ResourceQueryVisitor> {
public class ResourceQuery<U> extends StrolchElementQuery<ResourceQueryVisitor> {
protected ResourceVisitor<U> resourceVisitor;
protected StrolchQueryOrdering ordering;
/**
* @param navigation
* @param elementVisitor
*/
public ResourceQuery(Navigation navigation) {
super(navigation);
}
public ResourceVisitor<U> getResourceVisitor() {
return this.resourceVisitor;
}
public ResourceQuery<U> setResourceVisitor(ResourceVisitor<U> resourceVisitor) {
DBC.PRE.assertNotNull("resourceVisitor", resourceVisitor);
this.resourceVisitor = resourceVisitor;
return this;
}
public StrolchQueryOrdering getOrdering() {
return this.ordering;
}
public ResourceQuery<U> setOrdering(StrolchQueryOrdering ordering) {
this.ordering = ordering;
return this;
}
@Override
public ResourceQuery with(Selection selection) {
public ResourceQuery<U> with(Selection selection) {
super.with(selection);
return this;
}
@Override
public ResourceQuery not(Selection selection) {
public ResourceQuery<U> not(Selection selection) {
super.not(selection);
return this;
}
@Override
public ResourceQuery withAny() {
public ResourceQuery<U> withAny() {
super.withAny();
return this;
}
public static ResourceQuery query(Navigation navigation) {
return new ResourceQuery(navigation);
@Override
public void accept(ResourceQueryVisitor visitor) {
DBC.PRE.assertNotNull("resourceVisitor", this.resourceVisitor);
super.accept(visitor);
if (this.ordering != null)
this.ordering.accept(visitor);
}
public static ResourceQuery query(String type) {
return new ResourceQuery(new StrolchTypeNavigation(type));
public static ResourceQuery<Resource> query(String type) {
return new ResourceQuery<Resource>(new StrolchTypeNavigation(type))
.setResourceVisitor(new NoStrategyResourceVisitor());
}
public static ResourceQuery<Resource> query(String type, StrolchQueryOrdering ordering) {
return new ResourceQuery<Resource>(new StrolchTypeNavigation(type)).setResourceVisitor(
new NoStrategyResourceVisitor()).setOrdering(ordering);
}
public static <U> ResourceQuery<U> query(String type, ResourceVisitor<U> resourceVisitor) {
return new ResourceQuery<U>(new StrolchTypeNavigation(type)).setResourceVisitor(resourceVisitor);
}
public static <U> ResourceQuery<U> query(String type, ResourceVisitor<U> resourceVisitor,
StrolchQueryOrdering ordering) {
return new ResourceQuery<U>(new StrolchTypeNavigation(type)).setResourceVisitor(resourceVisitor).setOrdering(
ordering);
}
}

View File

@ -15,10 +15,13 @@
*/
package li.strolch.model.query;
import li.strolch.model.query.ordering.StrolchQueryOrderingVisitor;
/**
* @author Robert von Burg <eitch@eitchnet.ch>
*/
public interface ResourceQueryVisitor extends StrolchRootElementSelectionVisitor, ParameterSelectionVisitor {
public interface ResourceQueryVisitor extends StrolchRootElementSelectionVisitor, ParameterSelectionVisitor,
StrolchQueryOrderingVisitor {
// marker interface
}

View File

@ -15,6 +15,8 @@
*/
package li.strolch.model.query;
import ch.eitchnet.utils.dbc.DBC;
/**
* @author Robert von Burg <eitch@eitchnet.ch>
*/
@ -23,6 +25,7 @@ public class StrolchTypeNavigation implements Navigation {
private String type;
public StrolchTypeNavigation(String type) {
DBC.PRE.assertNotEmpty("type", type);
this.type = type;
}

View File

@ -0,0 +1,25 @@
/*
* Copyright 2013 Robert von Burg <eitch@eitchnet.ch>
*/
package li.strolch.model.query.ordering;
import java.util.Comparator;
import li.strolch.model.GroupedParameterizedElement;
/**
* @author Robert von Burg <eitch@eitchnet.ch>
*/
public class ByIdComparator<T extends GroupedParameterizedElement> implements Comparator<T> {
private boolean ascending;
public ByIdComparator(boolean ascending) {
this.ascending = ascending;
}
@Override
public int compare(T o1, T o2) {
return this.ascending ? o1.getId().compareTo(o2.getId()) : o2.getId().compareTo(o1.getId());
}
}

View File

@ -0,0 +1,25 @@
/*
* Copyright 2013 Robert von Burg <eitch@eitchnet.ch>
*/
package li.strolch.model.query.ordering;
import java.util.Comparator;
import li.strolch.model.GroupedParameterizedElement;
/**
* @author Robert von Burg <eitch@eitchnet.ch>
*/
public class ByNameComparator<T extends GroupedParameterizedElement> implements Comparator<T> {
private boolean ascending;
public ByNameComparator(boolean ascending) {
this.ascending = ascending;
}
@Override
public int compare(T o1, T o2) {
return this.ascending ? o1.getName().compareTo(o2.getName()) : o2.getName().compareTo(o1.getName());
}
}

View File

@ -0,0 +1,42 @@
/*
* Copyright 2013 Robert von Burg <eitch@eitchnet.ch>
*/
package li.strolch.model.query.ordering;
import java.util.Comparator;
import li.strolch.exception.StrolchException;
import li.strolch.model.GroupedParameterizedElement;
import li.strolch.model.parameter.Parameter;
/**
* @author Robert von Burg <eitch@eitchnet.ch>
*/
public class ByParamComparator<T extends GroupedParameterizedElement> implements Comparator<T> {
private String bagKey;
private String paramKey;
private boolean ascending;
public ByParamComparator(String bagKey, String paramKey, boolean ascending) {
this.bagKey = bagKey;
this.paramKey = paramKey;
this.ascending = ascending;
}
@Override
public int compare(T o1, T o2) {
Parameter<?> param1 = o1.getParameter(bagKey, paramKey);
if (param1 == null)
throw new StrolchException("Sorting parameter bag=" + bagKey + ", param=" + paramKey
+ " does not exist on " + o1.getLocator());
Parameter<?> param2 = o2.getParameter(bagKey, paramKey);
if (param2 == null)
throw new StrolchException("Sorting parameter bag=" + bagKey + ", param=" + paramKey
+ " does not exist on " + o2.getLocator());
return this.ascending ? param1.compareTo(param2) : param2.compareTo(param1);
}
}

View File

@ -0,0 +1,44 @@
/*
* Copyright 2015 Robert von Burg <eitch@eitchnet.ch>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package li.strolch.model.query.ordering;
/**
* @author Robert von Burg <eitch@eitchnet.ch>
*/
public class OrderById extends StrolchQueryOrdering {
/**
* Creates this ordering with ascending order
*/
public OrderById() {
super(true);
}
/**
* Creates this ordering the given ascending order
*
* @param ascending
* true for ascending, false for descending order
*/
public OrderById(boolean ascending) {
super(ascending);
}
@Override
public void accept(StrolchQueryOrderingVisitor visitor) {
visitor.visit(this);
}
}

View File

@ -0,0 +1,44 @@
/*
* Copyright 2015 Robert von Burg <eitch@eitchnet.ch>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package li.strolch.model.query.ordering;
/**
* @author Robert von Burg <eitch@eitchnet.ch>
*/
public class OrderByName extends StrolchQueryOrdering {
/**
* Creates this ordering with ascending order
*/
public OrderByName() {
super(true);
}
/**
* Creates this ordering the given ascending order
*
* @param ascending
* true for ascending, false for descending order
*/
public OrderByName(boolean ascending) {
super(ascending);
}
@Override
public void accept(StrolchQueryOrderingVisitor visitor) {
visitor.visit(this);
}
}

View File

@ -0,0 +1,71 @@
/*
* Copyright 2015 Robert von Burg <eitch@eitchnet.ch>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package li.strolch.model.query.ordering;
import li.strolch.model.ParameterBag;
import li.strolch.model.parameter.Parameter;
/**
* @author Robert von Burg <eitch@eitchnet.ch>
*/
public class OrderByParameter extends StrolchQueryOrdering {
private String bagKey;
private String paramKey;
/**
* Creates this ordering with the given bagKey and paramKey with ascending order
*
* @param bagKey
* the {@link ParameterBag} in which to retrieve the {@link Parameter} for ordering
* @param paramKey
* the {@link Parameter} with which to order
*/
public OrderByParameter(String bagKey, String paramKey) {
super(true);
this.bagKey = bagKey;
this.paramKey = paramKey;
}
/**
* Creates this ordering with the given bagKey and paramKey and ascending order
*
* @param ascending
* true for ascending, false for descending order
* @param bagKey
* the {@link ParameterBag} in which to retrieve the {@link Parameter} for ordering
* @param paramKey
* the {@link Parameter} with which to order
*/
public OrderByParameter(boolean ascending, String bagKey, String paramKey) {
super(ascending);
this.bagKey = bagKey;
this.paramKey = paramKey;
}
public String getBagKey() {
return this.bagKey;
}
public String getParamKey() {
return this.paramKey;
}
@Override
public void accept(StrolchQueryOrderingVisitor visitor) {
visitor.visit(this);
}
}

View File

@ -0,0 +1,38 @@
/*
* Copyright 2015 Robert von Burg <eitch@eitchnet.ch>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package li.strolch.model.query.ordering;
/**
* @author Robert von Burg <eitch@eitchnet.ch>
*/
public abstract class StrolchQueryOrdering {
private boolean ascending;
public StrolchQueryOrdering(boolean ascending) {
this.ascending = ascending;
}
public boolean isAscending() {
return this.ascending;
}
public void setAscending(boolean ascending) {
this.ascending = ascending;
}
public abstract void accept(StrolchQueryOrderingVisitor visitor);
}

View File

@ -0,0 +1,28 @@
/*
* Copyright 2015 Robert von Burg <eitch@eitchnet.ch>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package li.strolch.model.query.ordering;
/**
* @author Robert von Burg <eitch@eitchnet.ch>
*/
public interface StrolchQueryOrderingVisitor {
public StrolchQueryOrderingVisitor visit(OrderById orderById);
public StrolchQueryOrderingVisitor visit(OrderByName orderByName);
public StrolchQueryOrderingVisitor visit(OrderByParameter orderByParameter);
}

View File

@ -81,7 +81,7 @@ public class PerformanceTestService extends AbstractService<ServiceArgument, Ser
}
private boolean run(long start) {
return System.currentTimeMillis() < start + TimeUnit.MINUTES.toMillis(5);
return System.currentTimeMillis() < start + TimeUnit.MINUTES.toMillis(1);
}
private void deleteResource(StrolchTransaction tx, Resource toDelete) {

View File

@ -30,7 +30,6 @@ import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import javax.xml.transform.sax.SAXResult;
import li.strolch.model.ActivityVisitor;
import li.strolch.model.Tags;
import li.strolch.model.activity.Activity;
import li.strolch.model.query.ActivityQuery;
@ -150,7 +149,7 @@ public class PostgreSqlActivityDao extends PostgresqlDao<Activity> implements Ac
}
@Override
public <U> List<U> doQuery(ActivityQuery query, ActivityVisitor<U> activityVisitor) {
public <U> List<U> doQuery(ActivityQuery<U> query) {
PostgreSqlActivityQueryVisitor queryVisitor = new PostgreSqlActivityQueryVisitor("id, asxml");
query.accept(queryVisitor);
@ -167,7 +166,7 @@ public class PostgreSqlActivityDao extends PostgresqlDao<Activity> implements Ac
String id = result.getString("id");
SQLXML sqlxml = result.getSQLXML("asxml");
Activity t = parseFromXml(id, queryVisitor.getType(), sqlxml);
list.add(activityVisitor.visit(t));
list.add(query.getActivityVisitor().visit(t));
}
}
} catch (SQLException e) {

View File

@ -29,7 +29,6 @@ import java.util.Set;
import li.strolch.model.audit.AccessType;
import li.strolch.model.audit.Audit;
import li.strolch.model.audit.AuditQuery;
import li.strolch.model.audit.AuditVisitor;
import li.strolch.persistence.api.AuditDao;
import li.strolch.persistence.api.StrolchPersistenceException;
import ch.eitchnet.utils.collections.DateRange;
@ -293,7 +292,7 @@ public class PostgreSqlAuditDao implements AuditDao {
}
@Override
public <U> List<U> doQuery(AuditQuery query, AuditVisitor<U> auditVisitor) {
public <U> List<U> doQuery(AuditQuery<U> query) {
PostgreSqlAuditQueryVisitor queryVisitor = new PostgreSqlAuditQueryVisitor(FIELDS);
query.accept(queryVisitor);

View File

@ -77,7 +77,7 @@ public class PostgreSqlAuditQueryVisitor implements AuditQueryVisitor {
}
@Override
public void visit(AuditQuery auditQuery) {
public void visit(AuditQuery<?> auditQuery) {
ensureAnd();
this.sb.append(this.indent);
this.sb.append(PostgreSqlAuditDao.ELEMENT_TYPE);

View File

@ -33,7 +33,6 @@ import javax.xml.parsers.SAXParserFactory;
import javax.xml.transform.sax.SAXResult;
import li.strolch.model.Order;
import li.strolch.model.OrderVisitor;
import li.strolch.model.Tags;
import li.strolch.model.query.OrderQuery;
import li.strolch.model.xml.OrderToSaxVisitor;
@ -157,7 +156,7 @@ public class PostgreSqlOrderDao extends PostgresqlDao<Order> implements OrderDao
}
@Override
public <U> List<U> doQuery(OrderQuery query, OrderVisitor<U> orderVisitor) {
public <U> List<U> doQuery(OrderQuery<U> query) {
PostgreSqlOrderQueryVisitor queryVisitor = new PostgreSqlOrderQueryVisitor("id, asxml");
query.accept(queryVisitor);
@ -174,7 +173,7 @@ public class PostgreSqlOrderDao extends PostgresqlDao<Order> implements OrderDao
String id = result.getString("id");
SQLXML sqlxml = result.getSQLXML("asxml");
Order t = parseFromXml(id, queryVisitor.getType(), sqlxml);
list.add(orderVisitor.visit(t));
list.add(query.getOrderVisitor().visit(t));
}
}
} catch (SQLException e) {

View File

@ -47,17 +47,24 @@ import li.strolch.model.query.ParameterSelectionVisitor;
import li.strolch.model.query.Selection;
import li.strolch.model.query.StrolchRootElementSelectionVisitor;
import li.strolch.model.query.StrolchTypeNavigation;
import li.strolch.model.query.ordering.OrderById;
import li.strolch.model.query.ordering.OrderByName;
import li.strolch.model.query.ordering.OrderByParameter;
import li.strolch.model.query.ordering.StrolchQueryOrderingVisitor;
import ch.eitchnet.utils.StringMatchMode;
import ch.eitchnet.utils.dbc.DBC;
import ch.eitchnet.utils.helper.StringHelper;
import ch.eitchnet.utils.iso8601.ISO8601FormatFactory;
/**
* @author Robert von Burg <eitch@eitchnet.ch>
*/
public abstract class PostgreSqlQueryVisitor implements StrolchRootElementSelectionVisitor, ParameterSelectionVisitor {
public abstract class PostgreSqlQueryVisitor implements StrolchRootElementSelectionVisitor, ParameterSelectionVisitor,
StrolchQueryOrderingVisitor {
protected StringBuilder sql;
protected StringBuilder sb;
protected String ordering;
protected String type;
protected List<Object> values;
protected boolean any;
@ -70,9 +77,9 @@ public abstract class PostgreSqlQueryVisitor implements StrolchRootElementSelect
this.sb = new StringBuilder();
this.values = new ArrayList<>();
this.sql.append("select ");
this.sql.append("SELECT ");
this.sql.append(fields);
this.sql.append("\nfrom\n");
this.sql.append("\nFROM\n");
this.sql.append(" ");
this.sql.append(getTableName());
this.indent = " ";
@ -82,22 +89,32 @@ public abstract class PostgreSqlQueryVisitor implements StrolchRootElementSelect
if (this.sqlAsString != null)
return this.sqlAsString;
this.sql.append("\nwhere\n");
this.sql.append("\nWHERE\n");
this.sql.append(this.indent);
if (this.any) {
this.sql.append("type = ?");
appendOrdering();
this.sqlAsString = this.sql.toString();
return this.sqlAsString;
}
this.sql.append("type = ? and\n");
this.sql.append("type = ? AND\n");
this.sql.append(this.sb.toString());
appendOrdering();
this.sqlAsString = this.sql.toString();
return this.sqlAsString;
}
private void appendOrdering() {
if (StringHelper.isNotEmpty(this.ordering)) {
this.sql.append("\n");
this.sql.append(this.ordering);
}
}
/**
* @return the any
*/
@ -133,7 +150,7 @@ public abstract class PostgreSqlQueryVisitor implements StrolchRootElementSelect
this.sb.append("id = ?\n");
this.values.add(ids.get(0));
} else {
this.sb.append("id in (");
this.sb.append("id IN (");
Iterator<String> iter = ids.iterator();
while (iter.hasNext()) {
String id = iter.next();
@ -172,7 +189,7 @@ public abstract class PostgreSqlQueryVisitor implements StrolchRootElementSelect
selection.accept(this);
if (iter.hasNext()) {
this.sb.append(indent);
this.sb.append("and\n");
this.sb.append("AND\n");
}
}
this.indent = indent;
@ -193,7 +210,7 @@ public abstract class PostgreSqlQueryVisitor implements StrolchRootElementSelect
selection.accept(this);
if (iter.hasNext()) {
this.sb.append(indent);
this.sb.append("or\n");
this.sb.append("OR\n");
}
}
this.indent = indent;
@ -205,7 +222,7 @@ public abstract class PostgreSqlQueryVisitor implements StrolchRootElementSelect
public void visitNot(NotSelection notSelection) {
this.sb.append(this.indent);
List<Selection> selections = notSelection.getSelections();
this.sb.append("not ( \n");
this.sb.append("NOT ( \n");
Iterator<Selection> iter = selections.iterator();
String indent = this.indent;
this.indent += " ";
@ -214,7 +231,7 @@ public abstract class PostgreSqlQueryVisitor implements StrolchRootElementSelect
selection.accept(this);
if (iter.hasNext()) {
this.sb.append(indent);
this.sb.append("and\n");
this.sb.append("AND\n");
}
}
this.indent = indent;
@ -223,7 +240,7 @@ public abstract class PostgreSqlQueryVisitor implements StrolchRootElementSelect
}
private void xpath(String bagKey, String paramKey, String paramValue) {
String xpath = "cast(xpath('//${className}/ParameterBag[@Id=\"${bagKey}\"]/Parameter[@Id=\"${paramKey}\" and @Value=\"${paramValue}\"]', asxml) as text[]) != '{}'\n";
String xpath = "CAST(XPATH('//${className}/ParameterBag[@Id=\"${bagKey}\"]/Parameter[@Id=\"${paramKey}\" and @Value=\"${paramValue}\"]', asxml) AS TEXT[]) != '{}'\n";
this.sb.append(this.indent);
xpath = xpath.replace("${className}", getClassName());
xpath = xpath.replace("${bagKey}", bagKey);
@ -236,13 +253,13 @@ public abstract class PostgreSqlQueryVisitor implements StrolchRootElementSelect
public void visit(StringParameterSelection selection) {
String value = selection.getValue();
String xpath = "xpath('//${className}/ParameterBag[@Id=\"${bagKey}\"]/Parameter[@Id=\"${paramKey}\"]/@Value', asxml))::TEXT AS content";
String xpath = "XPATH('//${className}/ParameterBag[@Id=\"${bagKey}\"]/Parameter[@Id=\"${paramKey}\"]/@Value', asxml))::TEXT AS content";
xpath = xpath.replace("${className}", getClassName());
xpath = xpath.replace("${bagKey}", selection.getBagKey());
xpath = xpath.replace("${paramKey}", selection.getParamKey());
this.sb.append(this.indent);
this.sb.append("id in (\n");
this.sb.append("id IN (\n");
this.sb.append(this.indent);
this.sb.append(" SELECT id\n");
this.sb.append(this.indent);
@ -252,7 +269,7 @@ public abstract class PostgreSqlQueryVisitor implements StrolchRootElementSelect
this.sb.append(xpath);
this.sb.append("\n");
this.sb.append(this.indent);
this.sb.append("from ");
this.sb.append("FROM ");
this.sb.append(getTableName());
this.sb.append("\n");
this.sb.append(this.indent);
@ -315,7 +332,7 @@ public abstract class PostgreSqlQueryVisitor implements StrolchRootElementSelect
@Override
public void visit(NullParameterSelection selection) {
String xpath = "cast(xpath('//${className}/ParameterBag[@Id=\"${bagKey}\"]/Parameter[@Id=\"${paramKey}\"]', asxml) as text[]) = '{}'\n";
String xpath = "CAST(XPATH('//${className}/ParameterBag[@Id=\"${bagKey}\"]/Parameter[@Id=\"${paramKey}\"]', asxml) AS text[]) = '{}'\n";
this.sb.append(this.indent);
xpath = xpath.replace("${className}", getClassName());
xpath = xpath.replace("${bagKey}", selection.getBagKey());
@ -325,7 +342,7 @@ public abstract class PostgreSqlQueryVisitor implements StrolchRootElementSelect
@Override
public void visit(ParameterBagSelection selection) {
String xpath = "cast(xpath('//${className}/ParameterBag[@Id=\"${bagKey}\"]', asxml) as text[]) != '{}'\n";
String xpath = "CAST(XPATH('//${className}/ParameterBag[@Id=\"${bagKey}\"]', asxml) AS text[]) != '{}'\n";
this.sb.append(this.indent);
xpath = xpath.replace("${className}", getClassName());
xpath = xpath.replace("${bagKey}", selection.getBagKey());
@ -334,7 +351,7 @@ public abstract class PostgreSqlQueryVisitor implements StrolchRootElementSelect
@Override
public void visit(NullParameterBagSelection selection) {
String xpath = "cast(xpath('//${className}/ParameterBag[@Id=\"${bagKey}\"]', asxml) as text[]) = '{}'\n";
String xpath = "CAST(XPATH('//${className}/ParameterBag[@Id=\"${bagKey}\"]', asxml) AS text[]) = '{}'\n";
this.sb.append(this.indent);
xpath = xpath.replace("${className}", getClassName());
xpath = xpath.replace("${bagKey}", selection.getBagKey());
@ -366,6 +383,31 @@ public abstract class PostgreSqlQueryVisitor implements StrolchRootElementSelect
throw new UnsupportedOperationException("Not yet supported!");
}
@Override
public PostgreSqlQueryVisitor visit(OrderById ordering) {
if (ordering.isAscending())
this.ordering = "ORDER BY id";
else
this.ordering = "ORDER BY id DESC";
return this;
}
@Override
public PostgreSqlQueryVisitor visit(OrderByName ordering) {
if (ordering.isAscending())
this.ordering = "ORDER BY name";
else
this.ordering = "ORDER BY name DESC";
return this;
}
@Override
public PostgreSqlQueryVisitor visit(OrderByParameter ordering) {
throw new UnsupportedOperationException("Not yet supported!");
}
/**
* @param ps
* @throws SQLException

View File

@ -31,7 +31,6 @@ import javax.xml.parsers.SAXParserFactory;
import javax.xml.transform.sax.SAXResult;
import li.strolch.model.Resource;
import li.strolch.model.ResourceVisitor;
import li.strolch.model.Tags;
import li.strolch.model.query.ResourceQuery;
import li.strolch.model.xml.ResourceToSaxVisitor;
@ -149,7 +148,7 @@ public class PostgreSqlResourceDao extends PostgresqlDao<Resource> implements Re
}
@Override
public <U> List<U> doQuery(ResourceQuery query, ResourceVisitor<U> resourceVisitor) {
public <U> List<U> doQuery(ResourceQuery<U> query) {
PostgreSqlResourceQueryVisitor queryVisitor = new PostgreSqlResourceQueryVisitor("id, asxml");
query.accept(queryVisitor);
@ -166,7 +165,7 @@ public class PostgreSqlResourceDao extends PostgresqlDao<Resource> implements Re
String id = result.getString("id");
SQLXML sqlxml = result.getSQLXML("asxml");
Resource t = parseFromXml(id, queryVisitor.getType(), sqlxml);
list.add(resourceVisitor.visit(t));
list.add(query.getResourceVisitor().visit(t));
}
}
} catch (SQLException e) {

View File

@ -44,6 +44,8 @@ import li.strolch.model.Tags;
import li.strolch.model.audit.AccessType;
import li.strolch.model.audit.Audit;
import li.strolch.model.audit.AuditQuery;
import li.strolch.model.audit.AuditVisitor;
import li.strolch.model.audit.NoStrategyAuditVisitor;
import li.strolch.persistence.api.AbstractTransaction;
import li.strolch.persistence.api.StrolchTransaction;
import li.strolch.persistence.postgresql.PostgreSqlAuditQueryVisitor;
@ -172,92 +174,100 @@ public class AuditQueryTest {
@Test
public void shouldQueryTypeAndDateRange() throws SQLException {
AuditQuery query = new AuditQuery(Tags.AUDIT, new DateRange().from(earlier, true).to(later, true));
AuditVisitor<Audit> visitor = new NoStrategyAuditVisitor();
AuditQuery<Audit> query = new AuditQuery<>(visitor, Tags.AUDIT, new DateRange().from(earlier, true).to(later,
true));
performQuery(query, Arrays.asList("0", "1", "2", "3", "4"));
query = new AuditQuery(Tags.AUDIT, new DateRange().from(current, true).to(current, true));
query = new AuditQuery<>(visitor, Tags.AUDIT, new DateRange().from(current, true).to(current, true));
performQuery(query, Arrays.asList("1", "3", "4"));
query = new AuditQuery(Tags.AUDIT, new DateRange().from(current, true));
query = new AuditQuery<>(visitor, Tags.AUDIT, new DateRange().from(current, true));
performQuery(query, Arrays.asList("1", "2", "3", "4"));
query = new AuditQuery(Tags.AUDIT, new DateRange().to(current, true));
query = new AuditQuery<>(visitor, Tags.AUDIT, new DateRange().to(current, true));
performQuery(query, Arrays.asList("0", "1", "3", "4"));
query = new AuditQuery(Tags.RESOURCE, new DateRange().from(past, true).to(future, true));
query = new AuditQuery<>(visitor, Tags.RESOURCE, new DateRange().from(past, true).to(future, true));
performQuery(query, Arrays.<String> asList());
}
@Test
public void shouldQueryAudits() throws SQLException {
AuditQuery query = new AuditQuery(Tags.AUDIT, new DateRange().from(past, true).to(future, true));
AuditVisitor<Audit> visitor = new NoStrategyAuditVisitor();
AuditQuery<Audit> query = new AuditQuery<>(visitor, Tags.AUDIT, new DateRange().from(past, true).to(future,
true));
query.action().accessTypes(AccessType.CREATE, AccessType.READ);
performQuery(query, Arrays.asList("0", "1", "4"));
query = new AuditQuery(Tags.AUDIT, new DateRange().from(past, true).to(future, true));
query = new AuditQuery<>(visitor, Tags.AUDIT, new DateRange().from(past, true).to(future, true));
query.action().accessTypes(AccessType.CREATE);
performQuery(query, Arrays.asList("0", "4"));
query = new AuditQuery(Tags.AUDIT, new DateRange().from(past, true).to(future, true));
query = new AuditQuery<>(visitor, Tags.AUDIT, new DateRange().from(past, true).to(future, true));
query.action().accessTypes(AccessType.CREATE, AccessType.READ)
.actions(StringMatchMode.EQUALS_CASE_SENSITIVE, "create", "read");
performQuery(query, Arrays.asList("0", "1", "4"));
query = new AuditQuery(Tags.AUDIT, new DateRange().from(past, true).to(future, true));
query = new AuditQuery<>(visitor, Tags.AUDIT, new DateRange().from(past, true).to(future, true));
query.action().accessTypes(AccessType.CREATE, AccessType.READ)
.actions(StringMatchMode.EQUALS_CASE_SENSITIVE, "read");
performQuery(query, Arrays.asList("1"));
query = new AuditQuery(Tags.AUDIT, new DateRange().from(past, true).to(future, true));
query = new AuditQuery<>(visitor, Tags.AUDIT, new DateRange().from(past, true).to(future, true));
query.element().elementAccessed(StringMatchMode.CONTAINS_CASE_INSENSITIVE, "crea");
performQuery(query, Arrays.asList("0", "4"));
query = new AuditQuery(Tags.AUDIT, new DateRange().from(past, true).to(future, true));
query = new AuditQuery<>(visitor, Tags.AUDIT, new DateRange().from(past, true).to(future, true));
query.element().elementAccessed(StringMatchMode.CONTAINS_CASE_SENSITIVE, "crea");
performQuery(query, Arrays.<String> asList());
query = new AuditQuery(Tags.AUDIT, new DateRange().from(past, true).to(future, true));
query = new AuditQuery<>(visitor, Tags.AUDIT, new DateRange().from(past, true).to(future, true));
query.element().elementAccessed(StringMatchMode.EQUALS_CASE_INSENSITIVE, "create");
performQuery(query, Arrays.asList("0", "4"));
query = new AuditQuery(Tags.AUDIT, new DateRange().from(past, true).to(future, true));
query = new AuditQuery<>(visitor, Tags.AUDIT, new DateRange().from(past, true).to(future, true));
query.identity().usernames(StringMatchMode.EQUALS_CASE_INSENSITIVE, "earlier");
performQuery(query, Arrays.asList("0"));
query = new AuditQuery(Tags.AUDIT, new DateRange().from(past, true).to(future, true));
query = new AuditQuery<>(visitor, Tags.AUDIT, new DateRange().from(past, true).to(future, true));
query.identity().usernames(StringMatchMode.EQUALS_CASE_INSENSITIVE, "earlier", "later");
performQuery(query, Arrays.asList("0", "2"));
query = new AuditQuery(Tags.AUDIT, new DateRange().from(past, true).to(future, true));
query = new AuditQuery<>(visitor, Tags.AUDIT, new DateRange().from(past, true).to(future, true));
query.identity().usernames(StringMatchMode.EQUALS_CASE_INSENSITIVE, "earlier")
.firstnames(StringMatchMode.CONTAINS_CASE_INSENSITIVE, "enn");
performQuery(query, Arrays.asList("0"));
query = new AuditQuery(Tags.AUDIT, new DateRange().from(past, true).to(future, true));
query = new AuditQuery<>(visitor, Tags.AUDIT, new DateRange().from(past, true).to(future, true));
query.identity().usernames(StringMatchMode.EQUALS_CASE_INSENSITIVE, "earlier")
.firstnames(StringMatchMode.CONTAINS_CASE_INSENSITIVE, "enn")
.lastnames(StringMatchMode.CONTAINS_CASE_INSENSITIVE, "kennedy");
performQuery(query, Arrays.asList("0"));
query = new AuditQuery(Tags.AUDIT, new DateRange().from(past, true).to(future, true));
query = new AuditQuery<>(visitor, Tags.AUDIT, new DateRange().from(past, true).to(future, true));
query.identity().firstnames(StringMatchMode.CONTAINS_CASE_INSENSITIVE, "enn")
.lastnames(StringMatchMode.CONTAINS_CASE_INSENSITIVE, "kennedy");
performQuery(query, Arrays.asList("0", "1", "2", "3", "4"));
query = new AuditQuery(Tags.AUDIT, new DateRange().from(past, true).to(future, true));
query = new AuditQuery<>(visitor, Tags.AUDIT, new DateRange().from(past, true).to(future, true));
query.element().elementSubTypes(StringMatchMode.EQUALS_CASE_SENSITIVE, "Foo");
performQuery(query, Arrays.asList("0", "1", "2", "3", "4"));
query = new AuditQuery(Tags.AUDIT, new DateRange().from(past, true).to(future, true));
query = new AuditQuery<>(visitor, Tags.AUDIT, new DateRange().from(past, true).to(future, true));
query.element().elementSubTypes(StringMatchMode.EQUALS_CASE_SENSITIVE, "Bar");
performQuery(query, Arrays.asList());
query = new AuditQuery(Tags.AUDIT, new DateRange().from(past, true).to(future, true));
query = new AuditQuery<>(visitor, Tags.AUDIT, new DateRange().from(past, true).to(future, true));
query.limit(1).element().elementSubTypes(StringMatchMode.EQUALS_CASE_SENSITIVE, "Foo");
performQuery(query, Arrays.asList("2"));
}
private void performQuery(AuditQuery query, List<String> expected) throws SQLException {
private void performQuery(AuditQuery<Audit> query, List<String> expected) throws SQLException {
PostgreSqlAuditQueryVisitor visitor = new PostgreSqlAuditQueryVisitor("id");
query.accept(visitor);
List<String> ids = queryIds(visitor);

View File

@ -34,13 +34,14 @@ import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import li.strolch.agent.api.OrderMap;
import li.strolch.agent.api.ResourceMap;
import li.strolch.agent.api.StrolchRealm;
import li.strolch.model.ModelGenerator;
import li.strolch.model.Order;
import li.strolch.model.Resource;
import li.strolch.model.State;
import li.strolch.model.query.DateSelection;
import li.strolch.model.query.IdSelection;
@ -52,7 +53,7 @@ import li.strolch.model.query.ParameterBagSelection.NullParameterBagSelection;
import li.strolch.model.query.ParameterSelection;
import li.strolch.model.query.ResourceQuery;
import li.strolch.model.query.StateSelection;
import li.strolch.model.query.StrolchTypeNavigation;
import li.strolch.model.query.ordering.OrderById;
import li.strolch.persistence.api.StrolchTransaction;
import li.strolch.persistence.postgresql.PostgreSqlOrderQueryVisitor;
import li.strolch.persistence.postgresql.PostgreSqlQueryVisitor;
@ -149,7 +150,7 @@ public class QueryTest {
@Test
public void shouldQueryOrderAll() throws SQLException {
OrderQuery query = new OrderQuery(new StrolchTypeNavigation("MyType1"));
OrderQuery<Order> query = OrderQuery.query("MyType1", new OrderById());
query.withAny();
performOrderQuery(query, Arrays.asList("@1", "@2", "@3"));
}
@ -157,51 +158,51 @@ public class QueryTest {
@Test
public void shouldQueryResourceAll() throws SQLException {
ResourceQuery query = new ResourceQuery(new StrolchTypeNavigation("MyType2"));
ResourceQuery<Resource> query = ResourceQuery.query("MyType2", new OrderById(false));
query.withAny();
performResourceQuery(query, Arrays.asList("@4", "@5", "@6"));
performResourceQuery(query, Arrays.asList("@6", "@5", "@4"));
}
@Test
public void shouldQueryOrderByDate() throws SQLException {
// range
OrderQuery query = new OrderQuery(new StrolchTypeNavigation("MyType1"));
OrderQuery<Order> query = OrderQuery.query("MyType1", new OrderById());
query.and().with(new DateSelection().from(earlier, false).to(later, false));
performOrderQuery(query, Arrays.asList("@1", "@2", "@3"));
// equals current
query = new OrderQuery(new StrolchTypeNavigation("MyType1"));
query = OrderQuery.query("MyType1");
query.and().with(new DateSelection().from(current, false).to(current, false));
performOrderQuery(query, Arrays.asList("@2"));
// equals later
query = new OrderQuery(new StrolchTypeNavigation("MyType1"));
query = OrderQuery.query("MyType1");
query.and().with(new DateSelection().from(later, false).to(later, false));
performOrderQuery(query, Arrays.<String> asList("@3"));
// equals earlier
query = new OrderQuery(new StrolchTypeNavigation("MyType1"));
query = OrderQuery.query("MyType1");
query.and().with(new DateSelection().from(earlier, false).to(earlier, false));
performOrderQuery(query, Arrays.<String> asList("@1"));
// past
query = new OrderQuery(new StrolchTypeNavigation("MyType1"));
query = OrderQuery.query("MyType1");
query.and().with(new DateSelection().to(past, false));
performOrderQuery(query, Arrays.<String> asList());
// future
query = new OrderQuery(new StrolchTypeNavigation("MyType1"));
query = OrderQuery.query("MyType1");
query.and().with(new DateSelection().from(future, false));
performOrderQuery(query, Arrays.<String> asList());
// earlier
query = new OrderQuery(new StrolchTypeNavigation("MyType1"));
query = OrderQuery.query("MyType1");
query.and().with(new DateSelection().from(past, false).to(earlier, true));
performOrderQuery(query, Arrays.<String> asList("@1"));
// later
query = new OrderQuery(new StrolchTypeNavigation("MyType1"));
query = OrderQuery.query("MyType1");
query.and().with(new DateSelection().from(later, false).to(future, true));
performOrderQuery(query, Arrays.<String> asList("@3"));
}
@ -209,11 +210,11 @@ public class QueryTest {
@Test
public void shouldQueryOrderByState() throws SQLException {
OrderQuery query = new OrderQuery(new StrolchTypeNavigation("MyType1"));
OrderQuery<Order> query = OrderQuery.query("MyType1");
query.and().with(new StateSelection(State.CREATED));
performOrderQuery(query, Arrays.asList("@1"));
query = new OrderQuery(new StrolchTypeNavigation("MyType1"));
query = OrderQuery.query("MyType1");
query.and().with(new StateSelection(State.OPEN));
performOrderQuery(query, Arrays.<String> asList("@2"));
}
@ -221,7 +222,7 @@ public class QueryTest {
@Test
public void shouldQueryOrder1() throws SQLException {
OrderQuery query = new OrderQuery(new StrolchTypeNavigation("MyType1"));
OrderQuery<Order> query = OrderQuery.query("MyType1", new OrderById());
query.and().with(new IdSelection("@1", "@2"),
new NameSelection("Order 1", StringMatchMode.EQUALS_CASE_SENSITIVE));
performOrderQuery(query, Arrays.asList("@1"));
@ -230,7 +231,7 @@ public class QueryTest {
@Test
public void shouldQueryOrder2() throws SQLException {
OrderQuery query = new OrderQuery(new StrolchTypeNavigation("MyType1"));
OrderQuery<Order> query = OrderQuery.query("MyType1", new OrderById());
query.or().with(new IdSelection("@1", "@2"),
new NameSelection("order 1", StringMatchMode.EQUALS_CASE_SENSITIVE));
performOrderQuery(query, Arrays.asList("@1", "@2"));
@ -238,28 +239,28 @@ public class QueryTest {
@Test
public void shouldQueryOrderByBooleParam() throws SQLException {
OrderQuery query = new OrderQuery(new StrolchTypeNavigation("MyType1"));
OrderQuery<Order> query = OrderQuery.query("MyType1", new OrderById());
query.and().with(ParameterSelection.booleanSelection("@bag01", "@param1", true));
performOrderQuery(query, Arrays.asList("@1", "@2", "@3"));
}
@Test
public void shouldQueryOrderByFloagParam() throws SQLException {
OrderQuery query = new OrderQuery(new StrolchTypeNavigation("MyType1"));
OrderQuery<Order> query = OrderQuery.query("MyType1", new OrderById());
query.and().with(ParameterSelection.floatSelection("@bag01", "@param2", 44.3));
performOrderQuery(query, Arrays.asList("@1", "@2", "@3"));
}
@Test
public void shouldQueryOrderByIntegerParam() throws SQLException {
OrderQuery query = new OrderQuery(new StrolchTypeNavigation("MyType1"));
OrderQuery<Order> query = OrderQuery.query("MyType1", new OrderById());
query.and().with(ParameterSelection.integerSelection("@bag01", "@param3", 77));
performOrderQuery(query, Arrays.asList("@1", "@2", "@3"));
}
@Test
public void shouldQueryOrderByLongParam() throws SQLException {
OrderQuery query = new OrderQuery(new StrolchTypeNavigation("MyType2"));
OrderQuery<Order> query = OrderQuery.query("MyType2", new OrderById());
query.and().with(ParameterSelection.longSelection("@bag01", "@param4", 4453234566L));
performOrderQuery(query, Arrays.asList("@4", "@5", "@6"));
}
@ -269,25 +270,25 @@ public class QueryTest {
List<String> expected = Arrays.asList("@1", "@2", "@3");
OrderQuery query = new OrderQuery(new StrolchTypeNavigation("MyType1"));
OrderQuery<Order> query = OrderQuery.query("MyType1", new OrderById());
query.and().with(
ParameterSelection.stringSelection("@bag01", "@param5", "Strolch",
StringMatchMode.EQUALS_CASE_SENSITIVE));
performOrderQuery(query, expected);
query = new OrderQuery(new StrolchTypeNavigation("MyType1"));
query = OrderQuery.query("MyType1", new OrderById());
query.and().with(
ParameterSelection.stringSelection("@bag01", "@param5", "strolch",
StringMatchMode.EQUALS_CASE_SENSITIVE));
performOrderQuery(query, Arrays.<String> asList());
query = new OrderQuery(new StrolchTypeNavigation("MyType1"));
query = OrderQuery.query("MyType1", new OrderById());
query.and().with(
ParameterSelection.stringSelection("@bag01", "@param5", "strolch",
StringMatchMode.EQUALS_CASE_INSENSITIVE));
performOrderQuery(query, expected);
query = new OrderQuery(new StrolchTypeNavigation("MyType1"));
query = OrderQuery.query("MyType1", new OrderById());
query.and().with(
ParameterSelection.stringSelection("@bag01", "@param5", "olch",
StringMatchMode.CONTAINS_CASE_INSENSITIVE));
@ -296,42 +297,42 @@ public class QueryTest {
@Test
public void shouldQueryOrderByDateParam() throws SQLException {
OrderQuery query = new OrderQuery(new StrolchTypeNavigation("MyType1"));
OrderQuery<Order> query = OrderQuery.query("MyType1", new OrderById());
query.and().with(ParameterSelection.dateSelection("@bag01", "@param6", new Date(1354295525628L)));
performOrderQuery(query, Arrays.asList("@1", "@2", "@3"));
}
@Test
public void shouldQueryOrderByDurationParam() throws SQLException {
OrderQuery query = new OrderQuery(new StrolchTypeNavigation("MyType1"));
OrderQuery<Order> query = OrderQuery.query("MyType1", new OrderById());
query.and().with(ParameterSelection.durationSelection("@bag01", "@param8", "P1D"));
performOrderQuery(query, Arrays.asList("@1", "@2", "@3"));
}
@Test
public void shouldQueryOrderByNullParam1() throws SQLException {
OrderQuery query = new OrderQuery(new StrolchTypeNavigation("MyType1"));
OrderQuery<Order> query = OrderQuery.query("MyType1", new OrderById());
query.and().with(ParameterSelection.nullSelection("@bag01", "@param6"));
performOrderQuery(query, Arrays.<String> asList());
}
@Test
public void shouldQueryOrderByNullParam2() throws SQLException {
OrderQuery query = new OrderQuery(new StrolchTypeNavigation("MyType1"));
OrderQuery<Order> query = OrderQuery.query("MyType1", new OrderById());
query.and().with(ParameterSelection.nullSelection("@bag01", "@param"));
performOrderQuery(query, Arrays.asList("@1", "@2", "@3"));
}
@Test
public void shouldQueryOrderByBag() throws SQLException {
OrderQuery query = new OrderQuery(new StrolchTypeNavigation("MyType1"));
OrderQuery<Order> query = OrderQuery.query("MyType1", new OrderById());
query.and().with(new ParameterBagSelection("@bag01"));
performOrderQuery(query, Arrays.asList("@1", "@2", "@3"));
}
@Test
public void shouldQueryOrderByNullBag() throws SQLException {
OrderQuery query = new OrderQuery(new StrolchTypeNavigation("MyType1"));
OrderQuery<Order> query = OrderQuery.query("MyType1", new OrderById());
query.and().with(new NullParameterBagSelection("@bag01"));
performOrderQuery(query, Arrays.<String> asList());
}
@ -339,7 +340,7 @@ public class QueryTest {
@Test
public void shouldQueryResource1() throws SQLException {
ResourceQuery query = new ResourceQuery(new StrolchTypeNavigation("MyType1"));
ResourceQuery<Resource> query = ResourceQuery.query("MyType1", new OrderById());
query.or().with(new IdSelection("@1", "@2"),
new NameSelection("Resource 1", StringMatchMode.EQUALS_CASE_SENSITIVE));
performResourceQuery(query, Arrays.asList("@1", "@2"));
@ -347,7 +348,7 @@ public class QueryTest {
@Test
public void shouldQueryResource2() throws SQLException {
ResourceQuery query = new ResourceQuery(new StrolchTypeNavigation("MyType1"));
ResourceQuery<Resource> query = ResourceQuery.query("MyType1", new OrderById());
query.and().with(
new OrSelection(new IdSelection("@1"), new IdSelection("@2")),
new OrSelection(new NameSelection("Resource 1", StringMatchMode.EQUALS_CASE_SENSITIVE),
@ -357,28 +358,28 @@ public class QueryTest {
@Test
public void shouldQueryResourceByBooleParam() throws SQLException {
ResourceQuery query = new ResourceQuery(new StrolchTypeNavigation("MyType1"));
ResourceQuery<Resource> query = ResourceQuery.query("MyType1", new OrderById());
query.and().with(ParameterSelection.booleanSelection("@bag01", "@param1", true));
performResourceQuery(query, Arrays.asList("@1", "@2", "@3"));
}
@Test
public void shouldQueryResourceByFloagParam() throws SQLException {
ResourceQuery query = new ResourceQuery(new StrolchTypeNavigation("MyType1"));
ResourceQuery<Resource> query = ResourceQuery.query("MyType1", new OrderById());
query.and().with(ParameterSelection.floatSelection("@bag01", "@param2", 44.3));
performResourceQuery(query, Arrays.asList("@1", "@2", "@3"));
}
@Test
public void shouldQueryResourceByIntegerParam() throws SQLException {
ResourceQuery query = new ResourceQuery(new StrolchTypeNavigation("MyType1"));
ResourceQuery<Resource> query = ResourceQuery.query("MyType1", new OrderById());
query.and().with(ParameterSelection.integerSelection("@bag01", "@param3", 77));
performResourceQuery(query, Arrays.asList("@1", "@2", "@3"));
}
@Test
public void shouldQueryResourceByLongParam() throws SQLException {
ResourceQuery query = new ResourceQuery(new StrolchTypeNavigation("MyType2"));
ResourceQuery<Resource> query = ResourceQuery.query("MyType2", new OrderById());
query.and().with(ParameterSelection.longSelection("@bag01", "@param4", 4453234566L));
performResourceQuery(query, Arrays.asList("@4", "@5", "@6"));
}
@ -388,31 +389,31 @@ public class QueryTest {
List<String> expected = Arrays.asList("@1", "@2", "@3");
ResourceQuery query = new ResourceQuery(new StrolchTypeNavigation("MyType1"));
ResourceQuery<Resource> query = ResourceQuery.query("MyType1", new OrderById());
query.and().with(
ParameterSelection.stringSelection("@bag01", "@param5", "Strolch",
StringMatchMode.EQUALS_CASE_SENSITIVE));
performResourceQuery(query, expected);
query = new ResourceQuery(new StrolchTypeNavigation("MyType1"));
query = ResourceQuery.query("MyType1", new OrderById());
query.and().with(
ParameterSelection.stringSelection("@bag01", "@param5", "strolch",
StringMatchMode.EQUALS_CASE_SENSITIVE));
performResourceQuery(query, Arrays.<String> asList());
query = new ResourceQuery(new StrolchTypeNavigation("MyType1"));
query = ResourceQuery.query("MyType1", new OrderById());
query.and().with(
ParameterSelection.stringSelection("@bag01", "@param5", "strolch",
StringMatchMode.EQUALS_CASE_INSENSITIVE));
performResourceQuery(query, expected);
query = new ResourceQuery(new StrolchTypeNavigation("MyType1"));
query = ResourceQuery.query("MyType1", new OrderById());
query.and().with(
ParameterSelection.stringSelection("@bag01", "@param5", "olch",
StringMatchMode.CONTAINS_CASE_INSENSITIVE));
performResourceQuery(query, expected);
query = new ResourceQuery(new StrolchTypeNavigation("MyType1"));
query = ResourceQuery.query("MyType1", new OrderById());
query.and().with(
ParameterSelection.stringSelection("@bag01", "@param5", "olch",
StringMatchMode.CONTAINS_CASE_INSENSITIVE),
@ -425,58 +426,58 @@ public class QueryTest {
@Test
public void shouldQueryResourceByDateParam() throws SQLException {
ResourceQuery query = new ResourceQuery(new StrolchTypeNavigation("MyType1"));
ResourceQuery<Resource> query = ResourceQuery.query("MyType1", new OrderById());
query.and().with(ParameterSelection.dateSelection("@bag01", "@param6", new Date(1354295525628L)));
performResourceQuery(query, Arrays.asList("@1", "@2", "@3"));
}
@Test
public void shouldQueryResourceByDurationParam() throws SQLException {
ResourceQuery query = new ResourceQuery(new StrolchTypeNavigation("MyType1"));
ResourceQuery<Resource> query = ResourceQuery.query("MyType1", new OrderById());
query.and().with(ParameterSelection.durationSelection("@bag01", "@param8", "P1D"));
performResourceQuery(query, Arrays.asList("@1", "@2", "@3"));
}
@Test
public void shouldQueryResourceByNullParam1() throws SQLException {
ResourceQuery query = new ResourceQuery(new StrolchTypeNavigation("MyType1"));
ResourceQuery<Resource> query = ResourceQuery.query("MyType1", new OrderById());
query.and().with(ParameterSelection.nullSelection("@bag01", "@param6"));
performResourceQuery(query, Arrays.<String> asList());
}
@Test
public void shouldQueryResourceByNullParam2() throws SQLException {
ResourceQuery query = new ResourceQuery(new StrolchTypeNavigation("MyType1"));
ResourceQuery<Resource> query = ResourceQuery.query("MyType1", new OrderById());
query.and().with(ParameterSelection.nullSelection("@bag01", "@param"));
performResourceQuery(query, Arrays.asList("@1", "@2", "@3"));
}
@Test
public void shouldQueryResourceByBag() throws SQLException {
ResourceQuery query = new ResourceQuery(new StrolchTypeNavigation("MyType1"));
ResourceQuery<Resource> query = ResourceQuery.query("MyType1", new OrderById());
query.and().with(new ParameterBagSelection("@bag01"));
performResourceQuery(query, Arrays.asList("@1", "@2", "@3"));
}
@Test
public void shouldQueryResourceByNullBag() throws SQLException {
ResourceQuery query = new ResourceQuery(new StrolchTypeNavigation("MyType1"));
ResourceQuery<Resource> query = ResourceQuery.query("MyType1", new OrderById());
query.and().with(new NullParameterBagSelection("@bag01"));
performResourceQuery(query, Arrays.<String> asList());
}
private void performOrderQuery(OrderQuery query, List<String> expected) throws SQLException {
private void performOrderQuery(OrderQuery<Order> query, List<String> expected) throws SQLException {
PostgreSqlOrderQueryVisitor visitor = new PostgreSqlOrderQueryVisitor("id");
query.accept(visitor);
List<String> ids = queryIds(visitor);
assertEquals(new HashSet<>(expected), new HashSet<>(ids));
assertEquals(expected, ids);
}
private void performResourceQuery(ResourceQuery query, List<String> expected) throws SQLException {
private void performResourceQuery(ResourceQuery<Resource> query, List<String> expected) throws SQLException {
PostgreSqlResourceQueryVisitor visitor = new PostgreSqlResourceQueryVisitor("id");
query.accept(visitor);
List<String> ids = queryIds(visitor);
assertEquals(new HashSet<>(expected), new HashSet<>(ids));
assertEquals(expected, ids);
}
private List<String> queryIds(PostgreSqlQueryVisitor visitor) throws SQLException {

View File

@ -17,7 +17,6 @@ package li.strolch.persistence.xml;
import java.util.List;
import li.strolch.model.ActivityVisitor;
import li.strolch.model.Tags;
import li.strolch.model.activity.Activity;
import li.strolch.model.query.ActivityQuery;
@ -36,7 +35,7 @@ public class XmlActivityDao extends AbstractDao<Activity> implements ActivityDao
}
@Override
public <U> List<U> doQuery(ActivityQuery query, ActivityVisitor<U> activityVisitor) {
public <U> List<U> doQuery(ActivityQuery<U> query) {
// TODO implement XML file based querying...
throw new UnsupportedOperationException("not yet implemented!");
}

View File

@ -22,7 +22,6 @@ import java.util.Set;
import li.strolch.model.Tags;
import li.strolch.model.audit.Audit;
import li.strolch.model.audit.AuditQuery;
import li.strolch.model.audit.AuditVisitor;
import li.strolch.persistence.api.AuditDao;
import li.strolch.persistence.api.StrolchTransaction;
import ch.eitchnet.utils.collections.DateRange;
@ -190,7 +189,7 @@ public class XmlAuditDao implements AuditDao {
}
@Override
public <U> List<U> doQuery(AuditQuery query, AuditVisitor<U> auditVisitor) {
public <U> List<U> doQuery(AuditQuery<U> query) {
// TODO implement XML file based querying...
throw new UnsupportedOperationException("not yet implemented!");
}

View File

@ -18,7 +18,6 @@ package li.strolch.persistence.xml;
import java.util.List;
import li.strolch.model.Order;
import li.strolch.model.OrderVisitor;
import li.strolch.model.Tags;
import li.strolch.model.query.OrderQuery;
import li.strolch.persistence.api.OrderDao;
@ -36,7 +35,7 @@ public class XmlOrderDao extends AbstractDao<Order> implements OrderDao {
}
@Override
public <U> List<U> doQuery(OrderQuery query, OrderVisitor<U> orderVisitor) {
public <U> List<U> doQuery(OrderQuery<U> query) {
// TODO implement XML file based querying...
throw new UnsupportedOperationException("not yet implemented!");
}

View File

@ -18,7 +18,6 @@ package li.strolch.persistence.xml;
import java.util.List;
import li.strolch.model.Resource;
import li.strolch.model.ResourceVisitor;
import li.strolch.model.Tags;
import li.strolch.model.query.ResourceQuery;
import li.strolch.persistence.api.ResourceDao;
@ -36,7 +35,7 @@ public class XmlResourceDao extends AbstractDao<Resource> implements ResourceDao
}
@Override
public <U> List<U> doQuery(ResourceQuery query, ResourceVisitor<U> resourceVisitor) {
public <U> List<U> doQuery(ResourceQuery<U> query) {
// TODO implement XML file based querying...
throw new UnsupportedOperationException("not yet implemented!");
}

View File

@ -67,7 +67,7 @@ public class AuditsService {
try (StrolchTransaction tx = realm.openTx(cert, AuditsService.class)) {
li.strolch.model.audit.AuditQuery auditQuery = new ToAuditQueryVisitor().create(query);
li.strolch.model.audit.AuditQuery<Audit> auditQuery = new ToAuditQueryVisitor().create(query);
List<Audit> audits = tx.getAuditTrail().doQuery(tx, auditQuery);
return Response.ok(new AuditQueryResult(audits), MediaType.APPLICATION_JSON).build();

View File

@ -18,6 +18,7 @@ package li.strolch.rest.model.visitor;
import java.util.List;
import li.strolch.model.audit.AccessType;
import li.strolch.model.audit.Audit;
import li.strolch.rest.model.ActionSelection;
import li.strolch.rest.model.AuditQuery;
import li.strolch.rest.model.DateRange;
@ -27,7 +28,7 @@ import ch.eitchnet.utils.helper.StringHelper;
public class ToAuditQueryVisitor {
public li.strolch.model.audit.AuditQuery create(AuditQuery query) {
public li.strolch.model.audit.AuditQuery<Audit> create(AuditQuery query) {
// validate element type
String elementType = query.getElementType();
@ -45,7 +46,7 @@ public class ToAuditQueryVisitor {
dateRange.isToInclusive());
// create query
li.strolch.model.audit.AuditQuery auditQuery = new li.strolch.model.audit.AuditQuery(elementType, dr);
li.strolch.model.audit.AuditQuery<Audit> auditQuery = li.strolch.model.audit.AuditQuery.query(elementType, dr);
// limit
auditQuery.limit(query.getLimit());

View File

@ -19,7 +19,6 @@ import java.util.Map;
import ch.eitchnet.privilege.handler.SystemUserAction;
import ch.eitchnet.privilege.model.PrivilegeContext;
import ch.eitchnet.utils.Version;
/**
* @author Robert von Burg <eitch@eitchnet.ch>