]> git.mxchange.org Git - addressbook-mailer-ejb.git/blob - src/java/org/mxchange/addressbook/model/addressbook/AddressbookSessionBean.java
95e504518b6f46f8034e069225cbc92c2428448a
[addressbook-mailer-ejb.git] / src / java / org / mxchange / addressbook / model / addressbook / AddressbookSessionBean.java
1 /*
2  * Copyright (C) 2016, 2017 Roland Häder
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU Affero General Public License as
6  * published by the Free Software Foundation, either version 3 of the
7  * License, or (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 Affero General Public License for more details.
13  *
14  * You should have received a copy of the GNU Affero 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.GregorianCalendar;
21 import java.util.List;
22 import javax.ejb.Stateless;
23 import javax.persistence.NoResultException;
24 import javax.persistence.Query;
25 import org.mxchange.addressbook.database.BaseAddressbookDatabaseBean;
26 import org.mxchange.jaddressbook.exceptions.AddressbookNameAlreadyUsedException;
27 import org.mxchange.jaddressbook.exceptions.AddressbookNotFoundException;
28 import org.mxchange.jaddressbook.model.addressbook.Addressbook;
29 import org.mxchange.jaddressbook.model.addressbook.UserAddressbook;
30 import org.mxchange.jaddressbook.model.addressbook.entry.AddressbookEntry;
31 import org.mxchange.jusercore.model.user.User;
32
33 /**
34  * A stateless bean handling address books
35  * <p>
36  * @author Roland Häder<roland@mxchange.org>
37  */
38 @Stateless (name = "addressbook", description = "A stateless bean for handling Addressbook addressbooks")
39 public class AddressbookSessionBean extends BaseAddressbookDatabaseBean implements AddressbookSessionBeanRemote {
40
41         /**
42          * Serial number
43          */
44         private static final long serialVersionUID = 129_857_871_287_691L;
45
46         @Override
47         @SuppressWarnings ("unchecked")
48         public List<AddressbookEntry> allEntries (final Addressbook addressbook) {
49                 // Trace message
50                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("allEntries: addressbook={0} - CALLED!", addressbook)); //NOI18N
51
52                 // Validate parameter
53                 if (null == addressbook) {
54                         // Throw NPE
55                         throw new NullPointerException("addressbook is null");
56                 } else if (addressbook.getAddressbookId() == null) {
57                         // Throw NPE again
58                         throw new NullPointerException("addressbook.addressbookId is null");
59                 } else if (addressbook.getAddressbookId() < 1) {
60                         // Invalid id number
61                         throw new IllegalArgumentException(MessageFormat.format("addressbook.addressbookId={0} is invalid.", addressbook.getAddressbookId()));
62                 } else if (addressbook.getAddressbookUser() == null) {
63                         // Throw again NPE
64                         throw new NullPointerException("addressbook.addressbookUser is null");
65                 } else if (addressbook.getAddressbookUser().getUserId() == null) {
66                         // Throw again NPE
67                         throw new NullPointerException("addressbook.addressbookUser.userId is null");
68                 } else if (addressbook.getAddressbookUser().getUserId() < 1) {
69                         // Invalid id number again
70                         throw new IllegalArgumentException(MessageFormat.format("addressbook.addressbookUser.userId={0} is invalid", addressbook.getAddressbookUser().getUserId()));
71                 }
72
73                 // Generate query
74                 Query query = this.getEntityManager().createNamedQuery("SearchUsersAddressbookEntries", List.class); //NOI18N
75
76                 // Set parameters
77                 query.setParameter("addressbook", addressbook); //NOI18N
78                 query.setParameter("owner", addressbook.getAddressbookUser()); //NOI18N
79
80                 // Return it
81                 return query.getResultList();
82         }
83
84         @Override
85         public Addressbook createAddressbook (final Addressbook addressbook) throws AddressbookNameAlreadyUsedException {
86                 // Is it not null?
87                 if (null == addressbook) {
88                         // Abort here
89                         throw new NullPointerException("addressbook is null"); //NOI18N
90                 } else if (addressbook.getAddressbookUser() == null) {
91                         // User instance is null
92                         throw new NullPointerException("addressbook.user should not be null."); //NOI18N
93                 } else if (addressbook.getAddressbookName() == null) {
94                         // Address book name not set
95                         throw new NullPointerException("addressbook.addressbookName should not be null"); //NOI18N
96                 } else if (addressbook.getAddressbookName().isEmpty()) {
97                         // Address book name not set
98                         throw new IllegalArgumentException("addressbook.addressbookName should not be empty"); //NOI18N
99                 } else if (this.isAddressbookNameUsed(addressbook)) {
100                         // The assigned user already used that name
101                         throw new AddressbookNameAlreadyUsedException(addressbook);
102                 }
103
104                 // Add timestamp of creation
105                 addressbook.setAddressbookCreated(new GregorianCalendar());
106
107                 // Persist it now
108                 this.getEntityManager().persist(addressbook);
109
110                 // Flush it to get all data
111                 this.getEntityManager().flush();
112
113                 // Return it updated
114                 return addressbook;
115         }
116
117         @Override
118         public Addressbook getAddressbookById (final Long addressbookId) throws AddressbookNotFoundException {
119                 // Trace message
120                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("getAddressbookById: addressbookId={0} - CALLED!", addressbookId)); //NOI18N
121
122                 // addressbookId should not be null or below 1
123                 if (null == addressbookId) {
124                         // Throw NPE
125                         throw new NullPointerException("addressbookId is null"); //NOI18N
126                 } else if (addressbookId < 1) {
127                         // Not valid
128                         throw new IllegalArgumentException(MessageFormat.format("addressbookId is not valid: {0}", addressbookId)); //NOI18N
129                 } else if (!this.isAddressbookIdUsed(addressbookId)) {
130                         // No address book found
131                         throw new AddressbookNotFoundException(addressbookId);
132                 }
133
134                 // Get named query instance
135                 Query query = this.getEntityManager().createNamedQuery("SearchAddressbookById", UserAddressbook.class); //NOI18N
136
137                 // Set parameter
138                 query.setParameter("id", addressbookId); //NOI18N
139
140                 // Return it
141                 return (Addressbook) query.getSingleResult();
142         }
143
144         @Override
145         @SuppressWarnings ("unchecked")
146         public List<Addressbook> getUsersAddressbookList (final User loggedInUser) {
147                 // Trace message
148                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("getUsersList: loggedInUser={0} - CALLED!", loggedInUser)); //NOI18N
149
150                 // Is the user instance null?
151                 if (null == loggedInUser) {
152                         // Abort here
153                         throw new NullPointerException("loggedInUser is null"); //NOI18N
154                 }
155
156                 // Get query instance
157                 Query query = this.getEntityManager().createNamedQuery("AllUsersAddressbooks", List.class); //NOI18N
158
159                 // Set parameter
160                 query.setParameter("param", loggedInUser); //NOI18N
161
162                 // Get full list from JPA
163                 List<Addressbook> addressbooks = query.getResultList();
164
165                 // Return it
166                 return addressbooks;
167         }
168
169         @Override
170         public boolean isAddressbookIdUsed (final Long addressbookId) {
171                 // Trace message
172                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("isAddressbookIdUsed: addressbookId={0} - CALLED!", addressbookId)); //NOI18N
173
174                 // Is it null or zero?
175                 if (null == addressbookId) {
176                         // Throw NPE
177                         throw new NullPointerException("addressbookId is null"); //NOI18N
178                 } else if (addressbookId < 1) {
179                         // Not valid id number
180                         throw new IllegalArgumentException(MessageFormat.format("addressbookId is not valid: {0}", addressbookId)); //NOI18N
181                 }
182
183                 // Get query instance
184                 Query query = this.getEntityManager().createNamedQuery("SearchAddressbookById", UserAddressbook.class); //NOI18N
185
186                 // Set parameter
187                 query.setParameter("id", addressbookId); //NOI18N
188
189                 // Default is not valid
190                 boolean isValid = false;
191
192                 // Try it again, yes no other way
193                 try {
194                         // Get single result
195                         Addressbook addressbook = (Addressbook) query.getSingleResult();
196
197                         // Debug message
198                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("isAddressbookIdUsed: addressbook={0} - FOUND!", addressbook)); //NOI18N
199
200                         // Found one!
201                         isValid = true;
202                 } catch (final NoResultException ex) {
203                         // Debug log only, maybe out-dated link followed
204                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("isAddressbookIdUsed: addressbookId={0} is not valid: {1}", addressbookId, ex)); //NOI18N
205                 }
206
207                 // Trace message
208                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("isAddressbookIdUsed: isValid={0} - EXIT!", isValid)); //NOI18N
209
210                 // Return result
211                 return isValid;
212         }
213
214         @Override
215         public boolean isAddressbookNameUsed (final Addressbook addressbook) {
216                 // Is it not null?
217                 if (null == addressbook) {
218                         // Abort here
219                         throw new NullPointerException("addressbook is null"); //NOI18N
220                 } else if (addressbook.getAddressbookUser() == null) {
221                         // User instance is null
222                         throw new NullPointerException("addressbook.addressbookUser is null."); //NOI18N
223                 } else if (addressbook.getAddressbookUser().getUserId() == null) {
224                         // User instance is null
225                         throw new NullPointerException("addressbook.addressbookUser.userId is null."); //NOI18N
226                 } else if (addressbook.getAddressbookUser().getUserId() < 1) {
227                         // User instance is null
228                         throw new NullPointerException(MessageFormat.format("addressbook.addressbookUser.userId={0} is invalid.", addressbook.getAddressbookUser().getUserId())); //NOI18N
229                 } else if (addressbook.getAddressbookName() == null) {
230                         // Address book name not set
231                         throw new NullPointerException("addressbook.addressbookName should not be null"); //NOI18N
232                 } else if (addressbook.getAddressbookName().isEmpty()) {
233                         // Address book name not set
234                         throw new IllegalArgumentException("addressbook.addressbookName should not be empty"); //NOI18N
235                 }
236
237                 // Get query instance
238                 Query query = this.getEntityManager().createNamedQuery("SearchUserAddressbookName", Addressbook.class); //NOI18N
239
240                 // Set parameter
241                 query.setParameter("user", addressbook.getAddressbookUser()); //NOI18N
242                 query.setParameter("name", addressbook.getAddressbookName()); //NOI18N
243
244                 // Default is not found
245                 boolean isUsed = false;
246
247                 // Try it
248                 try {
249                         // Get a single result
250                         Addressbook dummy = (Addressbook) query.getSingleResult();
251
252                         // Log it
253                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("isAddressbookNameUsed: Found an address book: {0}", dummy)); //NOI18N
254
255                         // Found one
256                         isUsed = true;
257                 } catch (final NoResultException ex) {
258                         // No result found, so log it away
259                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("isAddressbookNameUsed: getSingleResult() did not return a result: {0}", ex)); //NOI18N
260                 }
261
262                 // Return result
263                 return isUsed;
264         }
265 }