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