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.addressbook.model.addressbook;
19 import java.text.MessageFormat;
20 import java.util.ArrayList;
21 import java.util.GregorianCalendar;
22 import java.util.LinkedList;
23 import java.util.List;
24 import javax.ejb.Stateless;
25 import javax.persistence.NoResultException;
26 import javax.persistence.Query;
27 import org.mxchange.jaddressbookcore.exceptions.AddressbookNameAlreadyUsedException;
28 import org.mxchange.jaddressbookcore.exceptions.AddressbookNotFoundException;
29 import org.mxchange.jaddressbookcore.model.addressbook.Addressbook;
30 import org.mxchange.jaddressbookcore.model.addressbook.UserAddressbook;
31 import org.mxchange.jaddressbookcore.model.addressbook.entry.AddressbookEntry;
32 import org.mxchange.jaddressbookcore.model.addressbook.shared.ShareableAddressbook;
33 import org.mxchange.jcoreee.database.BaseDatabaseBean;
34 import org.mxchange.jusercore.model.user.User;
37 * A stateless bean handling addressbooks
39 * @author Roland Haeder<roland@mxchange.org>
41 @Stateless (name = "addressbook", description = "A stateless bean for handling Addressbook addressbooks")
42 public class AddressbookSessionBean 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 @SuppressWarnings ("unchecked")
89 public List<User> allUsersNotSharing (final User user, final Addressbook addressbook) {
91 this.getLoggerBeanLocal().logTrace(MessageFormat.format("allUsersNotSharing: user={0},addressbook={1} - CALLED!", user, addressbook)); //NOI18N
96 throw new NullPointerException("user is null"); //NOI18N
97 } else if (user.getUserId() == null) {
99 throw new NullPointerException("user.userId is null"); //NOI18N
100 } else if (user.getUserId() < 1) {
102 throw new IllegalArgumentException(MessageFormat.format("user.userId={0} is invalid", user.getUserId())); //NOI18N
103 } else if (null == addressbook) {
105 throw new NullPointerException("addressbook is null"); //NOI18N
106 } else if (addressbook.getAddressbookId() == null) {
108 throw new NullPointerException("addressbook.addressbookId is null"); //NOI18N
109 } else if (addressbook.getAddressbookId() < 1) {
111 throw new IllegalArgumentException(MessageFormat.format("addressbook.getAddressbookId={0} is invalid", addressbook.getAddressbookId())); //NOI18N
114 // Get named query for a user list without given user
115 Query allUsersExceptQuery = this.getEntityManager().createNamedQuery("SearchAllUsersExcept", List.class); //NOI18N
118 allUsersExceptQuery.setParameter("user", user); //NOI18N
121 List<User> allUsersExcept = allUsersExceptQuery.getResultList();
124 this.getLoggerBeanLocal().logDebug(MessageFormat.format("allUsersNotSharing: allUsersExcept.size()={0}", allUsersExcept.size())); //NOI18N
126 // Now get all shares this user has created
127 Query allSharesQuery = this.getEntityManager().createNamedQuery("SearchUserSharedAddressbooks", List.class); //NOI18N
130 allSharesQuery.setParameter("user", user); //NOI18N
132 // Get full list again
133 List<ShareableAddressbook> allShares = allSharesQuery.getResultList();
136 this.getLoggerBeanLocal().logDebug(MessageFormat.format("allUsersNotSharing: allShares.size()={0}", allShares.size())); //NOI18N
138 // List for users aharing with given
139 List<User> sharingUsers = new ArrayList<>(allShares.size());
142 for (final ShareableAddressbook share : allShares) {
144 this.getLoggerBeanLocal().logDebug(MessageFormat.format("allUsersNotSharing: share.shareUserSharee={0}", share.getShareUserSharee())); //NOI18N
147 sharingUsers.add(share.getShareUserSharee());
151 this.getLoggerBeanLocal().logDebug(MessageFormat.format("allUsersNotSharing: sharingUsers.size()={0}", sharingUsers.size())); //NOI18N
153 // Init final user list
154 List<User> userList = new LinkedList<>();
156 // Walk through all users
157 for (final User foundUser : allUsersExcept) {
159 this.getLoggerBeanLocal().logDebug(MessageFormat.format("allUsersNotSharing: foundUser={0}", foundUser)); //NOI18N
161 // Does the list contain it ?
162 if (!sharingUsers.contains(foundUser)) {
164 this.getLoggerBeanLocal().logDebug(MessageFormat.format("allUsersNotSharing: foundUser={0} - ADDING!", foundUser)); //NOI18N
167 userList.add(foundUser);
172 this.getLoggerBeanLocal().logTrace(MessageFormat.format("allUsersNotSharing: userList.size()={0} - EXIT!", userList.size())); //NOI18N
179 @SuppressWarnings ("unchecked")
180 public Integer countAllUserSharedAddressbooks (final User user) {
182 this.getLoggerBeanLocal().logTrace(MessageFormat.format("countAllUserSharedAddressbooks: user={0} - CALLED!", user)); //NOI18N
184 // user should be valid
187 throw new NullPointerException("user is null"); //NOI18N
188 } else if (user.getUserId() == null) {
190 throw new NullPointerException("user.userId is null"); //NOI18N
191 } else if (user.getUserId() < 1) {
193 throw new IllegalArgumentException(MessageFormat.format("user.userId={0} is invalid", user.getUserId())); //NOI18N
197 Query query = this.getEntityManager().createNamedQuery("SearchUserSharedAddressbooks", List.class); //NOI18N
200 query.setParameter("user", user); //NOI18N
208 List<ShareableAddressbook> dummy = query.getResultList();
211 count = dummy.size();
212 } catch (final NoResultException ex) {
213 // Need to catch this, so log it
214 this.getLoggerBeanLocal().logDebug(MessageFormat.format("countAllUserSharedAddressbooks: getResultList() failed: {0}", ex)); //NOI18N
218 this.getLoggerBeanLocal().logTrace(MessageFormat.format("countAllUserSharedAddressbooks: count={0} - EXIT!", count)); //NOI18N
225 public Addressbook createAddressbook (final Addressbook addressbook) throws AddressbookNameAlreadyUsedException {
227 if (null == addressbook) {
229 throw new NullPointerException("addressbook is null"); //NOI18N
230 } else if (addressbook.getAddressbookUser() == null) {
231 // User instance is null
232 throw new NullPointerException("addressbook.user should not be null."); //NOI18N
233 } else if (addressbook.getAddressbookName() == null) {
234 // Address book name not set
235 throw new NullPointerException("addressbook.addressbookName should not be null"); //NOI18N
236 } else if (addressbook.getAddressbookName().isEmpty()) {
237 // Address book name not set
238 throw new IllegalArgumentException("addressbook.addressbookName should not be empty"); //NOI18N
239 } else if (this.isAddressbookNameUsed(addressbook)) {
240 // The assigned user already used that name
241 throw new AddressbookNameAlreadyUsedException(addressbook);
244 // Add timestamp of creation
245 addressbook.setAddressbookCreated(new GregorianCalendar());
248 this.getEntityManager().persist(addressbook);
250 // Flush it to get all data
251 this.getEntityManager().flush();
258 public Addressbook getAddressbookById (final Long addressbookId) throws AddressbookNotFoundException {
260 this.getLoggerBeanLocal().logTrace(MessageFormat.format("getAddressbookById: addressbookId={0} - CALLED!", addressbookId)); //NOI18N
262 // addressbookId should not be null or below 1
263 if (null == addressbookId) {
265 throw new NullPointerException("addressbookId is null"); //NOI18N
266 } else if (addressbookId < 1) {
268 throw new IllegalArgumentException(MessageFormat.format("addressbookId is not valid: {0}", addressbookId)); //NOI18N
269 } else if (!this.isAddressbookIdUsed(addressbookId)) {
270 // No address book found
271 throw new AddressbookNotFoundException(addressbookId);
274 // Get named query instance
275 Query query = this.getEntityManager().createNamedQuery("SearchAddressbookById", UserAddressbook.class); //NOI18N
278 query.setParameter("id", addressbookId); //NOI18N
281 return (Addressbook) query.getSingleResult();
285 @SuppressWarnings ("unchecked")
286 public List<Addressbook> getUsersAddressbookList (final User loggedInUser) {
288 this.getLoggerBeanLocal().logTrace(MessageFormat.format("getUsersList: loggedInUser={0} - CALLED!", loggedInUser)); //NOI18N
290 // Is the user instance null?
291 if (null == loggedInUser) {
293 throw new NullPointerException("loggedInUser is null"); //NOI18N
296 // Get query instance
297 Query query = this.getEntityManager().createNamedQuery("AllUsersAddressbooks", List.class); //NOI18N
300 query.setParameter("param", loggedInUser); //NOI18N
302 // Get full list from JPA
303 List<Addressbook> addressbooks = query.getResultList();
310 public boolean isAddressbookIdUsed (final Long addressbookId) {
312 this.getLoggerBeanLocal().logTrace(MessageFormat.format("isAddressbookIdUsed: addressbookId={0} - CALLED!", addressbookId)); //NOI18N
314 // Is it null or zero?
315 if (null == addressbookId) {
317 throw new NullPointerException("addressbookId is null"); //NOI18N
318 } else if (addressbookId < 1) {
319 // Not valid id number
320 throw new IllegalArgumentException(MessageFormat.format("addressbookId is not valid: {0}", addressbookId)); //NOI18N
323 // Get query instance
324 Query query = this.getEntityManager().createNamedQuery("SearchAddressbookById", UserAddressbook.class); //NOI18N
327 query.setParameter("id", addressbookId); //NOI18N
329 // Default is not valid
330 boolean isValid = false;
332 // Try it again, yes no other way
335 Addressbook addressbook = (Addressbook) query.getSingleResult();
338 this.getLoggerBeanLocal().logDebug(MessageFormat.format("isAddressbookIdUsed: addressbook={0} - FOUND!", addressbook)); //NOI18N
342 } catch (final NoResultException ex) {
343 // Debug log only, maybe out-dated link followed
344 this.getLoggerBeanLocal().logDebug(MessageFormat.format("isAddressbookIdUsed: addressbookId={0} is not valid: {1}", addressbookId, ex)); //NOI18N
348 this.getLoggerBeanLocal().logTrace(MessageFormat.format("isAddressbookIdUsed: isValid={0} - EXIT!", isValid)); //NOI18N
355 public boolean isAddressbookNameUsed (final Addressbook addressbook) {
357 if (null == addressbook) {
359 throw new NullPointerException("addressbook is null"); //NOI18N
360 } else if (addressbook.getAddressbookUser() == null) {
361 // User instance is null
362 throw new NullPointerException("addressbook.addressbookUser is null."); //NOI18N
363 } else if (addressbook.getAddressbookUser().getUserId() == null) {
364 // User instance is null
365 throw new NullPointerException("addressbook.addressbookUser.userId is null."); //NOI18N
366 } else if (addressbook.getAddressbookUser().getUserId() < 1) {
367 // User instance is null
368 throw new NullPointerException(MessageFormat.format("addressbook.addressbookUser.userId={0} is invalid.", addressbook.getAddressbookUser().getUserId())); //NOI18N
369 } else if (addressbook.getAddressbookName() == null) {
370 // Address book name not set
371 throw new NullPointerException("addressbook.addressbookName should not be null"); //NOI18N
372 } else if (addressbook.getAddressbookName().isEmpty()) {
373 // Address book name not set
374 throw new IllegalArgumentException("addressbook.addressbookName should not be empty"); //NOI18N
377 // Get query instance
378 Query query = this.getEntityManager().createNamedQuery("SearchUserAddressbookName", Addressbook.class); //NOI18N
381 query.setParameter("user", addressbook.getAddressbookUser()); //NOI18N
382 query.setParameter("name", addressbook.getAddressbookName()); //NOI18N
384 // Default is not found
385 boolean isUsed = false;
389 // Get a single result
390 Addressbook dummy = (Addressbook) query.getSingleResult();
393 this.getLoggerBeanLocal().logDebug(MessageFormat.format("isAddressbookNameUsed: Found an address book: {0}", dummy)); //NOI18N
397 } catch (final NoResultException ex) {
398 // No result found, so log it away
399 this.getLoggerBeanLocal().logDebug(MessageFormat.format("isAddressbookNameUsed: getSingleResult() did not return a result: {0}", ex)); //NOI18N