Skip to content

Package: AccountDAO

AccountDAO

nameinstructionbranchcomplexitylinemethod
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, AccountCreator)
M: 68 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 16 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%
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, Account)
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: *******************************************************************************/
13: package org.eclipse.kapua.service.account.internal;
14:
15: import org.eclipse.kapua.KapuaEntityNotFoundException;
16: import org.eclipse.kapua.KapuaException;
17: import org.eclipse.kapua.commons.jpa.EntityManager;
18: import org.eclipse.kapua.commons.service.internal.ServiceDAO;
19: import org.eclipse.kapua.model.KapuaNamedEntityAttributes;
20: import org.eclipse.kapua.model.id.KapuaId;
21: import org.eclipse.kapua.model.query.KapuaQuery;
22: import org.eclipse.kapua.service.account.Account;
23: import org.eclipse.kapua.service.account.AccountCreator;
24: import org.eclipse.kapua.service.account.AccountListResult;
25: import org.eclipse.kapua.service.account.AccountQuery;
26:
27: import javax.validation.constraints.NotNull;
28:
29: /**
30: * {@link Account} {@link ServiceDAO}.
31: *
32: * @since 1.0.0
33: */
34: public class AccountDAO {
35:
36: /**
37: * Constructor.
38: *
39: * @since 1.0.0
40: */
41: private AccountDAO() {
42: }
43:
44: /**
45: * Persists an Account from the given {@link AccountCreator}.
46: *
47: * @param em The {@link EntityManager} than owns the transaction.
48: * @param accountCreator The {@link AccountCreator} to persist.
49: * @return The newly persisted {@link Account}
50: * @throws KapuaException
51: * @since 1.0.0
52: */
53: public static Account create(@NotNull EntityManager em, @NotNull AccountCreator accountCreator)
54: throws KapuaException {
55: OrganizationImpl organizationImpl = new OrganizationImpl();
56: organizationImpl.setName(accountCreator.getOrganizationName());
57: organizationImpl.setPersonName(accountCreator.getOrganizationPersonName());
58: organizationImpl.setEmail(accountCreator.getOrganizationEmail());
59: organizationImpl.setPhoneNumber(accountCreator.getOrganizationPhoneNumber());
60: organizationImpl.setAddressLine1(accountCreator.getOrganizationAddressLine1());
61: organizationImpl.setAddressLine2(accountCreator.getOrganizationAddressLine2());
62: organizationImpl.setAddressLine3(accountCreator.getOrganizationAddressLine3());
63: organizationImpl.setCity(accountCreator.getOrganizationCity());
64: organizationImpl.setZipPostCode(accountCreator.getOrganizationZipPostCode());
65: organizationImpl.setStateProvinceCounty(accountCreator.getOrganizationStateProvinceCounty());
66: organizationImpl.setCountry(accountCreator.getOrganizationCountry());
67:
68: AccountImpl accountImpl = new AccountImpl(accountCreator.getScopeId(), accountCreator.getName());
69: accountImpl.setOrganization(organizationImpl);
70: accountImpl.setExpirationDate(accountCreator.getExpirationDate());
71:
72: // Do create
73: return ServiceDAO.create(em, accountImpl);
74: }
75:
76: /**
77: * Updates the given {@link Account}
78: *
79: * @param em The {@link EntityManager} than owns the transaction.
80: * @param account The {@link Account} to updated.
81: * @return The updated {@link Account}
82: * @throws KapuaException
83: * @since 1.0.0
84: */
85: public static Account update(@NotNull EntityManager em, @NotNull Account account)
86: throws KapuaException {
87: AccountImpl accountImpl = (AccountImpl) account;
88:
89: // Do update
90: return ServiceDAO.update(em, AccountImpl.class, accountImpl);
91: }
92:
93: /**
94: * Finds an {@link Account} by {@link Account#getScopeId()} and {@link Account#getId()}
95: *
96: * @param em The {@link EntityManager} than owns the transaction.
97: * @param scopeId The {@link Account#getScopeId()}.
98: * @param accountId The {@link Account#getId()}.
99: * @return The {@link Account} found or {@code null}
100: * @since 1.0.0
101: */
102: public static Account find(@NotNull EntityManager em, @NotNull KapuaId scopeId, @NotNull KapuaId accountId) {
103: return ServiceDAO.find(em, AccountImpl.class, scopeId, accountId);
104: }
105:
106: /**
107: * Finds an {@link Account} by {@link Account#getName()}.
108: *
109: * @param em The {@link EntityManager} than owns the transaction.
110: * @param name The {@link Account#getName()}.
111: * @return The {@link Account} found or {@code null}
112: * @since 1.0.0
113: */
114: public static Account findByName(@NotNull EntityManager em, @NotNull String name) {
115: return ServiceDAO.findByField(em, AccountImpl.class, KapuaNamedEntityAttributes.NAME, name);
116: }
117:
118: /**
119: * Finds the {@link Account}s that matches the given {@link AccountQuery}.
120: *
121: * @param em The {@link EntityManager} than owns the transaction.
122: * @param accountQuery The {@link AccountQuery} to filter results.
123: * @return The {@link AccountListResult} that matches the {@link AccountQuery}.
124: * @throws KapuaException
125: * @since 1.0.0
126: */
127: public static AccountListResult query(@NotNull EntityManager em, @NotNull KapuaQuery accountQuery)
128: throws KapuaException {
129: return ServiceDAO.query(em, Account.class, AccountImpl.class, new AccountListResultImpl(), accountQuery);
130: }
131:
132: /**
133: * Counts the {@link Account}s that matches the given {@link AccountQuery}.
134: *
135: * @param em The {@link EntityManager} than owns the transaction.
136: * @param accountQuery The {@link AccountQuery} to filter results.
137: * @return The count that matches the {@link AccountQuery}.
138: * @throws KapuaException
139: * @since 1.0.0
140: */
141: public static long count(@NotNull EntityManager em, @NotNull KapuaQuery accountQuery)
142: throws KapuaException {
143: return ServiceDAO.count(em, Account.class, AccountImpl.class, accountQuery);
144: }
145:
146: /**
147: * Deletes an {@link Account} by {@link Account#getScopeId()} and {@link Account#getId()}
148: *
149: * @param em The {@link EntityManager} than owns the transaction.
150: * @param scopeId The {@link Account#getScopeId()}.
151: * @param accountId The {@link Account#getId()}.
152: * @return The {@link Account} deleted
153: * @throws KapuaEntityNotFoundException if {@link Account} is not found before deletion.
154: * @since 1.0.0
155: */
156: public static Account delete(@NotNull EntityManager em, @NotNull KapuaId scopeId, @NotNull KapuaId accountId)
157: throws KapuaEntityNotFoundException {
158: return ServiceDAO.delete(em, AccountImpl.class, scopeId, accountId);
159: }
160: }