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.jcoreee.database.BaseDatabaseBean;
26 import org.mxchange.jusercore.model.user.User;
29 * A stateless bean handling addressbooks
31 * @author Roland Haeder
33 @Stateless (name = "addressbook", mappedName = "ejb/stateless-addressbook", description = "A stateless bean for handling addressbooks")
34 public class AddressbookSessionBean extends BaseDatabaseBean implements AddressbookSessionBeanRemote {
39 private static final long serialVersionUID = 129_857_871_287_691L;
42 @SuppressWarnings ("unchecked")
43 public List<Addressbook> getUsersList (final User loggedInUser) {
45 this.getLoggerBeanLocal().logTrace(MessageFormat.format("getUsersList: loggedInUser={0} - CALLED!", loggedInUser));
47 // Is the user instance null?
48 if (null == loggedInUser) {
50 throw new NullPointerException("loggedInUser is null");
54 Query query = this.getEntityManager().createNamedQuery("AllUsersAddressbooks", List.class); //NOI18N
57 query.setParameter("param", loggedInUser); //NOI18N
59 // Get full list from JPA
60 List<Addressbook> addressbooks = query.getResultList();
67 public Addressbook createAddressbook (final Addressbook addressbook) throws AddressbookNameAlreadyUsedException {
69 if (null == addressbook) {
71 throw new NullPointerException("addressbook is null"); //NOI18N
72 } else if (addressbook.getAddressbookUser() == null) {
73 // User instance is null
74 throw new NullPointerException("addressbook.user should not be null."); //NOI18N
75 } else if (addressbook.getAddressbookName() == null) {
76 // Address book name not set
77 throw new NullPointerException("addressbook.addressbookName should not be null"); //NOI18N
78 } else if (addressbook.getAddressbookName().isEmpty()) {
79 // Address book name not set
80 throw new IllegalArgumentException("addressbook.addressbookName should not be empty"); //NOI18N
81 } else if (this.isAddressbookNameUsed(addressbook)) {
82 // The assigned user already used that name
83 throw new AddressbookNameAlreadyUsedException(addressbook);
87 this.getEntityManager().persist(addressbook);
89 // Flush it to get all data
90 this.getEntityManager().flush();
97 public boolean isAddressbookNameUsed (final Addressbook addressbook) {
99 if (null == addressbook) {
101 throw new NullPointerException("addressbook is null"); //NOI18N
102 } else if (addressbook.getAddressbookUser() == null) {
103 // User instance is null
104 throw new NullPointerException("addressbook.user should not be null."); //NOI18N
105 } else if (addressbook.getAddressbookName() == null) {
106 // Address book name not set
107 throw new NullPointerException("addressbook.addressbookName should not be null"); //NOI18N
108 } else if (addressbook.getAddressbookName().isEmpty()) {
109 // Address book name not set
110 throw new IllegalArgumentException("addressbook.addressbookName should not be empty"); //NOI18N
113 // Get query instance
114 Query query = this.getEntityManager().createNamedQuery("SearchUserAddressbookName", Addressbook.class);
117 query.setParameter("user", addressbook.getAddressbookUser());
118 query.setParameter("name", addressbook.getAddressbookName());
120 // Default is not found
121 boolean isUsed = false;
125 // Get a single result
126 Addressbook dummy = (Addressbook) query.getSingleResult();
129 this.getLoggerBeanLocal().logDebug(MessageFormat.format("isAddressbookNameUsed: Found an address book: {0}", dummy));
133 } catch (final NoResultException ex) {
134 // No result found, so log it away
135 this.getLoggerBeanLocal().logDebug("isAddressbookNameUsed: getSingleResult() did not return a result: " + ex);