use of org.eclipse.kapua.model.query.predicate.KapuaAndPredicate in project kapua by eclipse.
the class GwtDeviceServiceImpl method findDeviceEvents.
public PagingLoadResult<GwtDeviceEvent> findDeviceEvents(PagingLoadConfig loadConfig, GwtDevice gwtDevice, Date startDate, Date endDate) throws GwtKapuaException {
ArrayList<GwtDeviceEvent> gwtDeviceEvents = new ArrayList<GwtDeviceEvent>();
BasePagingLoadResult<GwtDeviceEvent> gwtResults = null;
KapuaLocator locator = KapuaLocator.getInstance();
DeviceEventService des = locator.getService(DeviceEventService.class);
DeviceEventFactory deviceEventFactory = locator.getFactory(DeviceEventFactory.class);
try {
// prepare the query
BasePagingLoadConfig bplc = (BasePagingLoadConfig) loadConfig;
DeviceEventQuery query = deviceEventFactory.newQuery(KapuaEid.parseShortId(gwtDevice.getScopeId()));
KapuaAndPredicate andPredicate = new AndPredicate();
andPredicate.and(new AttributePredicate<KapuaId>(DeviceEventPredicates.DEVICE_ID, KapuaEid.parseShortId(gwtDevice.getId())));
// .and(new AttributePredicate<Date>(DeviceEventPredicates.RECEIVED_ON, startDate, Operator.GREATER_THAN));
// .and(new AttributePredicate<Date>(DeviceEventPredicates.RECEIVED_ON, startDate, Operator.LESS_THAN));
query.setPredicate(andPredicate);
query.setSortCriteria(new FieldSortCriteria(DeviceEventPredicates.RECEIVED_ON, SortOrder.DESCENDING));
query.setOffset(bplc.getOffset());
query.setLimit(bplc.getLimit());
// query execute
KapuaListResult<DeviceEvent> deviceEvents = des.query(query);
// prepare results
for (DeviceEvent deviceEvent : deviceEvents.getItems()) {
gwtDeviceEvents.add(KapuaGwtConverter.convert(deviceEvent));
}
gwtResults = new BasePagingLoadResult<GwtDeviceEvent>(gwtDeviceEvents);
gwtResults.setOffset(loadConfig.getOffset());
gwtResults.setTotalLength((int) des.count(query));
} catch (Throwable t) {
KapuaExceptionHandler.handle(t);
}
return gwtResults;
}
use of org.eclipse.kapua.model.query.predicate.KapuaAndPredicate in project kapua by eclipse.
the class Devices method getEvents.
/**
* Returns the events for the device identified by the specified
* ClientID under the account of the currently connected user.
* <p>
* If the flag DeviceEventsResult.limitExceeded is set, the maximum number
* of entries to be returned has been reached, more events exist and can
* be read by moving the offset forward in a new request
*
* @param deviceId
* The client ID of the device requested.
* @param limit
* Maximum number of entries to be returned.
* @param offset
* Starting offset for the entries to be returned.
* @param startDate
* Start date of the date range requested. The parameter
* is expressed as a long counting the number of milliseconds since
* January 1, 1970, 00:00:00 GMT. The default value of 0 means no
* start date. Alternatively, the date can be expressed as a string
* following the ISO 8601 format.
* @param endDate
* End date of the date range requested. The parameter
* is expressed as a long counting the number of milliseconds since
* January 1, 1970, 00:00:00 GMT. The default value of 0 means no
* start date. Alternatively, the date can be expressed as a string
* following the ISO 8601 format.
* @return The list of Events
*/
@GET
@Path("{deviceId}/events")
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
public DeviceEventListResult getEvents(@PathParam("deviceId") String deviceId, @QueryParam("limit") @DefaultValue("50") int limit, @QueryParam("offset") int offset, @QueryParam("startDate") String startDate, @QueryParam("endDate") String endDate) {
DeviceEventListResult deviceEvents = eventFactory.newDeviceListResult();
try {
KapuaId scopeId = KapuaSecurityUtils.getSession().getScopeId();
KapuaId id = KapuaEid.parseShortId(deviceId);
DeviceEventQuery query = eventFactory.newQuery(scopeId);
KapuaAndPredicate andPredicate = new AndPredicate();
andPredicate.and(new AttributePredicate<>(DeviceEventPredicates.DEVICE_ID, id));
// TODO Date filter not working?
if (startDate != null) {
DateTime parsedStartDate = DateTime.parse(startDate);
andPredicate = andPredicate.and(new AttributePredicate<>(DeviceEventPredicates.RECEIVED_ON, parsedStartDate.toDate(), KapuaAttributePredicate.Operator.GREATER_THAN));
}
if (endDate != null) {
DateTime parsedEndDate = DateTime.parse(endDate);
andPredicate = andPredicate.and(new AttributePredicate<>(DeviceEventPredicates.RECEIVED_ON, parsedEndDate.toDate(), KapuaAttributePredicate.Operator.LESS_THAN));
}
query.setPredicate(andPredicate);
query.setSortCriteria(new FieldSortCriteria(DeviceEventPredicates.RECEIVED_ON, FieldSortCriteria.SortOrder.DESCENDING));
query.setOffset(offset);
query.setLimit(limit);
// query execute
deviceEvents = (DeviceEventListResult) eventService.query(query);
} catch (Throwable t) {
handleException(t);
}
return returnNotNullEntity(deviceEvents);
}
use of org.eclipse.kapua.model.query.predicate.KapuaAndPredicate in project kapua by eclipse.
the class KapuaGwtConverter method convert.
public static GwtDevice convert(Device device) throws KapuaException {
GwtDevice gwtDevice = new GwtDevice();
gwtDevice.setId(device.getId().getShortId());
gwtDevice.setScopeId(device.getScopeId().getShortId());
gwtDevice.setGwtDeviceStatus(device.getStatus().toString());
gwtDevice.setClientId(device.getClientId());
gwtDevice.setDisplayName(device.getDisplayName());
gwtDevice.setModelId(device.getModelId());
gwtDevice.setSerialNumber(device.getSerialNumber());
gwtDevice.setFirmwareVersion(device.getFirmwareVersion());
gwtDevice.setBiosVersion(device.getBiosVersion());
gwtDevice.setOsVersion(device.getOsVersion());
gwtDevice.setJvmVersion(device.getJvmVersion());
gwtDevice.setOsgiVersion(device.getOsgiFrameworkVersion());
gwtDevice.setAcceptEncoding(device.getAcceptEncoding());
gwtDevice.setApplicationIdentifiers(device.getApplicationIdentifiers());
gwtDevice.setGpsLatitude(device.getGpsLatitude());
gwtDevice.setGpsLongitude(device.getGpsLongitude());
gwtDevice.setLastEventOn(device.getLastEventOn());
gwtDevice.setIccid(device.getIccid());
gwtDevice.setImei(device.getImei());
gwtDevice.setImsi(device.getImsi());
String lastEventType = device.getLastEventType() != null ? device.getLastEventType().name() : "";
gwtDevice.setLastEventType(lastEventType);
// custom Attributes
gwtDevice.setCustomAttribute1(device.getCustomAttribute1());
gwtDevice.setCustomAttribute2(device.getCustomAttribute2());
gwtDevice.setCustomAttribute3(device.getCustomAttribute3());
gwtDevice.setCustomAttribute4(device.getCustomAttribute4());
gwtDevice.setCustomAttribute5(device.getCustomAttribute5());
gwtDevice.setOptlock(device.getOptlock());
// Device connection
KapuaLocator locator = KapuaLocator.getInstance();
DeviceConnectionService deviceConnectionService = locator.getService(DeviceConnectionService.class);
DeviceConnectionFactory deviceConnectionFactory = locator.getFactory(DeviceConnectionFactory.class);
DeviceConnectionQuery query = deviceConnectionFactory.newQuery(device.getScopeId());
KapuaAndPredicate andPredicate = new AndPredicate();
andPredicate = andPredicate.and(new AttributePredicate<String>(DeviceConnectionPredicates.CLIENT_ID, device.getClientId()));
// andPredicate = andPredicate.and(new AttributePredicate<DeviceConnectionStatus[]>(DeviceConnectionPredicates.CONNECTION_STATUS,
// new DeviceConnectionStatus[] { DeviceConnectionStatus.CONNECTED, DeviceConnectionStatus.MISSING }));
query.setPredicate(andPredicate);
KapuaListResult<DeviceConnection> deviceConnections = deviceConnectionService.query(query);
if (!deviceConnections.isEmpty()) {
DeviceConnection connection = deviceConnections.getItem(0);
gwtDevice.setGwtDeviceConnectionStatus(connection.getStatus().toString());
gwtDevice.setConnectionIp(connection.getClientIp());
gwtDevice.setDeviceUserId(connection.getUserId().getShortId());
}
return gwtDevice;
}
use of org.eclipse.kapua.model.query.predicate.KapuaAndPredicate in project kapua by eclipse.
the class ServiceDAO method handleKapuaQueryPredicates.
/**
* Criteria for query entity utility method
*
* @param qp
* @param binds
* @param cb
* @param userPermissionRoot
* @param entityType
* @return
* @throws KapuaException
*/
@SuppressWarnings("rawtypes")
protected static <E> Expression<Boolean> handleKapuaQueryPredicates(KapuaPredicate qp, Map<ParameterExpression, Object> binds, CriteriaBuilder cb, Root<E> userPermissionRoot, EntityType<E> entityType) throws KapuaException {
Expression<Boolean> expr = null;
if (qp instanceof KapuaAttributePredicate) {
KapuaAttributePredicate attrPred = (KapuaAttributePredicate) qp;
expr = handleAttributePredicate(attrPred, binds, cb, userPermissionRoot, entityType);
} else if (qp instanceof KapuaAndPredicate) {
KapuaAndPredicate andPredicate = (KapuaAndPredicate) qp;
expr = handleAndPredicate(andPredicate, binds, cb, userPermissionRoot, entityType);
} else if (qp instanceof KapuaOrPredicate) {
KapuaOrPredicate andPredicate = (KapuaOrPredicate) qp;
expr = handleOrPredicate(andPredicate, binds, cb, userPermissionRoot, entityType);
}
return expr;
}
use of org.eclipse.kapua.model.query.predicate.KapuaAndPredicate in project kapua by eclipse.
the class ServiceDAO method handleAndPredicate.
@SuppressWarnings("rawtypes")
private static <E> Expression<Boolean> handleAndPredicate(KapuaAndPredicate andPredicate, Map<ParameterExpression, Object> binds, CriteriaBuilder cb, Root<E> entityRoot, EntityType<E> entityType) throws KapuaException {
List<Expression<Boolean>> exprs = new ArrayList<Expression<Boolean>>();
for (KapuaPredicate pred : andPredicate.getPredicates()) {
Expression<Boolean> expr = handleKapuaQueryPredicates(pred, binds, cb, entityRoot, entityType);
exprs.add(expr);
}
return cb.and(exprs.toArray(new Predicate[] {}));
}
Aggregations