2 * Copyright (C) 2016 - 2024 Free Software Foundation
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.jaddressbook.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.enterprise.BaseAddressbookEnterpriseBean;
26 import org.mxchange.addressbook.model.addressbook.AddressbookSessionBeanRemote;
27 import org.mxchange.jaddressbook.exceptions.AddressbookNameAlreadyUsedException;
28 import org.mxchange.jaddressbook.model.addressbook.entry.AddressbookEntry;
29 import org.mxchange.jusercore.model.user.User;
32 * A stateless bean handling address books
34 * @author Roland Häder<roland@mxchange.org>
36 @Stateless (name = "addressbook", description = "A stateless bean for handling Addressbook addressbooks")
37 public class AddressbookSessionBean extends BaseAddressbookEnterpriseBean implements AddressbookSessionBeanRemote {
42 private static final long serialVersionUID = 129_857_871_287_691L;
45 public Addressbook createAddressbook (final Addressbook addressbook) throws AddressbookNameAlreadyUsedException {
47 if (null == addressbook) {
49 throw new NullPointerException("addressbook is null"); //NOI18N
50 } else if (addressbook.getAddressbookUser() == null) {
51 // User instance is null
52 throw new NullPointerException("addressbook.user should not be null."); //NOI18N
53 } else if (addressbook.getAddressbookName() == null) {
54 // Address book name not set
55 throw new NullPointerException("addressbook.addressbookName should not be null"); //NOI18N
56 } else if (addressbook.getAddressbookName().isEmpty()) {
57 // Address book name not set
58 throw new IllegalArgumentException("addressbook.addressbookName should not be empty"); //NOI18N
59 } else if (this.isAddressbookNameUsed(addressbook)) {
60 // The assigned user already used that name
61 throw new AddressbookNameAlreadyUsedException(addressbook);
64 // Add timestamp of creation
65 addressbook.setAddressbookCreated(new Date());
68 this.getEntityManager().persist(addressbook);
70 // Flush it to get all data
71 this.getEntityManager().flush();
78 @SuppressWarnings ("unchecked")
79 public List<Addressbook> fetchAddressbooksByUser (final User loggedInUser) {
81 this.getLoggerBeanLocal().logTrace(MessageFormat.format("getUsersList: loggedInUser={0} - CALLED!", loggedInUser)); //NOI18N
83 // Is the user instance null?
84 if (null == loggedInUser) {
86 throw new NullPointerException("loggedInUser is null"); //NOI18N
90 final Query query = this.getEntityManager().createNamedQuery("AllUsersAddressbooks", List.class); //NOI18N
93 query.setParameter("param", loggedInUser); //NOI18N
95 // Get full list from JPA
96 final List<Addressbook> addressbooks = query.getResultList();
103 @SuppressWarnings ("unchecked")
104 public List<AddressbookEntry> fetchAllAddressbookEntries (final Addressbook addressbook) {
106 this.getLoggerBeanLocal().logTrace(MessageFormat.format("allEntries: addressbook={0} - CALLED!", addressbook)); //NOI18N
108 // Validate parameter
109 if (null == addressbook) {
111 throw new NullPointerException("addressbook is null");
112 } else if (addressbook.getAddressbookId() == null) {
114 throw new NullPointerException("addressbook.addressbookId is null");
115 } else if (addressbook.getAddressbookId() < 1) {
117 throw new IllegalArgumentException(MessageFormat.format("addressbook.addressbookId={0} is invalid.", addressbook.getAddressbookId()));
118 } else if (addressbook.getAddressbookUser() == null) {
120 throw new NullPointerException("addressbook.addressbookUser is null");
121 } else if (addressbook.getAddressbookUser().getUserId() == null) {
123 throw new NullPointerException("addressbook.addressbookUser.userId is null");
124 } else if (addressbook.getAddressbookUser().getUserId() < 1) {
125 // Invalid id number again
126 throw new IllegalArgumentException(MessageFormat.format("addressbook.addressbookUser.userId={0} is invalid", addressbook.getAddressbookUser().getUserId()));
130 Query query = this.getEntityManager().createNamedQuery("SearchUsersAddressbookEntries", List.class); //NOI18N
133 query.setParameter("addressbook", addressbook); //NOI18N
134 query.setParameter("owner", addressbook.getAddressbookUser()); //NOI18N
137 return query.getResultList();
141 public boolean isAddressbookIdUsed (final Long addressbookId) {
143 this.getLoggerBeanLocal().logTrace(MessageFormat.format("isAddressbookIdUsed: addressbookId={0} - CALLED!", addressbookId)); //NOI18N
145 // Is it null or zero?
146 if (null == addressbookId) {
148 throw new NullPointerException("addressbookId is null"); //NOI18N
149 } else if (addressbookId < 1) {
150 // Not valid id number
151 throw new IllegalArgumentException(MessageFormat.format("addressbookId is not valid: {0}", addressbookId)); //NOI18N
154 // Get query instance
155 final Query query = this.getEntityManager().createNamedQuery("SearchAddressbookById", UserAddressbook.class); //NOI18N
158 query.setParameter("id", addressbookId); //NOI18N
160 // Default is not valid
161 boolean isValid = false;
163 // Try it again, yes no other way
166 final Addressbook addressbook = (Addressbook) query.getSingleResult();
169 this.getLoggerBeanLocal().logDebug(MessageFormat.format("isAddressbookIdUsed: addressbook={0} - FOUND!", addressbook)); //NOI18N
173 } catch (final NoResultException ex) {
174 // Debug log only, maybe out-dated link followed
175 this.getLoggerBeanLocal().logDebug(MessageFormat.format("isAddressbookIdUsed: addressbookId={0} is not valid: {1}", addressbookId, ex)); //NOI18N
179 this.getLoggerBeanLocal().logTrace(MessageFormat.format("isAddressbookIdUsed: isValid={0} - EXIT!", isValid)); //NOI18N
186 public boolean isAddressbookNameUsed (final Addressbook addressbook) {
188 this.getLoggerBeanLocal().logTrace(MessageFormat.format("isAddressbookNameUsed: addressbook={0} - CALLED!", addressbook));
191 if (null == addressbook) {
193 throw new NullPointerException("addressbook is null"); //NOI18N
194 } else if (addressbook.getAddressbookUser() == null) {
195 // User instance is null
196 throw new NullPointerException("addressbook.addressbookUser is null."); //NOI18N
197 } else if (addressbook.getAddressbookUser().getUserId() == null) {
198 // User instance is null
199 throw new NullPointerException("addressbook.addressbookUser.userId is null."); //NOI18N
200 } else if (addressbook.getAddressbookUser().getUserId() < 1) {
201 // User instance is null
202 throw new NullPointerException(MessageFormat.format("addressbook.addressbookUser.userId={0} is invalid.", addressbook.getAddressbookUser().getUserId())); //NOI18N
203 } else if (addressbook.getAddressbookName() == null) {
204 // Address book name not set
205 throw new NullPointerException("addressbook.addressbookName should not be null"); //NOI18N
206 } else if (addressbook.getAddressbookName().isEmpty()) {
207 // Address book name not set
208 throw new IllegalArgumentException("addressbook.addressbookName should not be empty"); //NOI18N
211 // Get query instance
212 final Query query = this.getEntityManager().createNamedQuery("SearchUserAddressbookName", Addressbook.class); //NOI18N
215 query.setParameter("user", addressbook.getAddressbookUser()); //NOI18N
216 query.setParameter("name", addressbook.getAddressbookName()); //NOI18N
218 // Default is not found
219 boolean isUsed = false;
223 // Get a single result
224 final Addressbook dummy = (Addressbook) query.getSingleResult();
227 this.getLoggerBeanLocal().logDebug(MessageFormat.format("isAddressbookNameUsed: Found an address book: {0}", dummy)); //NOI18N
231 } catch (final NoResultException ex) {
232 // No result found, so log it away
233 this.getLoggerBeanLocal().logDebug(MessageFormat.format("isAddressbookNameUsed: getSingleResult() did not return a result: {0}", ex)); //NOI18N