2 * Copyright (C) 2016 - 2022 Free Software Foundation
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.
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.
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/>.
17 package org.mxchange.jaddressbookshare.model.addressbook.shared;
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;
34 * A stateless bean for handling address book sharing
36 * @author Roland Häder<roland@mxchange.org>
38 @Stateless (name = "share", description = "A stateless bean for handling shared addressbooks")
39 public class SharedAddressbooksSessionBean extends BaseAddressbookEnterpriseBean implements AddressbookShareSessionBeanRemote {
44 private static final long serialVersionUID = 136_984_697_285_694_710L;
47 @SuppressWarnings ("unchecked")
48 public List<ShareableAddressbook> allSharedAddressbooks (final User user) {
50 this.getLoggerBeanLocal().logTrace(MessageFormat.format("allSharedAddressbooks: user={0} - CALLED!", user)); //NOI18N
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) {
61 throw new IllegalArgumentException(MessageFormat.format("user.UserId={0} is an invalid value", user.getUserId())); //NOI18N
65 Query query = this.getEntityManager().createNamedQuery("SearchUserSharedAddressbooks", List.class); //NOI18N
68 query.setParameter("user", user); //NOI18N
71 List<ShareableAddressbook> list = query.getResultList();
74 this.getLoggerBeanLocal().logTrace(MessageFormat.format("allSharedAddressbooks: list.size()={0} - EXIT!", list.size()));
81 @SuppressWarnings ("unchecked")
82 public List<User> allUsersNotSharing (final User user, final Addressbook addressbook) {
84 this.getLoggerBeanLocal().logTrace(MessageFormat.format("allUsersNotSharing: user={0},addressbook={1} - CALLED!", user, addressbook)); //NOI18N
89 throw new NullPointerException("user is null"); //NOI18N
90 } else if (user.getUserId() == null) {
92 throw new NullPointerException("user.userId is null"); //NOI18N
93 } else if (user.getUserId() < 1) {
95 throw new IllegalArgumentException(MessageFormat.format("user.userId={0} is invalid", user.getUserId())); //NOI18N
96 } else if (null == addressbook) {
98 throw new NullPointerException("addressbook is null"); //NOI18N
99 } else if (addressbook.getAddressbookId() == null) {
101 throw new NullPointerException("addressbook.addressbookId is null"); //NOI18N
102 } else if (addressbook.getAddressbookId() < 1) {
104 throw new IllegalArgumentException(MessageFormat.format("addressbook.getAddressbookId={0} is invalid", addressbook.getAddressbookId())); //NOI18N
107 // Get named query for a user list without given user
108 Query allUsersExceptQuery = this.getEntityManager().createNamedQuery("SearchAllUsersExcept", List.class); //NOI18N
111 allUsersExceptQuery.setParameter("user", user); //NOI18N
114 List<User> allUsersExcept = allUsersExceptQuery.getResultList();
117 this.getLoggerBeanLocal().logDebug(MessageFormat.format("allUsersNotSharing: allUsersExcept.size()={0}", allUsersExcept.size())); //NOI18N
119 // Now get all shares this user has created
120 Query allSharesQuery = this.getEntityManager().createNamedQuery("SearchUserSharedAddressbooks", List.class); //NOI18N
123 allSharesQuery.setParameter("user", user); //NOI18N
125 // Get full list again
126 List<ShareableAddressbook> allShares = allSharesQuery.getResultList();
129 this.getLoggerBeanLocal().logDebug(MessageFormat.format("allUsersNotSharing: allShares.size()={0}", allShares.size())); //NOI18N
131 // List for users aharing with given
132 List<User> sharingUsers = new ArrayList<>(allShares.size());
135 for (final ShareableAddressbook share : allShares) {
137 this.getLoggerBeanLocal().logDebug(MessageFormat.format("allUsersNotSharing: share.shareUserSharee={0}", share.getShareUserSharee())); //NOI18N
140 sharingUsers.add(share.getShareUserSharee());
144 this.getLoggerBeanLocal().logDebug(MessageFormat.format("allUsersNotSharing: sharingUsers.size()={0}", sharingUsers.size())); //NOI18N
146 // Init final user list
147 List<User> userList = new LinkedList<>();
149 // Walk through all users
150 for (final User foundUser : allUsersExcept) {
152 this.getLoggerBeanLocal().logDebug(MessageFormat.format("allUsersNotSharing: foundUser={0}", foundUser)); //NOI18N
154 // Does the list contain it ?
155 if (!sharingUsers.contains(foundUser)) {
157 this.getLoggerBeanLocal().logDebug(MessageFormat.format("allUsersNotSharing: foundUser={0} - ADDING!", foundUser)); //NOI18N
160 userList.add(foundUser);
165 this.getLoggerBeanLocal().logTrace(MessageFormat.format("allUsersNotSharing: userList.size()={0} - EXIT!", userList.size())); //NOI18N
172 @SuppressWarnings ("unchecked")
173 public Integer countAllUserSharedAddressbooks (final User user) {
175 this.getLoggerBeanLocal().logTrace(MessageFormat.format("countAllUserSharedAddressbooks: user={0} - CALLED!", user)); //NOI18N
177 // user should be valid
180 throw new NullPointerException("user is null"); //NOI18N
181 } else if (user.getUserId() == null) {
183 throw new NullPointerException("user.userId is null"); //NOI18N
184 } else if (user.getUserId() < 1) {
186 throw new IllegalArgumentException(MessageFormat.format("user.userId={0} is invalid", user.getUserId())); //NOI18N
190 Query query = this.getEntityManager().createNamedQuery("SearchUserSharedAddressbooks", List.class); //NOI18N
193 query.setParameter("user", user); //NOI18N
201 List<ShareableAddressbook> dummy = query.getResultList();
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
211 this.getLoggerBeanLocal().logTrace(MessageFormat.format("countAllUserSharedAddressbooks: count={0} - EXIT!", count)); //NOI18N
218 public Boolean isUserSharingAddressbooks (final User user) {
220 this.getLoggerBeanLocal().logTrace(MessageFormat.format("isUserSharingAddressbooks: user={0} - CALLED!", user)); //NOI18N
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) {
231 throw new IllegalArgumentException(MessageFormat.format("user.UserId={0} is an invalid value", user.getUserId())); //NOI18N
235 List<ShareableAddressbook> list = this.allSharedAddressbooks(user);
238 this.getLoggerBeanLocal().logDebug(MessageFormat.format("isUserSharingAddressbooks: list.size()={0}", list.size())); //NOI18N
241 Boolean isSharing = (!list.isEmpty());
244 this.getLoggerBeanLocal().logTrace(MessageFormat.format("isUserSharingAddressbooks: iSharing={0} - EXIT!", isSharing)); //NOI18N
251 public ShareableAddressbook startSharing (final User sharee, final Addressbook addressbook) throws UserAlreadySharingAddressbookException {
253 this.getLoggerBeanLocal().logTrace(MessageFormat.format("startSharing: sharee={0},addressbook={1} - CALLED!", sharee, addressbook)); //NOI18N
255 // Check all conditions
256 if (null == sharee) {
258 throw new NullPointerException("sharee is null"); //NOI18N
259 } else if (sharee.getUserId() == null) {
261 throw new NullPointerException("sharee.userId is null"); //NOI18N
262 } else if (sharee.getUserId() < 1) {
264 throw new IllegalStateException(MessageFormat.format("sharee.userId={0} is invalid", sharee.getUserId())); //NOI18N
265 } else if (null == addressbook) {
267 throw new NullPointerException("addressbook is null"); //NOI18N
268 } else if (addressbook.getAddressbookId() == null) {
270 throw new NullPointerException("addressbook.addressbookId is null"); //NOI18N
271 } else if (addressbook.getAddressbookId() < 1) {
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
279 // Is the entry already there?
280 if (this.isUserAlreadySharingAddressbook(addressbook, sharee)) {
282 throw new UserAlreadySharingAddressbookException(addressbook, sharee);
285 // All fine so far, then create the instance
286 ShareableAddressbook share = new AddressbookShare(addressbook, sharee);
289 this.getLoggerBeanLocal().logDebug(MessageFormat.format("startSharing: share={0}", share)); //NOI18N
292 this.getEntityManager().persist(share);
294 // Flush to get id number
295 this.getEntityManager().flush();
297 // Return updated instance
302 * Checks whether the owner of the given address book is already sharing it
305 * @param addressbook Address book to be shared with
306 * @param sharee User sharee instance
308 * @return Whether the address book is already shared with the sharee
310 private boolean isUserAlreadySharingAddressbook (final Addressbook addressbook, final User sharee) {
312 this.getLoggerBeanLocal().logTrace(MessageFormat.format("isUserAlreadySharingAddressbook: addressbook={0},sharee={1} - CALLED!", addressbook, sharee)); //NOI18N
315 Query query = this.getEntityManager().createNamedQuery("SearchShareeAddressbookShare", AddressbookShare.class); //NOI18N
318 query.setParameter("addressbook", addressbook); //NOI18N
319 query.setParameter("sharee", sharee); //NOI18N
321 // Default is not found
322 boolean isFound = false;
326 // Get single instance
327 ShareableAddressbook share = (ShareableAddressbook) query.getSingleResult();
330 this.getLoggerBeanLocal().logDebug(MessageFormat.format("isUserAlreadySharingAddressbook: share={0} - FOUND!", share)); //NOI18N
334 } catch (final NoResultException ex) {
335 // Not found, log exception
336 this.getLoggerBeanLocal().logDebug(MessageFormat.format("isUserAlreadySharingAddressbook: Notfound. Exception: {0}", ex)); //NOI18N
340 this.getLoggerBeanLocal().logTrace(MessageFormat.format("isUserAlreadySharingAddressbook: isFound={0} - EXIT!", isFound)); //NOI18N