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 isAddressbookNameUsed (final Addressbook addressbook) {
115 if (null == addressbook) {
117 throw new NullPointerException("addressbook is null"); //NOI18N
118 } else if (addressbook.getAddressbookUser() == null) {
119 // User instance is null
120 throw new NullPointerException("addressbook.user should not be null."); //NOI18N
121 } else if (addressbook.getAddressbookName() == null) {
122 // Address book name not set
123 throw new NullPointerException("addressbook.addressbookName should not be null"); //NOI18N
124 } else if (addressbook.getAddressbookName().isEmpty()) {
125 // Address book name not set
126 throw new IllegalArgumentException("addressbook.addressbookName should not be empty"); //NOI18N
129 // Get query instance
130 Query query = this.getEntityManager().createNamedQuery("SearchUserAddressbookName", Addressbook.class); //NOI18N
133 query.setParameter("user", addressbook.getAddressbookUser()); //NOI18N
134 query.setParameter("name", addressbook.getAddressbookName()); //NOI18N
136 // Default is not found
137 boolean isUsed = false;
141 // Get a single result
142 Addressbook dummy = (Addressbook) query.getSingleResult();
145 this.getLoggerBeanLocal().logDebug(MessageFormat.format("isAddressbookNameUsed: Found an address book: {0}", dummy)); //NOI18N
149 } catch (final NoResultException ex) {
150 // No result found, so log it away
151 this.getLoggerBeanLocal().logDebug("isAddressbookNameUsed: getSingleResult() did not return a result: " + ex); //NOI18N