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.LinkedList;
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.exceptions.AddressbookNameAlreadyUsedException;
26 import org.mxchange.addressbook.exceptions.AddressbookNotFoundException;
27 import org.mxchange.addressbook.model.addressbook.entry.AddressbookEntry;
28 import org.mxchange.addressbook.model.addressbook.shared.ShareableAddressbook;
29 import org.mxchange.jcoreee.database.BaseDatabaseBean;
30 import org.mxchange.jusercore.model.user.User;
33 * A stateless bean handling addressbooks
35 * @author Roland Haeder
37 @Stateless (name = "addressbook", mappedName = "ejb/stateless-addressbook", description = "A stateless bean for handling addressbooks")
38 public class AddressbookSessionBean extends BaseDatabaseBean implements AddressbookSessionBeanRemote {
43 private static final long serialVersionUID = 129_857_871_287_691L;
46 @SuppressWarnings ("unchecked")
47 public List<AddressbookEntry> allEntries (final Addressbook addressbook) {
49 Query query = this.getEntityManager().createNamedQuery("AllAddressbookEntries", List.class); //NOI18N
52 query.setParameter("addressbook", addressbook); //NOI18N
53 query.setParameter("owner", addressbook.getAddressbookUser()); //NOI18N
54 query.setParameter("sharer", addressbook.getAddressbookUser()); //NOI18N
57 return query.getResultList();
61 @SuppressWarnings ("unchecked")
62 public List<User> allUsersNotSharing (final User user, final Addressbook addressbook) {
64 this.getLoggerBeanLocal().logTrace(MessageFormat.format("allUsersNotSharing: user={0},addressbook={1} - CALLED!", user, addressbook)); //NOI18N
69 throw new NullPointerException("user is null"); //NOI18N
70 } else if (user.getUserId() == null) {
72 throw new NullPointerException("user.userId is null"); //NOI18N
73 } else if (user.getUserId() < 1) {
75 throw new IllegalArgumentException(MessageFormat.format("user.userId={0} is invalid", user.getUserId())); //NOI18N
76 } else if (null == addressbook) {
78 throw new NullPointerException("addressbook is null"); //NOI18N
79 } else if (addressbook.getAddressbookId() == null) {
81 throw new NullPointerException("addressbook.addressbookId is null"); //NOI18N
82 } else if (addressbook.getAddressbookId() < 1) {
84 throw new IllegalArgumentException(MessageFormat.format("addressbook.getAddressbookId={0} is invalid", addressbook.getAddressbookId())); //NOI18N
87 // Get named query for a user list without given user
88 Query allUsersExceptQuery = this.getEntityManager().createNamedQuery("SearchAllUsersExcept", List.class); //NOI18N
91 allUsersExceptQuery.setParameter("user", user); //NOI18N
94 List<User> allUsersExcept = allUsersExceptQuery.getResultList();
97 this.getLoggerBeanLocal().logDebug(MessageFormat.format("allUsersNotSharing: allUsersExcept.size()={0}", allUsersExcept.size())); //NOI18N
99 // Now get all users this user is sharing with, first a new query
100 Query allShareesQuery = this.getEntityManager().createNamedQuery("SearchUserSharedAddressbooks", List.class); //NOI18N
103 allShareesQuery.setParameter("user", user); //NOI18N
105 // Get full list again
106 List<User> allSharees = allShareesQuery.getResultList();
109 this.getLoggerBeanLocal().logDebug(MessageFormat.format("allUsersNotSharing: allSharees.size()={0}", allSharees.size())); //NOI18N
111 // Init final user list
112 List<User> userList = new LinkedList<>();
114 // Walk through all users
115 for (final User u : allUsersExcept) {
117 this.getLoggerBeanLocal().logDebug(MessageFormat.format("allUsersNotSharing: u={0}", u)); //NOI18N
119 // Some entries in allSharees?
120 if (!allSharees.isEmpty()) {
121 // Default is not found
122 boolean isFound = false;
124 // Check all entries (user name matching)
125 for (final User sharee : allSharees) {
127 this.getLoggerBeanLocal().logDebug(MessageFormat.format("allUsersNotSharing: sharee={0}", sharee));
130 if (sharee.equals(u)) {
140 this.getLoggerBeanLocal().logDebug(MessageFormat.format("allUsersNotSharing: u={0} - ADDING!", u)); //NOI18N
149 this.getLoggerBeanLocal().logTrace(MessageFormat.format("allUsersNotSharing: userList.size()={0} - EXIT!", userList.size())); //NOI18N
156 @SuppressWarnings ("unchecked")
157 public Integer countAllUserSharedAddressbooks (final User user) {
159 this.getLoggerBeanLocal().logTrace(MessageFormat.format("countAllUserSharedAddressbooks: user={0} - CALLED!", user)); //NOI18N
161 // user should be valid
164 throw new NullPointerException("user is null"); //NOI18N
165 } else if (user.getUserId() == null) {
167 throw new NullPointerException("user.userId is null"); //NOI18N
168 } else if (user.getUserId() < 1) {
170 throw new IllegalArgumentException(MessageFormat.format("user.userId={0} is invalid", user.getUserId())); //NOI18N
174 Query query = this.getEntityManager().createNamedQuery("SearchUserSharedAddressbooks", List.class); //NOI18N
177 query.setParameter("user", user); //NOI18N
185 List<ShareableAddressbook> dummy = query.getResultList();
188 count = dummy.size();
189 } catch (final NoResultException ex) {
190 // Need to catch this, so log it
191 this.getLoggerBeanLocal().logDebug(MessageFormat.format("countAllUserSharedAddressbooks: getResultList() failed: {0}", ex)); //NOI18N
195 this.getLoggerBeanLocal().logTrace(MessageFormat.format("countAllUserSharedAddressbooks: count={0} - EXIT!", count)); //NOI18N
202 public Addressbook createAddressbook (final Addressbook addressbook) throws AddressbookNameAlreadyUsedException {
204 if (null == addressbook) {
206 throw new NullPointerException("addressbook is null"); //NOI18N
207 } else if (addressbook.getAddressbookUser() == null) {
208 // User instance is null
209 throw new NullPointerException("addressbook.user should not be null."); //NOI18N
210 } else if (addressbook.getAddressbookName() == null) {
211 // Address book name not set
212 throw new NullPointerException("addressbook.addressbookName should not be null"); //NOI18N
213 } else if (addressbook.getAddressbookName().isEmpty()) {
214 // Address book name not set
215 throw new IllegalArgumentException("addressbook.addressbookName should not be empty"); //NOI18N
216 } else if (this.isAddressbookNameUsed(addressbook)) {
217 // The assigned user already used that name
218 throw new AddressbookNameAlreadyUsedException(addressbook);
222 this.getEntityManager().persist(addressbook);
224 // Flush it to get all data
225 this.getEntityManager().flush();
232 public Addressbook getAddressbookById (final Long addressbookId) throws AddressbookNotFoundException {
234 this.getLoggerBeanLocal().logTrace(MessageFormat.format("getAddressbookById: addressbookId={0} - CALLED!", addressbookId)); //NOI18N
236 // addressbookId should not be null or below 1
237 if (null == addressbookId) {
239 throw new NullPointerException("addressbookId is null"); //NOI18N
240 } else if (addressbookId < 1) {
242 throw new IllegalArgumentException(MessageFormat.format("addressbookId is not valid: {0}", addressbookId)); //NOI18N
243 } else if (!this.isAddressbookIdUsed(addressbookId)) {
244 // No address book found
245 throw new AddressbookNotFoundException(addressbookId);
248 // Get named query instance
249 Query query = this.getEntityManager().createNamedQuery("SearchAddressbookById", UserAddressbook.class); //NOI18N
252 query.setParameter("id", addressbookId); //NOI18N
255 return (Addressbook) query.getSingleResult();
259 @SuppressWarnings ("unchecked")
260 public List<Addressbook> getUsersAddressbookList (final User loggedInUser) {
262 this.getLoggerBeanLocal().logTrace(MessageFormat.format("getUsersList: loggedInUser={0} - CALLED!", loggedInUser)); //NOI18N
264 // Is the user instance null?
265 if (null == loggedInUser) {
267 throw new NullPointerException("loggedInUser is null"); //NOI18N
270 // Get query instance
271 Query query = this.getEntityManager().createNamedQuery("AllUsersAddressbooks", List.class); //NOI18N
274 query.setParameter("param", loggedInUser); //NOI18N
276 // Get full list from JPA
277 List<Addressbook> addressbooks = query.getResultList();
284 public boolean isAddressbookIdUsed (final Long addressbookId) {
286 this.getLoggerBeanLocal().logTrace(MessageFormat.format("isAddressbookIdUsed: addressbookId={0} - CALLED!", addressbookId)); //NOI18N
288 // Is it null or zero?
289 if (null == addressbookId) {
291 throw new NullPointerException("addressbookId is null"); //NOI18N
292 } else if (addressbookId < 1) {
293 // Not valid id number
294 throw new IllegalArgumentException(MessageFormat.format("addressbookId is not valid: {0}", addressbookId)); //NOI18N
297 // Get query instance
298 Query query = this.getEntityManager().createNamedQuery("SearchAddressbookById", UserAddressbook.class); //NOI18N
301 query.setParameter("id", addressbookId); //NOI18N
303 // Default is not valid
304 boolean isValid = false;
306 // Try it again, yes no other way
309 Addressbook addressbook = (Addressbook) query.getSingleResult();
312 this.getLoggerBeanLocal().logDebug(MessageFormat.format("isAddressbookIdUsed: addressbook={0} - FOUND!", addressbook)); //NOI18N
316 } catch (final NoResultException ex) {
317 // Debug log only, maybe out-dated link followed
318 this.getLoggerBeanLocal().logDebug(MessageFormat.format("isAddressbookIdUsed: addressbookId={0} is not valid: {1}", addressbookId, ex)); //NOI18N
322 this.getLoggerBeanLocal().logTrace(MessageFormat.format("isAddressbookIdUsed: isValid={0} - EXIT!", isValid)); //NOI18N
329 public boolean isAddressbookNameUsed (final Addressbook addressbook) {
331 if (null == addressbook) {
333 throw new NullPointerException("addressbook is null"); //NOI18N
334 } else if (addressbook.getAddressbookUser() == null) {
335 // User instance is null
336 throw new NullPointerException("addressbook.user should not be null."); //NOI18N
337 } else if (addressbook.getAddressbookName() == null) {
338 // Address book name not set
339 throw new NullPointerException("addressbook.addressbookName should not be null"); //NOI18N
340 } else if (addressbook.getAddressbookName().isEmpty()) {
341 // Address book name not set
342 throw new IllegalArgumentException("addressbook.addressbookName should not be empty"); //NOI18N
345 // Get query instance
346 Query query = this.getEntityManager().createNamedQuery("SearchUserAddressbookName", Addressbook.class); //NOI18N
349 query.setParameter("user", addressbook.getAddressbookUser()); //NOI18N
350 query.setParameter("name", addressbook.getAddressbookName()); //NOI18N
352 // Default is not found
353 boolean isUsed = false;
357 // Get a single result
358 Addressbook dummy = (Addressbook) query.getSingleResult();
361 this.getLoggerBeanLocal().logDebug(MessageFormat.format("isAddressbookNameUsed: Found an address book: {0}", dummy)); //NOI18N
365 } catch (final NoResultException ex) {
366 // No result found, so log it away
367 this.getLoggerBeanLocal().logDebug(MessageFormat.format("isAddressbookNameUsed: getSingleResult() did not return a result: {0}", ex)); //NOI18N