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 General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (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 General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 package org.mxchange.addressbook.model.addressbook;
19 import java.text.MessageFormat;
20 import java.util.ArrayList;
21 import java.util.LinkedList;
22 import java.util.List;
23 import javax.ejb.Stateless;
24 import javax.persistence.NoResultException;
25 import javax.persistence.Query;
26 import org.mxchange.jcoreee.database.BaseDatabaseBean;
27 import org.mxchange.jjobs.exceptions.AddressbookNameAlreadyUsedException;
28 import org.mxchange.jjobs.exceptions.AddressbookNotFoundException;
29 import org.mxchange.jjobs.model.addressbook.Addressbook;
30 import org.mxchange.jjobs.model.addressbook.AddressbookSessionBeanRemote;
31 import org.mxchange.jjobs.model.addressbook.UserAddressbook;
32 import org.mxchange.jjobs.model.addressbook.entry.AddressbookEntry;
33 import org.mxchange.jjobs.model.addressbook.shared.ShareableAddressbook;
34 import org.mxchange.jusercore.model.user.User;
37 * A stateless bean handling addressbooks
39 * @author Roland Haeder<roland@mxchange.org>
41 @Stateless (name = "jjobs-adr", mappedName = "ejb/stateless-jjobs-adr", description = "A stateless bean for handling JJobs addressbooks")
42 public class JobsAddressbookSessionBean extends BaseDatabaseBean implements AddressbookSessionBeanRemote {
47 private static final long serialVersionUID = 129_857_871_287_691L;
50 @SuppressWarnings ("unchecked")
51 public List<AddressbookEntry> allEntries (final Addressbook addressbook) {
53 this.getLoggerBeanLocal().logTrace(MessageFormat.format("allEntries: addressbook={0} - CALLED!", addressbook)); //NOI18N
56 if (null == addressbook) {
58 throw new NullPointerException("addressbook is null");
59 } else if (addressbook.getAddressbookId() == null) {
61 throw new NullPointerException("addressbook.addressbookId is null");
62 } else if (addressbook.getAddressbookId() < 1) {
64 throw new IllegalArgumentException(MessageFormat.format("addressbook.addressbookId={0} is invalid.", addressbook.getAddressbookId()));
65 } else if (addressbook.getAddressbookUser() == null) {
67 throw new NullPointerException("addressbook.addressbookUser is null");
68 } else if (addressbook.getAddressbookUser().getUserId() == null) {
70 throw new NullPointerException("addressbook.addressbookUser.userId is null");
71 } else if (addressbook.getAddressbookUser().getUserId() < 1) {
72 // Invalid id number again
73 throw new IllegalArgumentException(MessageFormat.format("addressbook.addressbookUser.userId={0} is invalid", addressbook.getAddressbookUser().getUserId()));
77 Query query = this.getEntityManager().createNamedQuery("SearchUsersAddressbookEntries", List.class); //NOI18N
80 query.setParameter("addressbook", addressbook); //NOI18N
81 query.setParameter("owner", addressbook.getAddressbookUser()); //NOI18N
84 return query.getResultList();
88 public Addressbook createAddressbook (final Addressbook addressbook) throws AddressbookNameAlreadyUsedException {
90 if (null == addressbook) {
92 throw new NullPointerException("addressbook is null"); //NOI18N
93 } else if (addressbook.getAddressbookUser() == null) {
94 // User instance is null
95 throw new NullPointerException("addressbook.user should not be null."); //NOI18N
96 } else if (addressbook.getAddressbookName() == null) {
97 // Address book name not set
98 throw new NullPointerException("addressbook.addressbookName should not be null"); //NOI18N
99 } else if (addressbook.getAddressbookName().isEmpty()) {
100 // Address book name not set
101 throw new IllegalArgumentException("addressbook.addressbookName should not be empty"); //NOI18N
102 } else if (this.isAddressbookNameUsed(addressbook)) {
103 // The assigned user already used that name
104 throw new AddressbookNameAlreadyUsedException(addressbook);
108 this.getEntityManager().persist(addressbook);
110 // Flush it to get all data
111 this.getEntityManager().flush();
118 public Addressbook getAddressbookById (final Long addressbookId) throws AddressbookNotFoundException {
120 this.getLoggerBeanLocal().logTrace(MessageFormat.format("getAddressbookById: addressbookId={0} - CALLED!", addressbookId)); //NOI18N
122 // addressbookId should not be null or below 1
123 if (null == addressbookId) {
125 throw new NullPointerException("addressbookId is null"); //NOI18N
126 } else if (addressbookId < 1) {
128 throw new IllegalArgumentException(MessageFormat.format("addressbookId is not valid: {0}", addressbookId)); //NOI18N
129 } else if (!this.isAddressbookIdUsed(addressbookId)) {
130 // No address book found
131 throw new AddressbookNotFoundException(addressbookId);
134 // Get named query instance
135 Query query = this.getEntityManager().createNamedQuery("SearchAddressbookById", UserAddressbook.class); //NOI18N
138 query.setParameter("id", addressbookId); //NOI18N
141 return (Addressbook) query.getSingleResult();
145 @SuppressWarnings ("unchecked")
146 public List<Addressbook> getUsersAddressbookList (final User loggedInUser) {
148 this.getLoggerBeanLocal().logTrace(MessageFormat.format("getUsersList: loggedInUser={0} - CALLED!", loggedInUser)); //NOI18N
150 // Is the user instance null?
151 if (null == loggedInUser) {
153 throw new NullPointerException("loggedInUser is null"); //NOI18N
156 // Get query instance
157 Query query = this.getEntityManager().createNamedQuery("AllUsersAddressbooks", List.class); //NOI18N
160 query.setParameter("param", loggedInUser); //NOI18N
162 // Get full list from JPA
163 List<Addressbook> addressbooks = query.getResultList();
170 public boolean isAddressbookIdUsed (final Long addressbookId) {
172 this.getLoggerBeanLocal().logTrace(MessageFormat.format("isAddressbookIdUsed: addressbookId={0} - CALLED!", addressbookId)); //NOI18N
174 // Is it null or zero?
175 if (null == addressbookId) {
177 throw new NullPointerException("addressbookId is null"); //NOI18N
178 } else if (addressbookId < 1) {
179 // Not valid id number
180 throw new IllegalArgumentException(MessageFormat.format("addressbookId is not valid: {0}", addressbookId)); //NOI18N
183 // Get query instance
184 Query query = this.getEntityManager().createNamedQuery("SearchAddressbookById", UserAddressbook.class); //NOI18N
187 query.setParameter("id", addressbookId); //NOI18N
189 // Default is not valid
190 boolean isValid = false;
192 // Try it again, yes no other way
195 Addressbook addressbook = (Addressbook) query.getSingleResult();
198 this.getLoggerBeanLocal().logDebug(MessageFormat.format("isAddressbookIdUsed: addressbook={0} - FOUND!", addressbook)); //NOI18N
202 } catch (final NoResultException ex) {
203 // Debug log only, maybe out-dated link followed
204 this.getLoggerBeanLocal().logDebug(MessageFormat.format("isAddressbookIdUsed: addressbookId={0} is not valid: {1}", addressbookId, ex)); //NOI18N
208 this.getLoggerBeanLocal().logTrace(MessageFormat.format("isAddressbookIdUsed: isValid={0} - EXIT!", isValid)); //NOI18N
215 public boolean isAddressbookNameUsed (final Addressbook addressbook) {
217 if (null == addressbook) {
219 throw new NullPointerException("addressbook is null"); //NOI18N
220 } else if (addressbook.getAddressbookUser() == null) {
221 // User instance is null
222 throw new NullPointerException("addressbook.addressbookUser is null."); //NOI18N
223 } else if (addressbook.getAddressbookUser().getUserId() == null) {
224 // User instance is null
225 throw new NullPointerException("addressbook.addressbookUser.userId is null."); //NOI18N
226 } else if (addressbook.getAddressbookUser().getUserId() < 1) {
227 // User instance is null
228 throw new NullPointerException(MessageFormat.format("addressbook.addressbookUser.userId={0} is invalid.", addressbook.getAddressbookUser().getUserId())); //NOI18N
229 } else if (addressbook.getAddressbookName() == null) {
230 // Address book name not set
231 throw new NullPointerException("addressbook.addressbookName should not be null"); //NOI18N
232 } else if (addressbook.getAddressbookName().isEmpty()) {
233 // Address book name not set
234 throw new IllegalArgumentException("addressbook.addressbookName should not be empty"); //NOI18N
237 // Get query instance
238 Query query = this.getEntityManager().createNamedQuery("SearchUserAddressbookName", Addressbook.class); //NOI18N
241 query.setParameter("user", addressbook.getAddressbookUser()); //NOI18N
242 query.setParameter("name", addressbook.getAddressbookName()); //NOI18N
244 // Default is not found
245 boolean isUsed = false;
249 // Get a single result
250 Addressbook dummy = (Addressbook) query.getSingleResult();
253 this.getLoggerBeanLocal().logDebug(MessageFormat.format("isAddressbookNameUsed: Found an address book: {0}", dummy)); //NOI18N
257 } catch (final NoResultException ex) {
258 // No result found, so log it away
259 this.getLoggerBeanLocal().logDebug(MessageFormat.format("isAddressbookNameUsed: getSingleResult() did not return a result: {0}", ex)); //NOI18N