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