Skip to content

Package: EventStoreServiceImpl

EventStoreServiceImpl

nameinstructionbranchcomplexitylinemethod
EventStoreServiceImpl(EntityManagerFactory)
M: 4 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
count(KapuaQuery)
M: 19 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%
create(EventStoreRecordCreator)
M: 4 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
delete(KapuaId, KapuaId)
M: 23 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 6 C: 0
0%
M: 1 C: 0
0%
find(KapuaId)
M: 14 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%
find(KapuaId, KapuaId)
M: 17 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 4 C: 0
0%
M: 1 C: 0
0%
findById(KapuaId)
M: 10 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
lambda$count$3(KapuaQuery, EntityManager)
M: 5 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
lambda$delete$1(KapuaId, KapuaId, EntityManager)
M: 5 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
lambda$findById$4(KapuaId, EntityManager)
M: 5 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
lambda$query$2(KapuaQuery, EntityManager)
M: 4 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
lambda$update$0(EventStoreRecord, EntityManager)
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%
query(KapuaQuery)
M: 18 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%
static {...}
M: 13 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%
update(EventStoreRecord)
M: 19 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 3 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.commons.service.event.store.internal;
14:
15: import org.eclipse.kapua.KapuaEntityNotFoundException;
16: import org.eclipse.kapua.KapuaException;
17: import org.eclipse.kapua.commons.jpa.EntityManagerFactory;
18: import org.eclipse.kapua.commons.service.event.store.api.EventStoreDomains;
19: import org.eclipse.kapua.commons.service.event.store.api.EventStoreRecord;
20: import org.eclipse.kapua.commons.service.event.store.api.EventStoreRecordCreator;
21: import org.eclipse.kapua.commons.service.event.store.api.EventStoreRecordListResult;
22: import org.eclipse.kapua.commons.service.event.store.api.EventStoreService;
23: import org.eclipse.kapua.commons.service.internal.AbstractKapuaService;
24: import org.eclipse.kapua.commons.util.ArgumentValidator;
25: import org.eclipse.kapua.event.RaiseServiceEvent;
26: import org.eclipse.kapua.locator.KapuaLocator;
27: import org.eclipse.kapua.model.KapuaEntityAttributes;
28: import org.eclipse.kapua.model.domain.Actions;
29: import org.eclipse.kapua.model.id.KapuaId;
30: import org.eclipse.kapua.model.query.KapuaQuery;
31: import org.eclipse.kapua.service.authorization.AuthorizationService;
32: import org.eclipse.kapua.service.authorization.permission.PermissionFactory;
33:
34: import javax.inject.Inject;
35:
36: /**
37: * {@link EventStoreService} implementation.
38: *
39: * @since 1.0.0
40: */
41: public class EventStoreServiceImpl extends AbstractKapuaService implements EventStoreService {
42:
43: private static final KapuaLocator LOCATOR = KapuaLocator.getInstance();
44: private static final AuthorizationService AUTHORIZATION_SERVICE = LOCATOR.getService(AuthorizationService.class);
45: private static final PermissionFactory PERMISSION_FACTORY = LOCATOR.getFactory(PermissionFactory.class);
46:
47: /**
48: * Constructor.
49: *
50: * @since 1.0.0
51: */
52: @Inject
53: public EventStoreServiceImpl(EntityManagerFactory entityManagerFactory) {
54: super(entityManagerFactory);
55: }
56:
57: @Override
58: @RaiseServiceEvent
59: public EventStoreRecord create(EventStoreRecordCreator kapuaEventCreator)
60: throws KapuaException {
61:
62: throw new UnsupportedOperationException();
63: }
64:
65: @Override
66: @RaiseServiceEvent
67: public EventStoreRecord update(EventStoreRecord kapuaEvent)
68: throws KapuaException {
69: //
70: // Validation of the fields
71: ArgumentValidator.notNull(kapuaEvent.getId(), "kapuaEvent.id");
72:
73: //
74: // Check Access
75: AUTHORIZATION_SERVICE.checkPermission(PERMISSION_FACTORY.newPermission(EventStoreDomains.EVENT_STORE_DOMAIN, Actions.write, kapuaEvent.getScopeId()));
76:
77: //
78: // Do update
79: return entityManagerSession.doTransactedAction(em -> {
80: EventStoreRecord oldKapuaEvent = EventStoreDAO.find(em, kapuaEvent.getScopeId(), kapuaEvent.getId());
81:• if (oldKapuaEvent == null) {
82: throw new KapuaEntityNotFoundException(EventStoreRecord.TYPE, kapuaEvent.getId());
83: }
84:
85: // Update
86: return EventStoreDAO.update(em, kapuaEvent);
87: });
88: }
89:
90: @Override
91: @RaiseServiceEvent
92: public void delete(KapuaId scopeId, KapuaId kapuaEventId)
93: throws KapuaException {
94:
95: //
96: // Validation of the fields
97: ArgumentValidator.notNull(scopeId, KapuaEntityAttributes.SCOPE_ID);
98: ArgumentValidator.notNull(kapuaEventId, KapuaEntityAttributes.ENTITY_ID);
99:
100: //
101: // Check Access
102: Actions action = Actions.write;
103: AUTHORIZATION_SERVICE.checkPermission(PERMISSION_FACTORY.newPermission(EventStoreDomains.EVENT_STORE_DOMAIN, action, scopeId));
104:
105: //
106: // Do delete
107: entityManagerSession.doTransactedAction(em -> EventStoreDAO.delete(em, scopeId, kapuaEventId));
108: }
109:
110: @Override
111: public EventStoreRecord find(KapuaId scopeId, KapuaId kapuaEventId)
112: throws KapuaException {
113: //
114: // Validation of the fields
115: ArgumentValidator.notNull(scopeId, KapuaEntityAttributes.SCOPE_ID);
116: ArgumentValidator.notNull(kapuaEventId, KapuaEntityAttributes.ENTITY_ID);
117:
118: //
119: // Check Access
120: AUTHORIZATION_SERVICE.checkPermission(PERMISSION_FACTORY.newPermission(EventStoreDomains.EVENT_STORE_DOMAIN, Actions.read, scopeId));
121:
122: //
123: // Make sure kapuaEvent exists
124: return findById(kapuaEventId);
125: }
126:
127: @Override
128: public EventStoreRecord find(KapuaId kapuaEventId)
129: throws KapuaException {
130: //
131: // Validation of the fields
132: ArgumentValidator.notNull(kapuaEventId, KapuaEntityAttributes.ENTITY_ID);
133:
134: //
135: // Check Access
136: AUTHORIZATION_SERVICE.checkPermission(PERMISSION_FACTORY.newPermission(EventStoreDomains.EVENT_STORE_DOMAIN, Actions.read, kapuaEventId));
137:
138: //
139: // Make sure kapuaEvent exists
140: return findById(kapuaEventId);
141: }
142:
143: @Override
144: public EventStoreRecordListResult query(KapuaQuery query)
145: throws KapuaException {
146: ArgumentValidator.notNull(query, "query");
147:
148: //
149: // Check Access
150: AUTHORIZATION_SERVICE.checkPermission(PERMISSION_FACTORY.newPermission(EventStoreDomains.EVENT_STORE_DOMAIN, Actions.read, query.getScopeId()));
151:
152: return entityManagerSession.doAction(em -> EventStoreDAO.query(em, query));
153: }
154:
155: @Override
156: public long count(KapuaQuery query)
157: throws KapuaException {
158: ArgumentValidator.notNull(query, "query");
159:
160: //
161: // Check Access
162: AUTHORIZATION_SERVICE.checkPermission(PERMISSION_FACTORY.newPermission(EventStoreDomains.EVENT_STORE_DOMAIN, Actions.read, query.getScopeId()));
163:
164: return entityManagerSession.doAction(em -> EventStoreDAO.count(em, query));
165: }
166:
167: /**
168: * Find an {@link EventStoreRecord} without authorization checks.
169: *
170: * @param kapuaEventId
171: * @return
172: * @throws KapuaException
173: * @since 1.0.0
174: */
175: private EventStoreRecord findById(KapuaId kapuaEventId)
176: throws KapuaException {
177: //
178: // Argument Validation
179: ArgumentValidator.notNull(kapuaEventId, KapuaEntityAttributes.ENTITY_ID);
180:
181: return entityManagerSession.doAction(em -> EventStoreDAO.find(em, null, kapuaEventId));
182: }
183:
184: }