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 Query query = this.getEntityManager().createNamedQuery("AllAddressbookEntries", List.class); //NOI18N
53 query.setParameter("addressbook", addressbook); //NOI18N
54 query.setParameter("owner", addressbook.getAddressbookUser()); //NOI18N
55 query.setParameter("sharer", addressbook.getAddressbookUser()); //NOI18N
58 return query.getResultList();
62 @SuppressWarnings ("unchecked")
63 public List<User> allUsersNotSharing (final User user, final Addressbook addressbook) {
65 this.getLoggerBeanLocal().logTrace(MessageFormat.format("allUsersNotSharing: user={0},addressbook={1} - CALLED!", user, addressbook)); //NOI18N
70 throw new NullPointerException("user is null"); //NOI18N
71 } else if (user.getUserId() == null) {
73 throw new NullPointerException("user.userId is null"); //NOI18N
74 } else if (user.getUserId() < 1) {
76 throw new IllegalArgumentException(MessageFormat.format("user.userId={0} is invalid", user.getUserId())); //NOI18N
77 } else if (null == addressbook) {
79 throw new NullPointerException("addressbook is null"); //NOI18N
80 } else if (addressbook.getAddressbookId() == null) {
82 throw new NullPointerException("addressbook.addressbookId is null"); //NOI18N
83 } else if (addressbook.getAddressbookId() < 1) {
85 throw new IllegalArgumentException(MessageFormat.format("addressbook.getAddressbookId={0} is invalid", addressbook.getAddressbookId())); //NOI18N
88 // Get named query for a user list without given user
89 Query allUsersExceptQuery = this.getEntityManager().createNamedQuery("SearchAllUsersExcept", List.class); //NOI18N
92 allUsersExceptQuery.setParameter("user", user); //NOI18N
95 List<User> allUsersExcept = allUsersExceptQuery.getResultList();
98 this.getLoggerBeanLocal().logDebug(MessageFormat.format("allUsersNotSharing: allUsersExcept.size()={0}", allUsersExcept.size())); //NOI18N
100 // Now get all shares this user has created
101 Query allSharesQuery = this.getEntityManager().createNamedQuery("SearchUserSharedAddressbooks", List.class); //NOI18N
104 allSharesQuery.setParameter("user", user); //NOI18N
106 // Get full list again
107 List<ShareableAddressbook> allShares = allSharesQuery.getResultList();
110 this.getLoggerBeanLocal().logDebug(MessageFormat.format("allUsersNotSharing: allShares.size()={0}", allShares.size())); //NOI18N
112 // List for users aharing with given
113 List<User> sharingUsers = new ArrayList<>(allShares.size());
116 for (final ShareableAddressbook share : allShares) {
118 this.getLoggerBeanLocal().logDebug(MessageFormat.format("allUsersNotSharing: share.shareUserSharee={0}", share.getShareUserSharee())); //NOI18N
121 sharingUsers.add(share.getShareUserSharee());
125 this.getLoggerBeanLocal().logDebug("allUsersNotSharing: sharingUsers.size()=" + sharingUsers.size());
127 // Init final user list
128 List<User> userList = new LinkedList<>();
130 // Walk through all users
131 for (final User foundUser : allUsersExcept) {
133 this.getLoggerBeanLocal().logDebug(MessageFormat.format("allUsersNotSharing: foundUser={0}", foundUser)); //NOI18N
135 // Does the list contain it ?
136 if (!sharingUsers.contains(foundUser)) {
138 this.getLoggerBeanLocal().logDebug(MessageFormat.format("allUsersNotSharing: foundUser={0} - ADDING!", foundUser)); //NOI18N
141 userList.add(foundUser);
146 this.getLoggerBeanLocal().logTrace(MessageFormat.format("allUsersNotSharing: userList.size()={0} - EXIT!", userList.size())); //NOI18N
153 @SuppressWarnings ("unchecked")
154 public Integer countAllUserSharedAddressbooks (final User user) {
156 this.getLoggerBeanLocal().logTrace(MessageFormat.format("countAllUserSharedAddressbooks: user={0} - CALLED!", user)); //NOI18N
158 // user should be valid
161 throw new NullPointerException("user is null"); //NOI18N
162 } else if (user.getUserId() == null) {
164 throw new NullPointerException("user.userId is null"); //NOI18N
165 } else if (user.getUserId() < 1) {
167 throw new IllegalArgumentException(MessageFormat.format("user.userId={0} is invalid", user.getUserId())); //NOI18N
171 Query query = this.getEntityManager().createNamedQuery("SearchUserSharedAddressbooks", List.class); //NOI18N
174 query.setParameter("user", user); //NOI18N
182 List<ShareableAddressbook> dummy = query.getResultList();
185 count = dummy.size();
186 } catch (final NoResultException ex) {
187 // Need to catch this, so log it
188 this.getLoggerBeanLocal().logDebug(MessageFormat.format("countAllUserSharedAddressbooks: getResultList() failed: {0}", ex)); //NOI18N
192 this.getLoggerBeanLocal().logTrace(MessageFormat.format("countAllUserSharedAddressbooks: count={0} - EXIT!", count)); //NOI18N
199 public Addressbook createAddressbook (final Addressbook addressbook) throws AddressbookNameAlreadyUsedException {
201 if (null == addressbook) {
203 throw new NullPointerException("addressbook is null"); //NOI18N
204 } else if (addressbook.getAddressbookUser() == null) {
205 // User instance is null
206 throw new NullPointerException("addressbook.user should not be null."); //NOI18N
207 } else if (addressbook.getAddressbookName() == null) {
208 // Address book name not set
209 throw new NullPointerException("addressbook.addressbookName should not be null"); //NOI18N
210 } else if (addressbook.getAddressbookName().isEmpty()) {
211 // Address book name not set
212 throw new IllegalArgumentException("addressbook.addressbookName should not be empty"); //NOI18N
213 } else if (this.isAddressbookNameUsed(addressbook)) {
214 // The assigned user already used that name
215 throw new AddressbookNameAlreadyUsedException(addressbook);
219 this.getEntityManager().persist(addressbook);
221 // Flush it to get all data
222 this.getEntityManager().flush();
229 public Addressbook getAddressbookById (final Long addressbookId) throws AddressbookNotFoundException {
231 this.getLoggerBeanLocal().logTrace(MessageFormat.format("getAddressbookById: addressbookId={0} - CALLED!", addressbookId)); //NOI18N
233 // addressbookId should not be null or below 1
234 if (null == addressbookId) {
236 throw new NullPointerException("addressbookId is null"); //NOI18N
237 } else if (addressbookId < 1) {
239 throw new IllegalArgumentException(MessageFormat.format("addressbookId is not valid: {0}", addressbookId)); //NOI18N
240 } else if (!this.isAddressbookIdUsed(addressbookId)) {
241 // No address book found
242 throw new AddressbookNotFoundException(addressbookId);
245 // Get named query instance
246 Query query = this.getEntityManager().createNamedQuery("SearchAddressbookById", UserAddressbook.class); //NOI18N
249 query.setParameter("id", addressbookId); //NOI18N
252 return (Addressbook) query.getSingleResult();
256 @SuppressWarnings ("unchecked")
257 public List<Addressbook> getUsersAddressbookList (final User loggedInUser) {
259 this.getLoggerBeanLocal().logTrace(MessageFormat.format("getUsersList: loggedInUser={0} - CALLED!", loggedInUser)); //NOI18N
261 // Is the user instance null?
262 if (null == loggedInUser) {
264 throw new NullPointerException("loggedInUser is null"); //NOI18N
267 // Get query instance
268 Query query = this.getEntityManager().createNamedQuery("AllUsersAddressbooks", List.class); //NOI18N
271 query.setParameter("param", loggedInUser); //NOI18N
273 // Get full list from JPA
274 List<Addressbook> addressbooks = query.getResultList();
281 public boolean isAddressbookIdUsed (final Long addressbookId) {
283 this.getLoggerBeanLocal().logTrace(MessageFormat.format("isAddressbookIdUsed: addressbookId={0} - CALLED!", addressbookId)); //NOI18N
285 // Is it null or zero?
286 if (null == addressbookId) {
288 throw new NullPointerException("addressbookId is null"); //NOI18N
289 } else if (addressbookId < 1) {
290 // Not valid id number
291 throw new IllegalArgumentException(MessageFormat.format("addressbookId is not valid: {0}", addressbookId)); //NOI18N
294 // Get query instance
295 Query query = this.getEntityManager().createNamedQuery("SearchAddressbookById", UserAddressbook.class); //NOI18N
298 query.setParameter("id", addressbookId); //NOI18N
300 // Default is not valid
301 boolean isValid = false;
303 // Try it again, yes no other way
306 Addressbook addressbook = (Addressbook) query.getSingleResult();
309 this.getLoggerBeanLocal().logDebug(MessageFormat.format("isAddressbookIdUsed: addressbook={0} - FOUND!", addressbook)); //NOI18N
313 } catch (final NoResultException ex) {
314 // Debug log only, maybe out-dated link followed
315 this.getLoggerBeanLocal().logDebug(MessageFormat.format("isAddressbookIdUsed: addressbookId={0} is not valid: {1}", addressbookId, ex)); //NOI18N
319 this.getLoggerBeanLocal().logTrace(MessageFormat.format("isAddressbookIdUsed: isValid={0} - EXIT!", isValid)); //NOI18N
326 public boolean isAddressbookNameUsed (final Addressbook addressbook) {
328 if (null == addressbook) {
330 throw new NullPointerException("addressbook is null"); //NOI18N
331 } else if (addressbook.getAddressbookUser() == null) {
332 // User instance is null
333 throw new NullPointerException("addressbook.user should not be null."); //NOI18N
334 } else if (addressbook.getAddressbookName() == null) {
335 // Address book name not set
336 throw new NullPointerException("addressbook.addressbookName should not be null"); //NOI18N
337 } else if (addressbook.getAddressbookName().isEmpty()) {
338 // Address book name not set
339 throw new IllegalArgumentException("addressbook.addressbookName should not be empty"); //NOI18N
342 // Get query instance
343 Query query = this.getEntityManager().createNamedQuery("SearchUserAddressbookName", Addressbook.class); //NOI18N
346 query.setParameter("user", addressbook.getAddressbookUser()); //NOI18N
347 query.setParameter("name", addressbook.getAddressbookName()); //NOI18N
349 // Default is not found
350 boolean isUsed = false;
354 // Get a single result
355 Addressbook dummy = (Addressbook) query.getSingleResult();
358 this.getLoggerBeanLocal().logDebug(MessageFormat.format("isAddressbookNameUsed: Found an address book: {0}", dummy)); //NOI18N
362 } catch (final NoResultException ex) {
363 // No result found, so log it away
364 this.getLoggerBeanLocal().logDebug(MessageFormat.format("isAddressbookNameUsed: getSingleResult() did not return a result: {0}", ex)); //NOI18N