2 * Copyright (C) 2016, 2017 Roland Häder
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU Affero General Public License as
6 * published by the Free Software Foundation, either version 3 of the
7 * License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU Affero General Public License for more details.
14 * You should have received a copy of the GNU Affero General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 package org.mxchange.jjobs.model.addressbook;
19 import java.text.MessageFormat;
20 import java.util.Date;
21 import java.util.List;
22 import javax.ejb.Stateless;
23 import javax.persistence.NoResultException;
24 import javax.persistence.Query;
25 import org.mxchange.addressbook.model.addressbook.AddressbookSessionBeanRemote;
26 import org.mxchange.jaddressbook.exceptions.AddressbookNameAlreadyUsedException;
27 import org.mxchange.jaddressbook.exceptions.AddressbookNotFoundException;
28 import org.mxchange.jaddressbook.model.addressbook.Addressbook;
29 import org.mxchange.jaddressbook.model.addressbook.UserAddressbook;
30 import org.mxchange.jaddressbook.model.addressbook.entry.AddressbookEntry;
31 import org.mxchange.jjobs.beans.ejb.BaseJobsEnterpriseBean;
32 import org.mxchange.jusercore.model.user.User;
35 * A stateless bean handling address books
37 * @author Roland Häder<roland@mxchange.org>
39 @Stateless (name = "addressbook", description = "A stateless bean for handling JJobs addressbooks")
40 public class JobsAddressbookSessionBean extends BaseJobsEnterpriseBean implements AddressbookSessionBeanRemote {
45 private static final long serialVersionUID = 129_857_871_287_691L;
50 public JobsAddressbookSessionBean () {
51 // Call super constructor
56 @SuppressWarnings ("unchecked")
57 public List<AddressbookEntry> allEntries (final Addressbook addressbook) {
59 this.getLoggerBeanLocal().logTrace(MessageFormat.format("allEntries: addressbook={0} - CALLED!", addressbook)); //NOI18N
62 if (null == addressbook) {
64 throw new NullPointerException("addressbook is null"); //NOI18N
65 } else if (addressbook.getAddressbookId() == null) {
67 throw new NullPointerException("addressbook.addressbookId is null"); //NOI18N
68 } else if (addressbook.getAddressbookId() < 1) {
70 throw new IllegalArgumentException(MessageFormat.format("addressbook.addressbookId={0} is invalid.", addressbook.getAddressbookId())); //NOI18N
71 } else if (addressbook.getAddressbookUser() == null) {
73 throw new NullPointerException("addressbook.addressbookUser is null"); //NOI18N
74 } else if (addressbook.getAddressbookUser().getUserId() == null) {
76 throw new NullPointerException("addressbook.addressbookUser.userId is null"); //NOI18N
77 } else if (addressbook.getAddressbookUser().getUserId() < 1) {
78 // Invalid id number again
79 throw new IllegalArgumentException(MessageFormat.format("addressbook.addressbookUser.userId={0} is invalid", addressbook.getAddressbookUser().getUserId())); //NOI18N
83 Query query = this.getEntityManager().createNamedQuery("SearchUsersAddressbookEntries", List.class); //NOI18N
86 query.setParameter("addressbook", addressbook); //NOI18N
87 query.setParameter("owner", addressbook.getAddressbookUser()); //NOI18N
90 return query.getResultList();
94 public Addressbook createAddressbook (final Addressbook addressbook) throws AddressbookNameAlreadyUsedException {
96 if (null == addressbook) {
98 throw new NullPointerException("addressbook is null"); //NOI18N
99 } else if (addressbook.getAddressbookUser() == null) {
100 // User instance is null
101 throw new NullPointerException("addressbook.user should not be null."); //NOI18N
102 } else if (addressbook.getAddressbookName() == null) {
103 // Address book name not set
104 throw new NullPointerException("addressbook.addressbookName should not be null"); //NOI18N
105 } else if (addressbook.getAddressbookName().isEmpty()) {
106 // Address book name not set
107 throw new IllegalArgumentException("addressbook.addressbookName should not be empty"); //NOI18N
108 } else if (this.isAddressbookNameUsed(addressbook)) {
109 // The assigned user already used that name
110 throw new AddressbookNameAlreadyUsedException(addressbook);
113 // Add timestamp of creation
114 addressbook.setAddressbookCreated(new Date());
117 this.getEntityManager().persist(addressbook);
119 // Flush it to get all data
120 this.getEntityManager().flush();
127 public Addressbook getAddressbookById (final Long addressbookId) throws AddressbookNotFoundException {
129 this.getLoggerBeanLocal().logTrace(MessageFormat.format("getAddressbookById: addressbookId={0} - CALLED!", addressbookId)); //NOI18N
131 // addressbookId should not be null or below 1
132 if (null == addressbookId) {
134 throw new NullPointerException("addressbookId is null"); //NOI18N
135 } else if (addressbookId < 1) {
137 throw new IllegalArgumentException(MessageFormat.format("addressbookId is not valid: {0}", addressbookId)); //NOI18N
138 } else if (!this.isAddressbookIdUsed(addressbookId)) {
139 // No address book found
140 throw new AddressbookNotFoundException(addressbookId);
143 // Get named query instance
144 Query query = this.getEntityManager().createNamedQuery("SearchAddressbookById", UserAddressbook.class); //NOI18N
147 query.setParameter("id", addressbookId); //NOI18N
150 return (Addressbook) query.getSingleResult();
154 @SuppressWarnings ("unchecked")
155 public List<Addressbook> getUsersAddressbookList (final User loggedInUser) {
157 this.getLoggerBeanLocal().logTrace(MessageFormat.format("getUsersList: loggedInUser={0} - CALLED!", loggedInUser)); //NOI18N
159 // Is the user instance null?
160 if (null == loggedInUser) {
162 throw new NullPointerException("loggedInUser is null"); //NOI18N
165 // Get query instance
166 Query query = this.getEntityManager().createNamedQuery("AllUsersAddressbooks", List.class); //NOI18N
169 query.setParameter("param", loggedInUser); //NOI18N
171 // Get full list from JPA
172 List<Addressbook> addressbooks = query.getResultList();
179 public boolean isAddressbookIdUsed (final Long addressbookId) {
181 this.getLoggerBeanLocal().logTrace(MessageFormat.format("isAddressbookIdUsed: addressbookId={0} - CALLED!", addressbookId)); //NOI18N
183 // Is it null or zero?
184 if (null == addressbookId) {
186 throw new NullPointerException("addressbookId is null"); //NOI18N
187 } else if (addressbookId < 1) {
188 // Not valid id number
189 throw new IllegalArgumentException(MessageFormat.format("addressbookId is not valid: {0}", addressbookId)); //NOI18N
192 // Get query instance
193 Query query = this.getEntityManager().createNamedQuery("SearchAddressbookById", UserAddressbook.class); //NOI18N
196 query.setParameter("id", addressbookId); //NOI18N
198 // Default is not valid
199 boolean isValid = false;
201 // Try it again, yes no other way
204 Addressbook addressbook = (Addressbook) query.getSingleResult();
207 this.getLoggerBeanLocal().logDebug(MessageFormat.format("isAddressbookIdUsed: addressbook={0} - FOUND!", addressbook)); //NOI18N
211 } catch (final NoResultException ex) {
212 // Debug log only, maybe out-dated link followed
213 this.getLoggerBeanLocal().logDebug(MessageFormat.format("isAddressbookIdUsed: addressbookId={0} is not valid: {1}", addressbookId, ex)); //NOI18N
217 this.getLoggerBeanLocal().logTrace(MessageFormat.format("isAddressbookIdUsed: isValid={0} - EXIT!", isValid)); //NOI18N
224 public boolean isAddressbookNameUsed (final Addressbook addressbook) {
226 if (null == addressbook) {
228 throw new NullPointerException("addressbook is null"); //NOI18N
229 } else if (addressbook.getAddressbookUser() == null) {
230 // User instance is null
231 throw new NullPointerException("addressbook.addressbookUser is null."); //NOI18N
232 } else if (addressbook.getAddressbookUser().getUserId() == null) {
233 // User instance is null
234 throw new NullPointerException("addressbook.addressbookUser.userId is null."); //NOI18N
235 } else if (addressbook.getAddressbookUser().getUserId() < 1) {
236 // User instance is null
237 throw new NullPointerException(MessageFormat.format("addressbook.addressbookUser.userId={0} is invalid.", addressbook.getAddressbookUser().getUserId())); //NOI18N
238 } else if (addressbook.getAddressbookName() == null) {
239 // Address book name not set
240 throw new NullPointerException("addressbook.addressbookName should not be null"); //NOI18N
241 } else if (addressbook.getAddressbookName().isEmpty()) {
242 // Address book name not set
243 throw new IllegalArgumentException("addressbook.addressbookName should not be empty"); //NOI18N
246 // Get query instance
247 Query query = this.getEntityManager().createNamedQuery("SearchUserAddressbookName", Addressbook.class); //NOI18N
250 query.setParameter("user", addressbook.getAddressbookUser()); //NOI18N
251 query.setParameter("name", addressbook.getAddressbookName()); //NOI18N
253 // Default is not found
254 boolean isUsed = false;
258 // Get a single result
259 Addressbook dummy = (Addressbook) query.getSingleResult();
262 this.getLoggerBeanLocal().logDebug(MessageFormat.format("isAddressbookNameUsed: Found an address book: {0}", dummy)); //NOI18N
266 } catch (final NoResultException ex) {
267 // No result found, so log it away
268 this.getLoggerBeanLocal().logDebug(MessageFormat.format("isAddressbookNameUsed: getSingleResult() did not return a result: {0}", ex)); //NOI18N