]> git.mxchange.org Git - pizzaservice-mailer-ejb.git/blob - src/java/org/mxchange/addressbook/model/addressbook/AddressbookSessionBean.java
opps, that was a ClassCastException, that came from confusion ... :-(
[pizzaservice-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.LinkedList;
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.exceptions.AddressbookNameAlreadyUsedException;
26 import org.mxchange.addressbook.exceptions.AddressbookNotFoundException;
27 import org.mxchange.addressbook.model.addressbook.entry.AddressbookEntry;
28 import org.mxchange.addressbook.model.addressbook.shared.ShareableAddressbook;
29 import org.mxchange.jcoreee.database.BaseDatabaseBean;
30 import org.mxchange.jusercore.model.user.User;
31
32 /**
33  * A stateless bean handling addressbooks
34  * <p>
35  * @author Roland Haeder
36  */
37 @Stateless (name = "addressbook", mappedName = "ejb/stateless-addressbook", description = "A stateless bean for handling addressbooks")
38 public class AddressbookSessionBean extends BaseDatabaseBean implements AddressbookSessionBeanRemote {
39
40         /**
41          * Serial number
42          */
43         private static final long serialVersionUID = 129_857_871_287_691L;
44
45         @Override
46         @SuppressWarnings ("unchecked")
47         public List<AddressbookEntry> allEntries (final Addressbook addressbook) {
48                 // Generate query
49                 Query query = this.getEntityManager().createNamedQuery("AllAddressbookEntries", List.class); //NOI18N
50
51                 // Set parameters
52                 query.setParameter("addressbook", addressbook); //NOI18N
53                 query.setParameter("owner", addressbook.getAddressbookUser()); //NOI18N
54                 query.setParameter("sharer", addressbook.getAddressbookUser()); //NOI18N
55
56                 // Return it
57                 return query.getResultList();
58         }
59
60         @Override
61         @SuppressWarnings ("unchecked")
62         public List<User> allUsersNotSharing (final User user, final Addressbook addressbook) {
63                 // Trace message
64                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("allUsersNotSharing: user={0},addressbook={1} - CALLED!", user, addressbook)); //NOI18N
65
66                 // Test parameter
67                 if (null == user) {
68                         // Throw NPE
69                         throw new NullPointerException("user is null"); //NOI18N
70                 } else if (user.getUserId() == null) {
71                         // Throw NPE again
72                         throw new NullPointerException("user.userId is null"); //NOI18N
73                 } else if (user.getUserId() < 1) {
74                         // Invalid id
75                         throw new IllegalArgumentException(MessageFormat.format("user.userId={0} is invalid", user.getUserId())); //NOI18N
76                 } else if (null == addressbook) {
77                         // Again NPE
78                         throw new NullPointerException("addressbook is null"); //NOI18N
79                 } else if (addressbook.getAddressbookId() == null) {
80                         // Again NPE
81                         throw new NullPointerException("addressbook.addressbookId is null"); //NOI18N
82                 } else if (addressbook.getAddressbookId() < 1) {
83                         // Invalid id
84                         throw new IllegalArgumentException(MessageFormat.format("addressbook.getAddressbookId={0} is invalid", addressbook.getAddressbookId())); //NOI18N
85                 }
86
87                 // Get named query for a user list without given user
88                 Query allUsersExceptQuery = this.getEntityManager().createNamedQuery("SearchAllUsersExcept", List.class); //NOI18N
89
90                 // Set parameter
91                 allUsersExceptQuery.setParameter("user", user); //NOI18N
92
93                 // Get full list
94                 List<User> allUsersExcept = allUsersExceptQuery.getResultList();
95
96                 // Debug message
97                 this.getLoggerBeanLocal().logDebug(MessageFormat.format("allUsersNotSharing: allUsersExcept.size()={0}", allUsersExcept.size())); //NOI18N
98
99                 // Now get all shares this user has created
100                 Query allSharesQuery = this.getEntityManager().createNamedQuery("SearchUserSharedAddressbooks", List.class); //NOI18N
101
102                 // Set parameter
103                 allSharesQuery.setParameter("user", user); //NOI18N
104
105                 // Get full list again
106                 List<ShareableAddressbook> allShares = allSharesQuery.getResultList();
107
108                 // Debug message
109                 this.getLoggerBeanLocal().logDebug(MessageFormat.format("allUsersNotSharing: allShares.size()={0}", allShares.size())); //NOI18N
110
111                 // Init final user list
112                 List<User> userList = new LinkedList<>();
113
114                 // Walk through all users
115                 for (final ShareableAddressbook share : allShares) {
116                         // Log message
117                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("allUsersNotSharing: share.shareUserSharee={0}", share.getShareUserSharee())); //NOI18N
118
119                         // Does the list contain it ?
120                         if (!allUsersExcept.contains(share.getShareUserSharee())) {
121                                 // Found one to add
122                                 this.getLoggerBeanLocal().logDebug(MessageFormat.format("allUsersNotSharing: share.shareUserSharee={0} - ADDING!", share.getShareUserSharee())); //NOI18N
123
124                                 // Add it
125                                 userList.add(share.getShareUserSharee());
126                         }
127                 }
128
129                 // Trace message
130                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("allUsersNotSharing: userList.size()={0} - EXIT!", userList.size())); //NOI18N
131
132                 // Return it
133                 return userList;
134         }
135
136         @Override
137         @SuppressWarnings ("unchecked")
138         public Integer countAllUserSharedAddressbooks (final User user) {
139                 // Trace message
140                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("countAllUserSharedAddressbooks: user={0} - CALLED!", user)); //NOI18N
141
142                 // user should be valid
143                 if (null == user) {
144                         // Throw NPE
145                         throw new NullPointerException("user is null"); //NOI18N
146                 } else if (user.getUserId() == null) {
147                         // Throw NPE again
148                         throw new NullPointerException("user.userId is null"); //NOI18N
149                 } else if (user.getUserId() < 1) {
150                         // Invalid id
151                         throw new IllegalArgumentException(MessageFormat.format("user.userId={0} is invalid", user.getUserId())); //NOI18N
152                 }
153
154                 // Get named query
155                 Query query = this.getEntityManager().createNamedQuery("SearchUserSharedAddressbooks", List.class); //NOI18N
156
157                 // Set parameter
158                 query.setParameter("user", user); //NOI18N
159
160                 // Default is zero
161                 Integer count = 0;
162
163                 // Try it
164                 try {
165                         // Get whole list
166                         List<ShareableAddressbook> dummy = query.getResultList();
167
168                         // Set size
169                         count = dummy.size();
170                 } catch (final NoResultException ex) {
171                         // Need to catch this, so log it
172                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("countAllUserSharedAddressbooks: getResultList() failed: {0}", ex)); //NOI18N
173                 }
174
175                 // Trace message
176                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("countAllUserSharedAddressbooks: count={0} - EXIT!", count)); //NOI18N
177
178                 // Return count
179                 return count;
180         }
181
182         @Override
183         public Addressbook createAddressbook (final Addressbook addressbook) throws AddressbookNameAlreadyUsedException {
184                 // Is it not null?
185                 if (null == addressbook) {
186                         // Abort here
187                         throw new NullPointerException("addressbook is null"); //NOI18N
188                 } else if (addressbook.getAddressbookUser() == null) {
189                         // User instance is null
190                         throw new NullPointerException("addressbook.user should not be null."); //NOI18N
191                 } else if (addressbook.getAddressbookName() == null) {
192                         // Address book name not set
193                         throw new NullPointerException("addressbook.addressbookName should not be null"); //NOI18N
194                 } else if (addressbook.getAddressbookName().isEmpty()) {
195                         // Address book name not set
196                         throw new IllegalArgumentException("addressbook.addressbookName should not be empty"); //NOI18N
197                 } else if (this.isAddressbookNameUsed(addressbook)) {
198                         // The assigned user already used that name
199                         throw new AddressbookNameAlreadyUsedException(addressbook);
200                 }
201
202                 // Persist it now
203                 this.getEntityManager().persist(addressbook);
204
205                 // Flush it to get all data
206                 this.getEntityManager().flush();
207
208                 // Return it updated
209                 return addressbook;
210         }
211
212         @Override
213         public Addressbook getAddressbookById (final Long addressbookId) throws AddressbookNotFoundException {
214                 // Trace message
215                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("getAddressbookById: addressbookId={0} - CALLED!", addressbookId)); //NOI18N
216
217                 // addressbookId should not be null or below 1
218                 if (null == addressbookId) {
219                         // Throw NPE
220                         throw new NullPointerException("addressbookId is null"); //NOI18N
221                 } else if (addressbookId < 1) {
222                         // Not valid
223                         throw new IllegalArgumentException(MessageFormat.format("addressbookId is not valid: {0}", addressbookId)); //NOI18N
224                 } else if (!this.isAddressbookIdUsed(addressbookId)) {
225                         // No address book found
226                         throw new AddressbookNotFoundException(addressbookId);
227                 }
228
229                 // Get named query instance
230                 Query query = this.getEntityManager().createNamedQuery("SearchAddressbookById", UserAddressbook.class); //NOI18N
231
232                 // Set parameter
233                 query.setParameter("id", addressbookId); //NOI18N
234
235                 // Return it
236                 return (Addressbook) query.getSingleResult();
237         }
238
239         @Override
240         @SuppressWarnings ("unchecked")
241         public List<Addressbook> getUsersAddressbookList (final User loggedInUser) {
242                 // Trace message
243                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("getUsersList: loggedInUser={0} - CALLED!", loggedInUser)); //NOI18N
244
245                 // Is the user instance null?
246                 if (null == loggedInUser) {
247                         // Abort here
248                         throw new NullPointerException("loggedInUser is null"); //NOI18N
249                 }
250
251                 // Get query instance
252                 Query query = this.getEntityManager().createNamedQuery("AllUsersAddressbooks", List.class); //NOI18N
253
254                 // Set parameter
255                 query.setParameter("param", loggedInUser); //NOI18N
256
257                 // Get full list from JPA
258                 List<Addressbook> addressbooks = query.getResultList();
259
260                 // Return it
261                 return addressbooks;
262         }
263
264         @Override
265         public boolean isAddressbookIdUsed (final Long addressbookId) {
266                 // Trace message
267                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("isAddressbookIdUsed: addressbookId={0} - CALLED!", addressbookId)); //NOI18N
268
269                 // Is it null or zero?
270                 if (null == addressbookId) {
271                         // Throw NPE
272                         throw new NullPointerException("addressbookId is null"); //NOI18N
273                 } else if (addressbookId < 1) {
274                         // Not valid id number
275                         throw new IllegalArgumentException(MessageFormat.format("addressbookId is not valid: {0}", addressbookId)); //NOI18N
276                 }
277
278                 // Get query instance
279                 Query query = this.getEntityManager().createNamedQuery("SearchAddressbookById", UserAddressbook.class); //NOI18N
280
281                 // Set parameter
282                 query.setParameter("id", addressbookId); //NOI18N
283
284                 // Default is not valid
285                 boolean isValid = false;
286
287                 // Try it again, yes no other way
288                 try {
289                         // Get single result
290                         Addressbook addressbook = (Addressbook) query.getSingleResult();
291
292                         // Debug message
293                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("isAddressbookIdUsed: addressbook={0} - FOUND!", addressbook)); //NOI18N
294
295                         // Found one!
296                         isValid = true;
297                 } catch (final NoResultException ex) {
298                         // Debug log only, maybe out-dated link followed
299                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("isAddressbookIdUsed: addressbookId={0} is not valid: {1}", addressbookId, ex)); //NOI18N
300                 }
301
302                 // Trace message
303                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("isAddressbookIdUsed: isValid={0} - EXIT!", isValid)); //NOI18N
304
305                 // Return result
306                 return isValid;
307         }
308
309         @Override
310         public boolean isAddressbookNameUsed (final Addressbook addressbook) {
311                 // Is it not null?
312                 if (null == addressbook) {
313                         // Abort here
314                         throw new NullPointerException("addressbook is null"); //NOI18N
315                 } else if (addressbook.getAddressbookUser() == null) {
316                         // User instance is null
317                         throw new NullPointerException("addressbook.user should not be null."); //NOI18N
318                 } else if (addressbook.getAddressbookName() == null) {
319                         // Address book name not set
320                         throw new NullPointerException("addressbook.addressbookName should not be null"); //NOI18N
321                 } else if (addressbook.getAddressbookName().isEmpty()) {
322                         // Address book name not set
323                         throw new IllegalArgumentException("addressbook.addressbookName should not be empty"); //NOI18N
324                 }
325
326                 // Get query instance
327                 Query query = this.getEntityManager().createNamedQuery("SearchUserAddressbookName", Addressbook.class); //NOI18N
328
329                 // Set parameter
330                 query.setParameter("user", addressbook.getAddressbookUser()); //NOI18N
331                 query.setParameter("name", addressbook.getAddressbookName()); //NOI18N
332
333                 // Default is not found
334                 boolean isUsed = false;
335
336                 // Try it
337                 try {
338                         // Get a single result
339                         Addressbook dummy = (Addressbook) query.getSingleResult();
340
341                         // Log it
342                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("isAddressbookNameUsed: Found an address book: {0}", dummy)); //NOI18N
343
344                         // Found one
345                         isUsed = true;
346                 } catch (final NoResultException ex) {
347                         // No result found, so log it away
348                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("isAddressbookNameUsed: getSingleResult() did not return a result: {0}", ex)); //NOI18N
349                 }
350
351                 // Return result
352                 return isUsed;
353         }
354 }