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