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.addressbook.exceptions.AddressbookNameAlreadyUsedException;
27 import org.mxchange.addressbook.exceptions.AddressbookNotFoundException;
28 import org.mxchange.addressbook.model.addressbook.entry.AddressbookEntry;
29 import org.mxchange.addressbook.model.addressbook.shared.ShareableAddressbook;
30 import org.mxchange.jcoreee.database.BaseDatabaseBean;
31 import org.mxchange.jusercore.model.user.User;
34 * A stateless bean handling addressbooks
36 * @author Roland Haeder
38 @Stateless (name = "addressbook", mappedName = "ejb/stateless-addressbook-adr", description = "A stateless bean for handling Addressbook addressbooks")
39 public class AddressbookSessionBean extends BaseDatabaseBean implements AddressbookSessionBeanRemote {
44 private static final long serialVersionUID = 129_857_871_287_691L;
47 @SuppressWarnings ("unchecked")
48 public List<AddressbookEntry> allEntries (final Addressbook addressbook) {
50 this.getLoggerBeanLocal().logTrace(MessageFormat.format("allEntries: addressbook={0} - CALLED!", addressbook)); //NOI18N
53 if (null == addressbook) {
55 throw new NullPointerException("addressbook is null");
56 } else if (addressbook.getAddressbookId() == null) {
58 throw new NullPointerException("addressbook.addressbookId is null");
59 } else if (addressbook.getAddressbookId() < 1) {
61 throw new IllegalArgumentException(MessageFormat.format("addressbook.addressbookId={0} is invalid.", addressbook.getAddressbookId()));
62 } else if (addressbook.getAddressbookUser() == null) {
64 throw new NullPointerException("addressbook.addressbookUser is null");
65 } else if (addressbook.getAddressbookUser().getUserId() == null) {
67 throw new NullPointerException("addressbook.addressbookUser.userId is null");
68 } else if (addressbook.getAddressbookUser().getUserId() < 1) {
69 // Invalid id number again
70 throw new IllegalArgumentException(MessageFormat.format("addressbook.addressbookUser.userId={0} is invalid", addressbook.getAddressbookUser().getUserId()));
74 Query query = this.getEntityManager().createNamedQuery("SearchUsersAddressbookEntries", List.class); //NOI18N
77 query.setParameter("addressbook", addressbook); //NOI18N
78 query.setParameter("owner", addressbook.getAddressbookUser()); //NOI18N
81 return query.getResultList();
85 @SuppressWarnings ("unchecked")
86 public List<User> allUsersNotSharing (final User user, final Addressbook addressbook) {
88 this.getLoggerBeanLocal().logTrace(MessageFormat.format("allUsersNotSharing: user={0},addressbook={1} - CALLED!", user, addressbook)); //NOI18N
93 throw new NullPointerException("user is null"); //NOI18N
94 } else if (user.getUserId() == null) {
96 throw new NullPointerException("user.userId is null"); //NOI18N
97 } else if (user.getUserId() < 1) {
99 throw new IllegalArgumentException(MessageFormat.format("user.userId={0} is invalid", user.getUserId())); //NOI18N
100 } else if (null == addressbook) {
102 throw new NullPointerException("addressbook is null"); //NOI18N
103 } else if (addressbook.getAddressbookId() == null) {
105 throw new NullPointerException("addressbook.addressbookId is null"); //NOI18N
106 } else if (addressbook.getAddressbookId() < 1) {
108 throw new IllegalArgumentException(MessageFormat.format("addressbook.getAddressbookId={0} is invalid", addressbook.getAddressbookId())); //NOI18N
111 // Get named query for a user list without given user
112 Query allUsersExceptQuery = this.getEntityManager().createNamedQuery("SearchAllUsersExcept", List.class); //NOI18N
115 allUsersExceptQuery.setParameter("user", user); //NOI18N
118 List<User> allUsersExcept = allUsersExceptQuery.getResultList();
121 this.getLoggerBeanLocal().logDebug(MessageFormat.format("allUsersNotSharing: allUsersExcept.size()={0}", allUsersExcept.size())); //NOI18N
123 // Now get all shares this user has created
124 Query allSharesQuery = this.getEntityManager().createNamedQuery("SearchUserSharedAddressbooks", List.class); //NOI18N
127 allSharesQuery.setParameter("user", user); //NOI18N
129 // Get full list again
130 List<ShareableAddressbook> allShares = allSharesQuery.getResultList();
133 this.getLoggerBeanLocal().logDebug(MessageFormat.format("allUsersNotSharing: allShares.size()={0}", allShares.size())); //NOI18N
135 // List for users aharing with given
136 List<User> sharingUsers = new ArrayList<>(allShares.size());
139 for (final ShareableAddressbook share : allShares) {
141 this.getLoggerBeanLocal().logDebug(MessageFormat.format("allUsersNotSharing: share.shareUserSharee={0}", share.getShareUserSharee())); //NOI18N
144 sharingUsers.add(share.getShareUserSharee());
148 this.getLoggerBeanLocal().logDebug(MessageFormat.format("allUsersNotSharing: sharingUsers.size()={0}", sharingUsers.size())); //NOI18N
150 // Init final user list
151 List<User> userList = new LinkedList<>();
153 // Walk through all users
154 for (final User foundUser : allUsersExcept) {
156 this.getLoggerBeanLocal().logDebug(MessageFormat.format("allUsersNotSharing: foundUser={0}", foundUser)); //NOI18N
158 // Does the list contain it ?
159 if (!sharingUsers.contains(foundUser)) {
161 this.getLoggerBeanLocal().logDebug(MessageFormat.format("allUsersNotSharing: foundUser={0} - ADDING!", foundUser)); //NOI18N
164 userList.add(foundUser);
169 this.getLoggerBeanLocal().logTrace(MessageFormat.format("allUsersNotSharing: userList.size()={0} - EXIT!", userList.size())); //NOI18N
176 @SuppressWarnings ("unchecked")
177 public Integer countAllUserSharedAddressbooks (final User user) {
179 this.getLoggerBeanLocal().logTrace(MessageFormat.format("countAllUserSharedAddressbooks: user={0} - CALLED!", user)); //NOI18N
181 // user should be valid
184 throw new NullPointerException("user is null"); //NOI18N
185 } else if (user.getUserId() == null) {
187 throw new NullPointerException("user.userId is null"); //NOI18N
188 } else if (user.getUserId() < 1) {
190 throw new IllegalArgumentException(MessageFormat.format("user.userId={0} is invalid", user.getUserId())); //NOI18N
194 Query query = this.getEntityManager().createNamedQuery("SearchUserSharedAddressbooks", List.class); //NOI18N
197 query.setParameter("user", user); //NOI18N
205 List<ShareableAddressbook> dummy = query.getResultList();
208 count = dummy.size();
209 } catch (final NoResultException ex) {
210 // Need to catch this, so log it
211 this.getLoggerBeanLocal().logDebug(MessageFormat.format("countAllUserSharedAddressbooks: getResultList() failed: {0}", ex)); //NOI18N
215 this.getLoggerBeanLocal().logTrace(MessageFormat.format("countAllUserSharedAddressbooks: count={0} - EXIT!", count)); //NOI18N
222 public Addressbook createAddressbook (final Addressbook addressbook) throws AddressbookNameAlreadyUsedException {
224 if (null == addressbook) {
226 throw new NullPointerException("addressbook is null"); //NOI18N
227 } else if (addressbook.getAddressbookUser() == null) {
228 // User instance is null
229 throw new NullPointerException("addressbook.user should not be null."); //NOI18N
230 } else if (addressbook.getAddressbookName() == null) {
231 // Address book name not set
232 throw new NullPointerException("addressbook.addressbookName should not be null"); //NOI18N
233 } else if (addressbook.getAddressbookName().isEmpty()) {
234 // Address book name not set
235 throw new IllegalArgumentException("addressbook.addressbookName should not be empty"); //NOI18N
236 } else if (this.isAddressbookNameUsed(addressbook)) {
237 // The assigned user already used that name
238 throw new AddressbookNameAlreadyUsedException(addressbook);
242 this.getEntityManager().persist(addressbook);
244 // Flush it to get all data
245 this.getEntityManager().flush();
252 public Addressbook getAddressbookById (final Long addressbookId) throws AddressbookNotFoundException {
254 this.getLoggerBeanLocal().logTrace(MessageFormat.format("getAddressbookById: addressbookId={0} - CALLED!", addressbookId)); //NOI18N
256 // addressbookId should not be null or below 1
257 if (null == addressbookId) {
259 throw new NullPointerException("addressbookId is null"); //NOI18N
260 } else if (addressbookId < 1) {
262 throw new IllegalArgumentException(MessageFormat.format("addressbookId is not valid: {0}", addressbookId)); //NOI18N
263 } else if (!this.isAddressbookIdUsed(addressbookId)) {
264 // No address book found
265 throw new AddressbookNotFoundException(addressbookId);
268 // Get named query instance
269 Query query = this.getEntityManager().createNamedQuery("SearchAddressbookById", UserAddressbook.class); //NOI18N
272 query.setParameter("id", addressbookId); //NOI18N
275 return (Addressbook) query.getSingleResult();
279 @SuppressWarnings ("unchecked")
280 public List<Addressbook> getUsersAddressbookList (final User loggedInUser) {
282 this.getLoggerBeanLocal().logTrace(MessageFormat.format("getUsersList: loggedInUser={0} - CALLED!", loggedInUser)); //NOI18N
284 // Is the user instance null?
285 if (null == loggedInUser) {
287 throw new NullPointerException("loggedInUser is null"); //NOI18N
290 // Get query instance
291 Query query = this.getEntityManager().createNamedQuery("AllUsersAddressbooks", List.class); //NOI18N
294 query.setParameter("param", loggedInUser); //NOI18N
296 // Get full list from JPA
297 List<Addressbook> addressbooks = query.getResultList();
304 public boolean isAddressbookIdUsed (final Long addressbookId) {
306 this.getLoggerBeanLocal().logTrace(MessageFormat.format("isAddressbookIdUsed: addressbookId={0} - CALLED!", addressbookId)); //NOI18N
308 // Is it null or zero?
309 if (null == addressbookId) {
311 throw new NullPointerException("addressbookId is null"); //NOI18N
312 } else if (addressbookId < 1) {
313 // Not valid id number
314 throw new IllegalArgumentException(MessageFormat.format("addressbookId is not valid: {0}", addressbookId)); //NOI18N
317 // Get query instance
318 Query query = this.getEntityManager().createNamedQuery("SearchAddressbookById", UserAddressbook.class); //NOI18N
321 query.setParameter("id", addressbookId); //NOI18N
323 // Default is not valid
324 boolean isValid = false;
326 // Try it again, yes no other way
329 Addressbook addressbook = (Addressbook) query.getSingleResult();
332 this.getLoggerBeanLocal().logDebug(MessageFormat.format("isAddressbookIdUsed: addressbook={0} - FOUND!", addressbook)); //NOI18N
336 } catch (final NoResultException ex) {
337 // Debug log only, maybe out-dated link followed
338 this.getLoggerBeanLocal().logDebug(MessageFormat.format("isAddressbookIdUsed: addressbookId={0} is not valid: {1}", addressbookId, ex)); //NOI18N
342 this.getLoggerBeanLocal().logTrace(MessageFormat.format("isAddressbookIdUsed: isValid={0} - EXIT!", isValid)); //NOI18N
349 public boolean isAddressbookNameUsed (final Addressbook addressbook) {
351 if (null == addressbook) {
353 throw new NullPointerException("addressbook is null"); //NOI18N
354 } else if (addressbook.getAddressbookUser() == null) {
355 // User instance is null
356 throw new NullPointerException("addressbook.addressbookUser is null."); //NOI18N
357 } else if (addressbook.getAddressbookUser().getUserId() == null) {
358 // User instance is null
359 throw new NullPointerException("addressbook.addressbookUser.userId is null."); //NOI18N
360 } else if (addressbook.getAddressbookUser().getUserId() < 1) {
361 // User instance is null
362 throw new NullPointerException(MessageFormat.format("addressbook.addressbookUser.userId={0} is invalid.", addressbook.getAddressbookUser().getUserId())); //NOI18N
363 } else if (addressbook.getAddressbookName() == null) {
364 // Address book name not set
365 throw new NullPointerException("addressbook.addressbookName should not be null"); //NOI18N
366 } else if (addressbook.getAddressbookName().isEmpty()) {
367 // Address book name not set
368 throw new IllegalArgumentException("addressbook.addressbookName should not be empty"); //NOI18N
371 // Get query instance
372 Query query = this.getEntityManager().createNamedQuery("SearchUserAddressbookName", Addressbook.class); //NOI18N
375 query.setParameter("user", addressbook.getAddressbookUser()); //NOI18N
376 query.setParameter("name", addressbook.getAddressbookName()); //NOI18N
378 // Default is not found
379 boolean isUsed = false;
383 // Get a single result
384 Addressbook dummy = (Addressbook) query.getSingleResult();
387 this.getLoggerBeanLocal().logDebug(MessageFormat.format("isAddressbookNameUsed: Found an address book: {0}", dummy)); //NOI18N
391 } catch (final NoResultException ex) {
392 // No result found, so log it away
393 this.getLoggerBeanLocal().logDebug(MessageFormat.format("isAddressbookNameUsed: getSingleResult() did not return a result: {0}", ex)); //NOI18N