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