Search in sources :

Example 1 with KapuaAndPredicate

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;
}
Also used : KapuaLocator(org.eclipse.kapua.locator.KapuaLocator) GwtDeviceEvent(org.eclipse.kapua.app.console.shared.model.GwtDeviceEvent) DeviceEvent(org.eclipse.kapua.service.device.registry.event.DeviceEvent) BasePagingLoadConfig(com.extjs.gxt.ui.client.data.BasePagingLoadConfig) ArrayList(java.util.ArrayList) KapuaAndPredicate(org.eclipse.kapua.model.query.predicate.KapuaAndPredicate) AndPredicate(org.eclipse.kapua.commons.model.query.predicate.AndPredicate) DeviceEventFactory(org.eclipse.kapua.service.device.registry.event.DeviceEventFactory) DeviceEventQuery(org.eclipse.kapua.service.device.registry.event.DeviceEventQuery) GwtDeviceEvent(org.eclipse.kapua.app.console.shared.model.GwtDeviceEvent) FieldSortCriteria(org.eclipse.kapua.commons.model.query.FieldSortCriteria) KapuaAndPredicate(org.eclipse.kapua.model.query.predicate.KapuaAndPredicate) DeviceEventService(org.eclipse.kapua.service.device.registry.event.DeviceEventService) KapuaId(org.eclipse.kapua.model.id.KapuaId)

Example 2 with KapuaAndPredicate

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);
}
Also used : FieldSortCriteria(org.eclipse.kapua.commons.model.query.FieldSortCriteria) KapuaAndPredicate(org.eclipse.kapua.model.query.predicate.KapuaAndPredicate) DeviceEventListResult(org.eclipse.kapua.service.device.registry.event.DeviceEventListResult) KapuaAndPredicate(org.eclipse.kapua.model.query.predicate.KapuaAndPredicate) AndPredicate(org.eclipse.kapua.commons.model.query.predicate.AndPredicate) KapuaId(org.eclipse.kapua.model.id.KapuaId) DeviceEventQuery(org.eclipse.kapua.service.device.registry.event.DeviceEventQuery) DateTime(org.joda.time.DateTime) KapuaAttributePredicate(org.eclipse.kapua.model.query.predicate.KapuaAttributePredicate) AttributePredicate(org.eclipse.kapua.commons.model.query.predicate.AttributePredicate)

Example 3 with KapuaAndPredicate

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;
}
Also used : KapuaLocator(org.eclipse.kapua.locator.KapuaLocator) GwtDevice(org.eclipse.kapua.app.console.shared.model.GwtDevice) DeviceConnectionFactory(org.eclipse.kapua.service.device.registry.connection.DeviceConnectionFactory) KapuaAndPredicate(org.eclipse.kapua.model.query.predicate.KapuaAndPredicate) DeviceConnectionQuery(org.eclipse.kapua.service.device.registry.connection.DeviceConnectionQuery) KapuaAndPredicate(org.eclipse.kapua.model.query.predicate.KapuaAndPredicate) AndPredicate(org.eclipse.kapua.commons.model.query.predicate.AndPredicate) DeviceConnection(org.eclipse.kapua.service.device.registry.connection.DeviceConnection) DeviceConnectionService(org.eclipse.kapua.service.device.registry.connection.DeviceConnectionService) AttributePredicate(org.eclipse.kapua.commons.model.query.predicate.AttributePredicate)

Example 4 with KapuaAndPredicate

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;
}
Also used : KapuaAndPredicate(org.eclipse.kapua.model.query.predicate.KapuaAndPredicate) KapuaOrPredicate(org.eclipse.kapua.model.query.predicate.KapuaOrPredicate) KapuaAttributePredicate(org.eclipse.kapua.model.query.predicate.KapuaAttributePredicate)

Example 5 with KapuaAndPredicate

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[] {}));
}
Also used : Expression(javax.persistence.criteria.Expression) ParameterExpression(javax.persistence.criteria.ParameterExpression) ArrayList(java.util.ArrayList) KapuaPredicate(org.eclipse.kapua.model.query.predicate.KapuaPredicate) KapuaAndPredicate(org.eclipse.kapua.model.query.predicate.KapuaAndPredicate) KapuaOrPredicate(org.eclipse.kapua.model.query.predicate.KapuaOrPredicate) Predicate(javax.persistence.criteria.Predicate) KapuaPredicate(org.eclipse.kapua.model.query.predicate.KapuaPredicate) KapuaAttributePredicate(org.eclipse.kapua.model.query.predicate.KapuaAttributePredicate)

Aggregations

KapuaAndPredicate (org.eclipse.kapua.model.query.predicate.KapuaAndPredicate)5 AndPredicate (org.eclipse.kapua.commons.model.query.predicate.AndPredicate)3 KapuaAttributePredicate (org.eclipse.kapua.model.query.predicate.KapuaAttributePredicate)3 ArrayList (java.util.ArrayList)2 FieldSortCriteria (org.eclipse.kapua.commons.model.query.FieldSortCriteria)2 AttributePredicate (org.eclipse.kapua.commons.model.query.predicate.AttributePredicate)2 KapuaLocator (org.eclipse.kapua.locator.KapuaLocator)2 KapuaId (org.eclipse.kapua.model.id.KapuaId)2 KapuaOrPredicate (org.eclipse.kapua.model.query.predicate.KapuaOrPredicate)2 DeviceEventQuery (org.eclipse.kapua.service.device.registry.event.DeviceEventQuery)2 BasePagingLoadConfig (com.extjs.gxt.ui.client.data.BasePagingLoadConfig)1 Expression (javax.persistence.criteria.Expression)1 ParameterExpression (javax.persistence.criteria.ParameterExpression)1 Predicate (javax.persistence.criteria.Predicate)1 GwtDevice (org.eclipse.kapua.app.console.shared.model.GwtDevice)1 GwtDeviceEvent (org.eclipse.kapua.app.console.shared.model.GwtDeviceEvent)1 KapuaPredicate (org.eclipse.kapua.model.query.predicate.KapuaPredicate)1 DeviceConnection (org.eclipse.kapua.service.device.registry.connection.DeviceConnection)1 DeviceConnectionFactory (org.eclipse.kapua.service.device.registry.connection.DeviceConnectionFactory)1 DeviceConnectionQuery (org.eclipse.kapua.service.device.registry.connection.DeviceConnectionQuery)1