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