2 * Copyright (C) 2016, 2017 Roland Häder
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.addressbook.model.addressbook.share;
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.database.BaseAddressbookDatabaseBean;
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.jaddressbookshare.model.addressbook.shared.AddressbookShare;
32 import org.mxchange.jaddressbookshare.model.addressbook.shared.ShareableAddressbook;
33 import org.mxchange.jusercore.model.user.User;
36 * A stateless bean for handling address book sharing
38 * @author Roland Häder<roland@mxchange.org>
40 @Stateless (name = "share", description = "A stateless bean for handling shared addressbooks")
41 public class SharedAddressbooksSessionBean extends BaseAddressbookDatabaseBean implements AddressbookShareSessionBeanRemote {
46 private static final long serialVersionUID = 136_984_697_285_694_710L;
49 @SuppressWarnings ("unchecked")
50 public List<ShareableAddressbook> allSharedAddressbooks (final User user) {
52 this.getLoggerBeanLocal().logTrace(MessageFormat.format("allSharedAddressbooks: user={0} - CALLED!", user)); //NOI18N
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) {
63 throw new IllegalArgumentException(MessageFormat.format("user.UserId={0} is an invalid value", user.getUserId())); //NOI18N
67 Query query = this.getEntityManager().createNamedQuery("SearchUserSharedAddressbooks", List.class); //NOI18N
70 query.setParameter("user", user); //NOI18N
73 List<ShareableAddressbook> list = query.getResultList();
76 this.getLoggerBeanLocal().logTrace(MessageFormat.format("allSharedAddressbooks: list.size()={0} - EXIT!", list.size()));
83 @SuppressWarnings ("unchecked")
84 public List<User> allUsersNotSharing (final User user, final Addressbook addressbook) {
86 this.getLoggerBeanLocal().logTrace(MessageFormat.format("allUsersNotSharing: user={0},addressbook={1} - CALLED!", user, addressbook)); //NOI18N
91 throw new NullPointerException("user is null"); //NOI18N
92 } else if (user.getUserId() == null) {
94 throw new NullPointerException("user.userId is null"); //NOI18N
95 } else if (user.getUserId() < 1) {
97 throw new IllegalArgumentException(MessageFormat.format("user.userId={0} is invalid", user.getUserId())); //NOI18N
98 } else if (null == addressbook) {
100 throw new NullPointerException("addressbook is null"); //NOI18N
101 } else if (addressbook.getAddressbookId() == null) {
103 throw new NullPointerException("addressbook.addressbookId is null"); //NOI18N
104 } else if (addressbook.getAddressbookId() < 1) {
106 throw new IllegalArgumentException(MessageFormat.format("addressbook.getAddressbookId={0} is invalid", addressbook.getAddressbookId())); //NOI18N
109 // Get named query for a user list without given user
110 Query allUsersExceptQuery = this.getEntityManager().createNamedQuery("SearchAllUsersExcept", List.class); //NOI18N
113 allUsersExceptQuery.setParameter("user", user); //NOI18N
116 List<User> allUsersExcept = allUsersExceptQuery.getResultList();
119 this.getLoggerBeanLocal().logDebug(MessageFormat.format("allUsersNotSharing: allUsersExcept.size()={0}", allUsersExcept.size())); //NOI18N
121 // Now get all shares this user has created
122 Query allSharesQuery = this.getEntityManager().createNamedQuery("SearchUserSharedAddressbooks", List.class); //NOI18N
125 allSharesQuery.setParameter("user", user); //NOI18N
127 // Get full list again
128 List<ShareableAddressbook> allShares = allSharesQuery.getResultList();
131 this.getLoggerBeanLocal().logDebug(MessageFormat.format("allUsersNotSharing: allShares.size()={0}", allShares.size())); //NOI18N
133 // List for users aharing with given
134 List<User> sharingUsers = new ArrayList<>(allShares.size());
137 for (final ShareableAddressbook share : allShares) {
139 this.getLoggerBeanLocal().logDebug(MessageFormat.format("allUsersNotSharing: share.shareUserSharee={0}", share.getShareUserSharee())); //NOI18N
142 sharingUsers.add(share.getShareUserSharee());
146 this.getLoggerBeanLocal().logDebug(MessageFormat.format("allUsersNotSharing: sharingUsers.size()={0}", sharingUsers.size())); //NOI18N
148 // Init final user list
149 List<User> userList = new LinkedList<>();
151 // Walk through all users
152 for (final User foundUser : allUsersExcept) {
154 this.getLoggerBeanLocal().logDebug(MessageFormat.format("allUsersNotSharing: foundUser={0}", foundUser)); //NOI18N
156 // Does the list contain it ?
157 if (!sharingUsers.contains(foundUser)) {
159 this.getLoggerBeanLocal().logDebug(MessageFormat.format("allUsersNotSharing: foundUser={0} - ADDING!", foundUser)); //NOI18N
162 userList.add(foundUser);
167 this.getLoggerBeanLocal().logTrace(MessageFormat.format("allUsersNotSharing: userList.size()={0} - EXIT!", userList.size())); //NOI18N
174 @SuppressWarnings ("unchecked")
175 public Integer countAllUserSharedAddressbooks (final User user) {
177 this.getLoggerBeanLocal().logTrace(MessageFormat.format("countAllUserSharedAddressbooks: user={0} - CALLED!", user)); //NOI18N
179 // user should be valid
182 throw new NullPointerException("user is null"); //NOI18N
183 } else if (user.getUserId() == null) {
185 throw new NullPointerException("user.userId is null"); //NOI18N
186 } else if (user.getUserId() < 1) {
188 throw new IllegalArgumentException(MessageFormat.format("user.userId={0} is invalid", user.getUserId())); //NOI18N
192 Query query = this.getEntityManager().createNamedQuery("SearchUserSharedAddressbooks", List.class); //NOI18N
195 query.setParameter("user", user); //NOI18N
203 List<ShareableAddressbook> dummy = query.getResultList();
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
213 this.getLoggerBeanLocal().logTrace(MessageFormat.format("countAllUserSharedAddressbooks: count={0} - EXIT!", count)); //NOI18N
220 public Boolean isUserSharingAddressbooks (final User user) {
222 this.getLoggerBeanLocal().logTrace(MessageFormat.format("isUserSharingAddressbooks: user={0} - CALLED!", user)); //NOI18N
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) {
233 throw new IllegalArgumentException(MessageFormat.format("user.UserId={0} is an invalid value", user.getUserId())); //NOI18N
237 List<ShareableAddressbook> list = this.allSharedAddressbooks(user);
240 this.getLoggerBeanLocal().logDebug(MessageFormat.format("isUserSharingAddressbooks: list.size()={0}", list.size())); //NOI18N
243 Boolean isSharing = (!list.isEmpty());
246 this.getLoggerBeanLocal().logTrace(MessageFormat.format("isUserSharingAddressbooks: iSharing={0} - EXIT!", isSharing)); //NOI18N
253 public ShareableAddressbook startSharing (final User sharee, final Addressbook addressbook) throws UserAlreadySharingAddressbookException {
255 this.getLoggerBeanLocal().logTrace(MessageFormat.format("startSharing: sharee={0},addressbook={1} - CALLED!", sharee, addressbook)); //NOI18N
257 // Check all conditions
258 if (null == sharee) {
260 throw new NullPointerException("sharee is null"); //NOI18N
261 } else if (sharee.getUserId() == null) {
263 throw new NullPointerException("sharee.userId is null"); //NOI18N
264 } else if (sharee.getUserId() < 1) {
266 throw new IllegalStateException(MessageFormat.format("sharee.userId={0} is invalid", sharee.getUserId())); //NOI18N
267 } else if (null == addressbook) {
269 throw new NullPointerException("addressbook is null"); //NOI18N
270 } else if (addressbook.getAddressbookId() == null) {
272 throw new NullPointerException("addressbook.addressbookId is null"); //NOI18N
273 } else if (addressbook.getAddressbookId() < 1) {
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
281 // Is the entry already there?
282 if (this.isUserAlreadySharingAddressbook(addressbook, sharee)) {
284 throw new UserAlreadySharingAddressbookException(addressbook, sharee);
287 // All fine so far, then create the instance
288 ShareableAddressbook share = new AddressbookShare(addressbook, sharee);
291 this.getLoggerBeanLocal().logDebug(MessageFormat.format("startSharing: share={0}", share)); //NOI18N
294 this.getEntityManager().persist(share);
296 // Flush to get id number
297 this.getEntityManager().flush();
299 // Return updated instance
304 * Checks whether the owner of the given address book is already sharing it
307 * @param addressbook Address book to be shared with
308 * @param sharee User sharee instance
310 * @return Wether the address book is already shared with the sharee
312 private boolean isUserAlreadySharingAddressbook (final Addressbook addressbook, final User sharee) {
314 this.getLoggerBeanLocal().logTrace(MessageFormat.format("isUserAlreadySharingAddressbook: addressbook={0},sharee={1} - CALLED!", addressbook, sharee)); //NOI18N
317 Query query = this.getEntityManager().createNamedQuery("SearchShareeAddressbookShare", AddressbookShare.class); //NOI18N
320 query.setParameter("addressbook", addressbook); //NOI18N
321 query.setParameter("sharee", sharee); //NOI18N
323 // Default is not found
324 boolean isFound = false;
328 // Get single instance
329 ShareableAddressbook share = (ShareableAddressbook) query.getSingleResult();
332 this.getLoggerBeanLocal().logDebug(MessageFormat.format("isUserAlreadySharingAddressbook: share={0} - FOUND!", share)); //NOI18N
336 } catch (final NoResultException ex) {
337 // Not found, log exception
338 this.getLoggerBeanLocal().logDebug(MessageFormat.format("isUserAlreadySharingAddressbook: Notfound. Exception: {0}", ex)); //NOI18N
342 this.getLoggerBeanLocal().logTrace(MessageFormat.format("isUserAlreadySharingAddressbook: isFound={0} - EXIT!", isFound)); //NOI18N