]> git.mxchange.org Git - addressbook-war.git/blob - src/java/org/mxchange/addressbook/beans/addressbook/share/AddressbookSharesWebSessionBean.java
ported project to new libraries jaddressbook-share-core/lib
[addressbook-war.git] / src / java / org / mxchange / addressbook / beans / addressbook / share / AddressbookSharesWebSessionBean.java
1 /*
2  * Copyright (C) 2016 Roland Häder
3  *
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.
8  *
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.
13  *
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/>.
16  */
17 package org.mxchange.addressbook.beans.addressbook.share;
18
19 import java.text.MessageFormat;
20 import java.util.Collections;
21 import java.util.List;
22 import java.util.Objects;
23 import java.util.concurrent.ConcurrentHashMap;
24 import java.util.concurrent.ConcurrentMap;
25 import javax.annotation.PostConstruct;
26 import javax.enterprise.context.SessionScoped;
27 import javax.enterprise.event.Event;
28 import javax.enterprise.event.Observes;
29 import javax.enterprise.inject.Any;
30 import javax.faces.view.facelets.FaceletException;
31 import javax.inject.Inject;
32 import javax.inject.Named;
33 import javax.naming.Context;
34 import javax.naming.InitialContext;
35 import javax.naming.NamingException;
36 import org.mxchange.addressbook.beans.BaseAddressbookController;
37 import org.mxchange.addressbook.beans.addressbook.AddressbookWebSessionController;
38 import org.mxchange.addressbook.beans.login.user.AddressbookUserLoginWebSessionController;
39 import org.mxchange.addressbook.model.shared.AddressbookShareSessionBeanRemote;
40 import org.mxchange.jaddressbook.model.addressbook.Addressbook;
41 import org.mxchange.jaddressbookshare.events.sharing.ObservableAddressbookSharingEvent;
42 import org.mxchange.jaddressbookshare.events.sharing.StartedAddressbookSharingEvent;
43 import org.mxchange.jaddressbookshare.events.sharing.type.SharingType;
44 import org.mxchange.jaddressbookshare.exceptions.UserAlreadySharingAddressbookException;
45 import org.mxchange.jaddressbookshare.model.addressbook.shared.ShareableAddressbook;
46 import org.mxchange.jusercore.events.login.ObservableUserLoggedInEvent;
47 import org.mxchange.jusercore.model.user.User;
48 import org.mxchange.jusercore.model.user.profilemodes.ProfileMode;
49
50 /**
51  * A bean for sharing address books with other users
52  * <p>
53  * @author Roland Häder<roland@mxchange.org>
54  */
55 @Named (value = "shareController")
56 @SessionScoped
57 public class AddressbookSharesWebSessionBean extends BaseAddressbookController implements AddressbookSharesWebSessionController {
58
59         /**
60          * Map for count of user's shared addresses
61          */
62         private static ConcurrentMap<User, Integer> countSharesList;
63
64         /**
65          * Serial number
66          */
67         private static final long serialVersionUID = 19_868_976_871_976_780L;
68
69         /**
70          * Address book controller
71          */
72         @Inject
73         private AddressbookWebSessionController addressbookBean;
74
75         /**
76          * Remote bean for sharing address books
77          */
78         private AddressbookShareSessionBeanRemote addressbookShareBean;
79
80         /**
81          * Cached flag whether the user is sharing address books
82          */
83         private Boolean isUserSharing = null;
84
85         /**
86          * Share instance
87          */
88         private ShareableAddressbook share;
89
90         /**
91          * A list of all user's shared (with others) address books
92          */
93         private List<ShareableAddressbook> sharedAddressbooks;
94
95         /**
96          * User id of sharee
97          */
98         private Long shareeUserId;
99
100         /**
101          * An event triggered when address book sharing starts or ends
102          */
103         @Inject
104         @Any
105         private Event<ObservableAddressbookSharingEvent> sharingEvent;
106
107         /**
108          * Login controller injection
109          */
110         @Inject
111         private AddressbookUserLoginWebSessionController userLoginController;
112
113         /**
114          * Default constructor
115          */
116         public AddressbookSharesWebSessionBean () {
117                 // Init list
118                 countSharesList = new ConcurrentHashMap<>(0);
119         }
120
121         /**
122          * Observer method for ended sharing events
123          * <p>
124          * @param event Event instance
125          */
126         public void afterAdressbookShareEndedEvent (@Observes final ObservableAddressbookSharingEvent event) {
127                 // Validate parameter
128                 if (null == event) {
129                         // Throw NPE
130                         throw new NullPointerException("event is null"); //NOI18N
131                 } else if (event.getSharingType() == null) {
132                         // Throw NPE
133                         throw new NullPointerException("event.sharingType is null"); //NOI18N
134                 } else if (event.getSharingType() != SharingType.ENDED) {
135                         // Wrong event
136                         return;
137                 }
138
139                 // Validate event
140                 this.validateEvent(event);
141
142                 // Add it to list
143                 this.sharedAddressbooks.remove(event.getShareableAddressbook());
144         }
145
146         /**
147          * Observer method for started sharing events
148          * <p>
149          * @param event Event instance
150          */
151         public void afterAdressbookShareStartedEvent (@Observes final ObservableAddressbookSharingEvent event) {
152                 // Validate parameter
153                 if (null == event) {
154                         // Throw NPE
155                         throw new NullPointerException("event is null"); //NOI18N
156                 } else if (event.getSharingType() == null) {
157                         // Throw NPE
158                         throw new NullPointerException("event.sharingType is null"); //NOI18N
159                 } else if (event.getSharingType() != SharingType.STARTED) {
160                         // Wrong event
161                         return;
162                 }
163
164                 // Validate event
165                 this.validateEvent(event);
166
167                 // Add it to list
168                 this.sharedAddressbooks.add(event.getShareableAddressbook());
169         }
170
171         /**
172          * This method is called when a user has successfully logged in his/her
173          * account.
174          * <p>
175          * @param event Event instance
176          */
177         public void afterLoginEvent (@Observes final ObservableUserLoggedInEvent event) {
178                 // Is the user logged in?
179                 if (null == event) {
180                         // Is null
181                         throw new NullPointerException("event is null"); //NOI18N
182                 } else if (event.getLoggedInUser() == null) {
183                         // user is null
184                         throw new NullPointerException("event.user is null"); //NOI18N
185                 }
186
187                 // Init share list
188                 this.sharedAddressbooks = this.addressbookShareBean.allSharedAddressbooks(event.getLoggedInUser());
189         }
190
191         @Override
192         public List<ShareableAddressbook> allShares () {
193                 // Is the user logged in?
194                 if (!this.userLoginController.isUserLoggedIn()) {
195                         // Not logged in
196                         throw new FaceletException("This method can only be called as logged-in user."); //NOI18N
197                 }
198
199                 return Collections.unmodifiableList(this.sharedAddressbooks);
200         }
201
202         @Override
203         public List<User> allUsersNotSharing () {
204                 // Is the user logged in?
205                 if (!this.userLoginController.isUserLoggedIn()) {
206                         // Not logged in
207                         throw new FaceletException("This method can only be called as logged-in user."); //NOI18N
208                 }
209
210                 // Call EJB
211                 return this.addressbookShareBean.allUsersNotSharing(this.userLoginController.getLoggedInUser(), this.addressbookBean.getAddressbook());
212         }
213
214         @Override
215         public Integer countAllUserSharedAddressbooks (final User user) {
216                 // Is there cache?
217                 if (countSharesList.containsKey(user)) {
218                         // Return it instead
219                         return countSharesList.get(user);
220                 }
221
222                 // Call EJB ("expensive")
223                 Integer count = this.addressbookShareBean.countAllUserSharedAddressbooks(user);
224
225                 // Add to list
226                 countSharesList.put(user, count);
227
228                 // Return it
229                 return count;
230         }
231
232         @Override
233         public ShareableAddressbook getShare () {
234                 return this.share;
235         }
236
237         @Override
238         public void setShare (final ShareableAddressbook share) {
239                 this.share = share;
240         }
241
242         @Override
243         public Long getShareeUserId () {
244                 return this.shareeUserId;
245         }
246
247         @Override
248         public void setShareeUserId (final Long shareeUserId) {
249                 this.shareeUserId = shareeUserId;
250         }
251
252         /**
253          * Post-initialization of this class
254          */
255         @PostConstruct
256         public void init () {
257                 // Try it
258                 try {
259                         // Get initial context
260                         Context context = new InitialContext();
261
262                         // Look up bean
263                         this.addressbookShareBean = (AddressbookShareSessionBeanRemote) context.lookup("ejb/stateless-addressbook-share"); //NOI18N
264                 } catch (final NamingException ex) {
265                         // Continue to throw
266                         throw new FaceletException(ex);
267                 }
268
269         }
270
271         @Override
272         public boolean isShareeUserIdEmpty () {
273                 return (!this.isShareeUserIdSet());
274         }
275
276         @Override
277         public boolean isShareeUserIdSet () {
278                 return ((this.getShareeUserId() instanceof Long) && (this.getShareeUserId() > 0));
279         }
280
281         @Override
282         public boolean isSharingAddressbooks () {
283                 // Only to be called for logged-in users
284                 if (!this.userLoginController.isUserLoggedIn()) {
285                         // Not logged in
286                         throw new FaceletException("This method can only be called as logged-in user."); //NOI18N
287                 } else if (this.isUserSharing instanceof Boolean) {
288                         // Return cached value
289                         return this.isUserSharing;
290                 }
291
292                 // Call the proper bean
293                 this.isUserSharing = this.addressbookShareBean.isUserSharingAddressbooks(this.userLoginController.getLoggedInUser());
294
295                 // Return it
296                 return this.isUserSharing;
297         }
298
299         @Override
300         public String startSharing (final User user, final Addressbook addressbook) {
301                 // Check conditions
302                 if (!this.userLoginController.isUserLoggedIn()) {
303                         // No, then throw exception
304                         throw new FaceletException("This method can only be called as logged-in user."); //NOI18N
305                 } else if (null == user) {
306                         // Throw NPE
307                         throw new NullPointerException("user is null"); //NOI18N
308                 } else if (user.getUserId() == null) {
309                         // Throw NPE again
310                         throw new NullPointerException("user.userId is null"); //NOI18N
311                 } else if (user.getUserId() < 1) {
312                         // Invalid id number
313                         throw new IllegalStateException(MessageFormat.format("user.userId={0} is invalid", user.getUserId())); //NOI18N
314                 } else if (Objects.equals(user, this.userLoginController.getLoggedInUser())) {
315                         // Sharing with yourself!
316                         throw new IllegalStateException("User tries to share with himself."); //NOI18N
317                 } else if (null == addressbook) {
318                         // Throw NPE again
319                         throw new NullPointerException("addressbook is null"); //NOI18N
320                 } else if (addressbook.getAddressbookId() == null) {
321                         // Throw NPE again
322                         throw new NullPointerException("addressbook.addressbookId is null"); //NOI18N
323                 } else if (addressbook.getAddressbookId() < 1) {
324                         // Invalid id number
325                         throw new IllegalArgumentException(MessageFormat.format("addressbook.addressbookId={0} is invalid.", addressbook.getAddressbookId())); //NOI18N
326                 } else if (!Objects.equals(addressbook.getAddressbookUser(), this.userLoginController.getLoggedInUser())) {
327                         // Not the same user!
328                         throw new IllegalStateException(MessageFormat.format("Address book id {0} owner id {1} mismatching logged-in user id {2}", addressbook.getAddressbookId(), addressbook.getAddressbookUser().getUserId(), this.userLoginController.getLoggedInUser().getUserId())); //NOI18N
329                 } else if (this.userLoginController.getLoggedInUser().getUserProfileMode() == ProfileMode.INVISIBLE) {
330                         // User is invisible
331                         throw new FaceletException(MessageFormat.format("user {0} is invisible and cannot start sharing address books.", this.userLoginController.getLoggedInUser().getUserId())); //NOI18N
332                 } else if (user.getUserProfileMode() == ProfileMode.INVISIBLE) {
333                         // User is invisible
334                         throw new FaceletException(MessageFormat.format("user {0} is invisible and cannot be selected for sharing.", user.getUserId())); //NOI18N
335                 }
336
337                 try {
338                         // Init sharing
339                         ShareableAddressbook shared = this.addressbookShareBean.startSharing(user, addressbook);
340
341                         // TODO Set it here
342                         this.setShare(shared);
343
344                         /// Trigger event
345                         this.sharingEvent.fire(new StartedAddressbookSharingEvent(shared));
346                 } catch (final UserAlreadySharingAddressbookException ex) {
347                         // Throw again
348                         throw new FaceletException(ex);
349                 }
350
351                 // TODO Unfinished
352                 return null;
353         }
354
355         /**
356          * Validates given event for all values and throws exceptions
357          * <p>
358          * @param event Event to validate
359          */
360         private void validateEvent (final ObservableAddressbookSharingEvent event) {
361                 if (null == event) {
362                         // Throw NPE
363                         throw new NullPointerException("event is null"); //NOI18N
364                 } else if (event.getSharingType() == null) {
365                         // Throw NPE
366                         throw new NullPointerException("event.sharingType is null"); //NOI18N
367                 } else if (event.getShareableAddressbook() == null) {
368                         // Throw NPE again
369                         throw new NullPointerException("event.shareableAddressbook is null"); //NOI18N
370                 } else if (event.getShareableAddressbook().getShareId() == null) {
371                         // Throw NPE again
372                         throw new NullPointerException("event.shareableAddressbook.shareId is null"); //NOI18N
373                 } else if (event.getShareableAddressbook().getShareId() < 1) {
374                         // Throw NPE again
375                         throw new IllegalArgumentException(MessageFormat.format("event.shareableAddressbook.shareId={0} is invalid", event.getShareableAddressbook().getShareId())); //NOI18N
376                 } else if (event.getShareableAddressbook().getShareAddressbook() == null) {
377                         // Throw NPE again
378                         throw new NullPointerException("event.shareableAddressbook.shareAddressbook is null"); //NOI18N
379                 } else if (event.getShareableAddressbook().getShareAddressbook().getAddressbookId() == null) {
380                         // Throw NPE again
381                         throw new NullPointerException("event.shareableAddressbook.shareAddressbook.addressbookId is null"); //NOI18N
382                 } else if (event.getShareableAddressbook().getShareAddressbook().getAddressbookId() < 1) {
383                         // Throw NPE again
384                         throw new IllegalArgumentException(MessageFormat.format("event.shareableAddressbook.shareAddressbook.addressbookId={0} is invalid", event.getShareableAddressbook().getShareAddressbook().getAddressbookId())); //NOI18N
385                 } else if (event.getShareableAddressbook().getShareUserOwner() == null) {
386                         // Throw NPE again
387                         throw new NullPointerException("event.shareableAddressbook.shareUserOwner is null"); //NOI18N
388                 } else if (event.getShareableAddressbook().getShareUserOwner().getUserId() == null) {
389                         // Throw NPE again
390                         throw new NullPointerException("event.shareableAddressbook.shareUserOwner.userId is null"); //NOI18N
391                 } else if (event.getShareableAddressbook().getShareUserOwner().getUserId() < 1) {
392                         // Throw NPE again
393                         throw new IllegalArgumentException(MessageFormat.format("event.shareableAddressbook.shareUserOwner.userId={0} is invalid", event.getShareableAddressbook().getShareUserOwner().getUserId())); //NOI18N
394                 } else if (event.getShareableAddressbook().getShareUserSharee() == null) {
395                         // Throw NPE again
396                         throw new NullPointerException("event.shareableAddressbook.shareUserSharee is null"); //NOI18N
397                 } else if (event.getShareableAddressbook().getShareUserSharee().getUserId() == null) {
398                         // Throw NPE again
399                         throw new NullPointerException("event.shareableAddressbook.shareUserSharee.userId is null"); //NOI18N
400                 } else if (event.getShareableAddressbook().getShareUserSharee().getUserId() < 1) {
401                         // Throw NPE again
402                         throw new IllegalArgumentException(MessageFormat.format("event.shareableAddressbook.shareUserSharee.userId={0} is invalid", event.getShareableAddressbook().getShareUserOwner().getUserId())); //NOI18N
403                 }
404         }
405
406 }