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