]> git.mxchange.org Git - jjobs-mailer-ejb.git/blob - src/java/org/mxchange/addressbook/model/addressbook/AddressbookSessionBean.java
Rewrite:
[jjobs-mailer-ejb.git] / src / java / org / mxchange / addressbook / model / addressbook / AddressbookSessionBean.java
1 /*
2  * Copyright (C) 2015 Roland Haeder
3  *
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.
8  *
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.
13  *
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/>.
16  */
17 package org.mxchange.addressbook.model.addressbook;
18
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;
27
28 /**
29  * A stateless bean handling addressbooks
30  * <p>
31  * @author Roland Haeder
32  */
33 @Stateless (name = "addressbook", mappedName = "ejb/stateless-addressbook", description = "A stateless bean for handling addressbooks")
34 public class AddressbookSessionBean extends BaseDatabaseBean implements AddressbookSessionBeanRemote {
35
36         /**
37          * Serial number
38          */
39         private static final long serialVersionUID = 129_857_871_287_691L;
40
41         @Override
42         @SuppressWarnings ("unchecked")
43         public List<Addressbook> getUsersList (final User loggedInUser) {
44                 // Trace message
45                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("getUsersList: loggedInUser={0} - CALLED!", loggedInUser));
46
47                 // Is the user instance null?
48                 if (null == loggedInUser) {
49                         // Abort here
50                         throw new NullPointerException("loggedInUser is null");
51                 }
52
53                 // Get query instance
54                 Query query = this.getEntityManager().createNamedQuery("AllUsersAddressbooks", List.class); //NOI18N
55
56                 // Set parameter
57                 query.setParameter("param", loggedInUser); //NOI18N
58
59                 // Get full list from JPA
60                 List<Addressbook> addressbooks = query.getResultList();
61
62                 // Return it
63                 return addressbooks;
64         }
65
66         @Override
67         public Addressbook createAddressbook (final Addressbook addressbook) throws AddressbookNameAlreadyUsedException {
68                 // Is it not null?
69                 if (null == addressbook) {
70                         // Abort here
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);
84                 }
85
86                 // Persist it now
87                 this.getEntityManager().persist(addressbook);
88
89                 // Flush it to get all data
90                 this.getEntityManager().flush();
91
92                 // Return it updated
93                 return addressbook;
94         }
95
96         @Override
97         public boolean isAddressbookNameUsed (final Addressbook addressbook) {
98                 // Is it not null?
99                 if (null == addressbook) {
100                         // Abort here
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
111                 }
112
113                 // Get query instance
114                 Query query = this.getEntityManager().createNamedQuery("SearchUserAddressbookName", Addressbook.class);
115
116                 // Set parameter
117                 query.setParameter("user", addressbook.getAddressbookUser());
118                 query.setParameter("name", addressbook.getAddressbookName());
119
120                 // Default is not found
121                 boolean isUsed = false;
122
123                 // Try it
124                 try {
125                         // Get a single result
126                         Addressbook dummy = (Addressbook) query.getSingleResult();
127
128                         // Log it
129                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("isAddressbookNameUsed: Found an address book: {0}", dummy));
130
131                         // Found one
132                         isUsed = true;
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);
136                 }
137
138                 // Return result
139                 return isUsed;
140         }
141 }