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.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", description = "A stateless bean for handling 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) {
70 throw new IllegalArgumentException(MessageFormat.format("addressbook.addressbookUser.userId={0} is invalid", addressbook.getAddressbookUser().getUserId()));
74 Query query = this.getEntityManager().createNamedQuery("AllAddressbookEntries", List.class); //NOI18N
77 query.setParameter("addressbook", addressbook); //NOI18N
78 query.setParameter("owner", addressbook.getAddressbookUser()); //NOI18N
79 query.setParameter("sharer", addressbook.getAddressbookUser()); //NOI18N
82 return query.getResultList();
86 @SuppressWarnings ("unchecked")
87 public List<User> allUsersNotSharing (final User user, final Addressbook addressbook) {
89 this.getLoggerBeanLocal().logTrace(MessageFormat.format("allUsersNotSharing: user={0},addressbook={1} - CALLED!", user, addressbook)); //NOI18N
94 throw new NullPointerException("user is null"); //NOI18N
95 } else if (user.getUserId() == null) {
97 throw new NullPointerException("user.userId is null"); //NOI18N
98 } else if (user.getUserId() < 1) {
100 throw new IllegalArgumentException(MessageFormat.format("user.userId={0} is invalid", user.getUserId())); //NOI18N
101 } else if (null == addressbook) {
103 throw new NullPointerException("addressbook is null"); //NOI18N
104 } else if (addressbook.getAddressbookId() == null) {
106 throw new NullPointerException("addressbook.addressbookId is null"); //NOI18N
107 } else if (addressbook.getAddressbookId() < 1) {
109 throw new IllegalArgumentException(MessageFormat.format("addressbook.getAddressbookId={0} is invalid", addressbook.getAddressbookId())); //NOI18N
112 // Get named query for a user list without given user
113 Query allUsersExceptQuery = this.getEntityManager().createNamedQuery("SearchAllUsersExcept", List.class); //NOI18N
116 allUsersExceptQuery.setParameter("user", user); //NOI18N
119 List<User> allUsersExcept = allUsersExceptQuery.getResultList();
122 this.getLoggerBeanLocal().logDebug(MessageFormat.format("allUsersNotSharing: allUsersExcept.size()={0}", allUsersExcept.size())); //NOI18N
124 // Now get all shares this user has created
125 Query allSharesQuery = this.getEntityManager().createNamedQuery("SearchUserSharedAddressbooks", List.class); //NOI18N
128 allSharesQuery.setParameter("user", user); //NOI18N
130 // Get full list again
131 List<ShareableAddressbook> allShares = allSharesQuery.getResultList();
134 this.getLoggerBeanLocal().logDebug(MessageFormat.format("allUsersNotSharing: allShares.size()={0}", allShares.size())); //NOI18N
136 // List for users aharing with given
137 List<User> sharingUsers = new ArrayList<>(allShares.size());
140 for (final ShareableAddressbook share : allShares) {
142 this.getLoggerBeanLocal().logDebug(MessageFormat.format("allUsersNotSharing: share.shareUserSharee={0}", share.getShareUserSharee())); //NOI18N
145 sharingUsers.add(share.getShareUserSharee());
149 this.getLoggerBeanLocal().logDebug(MessageFormat.format("allUsersNotSharing: sharingUsers.size()={0}", sharingUsers.size())); //NOI18N
151 // Init final user list
152 List<User> userList = new LinkedList<>();
154 // Walk through all users
155 for (final User foundUser : allUsersExcept) {
157 this.getLoggerBeanLocal().logDebug(MessageFormat.format("allUsersNotSharing: foundUser={0}", foundUser)); //NOI18N
159 // Does the list contain it ?
160 if (!sharingUsers.contains(foundUser)) {
162 this.getLoggerBeanLocal().logDebug(MessageFormat.format("allUsersNotSharing: foundUser={0} - ADDING!", foundUser)); //NOI18N
165 userList.add(foundUser);
170 this.getLoggerBeanLocal().logTrace(MessageFormat.format("allUsersNotSharing: userList.size()={0} - EXIT!", userList.size())); //NOI18N
177 @SuppressWarnings ("unchecked")
178 public Integer countAllUserSharedAddressbooks (final User user) {
180 this.getLoggerBeanLocal().logTrace(MessageFormat.format("countAllUserSharedAddressbooks: user={0} - CALLED!", user)); //NOI18N
182 // user should be valid
185 throw new NullPointerException("user is null"); //NOI18N
186 } else if (user.getUserId() == null) {
188 throw new NullPointerException("user.userId is null"); //NOI18N
189 } else if (user.getUserId() < 1) {
191 throw new IllegalArgumentException(MessageFormat.format("user.userId={0} is invalid", user.getUserId())); //NOI18N
195 Query query = this.getEntityManager().createNamedQuery("SearchUserSharedAddressbooks", List.class); //NOI18N
198 query.setParameter("user", user); //NOI18N
206 List<ShareableAddressbook> dummy = query.getResultList();
209 count = dummy.size();
210 } catch (final NoResultException ex) {
211 // Need to catch this, so log it
212 this.getLoggerBeanLocal().logDebug(MessageFormat.format("countAllUserSharedAddressbooks: getResultList() failed: {0}", ex)); //NOI18N
216 this.getLoggerBeanLocal().logTrace(MessageFormat.format("countAllUserSharedAddressbooks: count={0} - EXIT!", count)); //NOI18N
223 public Addressbook createAddressbook (final Addressbook addressbook) throws AddressbookNameAlreadyUsedException {
225 if (null == addressbook) {
227 throw new NullPointerException("addressbook is null"); //NOI18N
228 } else if (addressbook.getAddressbookUser() == null) {
229 // User instance is null
230 throw new NullPointerException("addressbook.user should not be null."); //NOI18N
231 } else if (addressbook.getAddressbookName() == null) {
232 // Address book name not set
233 throw new NullPointerException("addressbook.addressbookName should not be null"); //NOI18N
234 } else if (addressbook.getAddressbookName().isEmpty()) {
235 // Address book name not set
236 throw new IllegalArgumentException("addressbook.addressbookName should not be empty"); //NOI18N
237 } else if (this.isAddressbookNameUsed(addressbook)) {
238 // The assigned user already used that name
239 throw new AddressbookNameAlreadyUsedException(addressbook);
243 this.getEntityManager().persist(addressbook);
245 // Flush it to get all data
246 this.getEntityManager().flush();
253 public Addressbook getAddressbookById (final Long addressbookId) throws AddressbookNotFoundException {
255 this.getLoggerBeanLocal().logTrace(MessageFormat.format("getAddressbookById: addressbookId={0} - CALLED!", addressbookId)); //NOI18N
257 // addressbookId should not be null or below 1
258 if (null == addressbookId) {
260 throw new NullPointerException("addressbookId is null"); //NOI18N
261 } else if (addressbookId < 1) {
263 throw new IllegalArgumentException(MessageFormat.format("addressbookId is not valid: {0}", addressbookId)); //NOI18N
264 } else if (!this.isAddressbookIdUsed(addressbookId)) {
265 // No address book found
266 throw new AddressbookNotFoundException(addressbookId);
269 // Get named query instance
270 Query query = this.getEntityManager().createNamedQuery("SearchAddressbookById", UserAddressbook.class); //NOI18N
273 query.setParameter("id", addressbookId); //NOI18N
276 return (Addressbook) query.getSingleResult();
280 @SuppressWarnings ("unchecked")
281 public List<Addressbook> getUsersAddressbookList (final User loggedInUser) {
283 this.getLoggerBeanLocal().logTrace(MessageFormat.format("getUsersList: loggedInUser={0} - CALLED!", loggedInUser)); //NOI18N
285 // Is the user instance null?
286 if (null == loggedInUser) {
288 throw new NullPointerException("loggedInUser is null"); //NOI18N
291 // Get query instance
292 Query query = this.getEntityManager().createNamedQuery("AllUsersAddressbooks", List.class); //NOI18N
295 query.setParameter("param", loggedInUser); //NOI18N
297 // Get full list from JPA
298 List<Addressbook> addressbooks = query.getResultList();
305 public boolean isAddressbookIdUsed (final Long addressbookId) {
307 this.getLoggerBeanLocal().logTrace(MessageFormat.format("isAddressbookIdUsed: addressbookId={0} - CALLED!", addressbookId)); //NOI18N
309 // Is it null or zero?
310 if (null == addressbookId) {
312 throw new NullPointerException("addressbookId is null"); //NOI18N
313 } else if (addressbookId < 1) {
314 // Not valid id number
315 throw new IllegalArgumentException(MessageFormat.format("addressbookId is not valid: {0}", addressbookId)); //NOI18N
318 // Get query instance
319 Query query = this.getEntityManager().createNamedQuery("SearchAddressbookById", UserAddressbook.class); //NOI18N
322 query.setParameter("id", addressbookId); //NOI18N
324 // Default is not valid
325 boolean isValid = false;
327 // Try it again, yes no other way
330 Addressbook addressbook = (Addressbook) query.getSingleResult();
333 this.getLoggerBeanLocal().logDebug(MessageFormat.format("isAddressbookIdUsed: addressbook={0} - FOUND!", addressbook)); //NOI18N
337 } catch (final NoResultException ex) {
338 // Debug log only, maybe out-dated link followed
339 this.getLoggerBeanLocal().logDebug(MessageFormat.format("isAddressbookIdUsed: addressbookId={0} is not valid: {1}", addressbookId, ex)); //NOI18N
343 this.getLoggerBeanLocal().logTrace(MessageFormat.format("isAddressbookIdUsed: isValid={0} - EXIT!", isValid)); //NOI18N
350 public boolean isAddressbookNameUsed (final Addressbook addressbook) {
352 if (null == addressbook) {
354 throw new NullPointerException("addressbook is null"); //NOI18N
355 } else if (addressbook.getAddressbookUser() == null) {
356 // User instance is null
357 throw new NullPointerException("addressbook.addressbookUser is null."); //NOI18N
358 } else if (addressbook.getAddressbookUser().getUserId() == null) {
359 // User instance is null
360 throw new NullPointerException("addressbook.addressbookUser.userId is null."); //NOI18N
361 } else if (addressbook.getAddressbookUser().getUserId() < 1) {
362 // User instance is null
363 throw new NullPointerException(MessageFormat.format("addressbook.addressbookUser.userId={0} is invalid.", addressbook.getAddressbookUser().getUserId())); //NOI18N
364 } else if (addressbook.getAddressbookName() == null) {
365 // Address book name not set
366 throw new NullPointerException("addressbook.addressbookName should not be null"); //NOI18N
367 } else if (addressbook.getAddressbookName().isEmpty()) {
368 // Address book name not set
369 throw new IllegalArgumentException("addressbook.addressbookName should not be empty"); //NOI18N
372 // Get query instance
373 Query query = this.getEntityManager().createNamedQuery("SearchUserAddressbookName", Addressbook.class); //NOI18N
376 query.setParameter("user", addressbook.getAddressbookUser()); //NOI18N
377 query.setParameter("name", addressbook.getAddressbookName()); //NOI18N
379 // Default is not found
380 boolean isUsed = false;
384 // Get a single result
385 Addressbook dummy = (Addressbook) query.getSingleResult();
388 this.getLoggerBeanLocal().logDebug(MessageFormat.format("isAddressbookNameUsed: Found an address book: {0}", dummy)); //NOI18N
392 } catch (final NoResultException ex) {
393 // No result found, so log it away
394 this.getLoggerBeanLocal().logDebug(MessageFormat.format("isAddressbookNameUsed: getSingleResult() did not return a result: {0}", ex)); //NOI18N