]> git.mxchange.org Git - addressbook-war.git/blob - src/java/org/mxchange/addressbook/beans/shares/SharesWebSessionBean.java
Added email address for author
[addressbook-war.git] / src / java / org / mxchange / addressbook / beans / shares / SharesWebSessionBean.java
1 /*
2  * Copyright (C) 2016 Roland Haeder
3  *
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.
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 General Public License for more details.
13  *
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/>.
16  */
17 package org.mxchange.addressbook.beans.shares;
18
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.UserLoginWebSessionController;
35 import org.mxchange.addressbook.events.sharing.AddressbookSharingEvent;
36 import org.mxchange.addressbook.events.sharing.StartedAddressbookSharingEvent;
37 import org.mxchange.addressbook.events.sharing.type.SharingType;
38 import org.mxchange.addressbook.exceptions.UserAlreadySharingAddressbookException;
39 import org.mxchange.addressbook.model.addressbook.Addressbook;
40 import org.mxchange.addressbook.model.addressbook.shared.ShareableAddressbook;
41 import org.mxchange.addressbook.model.shared.SharedAddressbooksSessionBeanRemote;
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;
45
46 /**
47  * A bean for sharing address books with other users
48  * <p>
49  * @author Roland Haeder<roland@mxchange.org>
50  */
51 @Named (value = "shareController")
52 @SessionScoped
53 public class SharesWebSessionBean implements SharesWebSessionController {
54
55         /**
56          * Serial number
57          */
58         private static final long serialVersionUID = 19_868_976_871_976_780L;
59
60         /**
61          * Cached flag whether the user is sharing address books
62          */
63         private Boolean isUserSharing = null;
64
65         /**
66          * Login controller injection
67          */
68         @Inject
69         private UserLoginWebSessionController loginController;
70
71         /**
72          * Share instance
73          */
74         private ShareableAddressbook share;
75
76         /**
77          * Remote bean for sharing address books
78          */
79         private SharedAddressbooksSessionBeanRemote shareBean;
80
81         /**
82          * A list of all user's shared (with others) address books
83          */
84         private List<ShareableAddressbook> sharedAddressbooks;
85
86         /**
87          * User id of sharee
88          */
89         private Long shareeUserId;
90
91         /**
92          * An event triggered when address book sharing starts or ends
93          */
94         @Inject
95         @Any
96         private Event<AddressbookSharingEvent> sharingEvent;
97
98         /**
99          * Default constructor
100          */
101         public SharesWebSessionBean () {
102                 // Try it
103                 try {
104                         // Get initial context
105                         Context context = new InitialContext();
106
107                         // Look up bean
108                         this.shareBean = (SharedAddressbooksSessionBeanRemote) context.lookup("ejb/stateless-addressbook-share"); //NOI18N
109                 } catch (final NamingException ex) {
110                         // Continue to throw
111                         throw new FaceletException(ex);
112                 }
113         }
114
115         @Override
116         public void afterAdressbookShareEnded (final @Observes AddressbookSharingEvent event) {
117                 // Validate parameter
118                 if (null == event) {
119                         // Throw NPE
120                         throw new NullPointerException("event is null"); //NOI18N
121                 } else if (event.getSharingType() == null) {
122                         // Throw NPE
123                         throw new NullPointerException("event.sharingType is null"); //NOI18N
124                 } else if (event.getSharingType() != SharingType.ENDED) {
125                         // Wrong event
126                         return;
127                 }
128
129                 // Validate event
130                 this.validateEvent(event);
131
132                 // Add it to list
133                 this.sharedAddressbooks.remove(event.getShareableAddressbook());
134         }
135
136         @Override
137         public void afterAdressbookShareStarted (final @Observes AddressbookSharingEvent event) {
138                 // Validate parameter
139                 if (null == event) {
140                         // Throw NPE
141                         throw new NullPointerException("event is null"); //NOI18N
142                 } else if (event.getSharingType() == null) {
143                         // Throw NPE
144                         throw new NullPointerException("event.sharingType is null"); //NOI18N
145                 } else if (event.getSharingType() != SharingType.STARTED) {
146                         // Wrong event
147                         return;
148                 }
149
150                 // Validate event
151                 this.validateEvent(event);
152
153                 // Add it to list
154                 this.sharedAddressbooks.add(event.getShareableAddressbook());
155         }
156
157         @Override
158         public void afterLoginEvent (final @Observes UserLoggedInEvent event) {
159                 // Is the user logged in?
160                 if (null == event) {
161                         // Is null
162                         throw new NullPointerException("event is null"); //NOI18N
163                 } else if (event.getUser() == null) {
164                         // user is null
165                         throw new NullPointerException("event.user is null"); //NOI18N
166                 }
167
168                 // Init share list
169                 this.sharedAddressbooks = this.shareBean.allSharedAddressbooks(event.getUser());
170         }
171
172         @Override
173         public List<ShareableAddressbook> allShares () {
174                 // Is the user logged in?
175                 if (!this.loginController.isUserLoggedIn()) {
176                         // Not logged in
177                         throw new FaceletException("This method can only be called as logged-in user."); //NOI18N
178                 }
179
180                 return Collections.unmodifiableList(this.sharedAddressbooks);
181         }
182
183         @Override
184         public ShareableAddressbook getShare () {
185                 return this.share;
186         }
187
188         @Override
189         public void setShare (final ShareableAddressbook share) {
190                 this.share = share;
191         }
192
193         @Override
194         public Long getShareeUserId () {
195                 return this.shareeUserId;
196         }
197
198         @Override
199         public void setShareeUserId (final Long shareeUserId) {
200                 this.shareeUserId = shareeUserId;
201         }
202
203         /**
204          * Post-initialization of this class
205          */
206         @PostConstruct
207         public void init () {
208         }
209
210         @Override
211         public boolean isShareeUserIdEmpty () {
212                 return (!this.isShareeUserIdSet());
213         }
214
215         @Override
216         public boolean isShareeUserIdSet () {
217                 return ((this.getShareeUserId() instanceof Long) && (this.getShareeUserId() > 0));
218         }
219
220         @Override
221         public boolean isSharingAddressbooks () {
222                 // Only to be called for logged-in users
223                 if (!this.loginController.isUserLoggedIn()) {
224                         // Not logged in
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;
229                 }
230
231                 // Call the proper bean
232                 this.isUserSharing = this.shareBean.isUserSharingAddressbooks(this.loginController.getLoggedInUser());
233
234                 // Return it
235                 return this.isUserSharing;
236         }
237
238         @Override
239         public String startSharing (final User user, final Addressbook addressbook) {
240                 // Check conditions
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) {
245                         // Throw NPE
246                         throw new NullPointerException("user is null"); //NOI18N
247                 } else if (user.getUserId() == null) {
248                         // Throw NPE again
249                         throw new NullPointerException("user.userId is null"); //NOI18N
250                 } else if (user.getUserId() < 1) {
251                         // Invalid id number
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) {
257                         // Throw NPE again
258                         throw new NullPointerException("addressbook is null"); //NOI18N
259                 } else if (addressbook.getAddressbookId() == null) {
260                         // Throw NPE again
261                         throw new NullPointerException("addressbook.addressbookId is null"); //NOI18N
262                 } else if (addressbook.getAddressbookId() < 1) {
263                         // Invalid id number
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) {
269                         // User is 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) {
272                         // User is invisible
273                         throw new FaceletException(MessageFormat.format("user {0} is invisible and cannot be selected for sharing.", user.getUserId())); //NOI18N
274                 }
275
276                 try {
277                         // Init sharing
278                         ShareableAddressbook shared = this.shareBean.startSharing(user, addressbook);
279
280                         // TODO Set it here
281                         this.setShare(shared);
282
283                         /// Trigger event
284                         this.sharingEvent.fire(new StartedAddressbookSharingEvent(shared));
285                 } catch (final UserAlreadySharingAddressbookException ex) {
286                         // Throw again
287                         throw new FaceletException(ex);
288                 }
289
290                 // TODO Unfinished
291                 return null;
292         }
293
294         /**
295          * Validates given event for all values and throws exceptions
296          * <p>
297          * @param event Event to validate
298          */
299         private void validateEvent (final AddressbookSharingEvent event) {
300                 if (null == event) {
301                         // Throw NPE
302                         throw new NullPointerException("event is null"); //NOI18N
303                 } else if (event.getSharingType() == null) {
304                         // Throw NPE
305                         throw new NullPointerException("event.sharingType is null"); //NOI18N
306                 } else if (event.getShareableAddressbook() == null) {
307                         // Throw NPE again
308                         throw new NullPointerException("event.shareableAddressbook is null"); //NOI18N
309                 } else if (event.getShareableAddressbook().getShareId() == null) {
310                         // Throw NPE again
311                         throw new NullPointerException("event.shareableAddressbook.shareId is null"); //NOI18N
312                 } else if (event.getShareableAddressbook().getShareId() < 1) {
313                         // Throw NPE again
314                         throw new IllegalArgumentException(MessageFormat.format("event.shareableAddressbook.shareId={0} is invalid", event.getShareableAddressbook().getShareId())); //NOI18N
315                 } else if (event.getShareableAddressbook().getShareAddressbook() == null) {
316                         // Throw NPE again
317                         throw new NullPointerException("event.shareableAddressbook.shareAddressbook is null"); //NOI18N
318                 } else if (event.getShareableAddressbook().getShareAddressbook().getAddressbookId() == null) {
319                         // Throw NPE again
320                         throw new NullPointerException("event.shareableAddressbook.shareAddressbook.addressbookId is null"); //NOI18N
321                 } else if (event.getShareableAddressbook().getShareAddressbook().getAddressbookId() < 1) {
322                         // Throw NPE again
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) {
325                         // Throw NPE again
326                         throw new NullPointerException("event.shareableAddressbook.shareUserOwner is null"); //NOI18N
327                 } else if (event.getShareableAddressbook().getShareUserOwner().getUserId() == null) {
328                         // Throw NPE again
329                         throw new NullPointerException("event.shareableAddressbook.shareUserOwner.userId is null"); //NOI18N
330                 } else if (event.getShareableAddressbook().getShareUserOwner().getUserId() < 1) {
331                         // Throw NPE again
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) {
334                         // Throw NPE again
335                         throw new NullPointerException("event.shareableAddressbook.shareUserSharee is null"); //NOI18N
336                 } else if (event.getShareableAddressbook().getShareUserSharee().getUserId() == null) {
337                         // Throw NPE again
338                         throw new NullPointerException("event.shareableAddressbook.shareUserSharee.userId is null"); //NOI18N
339                 } else if (event.getShareableAddressbook().getShareUserSharee().getUserId() < 1) {
340                         // Throw NPE again
341                         throw new IllegalArgumentException(MessageFormat.format("event.shareableAddressbook.shareUserSharee.userId={0} is invalid", event.getShareableAddressbook().getShareUserOwner().getUserId())); //NOI18N
342                 }
343         }
344 }