2 * Copyright (C) 2015 Roland Haeder
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.
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.
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/>.
17 package org.mxchange.addressbook.model.shared;
19 import java.text.MessageFormat;
20 import java.util.List;
21 import java.util.Objects;
22 import javax.ejb.Stateless;
23 import javax.persistence.NoResultException;
24 import javax.persistence.Query;
25 import org.mxchange.addressbook.exceptions.UserAlreadySharingAddressbookException;
26 import org.mxchange.addressbook.model.addressbook.Addressbook;
27 import org.mxchange.addressbook.model.addressbook.shared.AddressbookShare;
28 import org.mxchange.addressbook.model.addressbook.shared.ShareableAddressbook;
29 import org.mxchange.jcoreee.database.BaseDatabaseBean;
30 import org.mxchange.jusercore.model.user.User;
33 * A stateless bean for handling address book sharing
35 * @author Roland Haeder
37 @Stateless (name = "share", mappedName = "ejb/stateless-share", description = "A stateless bean for handling shared addressbooks")
38 public class SharedAddressbooksSessionBean extends BaseDatabaseBean implements SharedAddressbooksSessionBeanRemote {
43 private static final long serialVersionUID = 136_984_697_285_694_710L;
46 @SuppressWarnings ("unchecked")
47 public List<ShareableAddressbook> allSharedAddressbooks (final User user) {
49 this.getLoggerBeanLocal().logTrace(MessageFormat.format("allSharedAddressbooks: user={0} - CALLED!", user)); //NOI18N
54 throw new NullPointerException("user is null"); //NOI18N
55 } else if (user.getUserId() == null) {
56 // Null userId is not allowed
57 throw new NullPointerException("user.userId is null"); //NOI18N
58 } else if (user.getUserId() < 1) {
60 throw new IllegalArgumentException(MessageFormat.format("user.UserId={0} is an invalid value", user.getUserId())); //NOI18N
64 Query query = this.getEntityManager().createNamedQuery("SearchUserSharedAddressbooks"); //NOI18N
67 query.setParameter("user", user); //NOI18N
70 return query.getResultList();
74 public Boolean isUserSharingAddressbooks (final User user) {
76 this.getLoggerBeanLocal().logTrace(MessageFormat.format("isUserSharingAddressbooks: user={0} - CALLED!", user)); //NOI18N
81 throw new NullPointerException("user is null"); //NOI18N
82 } else if (user.getUserId() == null) {
83 // Null userId is not allowed
84 throw new NullPointerException("user.userId is null"); //NOI18N
85 } else if (user.getUserId() < 1) {
87 throw new IllegalArgumentException(MessageFormat.format("user.UserId={0} is an invalid value", user.getUserId())); //NOI18N
91 List<ShareableAddressbook> list = this.allSharedAddressbooks(user);
94 this.getLoggerBeanLocal().logDebug(MessageFormat.format("isUserSharingAddressbooks: list.size()={0}", list.size())); //NOI18N
97 Boolean isSharing = (!list.isEmpty());
100 this.getLoggerBeanLocal().logTrace(MessageFormat.format("isUserSharingAddressbooks: iSharing={0} - EXIT!", isSharing)); //NOI18N
107 public ShareableAddressbook startSharing (final User sharee, final Addressbook addressbook) throws UserAlreadySharingAddressbookException {
109 this.getLoggerBeanLocal().logTrace(MessageFormat.format("startSharing: sharee={0},addressbook={1} - CALLED!", sharee, addressbook)); //NOI18N
111 // Check all conditions
112 if (null == sharee) {
114 throw new NullPointerException("sharee is null"); //NOI18N
115 } else if (sharee.getUserId() == null) {
117 throw new NullPointerException("sharee.userId is null"); //NOI18N
118 } else if (sharee.getUserId() < 1) {
120 throw new IllegalStateException(MessageFormat.format("sharee.userId={0} is invalid", sharee.getUserId())); //NOI18N
121 } else if (null == addressbook) {
123 throw new NullPointerException("addressbook is null"); //NOI18N
124 } else if (addressbook.getAddressbookId() == null) {
126 throw new NullPointerException("addressbook.addressbookId is null"); //NOI18N
127 } else if (addressbook.getAddressbookId() < 1) {
129 throw new IllegalArgumentException(MessageFormat.format("addressbook.addressbookId={0} is invalid.", addressbook.getAddressbookId())); //NOI18N
130 } else if (Objects.equals(addressbook.getAddressbookUser(), sharee)) {
131 // Sharing with yourself!
132 throw new IllegalStateException("User tries to share with himself."); //NOI18N
135 // Is the entry already there?
136 if (this.isUserAlreadySharingAddressbook(addressbook, sharee)) {
138 throw new UserAlreadySharingAddressbookException(addressbook, sharee);
141 // All fine so far, then create the instance
142 ShareableAddressbook share = new AddressbookShare(addressbook, sharee);
145 this.getLoggerBeanLocal().logDebug(MessageFormat.format("startSharing: share={0}", share)); //NOI18N
148 this.getEntityManager().persist(share);
150 // Flush to get id number
151 this.getEntityManager().flush();
153 // Return updated instance
158 * Checks whether the owner of the given address book is already sharing it
161 * @param addressbook Address book to be shared with
162 * @param sharee User sharee instance
163 * @return Wether the address book is already shared with the sharee
165 private boolean isUserAlreadySharingAddressbook (final Addressbook addressbook, final User sharee) {
167 this.getLoggerBeanLocal().logTrace(MessageFormat.format("isUserAlreadySharingAddressbook: addressbook={0},sharee={1} - CALLED!", addressbook, sharee)); //NOI18N
170 Query query = this.getEntityManager().createNamedQuery("SearchShareeAddressbookShare", AddressbookShare.class); //NOI18N
173 query.setParameter("addressbook", addressbook); //NOI18N
174 query.setParameter("sharee", sharee); //NOI18N
176 // Default is not found
177 boolean isFound = false;
181 // Get single instance
182 ShareableAddressbook share = (ShareableAddressbook) query.getSingleResult();
185 this.getLoggerBeanLocal().logDebug(MessageFormat.format("isUserAlreadySharingAddressbook: share={0} - FOUND!", share)); //NOI18N
189 } catch (final NoResultException ex) {
190 // Not found, log exception
191 this.getLoggerBeanLocal().logDebug(MessageFormat.format("isUserAlreadySharingAddressbook: Notfound. Exception: {0}", ex)); //NOI18N
195 this.getLoggerBeanLocal().logTrace(MessageFormat.format("isUserAlreadySharingAddressbook: isFound={0} - EXIT!", isFound)); //NOI18N