]> git.mxchange.org Git - jfinancials-mailer-ejb.git/blob - src/java/org/mxchange/addressbook/model/addressbook/share/SharedAddressbooksSessionBean.java
901ca67b2e7c85cded1df4d1cd93dfcb17e74314
[jfinancials-mailer-ejb.git] / src / java / org / mxchange / addressbook / model / addressbook / share / SharedAddressbooksSessionBean.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.share;
18
19 import java.text.MessageFormat;
20 import java.util.ArrayList;
21 import java.util.LinkedList;
22 import java.util.List;
23 import java.util.Objects;
24 import javax.ejb.Stateless;
25 import javax.persistence.NoResultException;
26 import javax.persistence.Query;
27 import org.mxchange.addressbook.model.shared.AddressbookShareSessionBeanRemote;
28 import org.mxchange.jaddressbook.model.addressbook.Addressbook;
29 import org.mxchange.jaddressbookshare.exceptions.UserAlreadySharingAddressbookException;
30 import org.mxchange.jaddressbookshare.model.addressbook.shared.AddressbookShare;
31 import org.mxchange.jaddressbookshare.model.addressbook.shared.ShareableAddressbook;
32 import org.mxchange.jcoreee.database.BaseDatabaseBean;
33 import org.mxchange.jusercore.model.user.User;
34
35 /**
36  * A stateless bean for handling address book sharing
37  * <p>
38  * @author Roland Häder<roland@mxchange.org>
39  */
40 @Stateless (name = "share", description = "A stateless bean for handling shared addressbooks")
41 public class SharedAddressbooksSessionBean extends BaseDatabaseBean implements AddressbookShareSessionBeanRemote {
42
43         /**
44          * Serial number
45          */
46         private static final long serialVersionUID = 136_984_697_285_694_710L;
47
48         @Override
49         @SuppressWarnings ("unchecked")
50         public List<ShareableAddressbook> allSharedAddressbooks (final User user) {
51                 // Trace message
52                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("allSharedAddressbooks: user={0} - CALLED!", user)); //NOI18N
53
54                 // Is user null?
55                 if (null == user) {
56                         // Throw NPE
57                         throw new NullPointerException("user is null"); //NOI18N
58                 } else if (user.getUserId() == null) {
59                         // Null userId is not allowed
60                         throw new NullPointerException("user.userId is null"); //NOI18N
61                 } else if (user.getUserId() < 1) {
62                         // Not allowed value
63                         throw new IllegalArgumentException(MessageFormat.format("user.UserId={0} is an invalid value", user.getUserId())); //NOI18N
64                 }
65
66                 // Get named query
67                 Query query = this.getEntityManager().createNamedQuery("SearchUserSharedAddressbooks", List.class); //NOI18N
68
69                 // Set parameter
70                 query.setParameter("user", user); //NOI18N
71
72                 // Return full list
73                 List<ShareableAddressbook> list = query.getResultList();
74
75                 // Trace message
76                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("allSharedAddressbooks: list.size()={0} - EXIT!", list.size()));
77
78                 // Return list
79                 return list;
80         }
81
82         @Override
83         @SuppressWarnings ("unchecked")
84         public List<User> allUsersNotSharing (final User user, final Addressbook addressbook) {
85                 // Trace message
86                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("allUsersNotSharing: user={0},addressbook={1} - CALLED!", user, addressbook)); //NOI18N
87
88                 // Test parameter
89                 if (null == user) {
90                         // Throw NPE
91                         throw new NullPointerException("user is null"); //NOI18N
92                 } else if (user.getUserId() == null) {
93                         // Throw NPE again
94                         throw new NullPointerException("user.userId is null"); //NOI18N
95                 } else if (user.getUserId() < 1) {
96                         // Invalid id
97                         throw new IllegalArgumentException(MessageFormat.format("user.userId={0} is invalid", user.getUserId())); //NOI18N
98                 } else if (null == addressbook) {
99                         // Again NPE
100                         throw new NullPointerException("addressbook is null"); //NOI18N
101                 } else if (addressbook.getAddressbookId() == null) {
102                         // Again NPE
103                         throw new NullPointerException("addressbook.addressbookId is null"); //NOI18N
104                 } else if (addressbook.getAddressbookId() < 1) {
105                         // Invalid id
106                         throw new IllegalArgumentException(MessageFormat.format("addressbook.getAddressbookId={0} is invalid", addressbook.getAddressbookId())); //NOI18N
107                 }
108
109                 // Get named query for a user list without given user
110                 Query allUsersExceptQuery = this.getEntityManager().createNamedQuery("SearchAllUsersExcept", List.class); //NOI18N
111
112                 // Set parameter
113                 allUsersExceptQuery.setParameter("user", user); //NOI18N
114
115                 // Get full list
116                 List<User> allUsersExcept = allUsersExceptQuery.getResultList();
117
118                 // Debug message
119                 this.getLoggerBeanLocal().logDebug(MessageFormat.format("allUsersNotSharing: allUsersExcept.size()={0}", allUsersExcept.size())); //NOI18N
120
121                 // Now get all shares this user has created
122                 Query allSharesQuery = this.getEntityManager().createNamedQuery("SearchUserSharedAddressbooks", List.class); //NOI18N
123
124                 // Set parameter
125                 allSharesQuery.setParameter("user", user); //NOI18N
126
127                 // Get full list again
128                 List<ShareableAddressbook> allShares = allSharesQuery.getResultList();
129
130                 // Debug message
131                 this.getLoggerBeanLocal().logDebug(MessageFormat.format("allUsersNotSharing: allShares.size()={0}", allShares.size())); //NOI18N
132
133                 // List for users aharing with given
134                 List<User> sharingUsers = new ArrayList<>(allShares.size());
135
136                 // Check all entries
137                 for (final ShareableAddressbook share : allShares) {
138                         // Debug message
139                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("allUsersNotSharing: share.shareUserSharee={0}", share.getShareUserSharee())); //NOI18N
140
141                         // Add it
142                         sharingUsers.add(share.getShareUserSharee());
143                 }
144
145                 // Debug message
146                 this.getLoggerBeanLocal().logDebug(MessageFormat.format("allUsersNotSharing: sharingUsers.size()={0}", sharingUsers.size())); //NOI18N
147
148                 // Init final user list
149                 List<User> userList = new LinkedList<>();
150
151                 // Walk through all users
152                 for (final User foundUser : allUsersExcept) {
153                         // Debug message
154                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("allUsersNotSharing: foundUser={0}", foundUser)); //NOI18N
155
156                         // Does the list contain it ?
157                         if (!sharingUsers.contains(foundUser)) {
158                                 // Found one to add
159                                 this.getLoggerBeanLocal().logDebug(MessageFormat.format("allUsersNotSharing: foundUser={0} - ADDING!", foundUser)); //NOI18N
160
161                                 // Add it
162                                 userList.add(foundUser);
163                         }
164                 }
165
166                 // Trace message
167                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("allUsersNotSharing: userList.size()={0} - EXIT!", userList.size())); //NOI18N
168
169                 // Return it
170                 return userList;
171         }
172
173         @Override
174         @SuppressWarnings ("unchecked")
175         public Integer countAllUserSharedAddressbooks (final User user) {
176                 // Trace message
177                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("countAllUserSharedAddressbooks: user={0} - CALLED!", user)); //NOI18N
178
179                 // user should be valid
180                 if (null == user) {
181                         // Throw NPE
182                         throw new NullPointerException("user is null"); //NOI18N
183                 } else if (user.getUserId() == null) {
184                         // Throw NPE again
185                         throw new NullPointerException("user.userId is null"); //NOI18N
186                 } else if (user.getUserId() < 1) {
187                         // Invalid id
188                         throw new IllegalArgumentException(MessageFormat.format("user.userId={0} is invalid", user.getUserId())); //NOI18N
189                 }
190
191                 // Get named query
192                 Query query = this.getEntityManager().createNamedQuery("SearchUserSharedAddressbooks", List.class); //NOI18N
193
194                 // Set parameter
195                 query.setParameter("user", user); //NOI18N
196
197                 // Default is zero
198                 Integer count = 0;
199
200                 // Try it
201                 try {
202                         // Get whole list
203                         List<ShareableAddressbook> dummy = query.getResultList();
204
205                         // Set size
206                         count = dummy.size();
207                 } catch (final NoResultException ex) {
208                         // Need to catch this, so log it
209                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("countAllUserSharedAddressbooks: getResultList() failed: {0}", ex)); //NOI18N
210                 }
211
212                 // Trace message
213                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("countAllUserSharedAddressbooks: count={0} - EXIT!", count)); //NOI18N
214
215                 // Return count
216                 return count;
217         }
218
219         @Override
220         public Boolean isUserSharingAddressbooks (final User user) {
221                 // Trace message
222                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("isUserSharingAddressbooks: user={0} - CALLED!", user)); //NOI18N
223
224                 // Is user null?
225                 if (null == user) {
226                         // Throw NPE
227                         throw new NullPointerException("user is null"); //NOI18N
228                 } else if (user.getUserId() == null) {
229                         // Null userId is not allowed
230                         throw new NullPointerException("user.userId is null"); //NOI18N
231                 } else if (user.getUserId() < 1) {
232                         // Not allowed value
233                         throw new IllegalArgumentException(MessageFormat.format("user.UserId={0} is an invalid value", user.getUserId())); //NOI18N
234                 }
235
236                 // Get results
237                 List<ShareableAddressbook> list = this.allSharedAddressbooks(user);
238
239                 // Debug message
240                 this.getLoggerBeanLocal().logDebug(MessageFormat.format("isUserSharingAddressbooks: list.size()={0}", list.size())); //NOI18N
241
242                 // Is it not empty?
243                 Boolean isSharing = (!list.isEmpty());
244
245                 // Trace message
246                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("isUserSharingAddressbooks: iSharing={0} - EXIT!", isSharing)); //NOI18N
247
248                 // Return it
249                 return isSharing;
250         }
251
252         @Override
253         public ShareableAddressbook startSharing (final User sharee, final Addressbook addressbook) throws UserAlreadySharingAddressbookException {
254                 // Trace message
255                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("startSharing: sharee={0},addressbook={1} - CALLED!", sharee, addressbook)); //NOI18N
256
257                 // Check all conditions
258                 if (null == sharee) {
259                         // Throw NPE
260                         throw new NullPointerException("sharee is null"); //NOI18N
261                 } else if (sharee.getUserId() == null) {
262                         // Throw NPE again
263                         throw new NullPointerException("sharee.userId is null"); //NOI18N
264                 } else if (sharee.getUserId() < 1) {
265                         // Invalid id number
266                         throw new IllegalStateException(MessageFormat.format("sharee.userId={0} is invalid", sharee.getUserId())); //NOI18N
267                 } else if (null == addressbook) {
268                         // Throw NPE again
269                         throw new NullPointerException("addressbook is null"); //NOI18N
270                 } else if (addressbook.getAddressbookId() == null) {
271                         // Throw NPE again
272                         throw new NullPointerException("addressbook.addressbookId is null"); //NOI18N
273                 } else if (addressbook.getAddressbookId() < 1) {
274                         // Invalid id number
275                         throw new IllegalArgumentException(MessageFormat.format("addressbook.addressbookId={0} is invalid.", addressbook.getAddressbookId())); //NOI18N
276                 } else if (Objects.equals(addressbook.getAddressbookUser(), sharee)) {
277                         // Sharing with yourself!
278                         throw new IllegalStateException("User tries to share with himself."); //NOI18N
279                 }
280
281                 // Is the entry already there?
282                 if (this.isUserAlreadySharingAddressbook(addressbook, sharee)) {
283                         // Abort here
284                         throw new UserAlreadySharingAddressbookException(addressbook, sharee);
285                 }
286
287                 // All fine so far, then create the instance
288                 ShareableAddressbook share = new AddressbookShare(addressbook, sharee);
289
290                 // Debug message
291                 this.getLoggerBeanLocal().logDebug(MessageFormat.format("startSharing: share={0}", share)); //NOI18N
292
293                 // Persist it
294                 this.getEntityManager().persist(share);
295
296                 // Flush to get id number
297                 this.getEntityManager().flush();
298
299                 // Return updated instance
300                 return share;
301         }
302
303         /**
304          * Checks whether the owner of the given address book is already sharing it
305          * with the sharee.
306          * <p>
307          * @param addressbook Address book to be shared with
308          * @param sharee      User sharee instance
309          * <p>
310          * @return Wether the address book is already shared with the sharee
311          */
312         private boolean isUserAlreadySharingAddressbook (final Addressbook addressbook, final User sharee) {
313                 // Trace message
314                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("isUserAlreadySharingAddressbook: addressbook={0},sharee={1} - CALLED!", addressbook, sharee)); //NOI18N
315
316                 // Get named query
317                 Query query = this.getEntityManager().createNamedQuery("SearchShareeAddressbookShare", AddressbookShare.class); //NOI18N
318
319                 // Set parameter
320                 query.setParameter("addressbook", addressbook); //NOI18N
321                 query.setParameter("sharee", sharee); //NOI18N
322
323                 // Default is not found
324                 boolean isFound = false;
325
326                 // Try it
327                 try {
328                         // Get single instance
329                         ShareableAddressbook share = (ShareableAddressbook) query.getSingleResult();
330
331                         // Debug message
332                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("isUserAlreadySharingAddressbook: share={0} - FOUND!", share)); //NOI18N
333
334                         // Set found
335                         isFound = true;
336                 } catch (final NoResultException ex) {
337                         // Not found, log exception
338                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("isUserAlreadySharingAddressbook: Notfound. Exception: {0}", ex)); //NOI18N
339                 }
340
341                 // Trace message
342                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("isUserAlreadySharingAddressbook: isFound={0} - EXIT!", isFound)); //NOI18N
343
344                 // Return it
345                 return isFound;
346         }
347 }