2 * Copyright (C) 2016 Roland Haeder
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.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.annotation.PostConstruct;
24 import javax.enterprise.context.SessionScoped;
25 import javax.enterprise.event.Event;
26 import javax.enterprise.event.Observes;
27 import javax.enterprise.inject.Any;
28 import javax.faces.view.facelets.FaceletException;
29 import javax.inject.Inject;
30 import javax.inject.Named;
31 import javax.naming.Context;
32 import javax.naming.InitialContext;
33 import javax.naming.NamingException;
34 import org.mxchange.addressbook.beans.BaseAddressbookController;
35 import org.mxchange.addressbook.beans.login.AddressbookUserLoginWebSessionController;
36 import org.mxchange.addressbook.model.shared.SharedAddressbooksSessionBeanRemote;
37 import org.mxchange.jaddressbookcore.events.sharing.AddressbookSharingEvent;
38 import org.mxchange.jaddressbookcore.events.sharing.StartedAddressbookSharingEvent;
39 import org.mxchange.jaddressbookcore.events.sharing.type.SharingType;
40 import org.mxchange.jaddressbookcore.exceptions.UserAlreadySharingAddressbookException;
41 import org.mxchange.jaddressbookcore.model.addressbook.Addressbook;
42 import org.mxchange.jaddressbookcore.model.addressbook.shared.ShareableAddressbook;
43 import org.mxchange.jusercore.events.login.UserLoggedInEvent;
44 import org.mxchange.jusercore.model.user.User;
45 import org.mxchange.jusercore.model.user.profilemodes.ProfileMode;
48 * A bean for sharing address books with other users
50 * @author Roland Haeder<roland@mxchange.org>
52 @Named (value = "shareController")
54 public class AddressbookSharesWebSessionBean extends BaseAddressbookController implements AddressbookSharesWebSessionController {
59 private static final long serialVersionUID = 19_868_976_871_976_780L;
62 * Cached flag whether the user is sharing address books
64 private Boolean isUserSharing = null;
67 * Login controller injection
70 private AddressbookUserLoginWebSessionController loginController;
75 private ShareableAddressbook share;
78 * Remote bean for sharing address books
80 private SharedAddressbooksSessionBeanRemote shareBean;
83 * A list of all user's shared (with others) address books
85 private List<ShareableAddressbook> sharedAddressbooks;
90 private Long shareeUserId;
93 * An event triggered when address book sharing starts or ends
97 private Event<AddressbookSharingEvent> sharingEvent;
100 * Default constructor
102 public AddressbookSharesWebSessionBean () {
105 // Get initial context
106 Context context = new InitialContext();
109 this.shareBean = (SharedAddressbooksSessionBeanRemote) context.lookup("ejb/stateless-addressbook-share"); //NOI18N
110 } catch (final NamingException ex) {
112 throw new FaceletException(ex);
117 public void afterAdressbookShareEnded (final @Observes AddressbookSharingEvent event) {
118 // Validate parameter
121 throw new NullPointerException("event is null"); //NOI18N
122 } else if (event.getSharingType() == null) {
124 throw new NullPointerException("event.sharingType is null"); //NOI18N
125 } else if (event.getSharingType() != SharingType.ENDED) {
131 this.validateEvent(event);
134 this.sharedAddressbooks.remove(event.getShareableAddressbook());
138 public void afterAdressbookShareStarted (final @Observes AddressbookSharingEvent event) {
139 // Validate parameter
142 throw new NullPointerException("event is null"); //NOI18N
143 } else if (event.getSharingType() == null) {
145 throw new NullPointerException("event.sharingType is null"); //NOI18N
146 } else if (event.getSharingType() != SharingType.STARTED) {
152 this.validateEvent(event);
155 this.sharedAddressbooks.add(event.getShareableAddressbook());
159 public void afterLoginEvent (final @Observes UserLoggedInEvent event) {
160 // Is the user logged in?
163 throw new NullPointerException("event is null"); //NOI18N
164 } else if (event.getLoggedInUser() == null) {
166 throw new NullPointerException("event.user is null"); //NOI18N
170 this.sharedAddressbooks = this.shareBean.allSharedAddressbooks(event.getLoggedInUser());
174 public List<ShareableAddressbook> allShares () {
175 // Is the user logged in?
176 if (!this.loginController.isUserLoggedIn()) {
178 throw new FaceletException("This method can only be called as logged-in user."); //NOI18N
181 return Collections.unmodifiableList(this.sharedAddressbooks);
185 public ShareableAddressbook getShare () {
190 public void setShare (final ShareableAddressbook share) {
195 public Long getShareeUserId () {
196 return this.shareeUserId;
200 public void setShareeUserId (final Long shareeUserId) {
201 this.shareeUserId = shareeUserId;
205 * Post-initialization of this class
208 public void init () {
212 public boolean isShareeUserIdEmpty () {
213 return (!this.isShareeUserIdSet());
217 public boolean isShareeUserIdSet () {
218 return ((this.getShareeUserId() instanceof Long) && (this.getShareeUserId() > 0));
222 public boolean isSharingAddressbooks () {
223 // Only to be called for logged-in users
224 if (!this.loginController.isUserLoggedIn()) {
226 throw new FaceletException("This method can only be called as logged-in user."); //NOI18N
227 } else if (this.isUserSharing instanceof Boolean) {
228 // Return cached value
229 return this.isUserSharing;
232 // Call the proper bean
233 this.isUserSharing = this.shareBean.isUserSharingAddressbooks(this.loginController.getLoggedInUser());
236 return this.isUserSharing;
240 public String startSharing (final User user, final Addressbook addressbook) {
242 if (!this.loginController.isUserLoggedIn()) {
243 // No, then throw exception
244 throw new FaceletException("This method can only be called as logged-in user."); //NOI18N
245 } else if (null == user) {
247 throw new NullPointerException("user is null"); //NOI18N
248 } else if (user.getUserId() == null) {
250 throw new NullPointerException("user.userId is null"); //NOI18N
251 } else if (user.getUserId() < 1) {
253 throw new IllegalStateException(MessageFormat.format("user.userId={0} is invalid", user.getUserId())); //NOI18N
254 } else if (Objects.equals(user, this.loginController.getLoggedInUser())) {
255 // Sharing with yourself!
256 throw new IllegalStateException("User tries to share with himself."); //NOI18N
257 } else if (null == addressbook) {
259 throw new NullPointerException("addressbook is null"); //NOI18N
260 } else if (addressbook.getAddressbookId() == null) {
262 throw new NullPointerException("addressbook.addressbookId is null"); //NOI18N
263 } else if (addressbook.getAddressbookId() < 1) {
265 throw new IllegalArgumentException(MessageFormat.format("addressbook.addressbookId={0} is invalid.", addressbook.getAddressbookId())); //NOI18N
266 } else if (!Objects.equals(addressbook.getAddressbookUser(), this.loginController.getLoggedInUser())) {
267 // Not the same user!
268 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())); //NOI18N
269 } else if (this.loginController.getLoggedInUser().getUserProfileMode() == ProfileMode.INVISIBLE) {
271 throw new FaceletException(MessageFormat.format("user {0} is invisible and cannot start sharing address books.", this.loginController.getLoggedInUser().getUserId())); //NOI18N
272 } else if (user.getUserProfileMode() == ProfileMode.INVISIBLE) {
274 throw new FaceletException(MessageFormat.format("user {0} is invisible and cannot be selected for sharing.", user.getUserId())); //NOI18N
279 ShareableAddressbook shared = this.shareBean.startSharing(user, addressbook);
282 this.setShare(shared);
285 this.sharingEvent.fire(new StartedAddressbookSharingEvent(shared));
286 } catch (final UserAlreadySharingAddressbookException ex) {
288 throw new FaceletException(ex);
296 * Validates given event for all values and throws exceptions
298 * @param event Event to validate
300 private void validateEvent (final AddressbookSharingEvent event) {
303 throw new NullPointerException("event is null"); //NOI18N
304 } else if (event.getSharingType() == null) {
306 throw new NullPointerException("event.sharingType is null"); //NOI18N
307 } else if (event.getShareableAddressbook() == null) {
309 throw new NullPointerException("event.shareableAddressbook is null"); //NOI18N
310 } else if (event.getShareableAddressbook().getShareId() == null) {
312 throw new NullPointerException("event.shareableAddressbook.shareId is null"); //NOI18N
313 } else if (event.getShareableAddressbook().getShareId() < 1) {
315 throw new IllegalArgumentException(MessageFormat.format("event.shareableAddressbook.shareId={0} is invalid", event.getShareableAddressbook().getShareId())); //NOI18N
316 } else if (event.getShareableAddressbook().getShareAddressbook() == null) {
318 throw new NullPointerException("event.shareableAddressbook.shareAddressbook is null"); //NOI18N
319 } else if (event.getShareableAddressbook().getShareAddressbook().getAddressbookId() == null) {
321 throw new NullPointerException("event.shareableAddressbook.shareAddressbook.addressbookId is null"); //NOI18N
322 } else if (event.getShareableAddressbook().getShareAddressbook().getAddressbookId() < 1) {
324 throw new IllegalArgumentException(MessageFormat.format("event.shareableAddressbook.shareAddressbook.addressbookId={0} is invalid", event.getShareableAddressbook().getShareAddressbook().getAddressbookId())); //NOI18N
325 } else if (event.getShareableAddressbook().getShareUserOwner() == null) {
327 throw new NullPointerException("event.shareableAddressbook.shareUserOwner is null"); //NOI18N
328 } else if (event.getShareableAddressbook().getShareUserOwner().getUserId() == null) {
330 throw new NullPointerException("event.shareableAddressbook.shareUserOwner.userId is null"); //NOI18N
331 } else if (event.getShareableAddressbook().getShareUserOwner().getUserId() < 1) {
333 throw new IllegalArgumentException(MessageFormat.format("event.shareableAddressbook.shareUserOwner.userId={0} is invalid", event.getShareableAddressbook().getShareUserOwner().getUserId())); //NOI18N
334 } else if (event.getShareableAddressbook().getShareUserSharee() == null) {
336 throw new NullPointerException("event.shareableAddressbook.shareUserSharee is null"); //NOI18N
337 } else if (event.getShareableAddressbook().getShareUserSharee().getUserId() == null) {
339 throw new NullPointerException("event.shareableAddressbook.shareUserSharee.userId is null"); //NOI18N
340 } else if (event.getShareableAddressbook().getShareUserSharee().getUserId() < 1) {
342 throw new IllegalArgumentException(MessageFormat.format("event.shareableAddressbook.shareUserSharee.userId={0} is invalid", event.getShareableAddressbook().getShareUserOwner().getUserId())); //NOI18N