Skip to content

Package: DeviceEvents

DeviceEvents

nameinstructionbranchcomplexitylinemethod
DeviceEvents()
M: 27 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 5 C: 0
0%
M: 1 C: 0
0%
count(ScopeId, EntityId, DeviceEventQuery)
M: 29 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 5 C: 0
0%
M: 1 C: 0
0%
deleteDeviceEvent(ScopeId, EntityId, EntityId)
M: 20 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 4 C: 0
0%
M: 1 C: 0
0%
find(ScopeId, EntityId, EntityId)
M: 66 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 13 C: 0
0%
M: 1 C: 0
0%
query(ScopeId, EntityId, DeviceEventQuery)
M: 40 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 8 C: 0
0%
M: 1 C: 0
0%
simpleQuery(ScopeId, EntityId, String, DateParam, DateParam, String, SortOrder, boolean, int, int)
M: 92 C: 0
0%
M: 10 C: 0
0%
M: 6 C: 0
0%
M: 17 C: 0
0%
M: 1 C: 0
0%

Coverage

1: /*******************************************************************************
2: * Copyright (c) 2016, 2022 Eurotech and/or its affiliates and others
3: *
4: * This program and the accompanying materials are made
5: * available under the terms of the Eclipse Public License 2.0
6: * which is available at https://www.eclipse.org/legal/epl-2.0/
7: *
8: * SPDX-License-Identifier: EPL-2.0
9: *
10: * Contributors:
11: * Eurotech - initial API and implementation
12: *******************************************************************************/
13: package org.eclipse.kapua.app.api.resources.v1.resources;
14:
15: import com.google.common.base.Strings;
16:
17: import org.eclipse.kapua.KapuaEntityNotFoundException;
18: import org.eclipse.kapua.KapuaException;
19: import org.eclipse.kapua.app.api.core.resources.AbstractKapuaResource;
20: import org.eclipse.kapua.app.api.core.model.CountResult;
21: import org.eclipse.kapua.app.api.core.model.DateParam;
22: import org.eclipse.kapua.app.api.core.model.EntityId;
23: import org.eclipse.kapua.app.api.core.model.ScopeId;
24: import org.eclipse.kapua.locator.KapuaLocator;
25: import org.eclipse.kapua.model.KapuaEntityAttributes;
26: import org.eclipse.kapua.model.query.SortOrder;
27: import org.eclipse.kapua.model.query.predicate.AndPredicate;
28: import org.eclipse.kapua.model.query.predicate.AttributePredicate.Operator;
29: import org.eclipse.kapua.service.KapuaService;
30: import org.eclipse.kapua.service.device.registry.Device;
31: import org.eclipse.kapua.service.device.registry.DeviceRegistryService;
32: import org.eclipse.kapua.service.device.registry.event.DeviceEvent;
33: import org.eclipse.kapua.service.device.registry.event.DeviceEventAttributes;
34: import org.eclipse.kapua.service.device.registry.event.DeviceEventFactory;
35: import org.eclipse.kapua.service.device.registry.event.DeviceEventListResult;
36: import org.eclipse.kapua.service.device.registry.event.DeviceEventQuery;
37: import org.eclipse.kapua.service.device.registry.event.DeviceEventService;
38:
39: import javax.ws.rs.Consumes;
40: import javax.ws.rs.DELETE;
41: import javax.ws.rs.DefaultValue;
42: import javax.ws.rs.GET;
43: import javax.ws.rs.POST;
44: import javax.ws.rs.Path;
45: import javax.ws.rs.PathParam;
46: import javax.ws.rs.Produces;
47: import javax.ws.rs.QueryParam;
48: import javax.ws.rs.core.MediaType;
49: import javax.ws.rs.core.Response;
50:
51: @Path("{scopeId}/devices/{deviceId}/events")
52: public class DeviceEvents extends AbstractKapuaResource {
53:
54: private final KapuaLocator locator = KapuaLocator.getInstance();
55: private final DeviceEventService deviceEventService = locator.getService(DeviceEventService.class);
56: private final DeviceEventFactory deviceEventFactory = locator.getFactory(DeviceEventFactory.class);
57:
58: private final DeviceRegistryService deviceRegistryService = locator.getService(DeviceRegistryService.class);
59:
60: /**
61: * Gets the {@link DeviceEvent} list in the scope.
62: *
63: * @param scopeId The {@link ScopeId} in which to search results.
64: * @param deviceId The id of the {@link Device} in which to search results
65: * @param resource The resource of the {@link DeviceEvent} in which to search results
66: * @param sortParam The name of the parameter that will be used as a sorting key
67: * @param sortDir The sort direction. Can be ASCENDING (default), DESCENDING. Case-insensitive.
68: * @param askTotalCount Ask for the total count of the matched entities in the result
69: * @param offset The result set offset.
70: * @param limit The result set limit.
71: * @return The {@link DeviceEventListResult} of all the deviceEvents associated to the current selected scope.
72: * @throws KapuaException Whenever something bad happens. See specific {@link KapuaService} exceptions.
73: * @since 1.0.0
74: */
75: @GET
76: @Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
77: public DeviceEventListResult simpleQuery(
78: @PathParam("scopeId") ScopeId scopeId,
79: @PathParam("deviceId") EntityId deviceId,
80: @QueryParam("resource") String resource,
81: @QueryParam("startDate") DateParam startDateParam,
82: @QueryParam("endDate") DateParam endDateParam,
83: @QueryParam("sortParam") String sortParam,
84: @QueryParam("sortDir") @DefaultValue("ASCENDING") SortOrder sortDir,
85: @QueryParam("askTotalCount") boolean askTotalCount,
86: @QueryParam("offset") @DefaultValue("0") int offset,
87: @QueryParam("limit") @DefaultValue("50") int limit) throws KapuaException {
88: DeviceEventQuery query = deviceEventFactory.newQuery(scopeId);
89:
90:• if (deviceRegistryService.find(scopeId, deviceId) == null) {
91: throw new KapuaEntityNotFoundException(Device.TYPE, deviceId);
92: }
93:
94: AndPredicate andPredicate = query.andPredicate(query.attributePredicate(DeviceEventAttributes.DEVICE_ID, deviceId));
95:
96:• if (!Strings.isNullOrEmpty(resource)) {
97: andPredicate.and(query.attributePredicate(DeviceEventAttributes.RESOURCE, resource));
98: }
99:
100:• if (startDateParam != null) {
101: andPredicate.and(query.attributePredicate(DeviceEventAttributes.RECEIVED_ON, startDateParam.getDate(), Operator.GREATER_THAN_OR_EQUAL));
102: }
103:• if (endDateParam != null) {
104: andPredicate.and(query.attributePredicate(DeviceEventAttributes.RECEIVED_ON, endDateParam.getDate(), Operator.LESS_THAN_OR_EQUAL));
105: }
106:
107: query.setPredicate(andPredicate);
108:
109:• if (!Strings.isNullOrEmpty(sortParam)) {
110: query.setSortCriteria(query.fieldSortCriteria(sortParam, sortDir));
111: }
112: query.setAskTotalCount(askTotalCount);
113: query.setOffset(offset);
114: query.setLimit(limit);
115:
116: return query(scopeId, deviceId, query);
117: }
118:
119: /**
120: * Queries the results with the given {@link DeviceEventQuery} parameter.
121: *
122: * @param scopeId The {@link ScopeId} in which to search results.
123: * @param deviceId The id of the {@link Device} in which to search results
124: * @param query The {@link DeviceEventQuery} to use to filter results.
125: * @return The {@link DeviceEventListResult} of all the result matching the given {@link DeviceEventQuery} parameter.
126: * @throws KapuaException Whenever something bad happens. See specific {@link KapuaService} exceptions.
127: * @since 1.0.0
128: */
129: @POST
130: @Path("_query")
131: @Consumes({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
132: @Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
133: public DeviceEventListResult query(
134: @PathParam("scopeId") ScopeId scopeId,
135: @PathParam("deviceId") EntityId deviceId,
136: DeviceEventQuery query) throws KapuaException {
137: query.setScopeId(scopeId);
138:
139:• if (deviceRegistryService.find(scopeId, deviceId) == null) {
140: throw new KapuaEntityNotFoundException(Device.TYPE, deviceId);
141: }
142:
143: AndPredicate andPredicate = query.andPredicate(
144: query.attributePredicate(DeviceEventAttributes.DEVICE_ID, deviceId),
145: query.getPredicate()
146: );
147:
148: query.setPredicate(andPredicate);
149:
150: return deviceEventService.query(query);
151: }
152:
153: /**
154: * Counts the results with the given {@link DeviceEventQuery} parameter.
155: *
156: * @param scopeId The {@link ScopeId} in which to search results.
157: * @param deviceId The id of the {@link Device} in which to search results
158: * @param query The {@link DeviceEventQuery} to use to filter results.
159: * @return The count of all the result matching the given {@link DeviceEventQuery} parameter.
160: * @throws KapuaException Whenever something bad happens. See specific {@link KapuaService} exceptions.
161: * @since 1.0.0
162: */
163: @POST
164: @Path("_count")
165: @Consumes({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
166: @Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
167: public CountResult count(
168: @PathParam("scopeId") ScopeId scopeId,
169: @PathParam("deviceId") EntityId deviceId,
170: DeviceEventQuery query) throws KapuaException {
171:
172:• if (deviceRegistryService.find(scopeId, deviceId) == null) {
173: throw new KapuaEntityNotFoundException(Device.TYPE, deviceId);
174: }
175:
176: query.setScopeId(scopeId);
177: query.setPredicate(query.attributePredicate(DeviceEventAttributes.DEVICE_ID, deviceId));
178:
179: return new CountResult(deviceEventService.count(query));
180: }
181:
182: /**
183: * Returns the DeviceEvent specified by the "deviceEventId" path parameter.
184: *
185: * @param scopeId The {@link ScopeId} of the requested {@link DeviceEvent}.
186: * @param deviceId The {@link Device} id of the request {@link DeviceEvent}.
187: * @param deviceEventId The id of the requested DeviceEvent.
188: * @return The requested DeviceEvent object.
189: * @throws KapuaException Whenever something bad happens. See specific {@link KapuaService} exceptions.
190: * @since 1.0.0
191: */
192: @GET
193: @Path("{deviceEventId}")
194: @Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
195: public DeviceEvent find(
196: @PathParam("scopeId") ScopeId scopeId,
197: @PathParam("deviceId") EntityId deviceId,
198: @PathParam("deviceEventId") EntityId deviceEventId) throws KapuaException {
199:
200:• if (deviceRegistryService.find(scopeId, deviceId) == null) {
201: throw new KapuaEntityNotFoundException(Device.TYPE, deviceId);
202: }
203:
204: DeviceEventQuery query = deviceEventFactory.newQuery(scopeId);
205:
206: AndPredicate andPredicate = query.andPredicate(
207: query.attributePredicate(DeviceEventAttributes.DEVICE_ID, deviceId),
208: query.attributePredicate(KapuaEntityAttributes.ENTITY_ID, deviceEventId)
209: );
210:
211: query.setPredicate(andPredicate);
212: query.setOffset(0);
213: query.setLimit(1);
214:
215: DeviceEventListResult results = deviceEventService.query(query);
216:
217:• if (!results.isEmpty()) {
218: return results.getFirstItem();
219: } else {
220: throw new KapuaEntityNotFoundException(DeviceEvent.TYPE, deviceEventId);
221: }
222: }
223:
224: /**
225: * Deletes the DeviceEvent specified by the "deviceEventId" path parameter.
226: *
227: * @param deviceId The id of the Device in which to delete the event
228: * @param deviceEventId The id of the DeviceEvent to be deleted.
229: * @return HTTP 200 if operation has completed successfully.
230: * @throws KapuaException Whenever something bad happens. See specific {@link KapuaService} exceptions.
231: * @since 1.0.0
232: */
233: @DELETE
234: @Path("{deviceEventId}")
235: public Response deleteDeviceEvent(@PathParam("scopeId") ScopeId scopeId,
236: @PathParam("deviceId") EntityId deviceId,
237: @PathParam("deviceEventId") EntityId deviceEventId) throws KapuaException {
238:
239:• if (deviceRegistryService.find(scopeId, deviceId) == null) {
240: throw new KapuaEntityNotFoundException(Device.TYPE, deviceId);
241: }
242:
243: deviceEventService.delete(scopeId, deviceEventId);
244:
245: return returnNoContent();
246: }
247: }