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.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);
91 allUsersExceptQuery.setParameter("user", user);
94 List<User> allUsersExcept = allUsersExceptQuery.getResultList();
97 this.getLoggerBeanLocal().logDebug(MessageFormat.format("allUsersNotSharing: allUsersExcept.size()={0}", allUsersExcept.size()));
99 // Now get all users this user is sharing with, first a new query
100 Query allShareesQuery = this.getEntityManager().createNamedQuery("SearchUserSharedAddressbooks", List.class);
103 allShareesQuery.setParameter("user", user);
105 // Get full list again
106 List<User> allSharees = allShareesQuery.getResultList();
109 this.getLoggerBeanLocal().logDebug(MessageFormat.format("allUsersNotSharing: allSharees.size()={0}", allSharees.size()));
111 // Init final user list
112 List<User> userList = new ArrayList<>(0);
114 // Walk through all users
115 for (final User u : allUsersExcept) {
117 this.getLoggerBeanLocal().logDebug(MessageFormat.format("allUsersNotSharing: u={0}", u));
119 // Is this user not included?
120 if (!allSharees.contains(u)) {
123 this.getLoggerBeanLocal().logDebug(MessageFormat.format("allUsersNotSharing: u={0} - ADDING!", u));
131 this.getLoggerBeanLocal().logTrace(MessageFormat.format("allUsersNotSharing: userList.size()={0} - EXIT!", userList.size()));
138 @SuppressWarnings ("unchecked")
139 public Integer countAllUserSharedAddressbooks (final User user) {
141 this.getLoggerBeanLocal().logTrace(MessageFormat.format("countAllUserSharedAddressbooks: user={0} - CALLED!", user)); //NOI18N
143 // user should be valid
146 throw new NullPointerException("user is null"); //NOI18N
147 } else if (user.getUserId() == null) {
149 throw new NullPointerException("user.userId is null"); //NOI18N
150 } else if (user.getUserId() < 1) {
152 throw new IllegalArgumentException(MessageFormat.format("user.userId={0} is invalid", user.getUserId())); //NOI18N
156 Query query = this.getEntityManager().createNamedQuery("SearchUserSharedAddressbooks", List.class); //NOI18N
159 query.setParameter("user", user); //NOI18N
167 List<ShareableAddressbook> dummy = query.getResultList();
170 count = dummy.size();
171 } catch (final NoResultException ex) {
172 // Need to catch this, so log it
173 this.getLoggerBeanLocal().logDebug(MessageFormat.format("countAllUserSharedAddressbooks: getResultList() failed: {0}", ex)); //NOI18N
177 this.getLoggerBeanLocal().logTrace(MessageFormat.format("countAllUserSharedAddressbooks: count={0} - EXIT!", count)); //NOI18N
184 public Addressbook createAddressbook (final Addressbook addressbook) throws AddressbookNameAlreadyUsedException {
186 if (null == addressbook) {
188 throw new NullPointerException("addressbook is null"); //NOI18N
189 } else if (addressbook.getAddressbookUser() == null) {
190 // User instance is null
191 throw new NullPointerException("addressbook.user should not be null."); //NOI18N
192 } else if (addressbook.getAddressbookName() == null) {
193 // Address book name not set
194 throw new NullPointerException("addressbook.addressbookName should not be null"); //NOI18N
195 } else if (addressbook.getAddressbookName().isEmpty()) {
196 // Address book name not set
197 throw new IllegalArgumentException("addressbook.addressbookName should not be empty"); //NOI18N
198 } else if (this.isAddressbookNameUsed(addressbook)) {
199 // The assigned user already used that name
200 throw new AddressbookNameAlreadyUsedException(addressbook);
204 this.getEntityManager().persist(addressbook);
206 // Flush it to get all data
207 this.getEntityManager().flush();
214 public Addressbook getAddressbookById (final Long addressbookId) throws AddressbookNotFoundException {
216 this.getLoggerBeanLocal().logTrace(MessageFormat.format("getAddressbookById: addressbookId={0} - CALLED!", addressbookId)); //NOI18N
218 // addressbookId should not be null or below 1
219 if (null == addressbookId) {
221 throw new NullPointerException("addressbookId is null"); //NOI18N
222 } else if (addressbookId < 1) {
224 throw new IllegalArgumentException(MessageFormat.format("addressbookId is not valid: {0}", addressbookId)); //NOI18N
225 } else if (!this.isAddressbookIdUsed(addressbookId)) {
226 // No address book found
227 throw new AddressbookNotFoundException(addressbookId);
230 // Get named query instance
231 Query query = this.getEntityManager().createNamedQuery("SearchAddressbookById", UserAddressbook.class); //NOI18N
234 query.setParameter("id", addressbookId); //NOI18N
237 return (Addressbook) query.getSingleResult();
241 @SuppressWarnings ("unchecked")
242 public List<Addressbook> getUsersAddressbookList (final User loggedInUser) {
244 this.getLoggerBeanLocal().logTrace(MessageFormat.format("getUsersList: loggedInUser={0} - CALLED!", loggedInUser)); //NOI18N
246 // Is the user instance null?
247 if (null == loggedInUser) {
249 throw new NullPointerException("loggedInUser is null"); //NOI18N
252 // Get query instance
253 Query query = this.getEntityManager().createNamedQuery("AllUsersAddressbooks", List.class); //NOI18N
256 query.setParameter("param", loggedInUser); //NOI18N
258 // Get full list from JPA
259 List<Addressbook> addressbooks = query.getResultList();
266 public boolean isAddressbookIdUsed (final Long addressbookId) {
268 this.getLoggerBeanLocal().logTrace(MessageFormat.format("isAddressbookIdUsed: addressbookId={0} - CALLED!", addressbookId)); //NOI18N
270 // Is it null or zero?
271 if (null == addressbookId) {
273 throw new NullPointerException("addressbookId is null"); //NOI18N
274 } else if (addressbookId < 1) {
275 // Not valid id number
276 throw new IllegalArgumentException(MessageFormat.format("addressbookId is not valid: {0}", addressbookId)); //NOI18N
279 // Get query instance
280 Query query = this.getEntityManager().createNamedQuery("SearchAddressbookById", UserAddressbook.class); //NOI18N
283 query.setParameter("id", addressbookId); //NOI18N
285 // Default is not valid
286 boolean isValid = false;
288 // Try it again, yes no other way
291 Addressbook addressbook = (Addressbook) query.getSingleResult();
294 this.getLoggerBeanLocal().logDebug(MessageFormat.format("isAddressbookIdUsed: addressbook={0} - FOUND!", addressbook)); //NOI18N
298 } catch (final NoResultException ex) {
299 // Debug log only, maybe out-dated link followed
300 this.getLoggerBeanLocal().logDebug(MessageFormat.format("isAddressbookIdUsed: addressbookId={0} is not valid: {1}", addressbookId, ex)); //NOI18N
304 this.getLoggerBeanLocal().logTrace(MessageFormat.format("isAddressbookIdUsed: isValid={0} - EXIT!", isValid)); //NOI18N
311 public boolean isAddressbookNameUsed (final Addressbook addressbook) {
313 if (null == addressbook) {
315 throw new NullPointerException("addressbook is null"); //NOI18N
316 } else if (addressbook.getAddressbookUser() == null) {
317 // User instance is null
318 throw new NullPointerException("addressbook.user should not be null."); //NOI18N
319 } else if (addressbook.getAddressbookName() == null) {
320 // Address book name not set
321 throw new NullPointerException("addressbook.addressbookName should not be null"); //NOI18N
322 } else if (addressbook.getAddressbookName().isEmpty()) {
323 // Address book name not set
324 throw new IllegalArgumentException("addressbook.addressbookName should not be empty"); //NOI18N
327 // Get query instance
328 Query query = this.getEntityManager().createNamedQuery("SearchUserAddressbookName", Addressbook.class); //NOI18N
331 query.setParameter("user", addressbook.getAddressbookUser()); //NOI18N
332 query.setParameter("name", addressbook.getAddressbookName()); //NOI18N
334 // Default is not found
335 boolean isUsed = false;
339 // Get a single result
340 Addressbook dummy = (Addressbook) query.getSingleResult();
343 this.getLoggerBeanLocal().logDebug(MessageFormat.format("isAddressbookNameUsed: Found an address book: {0}", dummy)); //NOI18N
347 } catch (final NoResultException ex) {
348 // No result found, so log it away
349 this.getLoggerBeanLocal().logDebug(MessageFormat.format("isAddressbookNameUsed: getSingleResult() did not return a result: {0}", ex)); //NOI18N