Search in sources :

Example 1 with AndPredicate

use of org.eclipse.kapua.commons.model.query.predicate.AndPredicate in project kapua by eclipse.

the class AbstractKapuaConfigurableService method setConfigValues.

@Override
public void setConfigValues(KapuaId scopeId, Map<String, Object> values) throws KapuaException {
    KapuaLocator locator = KapuaLocator.getInstance();
    AuthorizationService authorizationService = locator.getService(AuthorizationService.class);
    PermissionFactory permissionFactory = locator.getFactory(PermissionFactory.class);
    authorizationService.checkPermission(permissionFactory.newPermission(domain, Actions.write, scopeId));
    KapuaTocd ocd = this.getConfigMetadata();
    validateConfigurations(this.pid, ocd, values);
    Properties props = toProperties(values);
    AndPredicate predicate = new AndPredicate().and(new AttributePredicate<String>("pid", this.pid, Operator.EQUAL)).and(new AttributePredicate<KapuaId>("scopeId", scopeId, Operator.EQUAL));
    ServiceConfigQueryImpl query = new ServiceConfigQueryImpl(scopeId);
    query.setPredicate(predicate);
    ServiceConfig serviceConfig = null;
    EntityManager em = this.entityManagerFactory.createEntityManager();
    ServiceConfigListResultImpl result = ServiceConfigDAO.query(em, ServiceConfig.class, ServiceConfigImpl.class, new ServiceConfigListResultImpl(), query);
    // In not exists create then return
    if (result == null || result.getSize() == 0) {
        ServiceConfigImpl serviceConfigNew = new ServiceConfigImpl(scopeId);
        serviceConfigNew.setPid(this.pid);
        serviceConfigNew.setConfigurations(props);
        serviceConfig = this.create(em, serviceConfigNew);
        return;
    }
    // If exists update it
    serviceConfig = result.getItem(0);
    serviceConfig.setConfigurations(props);
    this.update(em, serviceConfig);
    return;
}
Also used : KapuaLocator(org.eclipse.kapua.locator.KapuaLocator) PermissionFactory(org.eclipse.kapua.service.authorization.permission.PermissionFactory) AndPredicate(org.eclipse.kapua.commons.model.query.predicate.AndPredicate) Properties(java.util.Properties) AttributePredicate(org.eclipse.kapua.commons.model.query.predicate.AttributePredicate) EntityManager(org.eclipse.kapua.commons.jpa.EntityManager) AuthorizationService(org.eclipse.kapua.service.authorization.AuthorizationService) KapuaId(org.eclipse.kapua.model.id.KapuaId) KapuaTocd(org.eclipse.kapua.model.config.metatype.KapuaTocd)

Example 2 with AndPredicate

use of org.eclipse.kapua.commons.model.query.predicate.AndPredicate in project kapua by eclipse.

the class AbstractKapuaConfigurableService method getConfigValues.

@Override
public Map<String, Object> getConfigValues(KapuaId scopeId) throws KapuaException {
    KapuaLocator locator = KapuaLocator.getInstance();
    AuthorizationService authorizationService = locator.getService(AuthorizationService.class);
    PermissionFactory permissionFactory = locator.getFactory(PermissionFactory.class);
    authorizationService.checkPermission(permissionFactory.newPermission(domain, Actions.read, scopeId));
    AndPredicate predicate = new AndPredicate().and(new AttributePredicate<String>("pid", this.pid, Operator.EQUAL)).and(new AttributePredicate<KapuaId>("scopeId", scopeId, Operator.EQUAL));
    ServiceConfigQueryImpl query = new ServiceConfigQueryImpl(scopeId);
    query.setPredicate(predicate);
    Properties properties = null;
    EntityManager em = this.entityManagerFactory.createEntityManager();
    ServiceConfigListResult result = ServiceConfigDAO.query(em, ServiceConfig.class, ServiceConfigImpl.class, new ServiceConfigListResultImpl(), query);
    if (result != null && result.getSize() > 0)
        properties = result.getItem(0).getConfigurations();
    KapuaTocd ocd = this.getConfigMetadata();
    return toValues(ocd, properties);
}
Also used : KapuaLocator(org.eclipse.kapua.locator.KapuaLocator) PermissionFactory(org.eclipse.kapua.service.authorization.permission.PermissionFactory) AndPredicate(org.eclipse.kapua.commons.model.query.predicate.AndPredicate) Properties(java.util.Properties) AttributePredicate(org.eclipse.kapua.commons.model.query.predicate.AttributePredicate) EntityManager(org.eclipse.kapua.commons.jpa.EntityManager) AuthorizationService(org.eclipse.kapua.service.authorization.AuthorizationService) KapuaId(org.eclipse.kapua.model.id.KapuaId) KapuaTocd(org.eclipse.kapua.model.config.metatype.KapuaTocd)

