Search in sources :

Example 1 with DeviceEventListResult

use of org.eclipse.kapua.service.device.registry.event.DeviceEventListResult in project kapua by eclipse.

the class DeviceEventServiceImpl method query.

@Override
public DeviceEventListResult query(KapuaQuery<DeviceEvent> query) throws KapuaException {
    // 
    // Argument Validation
    ArgumentValidator.notNull(query, "query");
    ArgumentValidator.notNull(query.getScopeId(), "query.scopeId");
    // 
    // Check Access
    authorizationService.checkPermission(permissionFactory.newPermission(DeviceEventDomain.DEVICE_EVENT, Actions.read, query.getScopeId()));
    // 
    // Do Query
    DeviceEventListResult result = null;
    EntityManager em = DeviceEntityManagerFactory.getEntityManager();
    try {
        result = DeviceEventDAO.query(em, query);
    } catch (Exception e) {
        throw KapuaExceptionUtils.convertPersistenceException(e);
    } finally {
        em.close();
    }
    return result;
}
Also used : EntityManager(org.eclipse.kapua.commons.jpa.EntityManager) DeviceEventListResult(org.eclipse.kapua.service.device.registry.event.DeviceEventListResult) KapuaEntityNotFoundException(org.eclipse.kapua.KapuaEntityNotFoundException) KapuaException(org.eclipse.kapua.KapuaException)

Example 2 with DeviceEventListResult

use of org.eclipse.kapua.service.device.registry.event.DeviceEventListResult 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)

Aggregations

DeviceEventListResult (org.eclipse.kapua.service.device.registry.event.DeviceEventListResult)2 KapuaEntityNotFoundException (org.eclipse.kapua.KapuaEntityNotFoundException)1 KapuaException (org.eclipse.kapua.KapuaException)1 EntityManager (org.eclipse.kapua.commons.jpa.EntityManager)1 FieldSortCriteria (org.eclipse.kapua.commons.model.query.FieldSortCriteria)1 AndPredicate (org.eclipse.kapua.commons.model.query.predicate.AndPredicate)1 AttributePredicate (org.eclipse.kapua.commons.model.query.predicate.AttributePredicate)1 KapuaId (org.eclipse.kapua.model.id.KapuaId)1 KapuaAndPredicate (org.eclipse.kapua.model.query.predicate.KapuaAndPredicate)1 KapuaAttributePredicate (org.eclipse.kapua.model.query.predicate.KapuaAttributePredicate)1 DeviceEventQuery (org.eclipse.kapua.service.device.registry.event.DeviceEventQuery)1 DateTime (org.joda.time.DateTime)1