Skip to content

Package: UserDAO

UserDAO

nameinstructionbranchcomplexitylinemethod
UserDAO()
M: 3 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
count(EntityManager, KapuaQuery)
M: 6 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
create(EntityManager, UserCreator)
M: 45 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 10 C: 0
0%
M: 1 C: 0
0%
delete(EntityManager, KapuaId, KapuaId)
M: 7 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
find(EntityManager, KapuaId, KapuaId)
M: 7 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
findByExternalId(EntityManager, String)
M: 7 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
findByExternalUsername(EntityManager, String)
M: 7 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
findByName(EntityManager, String)
M: 7 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
query(EntityManager, KapuaQuery)
M: 10 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
update(EntityManager, User)
M: 9 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 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: * Red Hat Inc
13: *******************************************************************************/
14: package org.eclipse.kapua.service.user.internal;
15:
16: import org.eclipse.kapua.KapuaEntityNotFoundException;
17: import org.eclipse.kapua.KapuaException;
18: import org.eclipse.kapua.commons.jpa.EntityManager;
19: import org.eclipse.kapua.commons.service.internal.ServiceDAO;
20: import org.eclipse.kapua.model.KapuaNamedEntityAttributes;
21: import org.eclipse.kapua.model.id.KapuaId;
22: import org.eclipse.kapua.model.query.KapuaQuery;
23: import org.eclipse.kapua.service.user.User;
24: import org.eclipse.kapua.service.user.UserAttributes;
25: import org.eclipse.kapua.service.user.UserCreator;
26: import org.eclipse.kapua.service.user.UserListResult;
27:
28: /**
29: * {@link User} {@link ServiceDAO}
30: *
31: * @since 1.0.0
32: */
33: public class UserDAO extends ServiceDAO {
34:
35: /**
36: * Creates and return new {@link User}
37: *
38: * @param em
39: * @param userCreator
40: * @return
41: * @since 1.0.0
42: */
43: public static User create(EntityManager em, UserCreator userCreator) {
44: //
45: // Create User
46: UserImpl userImpl = new UserImpl(userCreator.getScopeId(), userCreator.getName());
47:
48: userImpl.setDisplayName(userCreator.getDisplayName());
49: userImpl.setEmail(userCreator.getEmail());
50: userImpl.setPhoneNumber(userCreator.getPhoneNumber());
51: userImpl.setUserType(userCreator.getUserType());
52: userImpl.setExternalId(userCreator.getExternalId());
53: userImpl.setExternalUsername(userCreator.getExternalUsername());
54: userImpl.setStatus(userCreator.getStatus());
55: userImpl.setExpirationDate(userCreator.getExpirationDate());
56:
57: return ServiceDAO.create(em, userImpl);
58: }
59:
60: /**
61: * Updates the provided {@link User}
62: *
63: * @param em
64: * @param user
65: * @return
66: * @throws KapuaException
67: * @since 1.0.0
68: */
69: public static User update(EntityManager em, User user) throws KapuaException {
70: UserImpl userImpl = (UserImpl) user;
71:
72: return ServiceDAO.update(em, UserImpl.class, userImpl);
73: }
74:
75: /**
76: * Finds the {@link User} by {@link User} identifier
77: *
78: * @param em
79: * @param scopeId
80: * @param userId
81: * @return
82: * @since 1.0.0
83: */
84: public static User find(EntityManager em, KapuaId scopeId, KapuaId userId) {
85: return ServiceDAO.find(em, UserImpl.class, scopeId, userId);
86: }
87:
88: /**
89: * Finds the {@link User} by the {@link org.eclipse.kapua.service.user.UserAttributes#NAME}
90: *
91: * @param em
92: * @param name
93: * @return
94: */
95: public static User findByName(EntityManager em, String name) {
96: return ServiceDAO.findByField(em, UserImpl.class, KapuaNamedEntityAttributes.NAME, name);
97: }
98:
99: /**
100: * Finds the {@link User} by the {@link UserAttributes#EXTERNAL_ID}
101: *
102: * @param em the entity manager to use
103: * @param externalId id the external ID so search for
104: * @return the user record, may be {@code null}
105: */
106: public static User findByExternalId(final EntityManager em, final String externalId) {
107: return ServiceDAO.findByField(em, UserImpl.class, UserAttributes.EXTERNAL_ID, externalId);
108: }
109:
110:
111: /**
112: * Finds the {@link User} by the {@link UserAttributes#EXTERNAL_USERNAME}.
113: *
114: * @param em the entity manager to use
115: * @param externalUsername id the external username so search for
116: * @return the user record, may be {@code null}
117: * @since 2.0.0
118: */
119: public static User findByExternalUsername(EntityManager em, String externalUsername) {
120: return ServiceDAO.findByField(em, UserImpl.class, UserAttributes.EXTERNAL_USERNAME, externalUsername);
121: }
122:
123: /**
124: * Returns the user list matching the provided query
125: *
126: * @param em
127: * @param userPermissionQuery
128: * @return
129: * @throws KapuaException
130: */
131: public static UserListResult query(EntityManager em, KapuaQuery userPermissionQuery)
132: throws KapuaException {
133: return ServiceDAO.query(em, User.class, UserImpl.class, new UserListResultImpl(), userPermissionQuery);
134: }
135:
136: /**
137: * Returns the user count matching the provided query
138: *
139: * @param em
140: * @param userPermissionQuery
141: * @return
142: * @throws KapuaException
143: */
144: public static long count(EntityManager em, KapuaQuery userPermissionQuery)
145: throws KapuaException {
146: return ServiceDAO.count(em, User.class, UserImpl.class, userPermissionQuery);
147: }
148:
149: /**
150: * Deletes the user by user identifier
151: *
152: * @param em
153: * @param scopeId
154: * @param userId
155: * @return the deleted {@link User}
156: * @throws KapuaEntityNotFoundException If {@link User} is not found.
157: */
158: public static User delete(EntityManager em, KapuaId scopeId, KapuaId userId)
159: throws KapuaEntityNotFoundException {
160: return ServiceDAO.delete(em, UserImpl.class, scopeId, userId);
161: }
162:
163: }