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.beans.shares;
19 import java.text.MessageFormat;
20 import java.util.Collections;
21 import java.util.List;
22 import java.util.Objects;
23 import javax.enterprise.context.SessionScoped;
24 import javax.faces.view.facelets.FaceletException;
25 import javax.inject.Inject;
26 import javax.inject.Named;
27 import javax.naming.Context;
28 import javax.naming.InitialContext;
29 import javax.naming.NamingException;
30 import org.mxchange.addressbook.beans.login.UserLoginWebController;
31 import org.mxchange.addressbook.exceptions.UserAlreadySharingAddressbookException;
32 import org.mxchange.addressbook.model.addressbook.Addressbook;
33 import org.mxchange.addressbook.model.addressbook.shared.ShareableAddressbook;
34 import org.mxchange.addressbook.model.shared.SharedAddressbooksSessionBeanRemote;
35 import org.mxchange.jusercore.model.user.User;
38 * A bean for sharing address books with other users
40 * @author Roland Haeder
42 @Named (value = "shareController")
44 public class SharesWebBean implements SharesWebController {
49 private static final long serialVersionUID = 19_868_976_871_976_780L;
52 * Cached flag whether the user is sharing address books
54 private Boolean isUserSharing = null;
57 * Login controller injection
60 private UserLoginWebController loginController;
65 private ShareableAddressbook share;
68 * A list of all user's shared (with others) address books
70 private List<ShareableAddressbook> sharedAddressbooks;
73 * Remote bean for sharing address books
75 private SharedAddressbooksSessionBeanRemote shareBean;
80 private Long shareeUserId;
85 public SharesWebBean () {
88 // Get initial context
89 Context context = new InitialContext();
92 this.shareBean = (SharedAddressbooksSessionBeanRemote) context.lookup("ejb/stateless-share"); //NOI18N
93 } catch (final NamingException ex) {
95 throw new FaceletException(ex);
100 public Long getShareeUserId () {
101 return this.shareeUserId;
105 public void setShareeUserId (final Long shareeUserId) {
106 this.shareeUserId = shareeUserId;
110 public boolean isShareeUserIdSet () {
111 return ((this.getShareeUserId() instanceof Long) && (this.getShareeUserId() > 0));
115 public boolean isShareeUserIdEmpty () {
116 return (!this.isShareeUserIdSet());
120 public boolean isSharingAddressbooks () {
121 // Only to be called for logged-in users
122 if (!this.loginController.isUserLoggedIn()) {
124 throw new FaceletException("This method can only be called as logged-in user."); //NOI18N
125 } else if (this.isUserSharing instanceof Boolean) {
126 // Return cached value
127 return this.isUserSharing;
130 // Call the proper bean
131 this.isUserSharing = this.shareBean.isUserSharingAddressbooks(this.loginController.getLoggedInUser());
134 return this.isUserSharing;
138 public String startSharing (final User user, final Addressbook addressbook) {
140 if (!this.loginController.isUserLoggedIn()) {
141 // No, then throw exception
142 throw new FaceletException("This method can only be called as logged-in user."); //NOI18N
143 } else if (null == user) {
145 throw new NullPointerException("user is null"); //NOI18N
146 } else if (user.getUserId() == null) {
148 throw new NullPointerException("user.userId is null"); //NOI18N
149 } else if (user.getUserId() < 1) {
151 throw new IllegalStateException(MessageFormat.format("user.userId={0} is invalid", user.getUserId())); //NOI18N
152 } else if (Objects.equals(user, this.loginController.getLoggedInUser())) {
153 // Sharing with yourself!
154 throw new IllegalStateException("User tries to share with himself."); //NOI18N
155 } else if (null == addressbook) {
157 throw new NullPointerException("addressbook is null"); //NOI18N
158 } else if (addressbook.getAddressbookId() == null) {
160 throw new NullPointerException("addressbook.addressbookId is null"); //NOI18N
161 } else if (addressbook.getAddressbookId() < 1) {
163 throw new IllegalArgumentException(MessageFormat.format("addressbook.addressbookId={0} is invalid.", addressbook.getAddressbookId())); //NOI18N
164 } else if (!Objects.equals(addressbook.getAddressbookUser(), this.loginController.getLoggedInUser())) {
165 // Not the same user!
166 throw new IllegalStateException(MessageFormat.format("Address book id {0} owner id {1} mismatching logged-in user id {2}", addressbook.getAddressbookId(), addressbook.getAddressbookUser().getUserId(), this.loginController.getLoggedInUser().getUserId()));
171 ShareableAddressbook shared = this.shareBean.startSharing(user, addressbook);
174 this.setShare(shared);
175 } catch (final UserAlreadySharingAddressbookException ex) {
177 throw new FaceletException(ex);
185 public List<ShareableAddressbook> allShares () {
186 // Is the user logged in?
187 if (!this.loginController.isUserLoggedIn()) {
189 throw new FaceletException("This method can only be called as logged-in user."); //NOI18N
192 return Collections.unmodifiableList(this.sharedAddressbooks);
196 public ShareableAddressbook getShare () {
201 public void setShare (final ShareableAddressbook share) {