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.List;
21 import javax.ejb.Stateless;
22 import javax.persistence.NoResultException;
23 import javax.persistence.Query;
24 import org.mxchange.addressbook.exceptions.AddressbookNameAlreadyUsedException;
25 import org.mxchange.addressbook.exceptions.AddressbookNotFoundException;
26 import org.mxchange.addressbook.model.addressbook.entry.AddressbookEntry;
27 import org.mxchange.jcoreee.database.BaseDatabaseBean;
28 import org.mxchange.jusercore.model.user.User;
31 * A stateless bean handling addressbooks
33 * @author Roland Haeder
35 @Stateless (name = "addressbook", mappedName = "ejb/stateless-addressbook", description = "A stateless bean for handling addressbooks")
36 public class AddressbookSessionBean extends BaseDatabaseBean implements AddressbookSessionBeanRemote {
41 private static final long serialVersionUID = 129_857_871_287_691L;
44 @SuppressWarnings ("unchecked")
45 public List<AddressbookEntry> allEntries (final Addressbook addressbook) {
47 Query query = this.getEntityManager().createNamedQuery("AllAddressbookEntries"); //NOI18N
50 query.setParameter("addressbook", addressbook); //NOI18N
51 query.setParameter("owner", addressbook.getAddressbookUser()); //NOI18N
52 query.setParameter("sharer", addressbook.getAddressbookUser()); //NOI18N
55 return query.getResultList();
59 public Addressbook createAddressbook (final Addressbook addressbook) throws AddressbookNameAlreadyUsedException {
61 if (null == addressbook) {
63 throw new NullPointerException("addressbook is null"); //NOI18N
64 } else if (addressbook.getAddressbookUser() == null) {
65 // User instance is null
66 throw new NullPointerException("addressbook.user should not be null."); //NOI18N
67 } else if (addressbook.getAddressbookName() == null) {
68 // Address book name not set
69 throw new NullPointerException("addressbook.addressbookName should not be null"); //NOI18N
70 } else if (addressbook.getAddressbookName().isEmpty()) {
71 // Address book name not set
72 throw new IllegalArgumentException("addressbook.addressbookName should not be empty"); //NOI18N
73 } else if (this.isAddressbookNameUsed(addressbook)) {
74 // The assigned user already used that name
75 throw new AddressbookNameAlreadyUsedException(addressbook);
79 this.getEntityManager().persist(addressbook);
81 // Flush it to get all data
82 this.getEntityManager().flush();
89 public Addressbook getAddressbookById (final Long addressbookId) throws AddressbookNotFoundException {
91 this.getLoggerBeanLocal().logTrace(MessageFormat.format("getAddressbookById: addressbookId={0} - CALLED!", addressbookId)); //NOI18N
93 // addressbookId should not be null or below 1
94 if (null == addressbookId) {
96 throw new NullPointerException("addressbookId is null"); //NOI18N
97 } else if (addressbookId < 1) {
99 throw new IllegalArgumentException(MessageFormat.format("addressbookId is not valid: {0}", addressbookId)); //NOI18N
100 } else if (!this.isAddressbookIdUsed(addressbookId)) {
101 // No address book found
102 throw new AddressbookNotFoundException(addressbookId);
105 // Get named query instance
106 Query query = this.getEntityManager().createNamedQuery("SearchAddressbookById"); //NOI18N
109 query.setParameter("id", addressbookId); //NOI18N
112 return (Addressbook) query.getSingleResult();
116 @SuppressWarnings ("unchecked")
117 public List<Addressbook> getUsersList (final User loggedInUser) {
119 this.getLoggerBeanLocal().logTrace(MessageFormat.format("getUsersList: loggedInUser={0} - CALLED!", loggedInUser)); //NOI18N
121 // Is the user instance null?
122 if (null == loggedInUser) {
124 throw new NullPointerException("loggedInUser is null"); //NOI18N
127 // Get query instance
128 Query query = this.getEntityManager().createNamedQuery("AllUsersAddressbooks", List.class); //NOI18N
131 query.setParameter("param", loggedInUser); //NOI18N
133 // Get full list from JPA
134 List<Addressbook> addressbooks = query.getResultList();
141 public boolean isAddressbookIdUsed (final Long addressbookId) {
143 this.getLoggerBeanLocal().logTrace(MessageFormat.format("isAddressbookIdUsed: addressbookId={0} - CALLED!", addressbookId)); //NOI18N
145 // Is it null or zero?
146 if (null == addressbookId) {
148 throw new NullPointerException("addressbookId is null"); //NOI18N
149 } else if (addressbookId < 1) {
150 // Not valid id number
151 throw new IllegalArgumentException(MessageFormat.format("addressbookId is not valid: {0}", addressbookId)); //NOI18N
154 // Get query instance
155 Query query = this.getEntityManager().createNamedQuery("SearchAddressbookById"); //NOI18N
158 query.setParameter("id", addressbookId); //NOI18N
160 // Default is not valid
161 boolean isValid = false;
163 // Try it again, yes no other way
166 Addressbook addressbook = (Addressbook) query.getSingleResult();
169 this.getLoggerBeanLocal().logDebug(MessageFormat.format("isAddressbookIdUsed: addressbook={0} - FOUND!", addressbook)); //NOI18N
173 } catch (final NoResultException ex) {
174 // Debug log only, maybe out-dated link followed
175 this.getLoggerBeanLocal().logDebug(MessageFormat.format("isAddressbookIdUsed: addressbookId={0} is not valid: {1}", addressbookId, ex)); //NOI18N
179 this.getLoggerBeanLocal().logTrace(MessageFormat.format("isAddressbookIdUsed: isValid={0} - EXIT!", isValid)); //NOI18N
186 public boolean isAddressbookNameUsed (final Addressbook addressbook) {
188 if (null == addressbook) {
190 throw new NullPointerException("addressbook is null"); //NOI18N
191 } else if (addressbook.getAddressbookUser() == null) {
192 // User instance is null
193 throw new NullPointerException("addressbook.user should not be null."); //NOI18N
194 } else if (addressbook.getAddressbookName() == null) {
195 // Address book name not set
196 throw new NullPointerException("addressbook.addressbookName should not be null"); //NOI18N
197 } else if (addressbook.getAddressbookName().isEmpty()) {
198 // Address book name not set
199 throw new IllegalArgumentException("addressbook.addressbookName should not be empty"); //NOI18N
202 // Get query instance
203 Query query = this.getEntityManager().createNamedQuery("SearchUserAddressbookName", Addressbook.class); //NOI18N
206 query.setParameter("user", addressbook.getAddressbookUser()); //NOI18N
207 query.setParameter("name", addressbook.getAddressbookName()); //NOI18N
209 // Default is not found
210 boolean isUsed = false;
214 // Get a single result
215 Addressbook dummy = (Addressbook) query.getSingleResult();
218 this.getLoggerBeanLocal().logDebug(MessageFormat.format("isAddressbookNameUsed: Found an address book: {0}", dummy)); //NOI18N
222 } catch (final NoResultException ex) {
223 // No result found, so log it away
224 this.getLoggerBeanLocal().logDebug(MessageFormat.format("isAddressbookNameUsed: getSingleResult() did not return a result: {0}", ex)); //NOI18N