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.model.addressbook.entry.AddressbookEntry;
26 import org.mxchange.jcoreee.database.BaseDatabaseBean;
27 import org.mxchange.jusercore.model.user.User;
30 * A stateless bean handling addressbooks
32 * @author Roland Haeder
34 @Stateless (name = "addressbook", mappedName = "ejb/stateless-addressbook", description = "A stateless bean for handling addressbooks")
35 public class AddressbookSessionBean extends BaseDatabaseBean implements AddressbookSessionBeanRemote {
40 private static final long serialVersionUID = 129_857_871_287_691L;
43 @SuppressWarnings ("unchecked")
44 public List<AddressbookEntry> allEntries (final Addressbook addressbook) {
46 Query query = this.getEntityManager().createNamedQuery("AllAddressbookEntries"); //NOI18N
49 query.setParameter("addressbook", addressbook); //NOI18N
50 query.setParameter("owner", addressbook.getAddressbookUser()); //NOI18N
51 query.setParameter("sharer", addressbook.getAddressbookUser()); //NOI18N
54 return query.getResultList();
58 @SuppressWarnings ("unchecked")
59 public List<Addressbook> getUsersList (final User loggedInUser) {
61 this.getLoggerBeanLocal().logTrace(MessageFormat.format("getUsersList: loggedInUser={0} - CALLED!", loggedInUser)); //NOI18N
63 // Is the user instance null?
64 if (null == loggedInUser) {
66 throw new NullPointerException("loggedInUser is null"); //NOI18N
70 Query query = this.getEntityManager().createNamedQuery("AllUsersAddressbooks", List.class); //NOI18N
73 query.setParameter("param", loggedInUser); //NOI18N
75 // Get full list from JPA
76 List<Addressbook> addressbooks = query.getResultList();
83 public Addressbook createAddressbook (final Addressbook addressbook) throws AddressbookNameAlreadyUsedException {
85 if (null == addressbook) {
87 throw new NullPointerException("addressbook is null"); //NOI18N
88 } else if (addressbook.getAddressbookUser() == null) {
89 // User instance is null
90 throw new NullPointerException("addressbook.user should not be null."); //NOI18N
91 } else if (addressbook.getAddressbookName() == null) {
92 // Address book name not set
93 throw new NullPointerException("addressbook.addressbookName should not be null"); //NOI18N
94 } else if (addressbook.getAddressbookName().isEmpty()) {
95 // Address book name not set
96 throw new IllegalArgumentException("addressbook.addressbookName should not be empty"); //NOI18N
97 } else if (this.isAddressbookNameUsed(addressbook)) {
98 // The assigned user already used that name
99 throw new AddressbookNameAlreadyUsedException(addressbook);
103 this.getEntityManager().persist(addressbook);
105 // Flush it to get all data
106 this.getEntityManager().flush();
113 public boolean isAddressbookIdUsed (final Long addressbookId) {
114 // Is it null or zero?
115 if (null == addressbookId) {
117 throw new NullPointerException("addressbookId is null"); //NOI18N
118 } else if (addressbookId < 1) {
119 // Not valid id number
120 throw new IllegalArgumentException(MessageFormat.format("addressbookId is not valid: {0}", addressbookId)); //NOI18N
123 // Get query instance
124 Query query = this.getEntityManager().createNamedQuery("FindAddressbookById");
127 query.setParameter("id", addressbookId);
129 // Default is not valid
130 boolean isValid = false;
132 // Try it again, yes no other way
135 Addressbook addressbook = (Addressbook) query.getSingleResult();
139 } catch (final NoResultException ex) {
140 // Debug log only, maybe out-dated link followed
141 this.getLoggerBeanLocal().logDebug(MessageFormat.format("isAddressbookIdUsed: addressbookId={0} is not valid: {1}", addressbookId, ex));
149 public boolean isAddressbookNameUsed (final Addressbook addressbook) {
151 if (null == addressbook) {
153 throw new NullPointerException("addressbook is null"); //NOI18N
154 } else if (addressbook.getAddressbookUser() == null) {
155 // User instance is null
156 throw new NullPointerException("addressbook.user should not be null."); //NOI18N
157 } else if (addressbook.getAddressbookName() == null) {
158 // Address book name not set
159 throw new NullPointerException("addressbook.addressbookName should not be null"); //NOI18N
160 } else if (addressbook.getAddressbookName().isEmpty()) {
161 // Address book name not set
162 throw new IllegalArgumentException("addressbook.addressbookName should not be empty"); //NOI18N
165 // Get query instance
166 Query query = this.getEntityManager().createNamedQuery("SearchUserAddressbookName", Addressbook.class); //NOI18N
169 query.setParameter("user", addressbook.getAddressbookUser()); //NOI18N
170 query.setParameter("name", addressbook.getAddressbookName()); //NOI18N
172 // Default is not found
173 boolean isUsed = false;
177 // Get a single result
178 Addressbook dummy = (Addressbook) query.getSingleResult();
181 this.getLoggerBeanLocal().logDebug(MessageFormat.format("isAddressbookNameUsed: Found an address book: {0}", dummy)); //NOI18N
185 } catch (final NoResultException ex) {
186 // No result found, so log it away
187 this.getLoggerBeanLocal().logDebug(MessageFormat.format("isAddressbookNameUsed: getSingleResult() did not return a result: {0}", ex)); //NOI18N //NOI18N