Example 3 with AndPredicate

use of org.eclipse.kapua.commons.model.query.predicate.AndPredicate 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 4 with AndPredicate

use of org.eclipse.kapua.commons.model.query.predicate.AndPredicate in project kapua by eclipse.

the class DeviceExporterServlet method internalDoGet.

private void internalDoGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    try {
        // parameter extraction
        String format = request.getParameter("format");
        String scopeIdString = request.getParameter("scopeIdString");
        // data exporter
        DeviceExporter deviceExporter = null;
        if ("xls".equals(format)) {
            deviceExporter = new DeviceExporterExcel(response);
        } else if ("csv".equals(format)) {
            deviceExporter = new DeviceExporterCsv(response);
        } else {
            throw new IllegalArgumentException("format");
        }
        if (scopeIdString == null || scopeIdString.isEmpty()) {
            throw new IllegalArgumentException("account");
        }
        deviceExporter.init(scopeIdString);
        // 
        // get the devices and append them to the exporter
        KapuaLocator locator = KapuaLocator.getInstance();
        DeviceRegistryService drs = locator.getService(DeviceRegistryService.class);
        DeviceFactory drf = locator.getFactory(DeviceFactory.class);
        int resultsCount = 0;
        int offset = 0;
        // paginate through the matching message
        DeviceQuery dq = drf.newQuery(KapuaEid.parseShortId(scopeIdString));
        dq.setLimit(250);
        // Inserting filter parameter if specified
        AndPredicate andPred = new AndPredicate();
        String clientId = request.getParameter("clientId");
        if (clientId != null && !clientId.isEmpty()) {
            andPred = andPred.and(new AttributePredicate<String>(DevicePredicates.CLIENT_ID, clientId, Operator.STARTS_WITH));
        }
        String displayName = request.getParameter("displayName");
        if (displayName != null && !displayName.isEmpty()) {
            andPred = andPred.and(new AttributePredicate<String>(DevicePredicates.DISPLAY_NAME, displayName, Operator.STARTS_WITH));
        }
        String serialNumber = request.getParameter("serialNumber");
        if (serialNumber != null && !serialNumber.isEmpty()) {
            andPred = andPred.and(new AttributePredicate<String>(DevicePredicates.SERIAL_NUMBER, serialNumber));
        }
        String deviceStatus = request.getParameter("deviceStatus");
        if (deviceStatus != null && !deviceStatus.isEmpty()) {
            andPred = andPred.and(new AttributePredicate<DeviceStatus>(DevicePredicates.STATUS, DeviceStatus.valueOf(deviceStatus)));
        }
        String sortAttribute = request.getParameter("sortAttribute");
        if (sortAttribute != null && !sortAttribute.isEmpty()) {
            String sortOrderString = request.getParameter("sortOrder");
            SortOrder sortOrder;
            if (sortOrderString != null && !sortOrderString.isEmpty()) {
                sortOrder = SortOrder.valueOf(sortOrderString);
            } else {
                sortOrder = SortOrder.ASCENDING;
            }
            if (sortAttribute.compareTo("CLIENT_ID") == 0) {
                dq.setSortCriteria(new FieldSortCriteria(DevicePredicates.CLIENT_ID, sortOrder));
            } else if (sortAttribute.compareTo("DISPLAY_NAME") == 0) {
                dq.setSortCriteria(new FieldSortCriteria(DevicePredicates.DISPLAY_NAME, sortOrder));
            } else if (sortAttribute.compareTo("LAST_EVENT_ON") == 0) {
                dq.setSortCriteria(new FieldSortCriteria(DevicePredicates.LAST_EVENT_ON, sortOrder));
            }
        }
        dq.setPredicate(andPred);
        KapuaListResult<Device> results = null;
        do {
            dq.setOffset(offset);
            results = drs.query(dq);
            deviceExporter.append(results);
            offset += results.getSize();
            resultsCount += results.getSize();
        } while (results.getSize() > 0);
        // Close things up
        deviceExporter.close();
    } catch (IllegalArgumentException iae) {
        response.sendError(400, "Illegal value for query parameter: " + iae.getMessage());
        return;
    } catch (KapuaEntityNotFoundException eenfe) {
        response.sendError(400, eenfe.getMessage());
        return;
    } catch (KapuaUnauthenticatedException eiae) {
        response.sendError(401, eiae.getMessage());
        return;
    } catch (KapuaIllegalAccessException eiae) {
        response.sendError(403, eiae.getMessage());
        return;
    } catch (Exception e) {
        s_logger.error("Error creating device export", e);
        throw new ServletException(e);
    }
}
Also used : KapuaLocator(org.eclipse.kapua.locator.KapuaLocator) Device(org.eclipse.kapua.service.device.registry.Device) AndPredicate(org.eclipse.kapua.commons.model.query.predicate.AndPredicate) SortOrder(org.eclipse.kapua.commons.model.query.FieldSortCriteria.SortOrder) DeviceFactory(org.eclipse.kapua.service.device.registry.DeviceFactory) AttributePredicate(org.eclipse.kapua.commons.model.query.predicate.AttributePredicate) KapuaIllegalAccessException(org.eclipse.kapua.KapuaIllegalAccessException) ServletException(javax.servlet.ServletException) KapuaEntityNotFoundException(org.eclipse.kapua.KapuaEntityNotFoundException) IOException(java.io.IOException) KapuaUnauthenticatedException(org.eclipse.kapua.KapuaUnauthenticatedException) KapuaIllegalAccessException(org.eclipse.kapua.KapuaIllegalAccessException) ServletException(javax.servlet.ServletException) FieldSortCriteria(org.eclipse.kapua.commons.model.query.FieldSortCriteria) DeviceRegistryService(org.eclipse.kapua.service.device.registry.DeviceRegistryService) DeviceQuery(org.eclipse.kapua.service.device.registry.DeviceQuery) KapuaEntityNotFoundException(org.eclipse.kapua.KapuaEntityNotFoundException) KapuaUnauthenticatedException(org.eclipse.kapua.KapuaUnauthenticatedException)

