2 * Copyright (C) 2015 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.List;
21 import javax.ejb.Stateless;
22 import javax.persistence.NoResultException;
23 import javax.persistence.Query;
24 import org.mxchange.addressbook.exceptions.AddressbookNameAlreadyUsedException;
25 import org.mxchange.addressbook.exceptions.AddressbookNotFoundException;
26 import org.mxchange.addressbook.model.addressbook.entry.AddressbookEntry;
27 import org.mxchange.addressbook.model.addressbook.shared.ShareableAddressbook;
28 import org.mxchange.jcoreee.database.BaseDatabaseBean;
29 import org.mxchange.jusercore.model.user.User;
32 * A stateless bean handling addressbooks
34 * @author Roland Haeder
36 @Stateless (name = "addressbook", mappedName = "ejb/stateless-addressbook", description = "A stateless bean for handling addressbooks")
37 public class AddressbookSessionBean extends BaseDatabaseBean implements AddressbookSessionBeanRemote {
42 private static final long serialVersionUID = 129_857_871_287_691L;
45 @SuppressWarnings ("unchecked")
46 public List<AddressbookEntry> allEntries (final Addressbook addressbook) {
48 Query query = this.getEntityManager().createNamedQuery("AllAddressbookEntries"); //NOI18N
51 query.setParameter("addressbook", addressbook); //NOI18N
52 query.setParameter("owner", addressbook.getAddressbookUser()); //NOI18N
53 query.setParameter("sharer", addressbook.getAddressbookUser()); //NOI18N
56 return query.getResultList();
60 @SuppressWarnings ("unchecked")
61 public Integer countAllUserSharedAddressbooks (final User user) {
63 this.getLoggerBeanLocal().logTrace(MessageFormat.format("countAllUserSharedAddressbooks: user={0} - CALLED!", user)); //NOI18N
65 // user should be valid
68 throw new NullPointerException("user is null"); //NOI18N
69 } else if (user.getUserId() == null) {
71 throw new NullPointerException("user.userId is null"); //NOI18N
72 } else if (user.getUserId() < 1) {
74 throw new IllegalArgumentException(MessageFormat.format("user.userId={0} is invalid", user.getUserId())); //NOI18N
78 Query query = this.getEntityManager().createNamedQuery("SearchUserSharedAddressbooks"); //NOI18N
81 query.setParameter("user", user); //NOI18N
89 List<ShareableAddressbook> dummy = query.getResultList();
93 } catch (final NoResultException ex) {
94 // Need to catch this, so log it
95 this.getLoggerBeanLocal().logDebug(MessageFormat.format("countAllUserSharedAddressbooks: getResultList() failed: {0}", ex)); //NOI18N
99 this.getLoggerBeanLocal().logTrace(MessageFormat.format("countAllUserSharedAddressbooks: count={0} - EXIT!", count)); //NOI18N
106 public Addressbook createAddressbook (final Addressbook addressbook) throws AddressbookNameAlreadyUsedException {
108 if (null == addressbook) {
110 throw new NullPointerException("addressbook is null"); //NOI18N
111 } else if (addressbook.getAddressbookUser() == null) {
112 // User instance is null
113 throw new NullPointerException("addressbook.user should not be null."); //NOI18N
114 } else if (addressbook.getAddressbookName() == null) {
115 // Address book name not set
116 throw new NullPointerException("addressbook.addressbookName should not be null"); //NOI18N
117 } else if (addressbook.getAddressbookName().isEmpty()) {
118 // Address book name not set
119 throw new IllegalArgumentException("addressbook.addressbookName should not be empty"); //NOI18N
120 } else if (this.isAddressbookNameUsed(addressbook)) {
121 // The assigned user already used that name
122 throw new AddressbookNameAlreadyUsedException(addressbook);
126 this.getEntityManager().persist(addressbook);
128 // Flush it to get all data
129 this.getEntityManager().flush();
136 public Addressbook getAddressbookById (final Long addressbookId) throws AddressbookNotFoundException {
138 this.getLoggerBeanLocal().logTrace(MessageFormat.format("getAddressbookById: addressbookId={0} - CALLED!", addressbookId)); //NOI18N
140 // addressbookId should not be null or below 1
141 if (null == addressbookId) {
143 throw new NullPointerException("addressbookId is null"); //NOI18N
144 } else if (addressbookId < 1) {
146 throw new IllegalArgumentException(MessageFormat.format("addressbookId is not valid: {0}", addressbookId)); //NOI18N
147 } else if (!this.isAddressbookIdUsed(addressbookId)) {
148 // No address book found
149 throw new AddressbookNotFoundException(addressbookId);
152 // Get named query instance
153 Query query = this.getEntityManager().createNamedQuery("SearchAddressbookById"); //NOI18N
156 query.setParameter("id", addressbookId); //NOI18N
159 return (Addressbook) query.getSingleResult();
163 @SuppressWarnings ("unchecked")
164 public List<Addressbook> getUsersList (final User loggedInUser) {
166 this.getLoggerBeanLocal().logTrace(MessageFormat.format("getUsersList: loggedInUser={0} - CALLED!", loggedInUser)); //NOI18N
168 // Is the user instance null?
169 if (null == loggedInUser) {
171 throw new NullPointerException("loggedInUser is null"); //NOI18N
174 // Get query instance
175 Query query = this.getEntityManager().createNamedQuery("AllUsersAddressbooks", List.class); //NOI18N
178 query.setParameter("param", loggedInUser); //NOI18N
180 // Get full list from JPA
181 List<Addressbook> addressbooks = query.getResultList();
188 public boolean isAddressbookIdUsed (final Long addressbookId) {
190 this.getLoggerBeanLocal().logTrace(MessageFormat.format("isAddressbookIdUsed: addressbookId={0} - CALLED!", addressbookId)); //NOI18N
192 // Is it null or zero?
193 if (null == addressbookId) {
195 throw new NullPointerException("addressbookId is null"); //NOI18N
196 } else if (addressbookId < 1) {
197 // Not valid id number
198 throw new IllegalArgumentException(MessageFormat.format("addressbookId is not valid: {0}", addressbookId)); //NOI18N
201 // Get query instance
202 Query query = this.getEntityManager().createNamedQuery("SearchAddressbookById"); //NOI18N
205 query.setParameter("id", addressbookId); //NOI18N
207 // Default is not valid
208 boolean isValid = false;
210 // Try it again, yes no other way
213 Addressbook addressbook = (Addressbook) query.getSingleResult();
216 this.getLoggerBeanLocal().logDebug(MessageFormat.format("isAddressbookIdUsed: addressbook={0} - FOUND!", addressbook)); //NOI18N
220 } catch (final NoResultException ex) {
221 // Debug log only, maybe out-dated link followed
222 this.getLoggerBeanLocal().logDebug(MessageFormat.format("isAddressbookIdUsed: addressbookId={0} is not valid: {1}", addressbookId, ex)); //NOI18N
226 this.getLoggerBeanLocal().logTrace(MessageFormat.format("isAddressbookIdUsed: isValid={0} - EXIT!", isValid)); //NOI18N
233 public boolean isAddressbookNameUsed (final Addressbook addressbook) {
235 if (null == addressbook) {
237 throw new NullPointerException("addressbook is null"); //NOI18N
238 } else if (addressbook.getAddressbookUser() == null) {
239 // User instance is null
240 throw new NullPointerException("addressbook.user should not be null."); //NOI18N
241 } else if (addressbook.getAddressbookName() == null) {
242 // Address book name not set
243 throw new NullPointerException("addressbook.addressbookName should not be null"); //NOI18N
244 } else if (addressbook.getAddressbookName().isEmpty()) {
245 // Address book name not set
246 throw new IllegalArgumentException("addressbook.addressbookName should not be empty"); //NOI18N
249 // Get query instance
250 Query query = this.getEntityManager().createNamedQuery("SearchUserAddressbookName", Addressbook.class); //NOI18N
253 query.setParameter("user", addressbook.getAddressbookUser()); //NOI18N
254 query.setParameter("name", addressbook.getAddressbookName()); //NOI18N
256 // Default is not found
257 boolean isUsed = false;
261 // Get a single result
262 Addressbook dummy = (Addressbook) query.getSingleResult();
265 this.getLoggerBeanLocal().logDebug(MessageFormat.format("isAddressbookNameUsed: Found an address book: {0}", dummy)); //NOI18N
269 } catch (final NoResultException ex) {
270 // No result found, so log it away
271 this.getLoggerBeanLocal().logDebug(MessageFormat.format("isAddressbookNameUsed: getSingleResult() did not return a result: {0}", ex)); //NOI18N