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