]> git.mxchange.org Git - jjobs-war.git/blob - src/java/org/mxchange/jjobs/beans/user/JobsUserWebSessionBean.java
Rewrites:
[jjobs-war.git] / src / java / org / mxchange / jjobs / beans / user / JobsUserWebSessionBean.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.jjobs.beans.user;
18
19 import java.text.MessageFormat;
20 import java.util.Iterator;
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.jcontacts.contact.Contact;
35 import org.mxchange.jjobs.beans.contact.JobsContactWebSessionController;
36 import org.mxchange.jjobs.beans.login.JobsUserLoginWebSessionController;
37 import org.mxchange.jphone.phonenumbers.cellphone.DialableCellphoneNumber;
38 import org.mxchange.jphone.phonenumbers.fax.DialableFaxNumber;
39 import org.mxchange.jphone.phonenumbers.landline.DialableLandLineNumber;
40 import org.mxchange.jusercore.events.login.UserLoggedInEvent;
41 import org.mxchange.jusercore.events.registration.UserRegisteredEvent;
42 import org.mxchange.jusercore.events.user.update.UpdatedUserPersonalDataEvent;
43 import org.mxchange.jusercore.events.user.update.UserUpdatedPersonalDataEvent;
44 import org.mxchange.jusercore.exceptions.UserNotFoundException;
45 import org.mxchange.jusercore.exceptions.UserPasswordMismatchException;
46 import org.mxchange.jusercore.model.user.LoginUser;
47 import org.mxchange.jusercore.model.user.User;
48 import org.mxchange.jusercore.model.user.UserSessionBeanRemote;
49 import org.mxchange.jusercore.model.user.profilemodes.ProfileMode;
50
51 /**
52  * A user bean (controller)
53  * <p>
54  * @author Roland Haeder<roland@mxchange.org>
55  */
56 @Named ("userController")
57 @SessionScoped
58 public class JobsUserWebSessionBean implements JobsUserWebSessionController {
59
60         /**
61          * Serial number
62          */
63         private static final long serialVersionUID = 542_145_347_916L;
64
65         /**
66          * General contact controller
67          */
68         @Inject
69         private JobsContactWebSessionController contactController;
70
71         /**
72          * Event being fired when user updated personal data
73          */
74         @Inject
75         @Any
76         private Event<UpdatedUserPersonalDataEvent> updatedPersonalDataEvent;
77
78         /**
79          * Remote user bean
80          */
81         private final UserSessionBeanRemote userBean;
82
83         /**
84          * User id
85          */
86         private Long userId;
87
88         /**
89          * Login bean (controller)
90          */
91         @Inject
92         private JobsUserLoginWebSessionController userLoginController;
93
94         /**
95          * User name
96          */
97         private String userName;
98
99         /**
100          * User password (unencrypted from web form)
101          */
102         private String userPassword;
103
104         /**
105          * User password repeated (unencrypted from web form)
106          */
107         private String userPasswordRepeat;
108
109         /**
110          * Whether the user wants a public profile
111          */
112         private ProfileMode userProfileMode;
113
114         /**
115          * A list of all public user profiles
116          */
117         private List<User> visibleUserList;
118
119         /**
120          * Default constructor
121          */
122         public JobsUserWebSessionBean () {
123                 // Try it
124                 try {
125                         // Get initial context
126                         Context context = new InitialContext();
127
128                         // Try to lookup
129                         this.userBean = (UserSessionBeanRemote) context.lookup("java:global/jjobs-ejb/user!org.mxchange.jusercore.model.user.UserSessionBeanRemote"); //NOI18N
130                 } catch (final NamingException e) {
131                         // Throw again
132                         throw new FaceletException(e);
133                 }
134         }
135
136         @Override
137         public void afterRegistrationEvent (@Observes final UserRegisteredEvent event) {
138                 // Trace message
139                 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("UserWebBean:afterRegistration: event={0} - CALLED!", event)); //NOI18N
140
141                 // event should not be null
142                 if (null == event) {
143                         // Throw NPE
144                         throw new NullPointerException("event is null"); //NOI18N
145                 } else if (event.getRegisteredUser() == null) {
146                         // Throw NPE again
147                         throw new NullPointerException("event.user is null"); //NOI18N
148                 } else if (event.getRegisteredUser().getUserId() == null) {
149                         // userId is null
150                         throw new NullPointerException("event.user.userId is null"); //NOI18N
151                 } else if (event.getRegisteredUser().getUserId() < 1) {
152                         // Not avalid id
153                         throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getRegisteredUser(), event.getRegisteredUser().getUserId())); //NOI18N
154                 }
155
156                 // Get user instance
157                 User registeredUser = event.getRegisteredUser();
158
159                 // Debug message
160                 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("UserWebBean:afterRegistration: registeredUser={0}", registeredUser)); //NOI18N
161
162                 // Copy all data from registered->user
163                 this.copyUser(registeredUser);
164
165                 // Clear all data
166                 this.clear();
167
168                 // Set user id again
169                 this.setUserId(registeredUser.getUserId());
170
171                 // Trace message
172                 //* NOISY-DEBUG: */ System.out.println("UserWebBean:afterRegistration: EXIT!"); //NOI18N
173         }
174
175         @Override
176         public void afterUserLogin (final @Observes UserLoggedInEvent event) {
177                 // Trace message
178                 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("UserWebBean:afterUserLogin: event={0} - CALLED!", event)); //NOI18N
179
180                 // event should not be null
181                 if (null == event) {
182                         // Throw NPE
183                         throw new NullPointerException("event is null"); //NOI18N
184                 } else if (event.getLoggedInUser() == null) {
185                         // Throw NPE again
186                         throw new NullPointerException("event.user is null"); //NOI18N
187                 } else if (event.getLoggedInUser().getUserId() == null) {
188                         // userId is null
189                         throw new NullPointerException("event.user.userId is null"); //NOI18N
190                 } else if (event.getLoggedInUser().getUserId() < 1) {
191                         // Not avalid id
192                         throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getLoggedInUser(), event.getLoggedInUser().getUserId())); //NOI18N
193                 }
194
195                 // Copy all data to this bean
196                 this.copyUser(event.getLoggedInUser());
197
198                 // Trace message
199                 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("UserWebBean:afterUserLogin: this.visibleUserList.size()={0} - EXIT!", this.visibleUserList.size())); //NOI18N
200         }
201
202         @Override
203         public User createUserInstance () {
204                 // User message
205                 //this.getLogger().logTrace("createUserInstance: CALLED!");
206
207                 // Required personal data must be set
208                 assert (this.isRequiredPersonalDataSet()) : "not all personal data is set"; //NOI18N
209
210                 // Create new user instance
211                 User localUser = new LoginUser();
212
213                 // Update all data ...
214                 localUser.setUserName(this.getUserName());
215                 localUser.setUserProfileMode(this.getUserProfileMode());
216
217                 // Create contact instance
218                 Contact contact = this.contactController.createContactInstance();
219
220                 // Set contact in user
221                 localUser.setUserContact(contact);
222
223                 // Trace message
224                 //this.getLogger().logTrace(MessageFormat.format("createUserInstance: user={0} - EXIT!", user));
225                 // Return it
226                 return localUser;
227         }
228
229         @Override
230         public String doChangePersonalData () {
231                 // This method shall only be called if the user is logged-in
232                 if (!this.userLoginController.isUserLoggedIn()) {
233                         // Not logged-in
234                         throw new IllegalStateException("User is not logged-in"); //NOI18N
235                 } else if (!this.isRequiredChangePersonalDataSet()) {
236                         // Not all required fields are set
237                         throw new FaceletException("Not all required fields are set."); //NOI18N
238                 } else if (!this.userLoginController.ifCurrentPasswordMatches()) {
239                         // Password not matching
240                         throw new FaceletException(new UserPasswordMismatchException(this.userLoginController.getLoggedInUser()));
241                 }
242
243                 // Get user instance
244                 User user = this.userLoginController.getLoggedInUser();
245
246                 // Copy contact data to contact instance
247                 this.contactController.updateContactDataFromController(user.getUserContact());
248
249                 // It should be there, so run some tests on it
250                 assert (user instanceof User) : "Instance userLoginController.loggedInUser is null"; //NOI18N
251                 assert (user.getUserId() instanceof Long) : "Instance userLoginController.loggedInUser.userId is null"; //NOI18N
252                 assert (user.getUserId() > 0) : MessageFormat.format("userLoginController.loggedInUser.userId={0} is invalid", user.getUserId()); //NOI18N
253                 assert (user.getUserContact() instanceof Contact) : "Instance userLoginController.loggedInUser.userContact is null"; //NOI18N
254                 assert (user.getUserContact().getContactId() instanceof Long) : "Instance userLoginController.userContact.contactId is null"; //NOI18N
255                 assert (user.getUserContact().getContactId() > 0) : MessageFormat.format("Instance userLoginController.userContact.contactId={0} is invalid", user.getUserContact().getContactId()); //NOI18N
256
257                 // Update all fields
258                 user.setUserProfileMode(this.getUserProfileMode());
259
260                 // Send it to the EJB
261                 User updatedUser = this.userBean.updateUserPersonalData(user);
262
263                 // Fire event
264                 this.updatedPersonalDataEvent.fire(new UserUpdatedPersonalDataEvent(updatedUser));
265
266                 // All fine
267                 return "user_data_saved"; //NOI18N
268         }
269
270         @Override
271         public Long getUserId () {
272                 return this.userId;
273         }
274
275         @Override
276         public void setUserId (final Long userId) {
277                 this.userId = userId;
278         }
279
280         @Override
281         public String getUserName () {
282                 return this.userName;
283         }
284
285         @Override
286         public void setUserName (final String userName) {
287                 this.userName = userName;
288         }
289
290         @Override
291         public String getUserPassword () {
292                 return this.userPassword;
293         }
294
295         @Override
296         public void setUserPassword (final String userPassword) {
297                 this.userPassword = userPassword;
298         }
299
300         @Override
301         public String getUserPasswordRepeat () {
302                 return this.userPasswordRepeat;
303         }
304
305         @Override
306         public void setUserPasswordRepeat (final String userPasswordRepeat) {
307                 this.userPasswordRepeat = userPasswordRepeat;
308         }
309
310         @Override
311         public ProfileMode getUserProfileMode () {
312                 return this.userProfileMode;
313         }
314
315         @Override
316         public void setUserProfileMode (final ProfileMode userProfileMode) {
317                 this.userProfileMode = userProfileMode;
318         }
319
320         /**
321          * Post-initialization of this class
322          */
323         @PostConstruct
324         public void init () {
325         }
326
327         @Override
328         public boolean isRequiredChangePersonalDataSet () {
329                 return ((this.getUserProfileMode() != null) &&
330                                 (this.getUserName() != null) && (!this.getUserName().isEmpty()) &&
331                                 (this.contactController.isRequiredChangePersonalDataSet()));
332         }
333
334         @Override
335         public boolean isRequiredPersonalDataSet () {
336                 return ((this.getUserName() != null) &&
337                                 (this.getUserProfileMode() != null) &&
338                                 (this.contactController.isRequiredPersonalDataSet()) &&
339                                 (this.getUserPassword() != null) &&
340                                 (this.getUserPasswordRepeat() != null));
341         }
342
343         @Override
344         public boolean isSamePasswordEntered () {
345                 return ((!this.getUserPassword().isEmpty()) && (Objects.equals(this.getUserPassword(), this.getUserPasswordRepeat())));
346         }
347
348         @Override
349         public boolean isUserIdEmpty () {
350                 return ((this.getUserId() == null) || (this.getUserId() == 0));
351         }
352
353         @Override
354         public User lookupUserById (final Long userId) throws UserNotFoundException {
355                 // Init variable
356                 User localUser = null;
357
358                 // Clear this bean
359                 this.clear();
360
361                 // Try to lookup it in visible user list
362                 for (final Iterator<User> iterator = this.visibleUserList.iterator(); iterator.hasNext();) {
363                         // Get next user
364                         User next = iterator.next();
365
366                         // Is the user id found?
367                         if (Objects.equals(next.getUserId(), userId)) {
368                                 // Copy to other variable
369                                 localUser = next;
370                                 break;
371                         }
372                 }
373
374                 // Is it still null?
375                 if (null == localUser) {
376                         // Not visible for the current user
377                         throw new UserNotFoundException(userId);
378                 }
379
380                 // Copy all data to this bean
381                 this.copyUser(localUser);
382
383                 // Return it
384                 return localUser;
385         }
386
387         /**
388          * Clears this bean
389          */
390         private void clear () {
391                 // Clear all data
392                 // - personal data
393                 this.setUserId(null);
394                 this.setUserProfileMode(null);
395
396                 // - other data
397                 this.setUserName(null);
398                 this.setUserPassword(null);
399                 this.setUserPasswordRepeat(null);
400         }
401
402         /**
403          * Copies given user into the controller
404          * <p>
405          * @param user User instance
406          */
407         private void copyUser (final User user) {
408                 // Copy all fields:
409                 // - base data
410                 this.setUserId(user.getUserId());
411                 this.setUserProfileMode(user.getUserProfileMode());
412
413                 // Get cellphone, phone and fax instance
414                 DialableCellphoneNumber cellphone = user.getUserContact().getContactCellphoneNumber();
415                 DialableFaxNumber fax = user.getUserContact().getContactFaxNumber();
416                 DialableLandLineNumber phone = user.getUserContact().getContactLandLineNumber();
417         }
418
419 }