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.LinkedList;
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); //NOI18N
91 allUsersExceptQuery.setParameter("user", user); //NOI18N
94 List<User> allUsersExcept = allUsersExceptQuery.getResultList();
97 this.getLoggerBeanLocal().logDebug(MessageFormat.format("allUsersNotSharing: allUsersExcept.size()={0}", allUsersExcept.size())); //NOI18N
99 // Now get all shares this user has created
100 Query allSharesQuery = this.getEntityManager().createNamedQuery("SearchUserSharedAddressbooks", List.class); //NOI18N
103 allSharesQuery.setParameter("user", user); //NOI18N
105 // Get full list again
106 List<ShareableAddressbook> allShares = allSharesQuery.getResultList();
109 this.getLoggerBeanLocal().logDebug(MessageFormat.format("allUsersNotSharing: allShares.size()={0}", allShares.size())); //NOI18N
111 // Init final user list
112 List<User> userList = new LinkedList<>();
114 // Walk through all users
115 for (final ShareableAddressbook share : allShares) {
117 this.getLoggerBeanLocal().logDebug(MessageFormat.format("allUsersNotSharing: share.shareUserSharee={0}", share.getShareUserSharee())); //NOI18N
119 // Does the list contain it ?
120 if (!allUsersExcept.contains(share.getShareUserSharee())) {
122 this.getLoggerBeanLocal().logDebug(MessageFormat.format("allUsersNotSharing: share.shareUserSharee={0} - ADDING!", share.getShareUserSharee())); //NOI18N
125 userList.add(share.getShareUserSharee());
130 this.getLoggerBeanLocal().logTrace(MessageFormat.format("allUsersNotSharing: userList.size()={0} - EXIT!", userList.size())); //NOI18N
137 @SuppressWarnings ("unchecked")
138 public Integer countAllUserSharedAddressbooks (final User user) {
140 this.getLoggerBeanLocal().logTrace(MessageFormat.format("countAllUserSharedAddressbooks: user={0} - CALLED!", user)); //NOI18N
142 // user should be valid
145 throw new NullPointerException("user is null"); //NOI18N
146 } else if (user.getUserId() == null) {
148 throw new NullPointerException("user.userId is null"); //NOI18N
149 } else if (user.getUserId() < 1) {
151 throw new IllegalArgumentException(MessageFormat.format("user.userId={0} is invalid", user.getUserId())); //NOI18N
155 Query query = this.getEntityManager().createNamedQuery("SearchUserSharedAddressbooks", List.class); //NOI18N
158 query.setParameter("user", user); //NOI18N
166 List<ShareableAddressbook> dummy = query.getResultList();
169 count = dummy.size();
170 } catch (final NoResultException ex) {
171 // Need to catch this, so log it
172 this.getLoggerBeanLocal().logDebug(MessageFormat.format("countAllUserSharedAddressbooks: getResultList() failed: {0}", ex)); //NOI18N
176 this.getLoggerBeanLocal().logTrace(MessageFormat.format("countAllUserSharedAddressbooks: count={0} - EXIT!", count)); //NOI18N
183 public Addressbook createAddressbook (final Addressbook addressbook) throws AddressbookNameAlreadyUsedException {
185 if (null == addressbook) {
187 throw new NullPointerException("addressbook is null"); //NOI18N
188 } else if (addressbook.getAddressbookUser() == null) {
189 // User instance is null
190 throw new NullPointerException("addressbook.user should not be null."); //NOI18N
191 } else if (addressbook.getAddressbookName() == null) {
192 // Address book name not set
193 throw new NullPointerException("addressbook.addressbookName should not be null"); //NOI18N
194 } else if (addressbook.getAddressbookName().isEmpty()) {
195 // Address book name not set
196 throw new IllegalArgumentException("addressbook.addressbookName should not be empty"); //NOI18N
197 } else if (this.isAddressbookNameUsed(addressbook)) {
198 // The assigned user already used that name
199 throw new AddressbookNameAlreadyUsedException(addressbook);
203 this.getEntityManager().persist(addressbook);
205 // Flush it to get all data
206 this.getEntityManager().flush();
213 public Addressbook getAddressbookById (final Long addressbookId) throws AddressbookNotFoundException {
215 this.getLoggerBeanLocal().logTrace(MessageFormat.format("getAddressbookById: addressbookId={0} - CALLED!", addressbookId)); //NOI18N
217 // addressbookId should not be null or below 1
218 if (null == addressbookId) {
220 throw new NullPointerException("addressbookId is null"); //NOI18N
221 } else if (addressbookId < 1) {
223 throw new IllegalArgumentException(MessageFormat.format("addressbookId is not valid: {0}", addressbookId)); //NOI18N
224 } else if (!this.isAddressbookIdUsed(addressbookId)) {
225 // No address book found
226 throw new AddressbookNotFoundException(addressbookId);
229 // Get named query instance
230 Query query = this.getEntityManager().createNamedQuery("SearchAddressbookById", UserAddressbook.class); //NOI18N
233 query.setParameter("id", addressbookId); //NOI18N
236 return (Addressbook) query.getSingleResult();
240 @SuppressWarnings ("unchecked")
241 public List<Addressbook> getUsersAddressbookList (final User loggedInUser) {
243 this.getLoggerBeanLocal().logTrace(MessageFormat.format("getUsersList: loggedInUser={0} - CALLED!", loggedInUser)); //NOI18N
245 // Is the user instance null?
246 if (null == loggedInUser) {
248 throw new NullPointerException("loggedInUser is null"); //NOI18N
251 // Get query instance
252 Query query = this.getEntityManager().createNamedQuery("AllUsersAddressbooks", List.class); //NOI18N
255 query.setParameter("param", loggedInUser); //NOI18N
257 // Get full list from JPA
258 List<Addressbook> addressbooks = query.getResultList();
265 public boolean isAddressbookIdUsed (final Long addressbookId) {
267 this.getLoggerBeanLocal().logTrace(MessageFormat.format("isAddressbookIdUsed: addressbookId={0} - CALLED!", addressbookId)); //NOI18N
269 // Is it null or zero?
270 if (null == addressbookId) {
272 throw new NullPointerException("addressbookId is null"); //NOI18N
273 } else if (addressbookId < 1) {
274 // Not valid id number
275 throw new IllegalArgumentException(MessageFormat.format("addressbookId is not valid: {0}", addressbookId)); //NOI18N
278 // Get query instance
279 Query query = this.getEntityManager().createNamedQuery("SearchAddressbookById", UserAddressbook.class); //NOI18N
282 query.setParameter("id", addressbookId); //NOI18N
284 // Default is not valid
285 boolean isValid = false;
287 // Try it again, yes no other way
290 Addressbook addressbook = (Addressbook) query.getSingleResult();
293 this.getLoggerBeanLocal().logDebug(MessageFormat.format("isAddressbookIdUsed: addressbook={0} - FOUND!", addressbook)); //NOI18N
297 } catch (final NoResultException ex) {
298 // Debug log only, maybe out-dated link followed
299 this.getLoggerBeanLocal().logDebug(MessageFormat.format("isAddressbookIdUsed: addressbookId={0} is not valid: {1}", addressbookId, ex)); //NOI18N
303 this.getLoggerBeanLocal().logTrace(MessageFormat.format("isAddressbookIdUsed: isValid={0} - EXIT!", isValid)); //NOI18N
310 public boolean isAddressbookNameUsed (final Addressbook addressbook) {
312 if (null == addressbook) {
314 throw new NullPointerException("addressbook is null"); //NOI18N
315 } else if (addressbook.getAddressbookUser() == null) {
316 // User instance is null
317 throw new NullPointerException("addressbook.user should not be null."); //NOI18N
318 } else if (addressbook.getAddressbookName() == null) {
319 // Address book name not set
320 throw new NullPointerException("addressbook.addressbookName should not be null"); //NOI18N
321 } else if (addressbook.getAddressbookName().isEmpty()) {
322 // Address book name not set
323 throw new IllegalArgumentException("addressbook.addressbookName should not be empty"); //NOI18N
326 // Get query instance
327 Query query = this.getEntityManager().createNamedQuery("SearchUserAddressbookName", Addressbook.class); //NOI18N
330 query.setParameter("user", addressbook.getAddressbookUser()); //NOI18N
331 query.setParameter("name", addressbook.getAddressbookName()); //NOI18N
333 // Default is not found
334 boolean isUsed = false;
338 // Get a single result
339 Addressbook dummy = (Addressbook) query.getSingleResult();
342 this.getLoggerBeanLocal().logDebug(MessageFormat.format("isAddressbookNameUsed: Found an address book: {0}", dummy)); //NOI18N
346 } catch (final NoResultException ex) {
347 // No result found, so log it away
348 this.getLoggerBeanLocal().logDebug(MessageFormat.format("isAddressbookNameUsed: getSingleResult() did not return a result: {0}", ex)); //NOI18N