2 * Copyright (C) 2016 Roland Haeder
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.List;
21 import javax.ejb.Stateless;
22 import javax.persistence.NoResultException;
23 import javax.persistence.Query;
24 import org.mxchange.jcoreee.database.BaseDatabaseBean;
25 import org.mxchange.jjobs.exceptions.AddressbookNameAlreadyUsedException;
26 import org.mxchange.jjobs.exceptions.AddressbookNotFoundException;
27 import org.mxchange.jjobs.model.addressbook.entry.AddressbookEntry;
28 import org.mxchange.jusercore.model.user.User;
31 * A stateless bean handling addressbooks
33 * @author Roland Haeder<roland@mxchange.org>
35 @Stateless (name = "jjobs-adr", mappedName = "ejb/stateless-jjobs-adr", description = "A stateless bean for handling JJobs addressbooks")
36 public class JobsAddressbookSessionBean extends BaseDatabaseBean implements AddressbookSessionBeanRemote {
41 private static final long serialVersionUID = 129_857_871_287_691L;
44 @SuppressWarnings ("unchecked")
45 public List<AddressbookEntry> allEntries (final Addressbook addressbook) {
47 this.getLoggerBeanLocal().logTrace(MessageFormat.format("allEntries: addressbook={0} - CALLED!", addressbook)); //NOI18N
50 if (null == addressbook) {
52 throw new NullPointerException("addressbook is null");
53 } else if (addressbook.getAddressbookId() == null) {
55 throw new NullPointerException("addressbook.addressbookId is null");
56 } else if (addressbook.getAddressbookId() < 1) {
58 throw new IllegalArgumentException(MessageFormat.format("addressbook.addressbookId={0} is invalid.", addressbook.getAddressbookId()));
59 } else if (addressbook.getAddressbookUser() == null) {
61 throw new NullPointerException("addressbook.addressbookUser is null");
62 } else if (addressbook.getAddressbookUser().getUserId() == null) {
64 throw new NullPointerException("addressbook.addressbookUser.userId is null");
65 } else if (addressbook.getAddressbookUser().getUserId() < 1) {
66 // Invalid id number again
67 throw new IllegalArgumentException(MessageFormat.format("addressbook.addressbookUser.userId={0} is invalid", addressbook.getAddressbookUser().getUserId()));
71 Query query = this.getEntityManager().createNamedQuery("SearchUsersAddressbookEntries", List.class); //NOI18N
74 query.setParameter("addressbook", addressbook); //NOI18N
75 query.setParameter("owner", addressbook.getAddressbookUser()); //NOI18N
78 return query.getResultList();
82 public Addressbook createAddressbook (final Addressbook addressbook) throws AddressbookNameAlreadyUsedException {
84 if (null == addressbook) {
86 throw new NullPointerException("addressbook is null"); //NOI18N
87 } else if (addressbook.getAddressbookUser() == null) {
88 // User instance is null
89 throw new NullPointerException("addressbook.user should not be null."); //NOI18N
90 } else if (addressbook.getAddressbookName() == null) {
91 // Address book name not set
92 throw new NullPointerException("addressbook.addressbookName should not be null"); //NOI18N
93 } else if (addressbook.getAddressbookName().isEmpty()) {
94 // Address book name not set
95 throw new IllegalArgumentException("addressbook.addressbookName should not be empty"); //NOI18N
96 } else if (this.isAddressbookNameUsed(addressbook)) {
97 // The assigned user already used that name
98 throw new AddressbookNameAlreadyUsedException(addressbook);
102 this.getEntityManager().persist(addressbook);
104 // Flush it to get all data
105 this.getEntityManager().flush();
112 public Addressbook getAddressbookById (final Long addressbookId) throws AddressbookNotFoundException {
114 this.getLoggerBeanLocal().logTrace(MessageFormat.format("getAddressbookById: addressbookId={0} - CALLED!", addressbookId)); //NOI18N
116 // addressbookId should not be null or below 1
117 if (null == addressbookId) {
119 throw new NullPointerException("addressbookId is null"); //NOI18N
120 } else if (addressbookId < 1) {
122 throw new IllegalArgumentException(MessageFormat.format("addressbookId is not valid: {0}", addressbookId)); //NOI18N
123 } else if (!this.isAddressbookIdUsed(addressbookId)) {
124 // No address book found
125 throw new AddressbookNotFoundException(addressbookId);
128 // Get named query instance
129 Query query = this.getEntityManager().createNamedQuery("SearchAddressbookById", UserAddressbook.class); //NOI18N
132 query.setParameter("id", addressbookId); //NOI18N
135 return (Addressbook) query.getSingleResult();
139 @SuppressWarnings ("unchecked")
140 public List<Addressbook> getUsersAddressbookList (final User loggedInUser) {
142 this.getLoggerBeanLocal().logTrace(MessageFormat.format("getUsersList: loggedInUser={0} - CALLED!", loggedInUser)); //NOI18N
144 // Is the user instance null?
145 if (null == loggedInUser) {
147 throw new NullPointerException("loggedInUser is null"); //NOI18N
150 // Get query instance
151 Query query = this.getEntityManager().createNamedQuery("AllUsersAddressbooks", List.class); //NOI18N
154 query.setParameter("param", loggedInUser); //NOI18N
156 // Get full list from JPA
157 List<Addressbook> addressbooks = query.getResultList();
164 public boolean isAddressbookIdUsed (final Long addressbookId) {
166 this.getLoggerBeanLocal().logTrace(MessageFormat.format("isAddressbookIdUsed: addressbookId={0} - CALLED!", addressbookId)); //NOI18N
168 // Is it null or zero?
169 if (null == addressbookId) {
171 throw new NullPointerException("addressbookId is null"); //NOI18N
172 } else if (addressbookId < 1) {
173 // Not valid id number
174 throw new IllegalArgumentException(MessageFormat.format("addressbookId is not valid: {0}", addressbookId)); //NOI18N
177 // Get query instance
178 Query query = this.getEntityManager().createNamedQuery("SearchAddressbookById", UserAddressbook.class); //NOI18N
181 query.setParameter("id", addressbookId); //NOI18N
183 // Default is not valid
184 boolean isValid = false;
186 // Try it again, yes no other way
189 Addressbook addressbook = (Addressbook) query.getSingleResult();
192 this.getLoggerBeanLocal().logDebug(MessageFormat.format("isAddressbookIdUsed: addressbook={0} - FOUND!", addressbook)); //NOI18N
196 } catch (final NoResultException ex) {
197 // Debug log only, maybe out-dated link followed
198 this.getLoggerBeanLocal().logDebug(MessageFormat.format("isAddressbookIdUsed: addressbookId={0} is not valid: {1}", addressbookId, ex)); //NOI18N
202 this.getLoggerBeanLocal().logTrace(MessageFormat.format("isAddressbookIdUsed: isValid={0} - EXIT!", isValid)); //NOI18N
209 public boolean isAddressbookNameUsed (final Addressbook addressbook) {
211 if (null == addressbook) {
213 throw new NullPointerException("addressbook is null"); //NOI18N
214 } else if (addressbook.getAddressbookUser() == null) {
215 // User instance is null
216 throw new NullPointerException("addressbook.addressbookUser is null."); //NOI18N
217 } else if (addressbook.getAddressbookUser().getUserId() == null) {
218 // User instance is null
219 throw new NullPointerException("addressbook.addressbookUser.userId is null."); //NOI18N
220 } else if (addressbook.getAddressbookUser().getUserId() < 1) {
221 // User instance is null
222 throw new NullPointerException(MessageFormat.format("addressbook.addressbookUser.userId={0} is invalid.", addressbook.getAddressbookUser().getUserId())); //NOI18N
223 } else if (addressbook.getAddressbookName() == null) {
224 // Address book name not set
225 throw new NullPointerException("addressbook.addressbookName should not be null"); //NOI18N
226 } else if (addressbook.getAddressbookName().isEmpty()) {
227 // Address book name not set
228 throw new IllegalArgumentException("addressbook.addressbookName should not be empty"); //NOI18N
231 // Get query instance
232 Query query = this.getEntityManager().createNamedQuery("SearchUserAddressbookName", Addressbook.class); //NOI18N
235 query.setParameter("user", addressbook.getAddressbookUser()); //NOI18N
236 query.setParameter("name", addressbook.getAddressbookName()); //NOI18N
238 // Default is not found
239 boolean isUsed = false;
243 // Get a single result
244 Addressbook dummy = (Addressbook) query.getSingleResult();
247 this.getLoggerBeanLocal().logDebug(MessageFormat.format("isAddressbookNameUsed: Found an address book: {0}", dummy)); //NOI18N
251 } catch (final NoResultException ex) {
252 // No result found, so log it away
253 this.getLoggerBeanLocal().logDebug(MessageFormat.format("isAddressbookNameUsed: getSingleResult() did not return a result: {0}", ex)); //NOI18N