Example 5 with AndPredicate

use of org.eclipse.kapua.commons.model.query.predicate.AndPredicate 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

AndPredicate (org.eclipse.kapua.commons.model.query.predicate.AndPredicate)7 AttributePredicate (org.eclipse.kapua.commons.model.query.predicate.AttributePredicate)6 KapuaLocator (org.eclipse.kapua.locator.KapuaLocator)6 KapuaId (org.eclipse.kapua.model.id.KapuaId)5 FieldSortCriteria (org.eclipse.kapua.commons.model.query.FieldSortCriteria)4 KapuaAndPredicate (org.eclipse.kapua.model.query.predicate.KapuaAndPredicate)4 DeviceEventQuery (org.eclipse.kapua.service.device.registry.event.DeviceEventQuery)3 BasePagingLoadConfig (com.extjs.gxt.ui.client.data.BasePagingLoadConfig)2 ArrayList (java.util.ArrayList)2 Properties (java.util.Properties)2 GwtDevice (org.eclipse.kapua.app.console.shared.model.GwtDevice)2 GwtDeviceEvent (org.eclipse.kapua.app.console.shared.model.GwtDeviceEvent)2 EntityManager (org.eclipse.kapua.commons.jpa.EntityManager)2 SortOrder (org.eclipse.kapua.commons.model.query.FieldSortCriteria.SortOrder)2 KapuaTocd (org.eclipse.kapua.model.config.metatype.KapuaTocd)2 AuthorizationService (org.eclipse.kapua.service.authorization.AuthorizationService)2 PermissionFactory (org.eclipse.kapua.service.authorization.permission.PermissionFactory)2 Device (org.eclipse.kapua.service.device.registry.Device)2 DeviceFactory (org.eclipse.kapua.service.device.registry.DeviceFactory)2 DeviceQuery (org.eclipse.kapua.service.device.registry.DeviceQuery)2