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