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.login.AddressbookUserLoginWebSessionController;
35 import org.mxchange.addressbook.model.shared.SharedAddressbooksSessionBeanRemote;
36 import org.mxchange.jaddressbookcore.events.sharing.AddressbookSharingEvent;
37 import org.mxchange.jaddressbookcore.events.sharing.StartedAddressbookSharingEvent;
38 import org.mxchange.jaddressbookcore.events.sharing.type.SharingType;
39 import org.mxchange.jaddressbookcore.exceptions.UserAlreadySharingAddressbookException;
40 import org.mxchange.jaddressbookcore.model.addressbook.Addressbook;
41 import org.mxchange.jaddressbookcore.model.addressbook.shared.ShareableAddressbook;
42 import org.mxchange.jusercore.events.login.UserLoggedInEvent;
43 import org.mxchange.jusercore.model.user.User;
44 import org.mxchange.jusercore.model.user.profilemodes.ProfileMode;
47 * A bean for sharing address books with other users
49 * @author Roland Haeder<roland@mxchange.org>
51 @Named (value = "shareController")
53 public class AddressbookSharesWebSessionBean implements AddressbookSharesWebSessionController {
58 private static final long serialVersionUID = 19_868_976_871_976_780L;
61 * Cached flag whether the user is sharing address books
63 private Boolean isUserSharing = null;
66 * Login controller injection
69 private AddressbookUserLoginWebSessionController loginController;
74 private ShareableAddressbook share;
77 * Remote bean for sharing address books
79 private SharedAddressbooksSessionBeanRemote shareBean;
82 * A list of all user's shared (with others) address books
84 private List<ShareableAddressbook> sharedAddressbooks;
89 private Long shareeUserId;
92 * An event triggered when address book sharing starts or ends
96 private Event<AddressbookSharingEvent> sharingEvent;
101 public AddressbookSharesWebSessionBean () {
104 // Get initial context
105 Context context = new InitialContext();
108 this.shareBean = (SharedAddressbooksSessionBeanRemote) context.lookup("ejb/stateless-addressbook-share"); //NOI18N
109 } catch (final NamingException ex) {
111 throw new FaceletException(ex);
116 public void afterAdressbookShareEnded (final @Observes AddressbookSharingEvent event) {
117 // Validate parameter
120 throw new NullPointerException("event is null"); //NOI18N
121 } else if (event.getSharingType() == null) {
123 throw new NullPointerException("event.sharingType is null"); //NOI18N
124 } else if (event.getSharingType() != SharingType.ENDED) {
130 this.validateEvent(event);
133 this.sharedAddressbooks.remove(event.getShareableAddressbook());
137 public void afterAdressbookShareStarted (final @Observes AddressbookSharingEvent event) {
138 // Validate parameter
141 throw new NullPointerException("event is null"); //NOI18N
142 } else if (event.getSharingType() == null) {
144 throw new NullPointerException("event.sharingType is null"); //NOI18N
145 } else if (event.getSharingType() != SharingType.STARTED) {
151 this.validateEvent(event);
154 this.sharedAddressbooks.add(event.getShareableAddressbook());
158 public void afterLoginEvent (final @Observes UserLoggedInEvent event) {
159 // Is the user logged in?
162 throw new NullPointerException("event is null"); //NOI18N
163 } else if (event.getLoggedInUser() == null) {
165 throw new NullPointerException("event.user is null"); //NOI18N
169 this.sharedAddressbooks = this.shareBean.allSharedAddressbooks(event.getLoggedInUser());
173 public List<ShareableAddressbook> allShares () {
174 // Is the user logged in?
175 if (!this.loginController.isUserLoggedIn()) {
177 throw new FaceletException("This method can only be called as logged-in user."); //NOI18N
180 return Collections.unmodifiableList(this.sharedAddressbooks);
184 public ShareableAddressbook getShare () {
189 public void setShare (final ShareableAddressbook share) {
194 public Long getShareeUserId () {
195 return this.shareeUserId;
199 public void setShareeUserId (final Long shareeUserId) {
200 this.shareeUserId = shareeUserId;
204 * Post-initialization of this class
207 public void init () {
211 public boolean isShareeUserIdEmpty () {
212 return (!this.isShareeUserIdSet());
216 public boolean isShareeUserIdSet () {
217 return ((this.getShareeUserId() instanceof Long) && (this.getShareeUserId() > 0));
221 public boolean isSharingAddressbooks () {
222 // Only to be called for logged-in users
223 if (!this.loginController.isUserLoggedIn()) {
225 throw new FaceletException("This method can only be called as logged-in user."); //NOI18N
226 } else if (this.isUserSharing instanceof Boolean) {
227 // Return cached value
228 return this.isUserSharing;
231 // Call the proper bean
232 this.isUserSharing = this.shareBean.isUserSharingAddressbooks(this.loginController.getLoggedInUser());
235 return this.isUserSharing;
239 public String startSharing (final User user, final Addressbook addressbook) {
241 if (!this.loginController.isUserLoggedIn()) {
242 // No, then throw exception
243 throw new FaceletException("This method can only be called as logged-in user."); //NOI18N
244 } else if (null == user) {
246 throw new NullPointerException("user is null"); //NOI18N
247 } else if (user.getUserId() == null) {
249 throw new NullPointerException("user.userId is null"); //NOI18N
250 } else if (user.getUserId() < 1) {
252 throw new IllegalStateException(MessageFormat.format("user.userId={0} is invalid", user.getUserId())); //NOI18N
253 } else if (Objects.equals(user, this.loginController.getLoggedInUser())) {
254 // Sharing with yourself!
255 throw new IllegalStateException("User tries to share with himself."); //NOI18N
256 } else if (null == addressbook) {
258 throw new NullPointerException("addressbook is null"); //NOI18N
259 } else if (addressbook.getAddressbookId() == null) {
261 throw new NullPointerException("addressbook.addressbookId is null"); //NOI18N
262 } else if (addressbook.getAddressbookId() < 1) {
264 throw new IllegalArgumentException(MessageFormat.format("addressbook.addressbookId={0} is invalid.", addressbook.getAddressbookId())); //NOI18N
265 } else if (!Objects.equals(addressbook.getAddressbookUser(), this.loginController.getLoggedInUser())) {
266 // Not the same user!
267 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
268 } else if (this.loginController.getLoggedInUser().getUserProfileMode() == ProfileMode.INVISIBLE) {
270 throw new FaceletException(MessageFormat.format("user {0} is invisible and cannot start sharing address books.", this.loginController.getLoggedInUser().getUserId())); //NOI18N
271 } else if (user.getUserProfileMode() == ProfileMode.INVISIBLE) {
273 throw new FaceletException(MessageFormat.format("user {0} is invisible and cannot be selected for sharing.", user.getUserId())); //NOI18N
278 ShareableAddressbook shared = this.shareBean.startSharing(user, addressbook);
281 this.setShare(shared);
284 this.sharingEvent.fire(new StartedAddressbookSharingEvent(shared));
285 } catch (final UserAlreadySharingAddressbookException ex) {
287 throw new FaceletException(ex);
295 * Validates given event for all values and throws exceptions
297 * @param event Event to validate
299 private void validateEvent (final AddressbookSharingEvent event) {
302 throw new NullPointerException("event is null"); //NOI18N
303 } else if (event.getSharingType() == null) {
305 throw new NullPointerException("event.sharingType is null"); //NOI18N
306 } else if (event.getShareableAddressbook() == null) {
308 throw new NullPointerException("event.shareableAddressbook is null"); //NOI18N
309 } else if (event.getShareableAddressbook().getShareId() == null) {
311 throw new NullPointerException("event.shareableAddressbook.shareId is null"); //NOI18N
312 } else if (event.getShareableAddressbook().getShareId() < 1) {
314 throw new IllegalArgumentException(MessageFormat.format("event.shareableAddressbook.shareId={0} is invalid", event.getShareableAddressbook().getShareId())); //NOI18N
315 } else if (event.getShareableAddressbook().getShareAddressbook() == null) {
317 throw new NullPointerException("event.shareableAddressbook.shareAddressbook is null"); //NOI18N
318 } else if (event.getShareableAddressbook().getShareAddressbook().getAddressbookId() == null) {
320 throw new NullPointerException("event.shareableAddressbook.shareAddressbook.addressbookId is null"); //NOI18N
321 } else if (event.getShareableAddressbook().getShareAddressbook().getAddressbookId() < 1) {
323 throw new IllegalArgumentException(MessageFormat.format("event.shareableAddressbook.shareAddressbook.addressbookId={0} is invalid", event.getShareableAddressbook().getShareAddressbook().getAddressbookId())); //NOI18N
324 } else if (event.getShareableAddressbook().getShareUserOwner() == null) {
326 throw new NullPointerException("event.shareableAddressbook.shareUserOwner is null"); //NOI18N
327 } else if (event.getShareableAddressbook().getShareUserOwner().getUserId() == null) {
329 throw new NullPointerException("event.shareableAddressbook.shareUserOwner.userId is null"); //NOI18N
330 } else if (event.getShareableAddressbook().getShareUserOwner().getUserId() < 1) {
332 throw new IllegalArgumentException(MessageFormat.format("event.shareableAddressbook.shareUserOwner.userId={0} is invalid", event.getShareableAddressbook().getShareUserOwner().getUserId())); //NOI18N
333 } else if (event.getShareableAddressbook().getShareUserSharee() == null) {
335 throw new NullPointerException("event.shareableAddressbook.shareUserSharee is null"); //NOI18N
336 } else if (event.getShareableAddressbook().getShareUserSharee().getUserId() == null) {
338 throw new NullPointerException("event.shareableAddressbook.shareUserSharee.userId is null"); //NOI18N
339 } else if (event.getShareableAddressbook().getShareUserSharee().getUserId() < 1) {
341 throw new IllegalArgumentException(MessageFormat.format("event.shareableAddressbook.shareUserSharee.userId={0} is invalid", event.getShareableAddressbook().getShareUserOwner().getUserId())); //NOI18N