]> git.mxchange.org Git - addressbook-war.git/blob - src/java/org/mxchange/addressbook/beans/user/UserWebBean.java
updated jar(s) + fixed method calls + template
[addressbook-war.git] / src / java / org / mxchange / addressbook / beans / user / UserWebBean.java
1 /*
2  * Copyright (C) 2015 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.user;
18
19 import java.text.MessageFormat;
20 import java.util.Date;
21 import java.util.GregorianCalendar;
22 import java.util.List;
23 import java.util.Objects;
24 import javax.annotation.PostConstruct;
25 import javax.enterprise.context.SessionScoped;
26 import javax.faces.view.facelets.FaceletException;
27 import javax.inject.Named;
28 import javax.naming.Context;
29 import javax.naming.InitialContext;
30 import javax.naming.NamingException;
31 import org.mxchange.jcontacts.contact.Contact;
32 import org.mxchange.jcontacts.contact.UserContact;
33 import org.mxchange.jcontacts.contact.gender.Gender;
34 import org.mxchange.jcountry.data.Country;
35 import org.mxchange.jphone.phonenumbers.cellphone.CellphoneNumber;
36 import org.mxchange.jphone.phonenumbers.cellphone.DialableCellphoneNumber;
37 import org.mxchange.jphone.phonenumbers.fax.DialableFaxNumber;
38 import org.mxchange.jphone.phonenumbers.fax.FaxNumber;
39 import org.mxchange.jphone.phonenumbers.landline.DialableLandLineNumber;
40 import org.mxchange.jphone.phonenumbers.landline.LandLineNumber;
41 import org.mxchange.jphone.phonenumbers.smsprovider.SmsProvider;
42 import org.mxchange.jusercore.model.user.LoginUser;
43 import org.mxchange.jusercore.model.user.User;
44 import org.mxchange.jusercore.model.user.UserSessionBeanRemote;
45
46 /**
47  * A user bean (controller)
48  * <p>
49  * @author Roland Haeder<roland@mxchange.org>
50  */
51 @Named ("userController")
52 @SessionScoped
53 public class UserWebBean implements UserWebController {
54
55         /**
56          * Serial number
57          */
58         private static final long serialVersionUID = 542_145_347_916L;
59
60         /////////////////////// Properties /////////////////////
61         /**
62          * Birth day
63          */
64         private Date birthday;
65
66         /**
67          * Cellphone number's carrier
68          */
69         private SmsProvider cellphoneCarrier;
70
71         /**
72          * Cellphone number
73          */
74         private Long cellphoneNumber;
75
76         /**
77          * City
78          */
79         private String city;
80
81         /**
82          * Optional comments
83          */
84         private String comment;
85
86         /**
87          * Country instance
88          */
89         private Country country;
90
91         /**
92          * Email address
93          */
94         private String emailAddress;
95
96         /**
97          * Email address list
98          */
99         private List<String> emailAddressList;
100
101         /**
102          * Email address repeated
103          */
104         private String emailAddressRepeat;
105
106         /**
107          * Family name
108          */
109         private String familyName;
110
111         /**
112          * Fax number's area code
113          */
114         private Integer faxAreaCode;
115
116         /**
117          * Country instance for fax number
118          */
119         private Country faxCountry;
120
121         /**
122          * Fax number
123          */
124         private Long faxNumber;
125
126         /**
127          * First name
128          */
129         private String firstName;
130
131         /**
132          * Gender instance
133          */
134         private Gender gender;
135
136         /**
137          * House number
138          */
139         private Short houseNumber;
140
141         /**
142          * Phone number area code
143          */
144         private Integer phoneAreaCode;
145
146         /**
147          * Country instance for phone number
148          */
149         private Country phoneCountry;
150
151         /**
152          * Phone number
153          */
154         private Long phoneNumber;
155
156         /**
157          * Street
158          */
159         private String street;
160
161         /**
162          * Remote user bean
163          */
164         private final UserSessionBeanRemote userBean;
165
166         /**
167          * User id
168          */
169         private Long userId;
170
171         /**
172          * User name
173          */
174         private String userName;
175
176         /**
177          * User name list
178          */
179         private List<String> userNameList;
180
181         /**
182          * User password (unencrypted from web form)
183          */
184         private String userPassword;
185
186         /**
187          * User password repeated (unencrypted from web form)
188          */
189         private String userPasswordRepeat;
190
191         /**
192          * ZIP code
193          */
194         private Integer zipCode;
195
196         /**
197          * Default constructor
198          */
199         public UserWebBean () {
200                 // Set gender to UNKNOWN
201                 this.gender = Gender.UNKNOWN;
202
203                 // Try it
204                 try {
205                         // Get initial context
206                         Context context = new InitialContext();
207
208                         // Try to lookup
209                         this.userBean = (UserSessionBeanRemote) context.lookup("ejb/stateless-user"); //NOI18N
210                 } catch (final NamingException e) {
211                         // Throw again
212                         throw new FaceletException(e);
213                 }
214         }
215
216         @Override
217         public void addUserNameEmailAddress (final User user) {
218                 // Make sure the entry is not added yet
219                 if (this.userNameList.contains(user.getUserName())) {
220                         // Abort here
221                         throw new IllegalArgumentException(MessageFormat.format("User name {0} already added.", user.getUserName()));
222                 } else if (this.emailAddressList.contains(user.getUserContact().getContactEmailAddress())) {
223                         // Already added
224                         throw new IllegalArgumentException(MessageFormat.format("Email address {0} already added.", user.getUserContact().getContactEmailAddress()));
225                 }
226
227                 // Add user name
228                 this.userNameList.add(user.getUserName());
229
230                 // Add email addres
231                 this.emailAddressList.add(user.getUserContact().getContactEmailAddress());
232         }
233
234         @Override
235         public void clearData () {
236                 // Clear all data
237                 // - personal data
238                 this.setUserId(null);
239                 this.setGender(Gender.UNKNOWN);
240                 this.setFirstName(null);
241                 this.setFamilyName(null);
242                 this.setStreet(null);
243                 this.setHouseNumber(null);
244                 this.setZipCode(null);
245                 this.setCity(null);
246                 this.setCountry(null);
247
248                 // - contact data
249                 this.setEmailAddress(null);
250                 this.setEmailAddressRepeat(null);
251                 this.setPhoneAreaCode(null);
252                 this.setCellphoneCarrier(null);
253                 this.setFaxAreaCode(null);
254
255                 // - other data
256                 this.setBirthday(null);
257                 this.setComment(null);
258                 this.setUserName(null);
259                 this.setUserPassword(null);
260                 this.setUserPasswordRepeat(null);
261         }
262
263         @Override
264         public void copyUser (final User user) {
265                 // Copy all fields:
266                 // - base data
267                 this.setUserId(user.getUserId());
268                 this.setGender(user.getUserContact().getContactGender());
269                 this.setFirstName(user.getUserContact().getContactFirstName());
270                 this.setFamilyName(user.getUserContact().getContactFamilyName());
271                 this.setStreet(user.getUserContact().getContactStreet());
272                 this.setHouseNumber(user.getUserContact().getContactHouseNumber());
273                 this.setZipCode(user.getUserContact().getContactZipCode());
274                 this.setCity(user.getUserContact().getContactCity());
275                 this.setCountry(user.getUserContact().getContactCountry());
276
277                 // Get cellphone, phone and fax instance
278                 DialableCellphoneNumber cellphone = user.getUserContact().getContactCellphoneNumber();
279                 DialableFaxNumber fax = user.getUserContact().getContactFaxNumber();
280                 DialableLandLineNumber phone = user.getUserContact().getContactPhoneNumber();
281
282                 // - contact data
283                 if (phone instanceof DialableLandLineNumber) {
284                         this.setPhoneCountry(phone.getPhoneCountry());
285                         this.setPhoneAreaCode(phone.getPhoneAreaCode());
286                         this.setPhoneNumber(phone.getPhoneNumber());
287                 }
288                 if (cellphone instanceof DialableCellphoneNumber) {
289                         this.setCellphoneCarrier(cellphone.getCellphoneProvider());
290                         this.setCellphoneNumber(cellphone.getPhoneNumber());
291                 }
292                 if (fax instanceof DialableFaxNumber) {
293                         this.setFaxCountry(fax.getPhoneCountry());
294                         this.setFaxAreaCode(fax.getPhoneAreaCode());
295                         this.setFaxNumber(fax.getPhoneNumber());
296                 }
297                 this.setEmailAddress(user.getUserContact().getContactEmailAddress());
298
299                 // -- other data
300                 this.setBirthday(user.getUserContact().getContactBirthday());
301                 this.setComment(user.getUserContact().getContactComment());
302         }
303
304         @Override
305         public User createUserInstance () {
306                 // User message
307                 //this.getLogger().logTrace("createUserInstance: CALLED!");
308
309                 // Required personal data must be set
310                 assert (this.isRequiredPersonalDataSet()) : "not all personal data is set"; //NOI18N
311
312                 // Create new user instance
313                 User user = new LoginUser();
314                 user.setUserName(this.getUserName());
315
316                 // Generate phone number
317                 DialableLandLineNumber phone = new LandLineNumber(this.getPhoneCountry(), this.getPhoneAreaCode(), this.getPhoneNumber());
318                 DialableCellphoneNumber cellphone = new CellphoneNumber(this.getCellphoneCarrier(), this.getCellphoneNumber());
319                 DialableFaxNumber fax = new FaxNumber(this.getFaxCountry(), this.getFaxAreaCode(), this.getFaxNumber());
320
321                 // Create new contact
322                 Contact contact = new UserContact(this.getGender(), this.getFirstName(), this.getFamilyName());
323                 contact.setContactStreet(this.getStreet());
324                 contact.setContactHouseNumber(this.getHouseNumber());
325                 contact.setContactZipCode(this.getZipCode());
326                 contact.setContactCity(this.getCity());
327                 contact.setContactCountry(this.getCountry());
328                 contact.setContactEmailAddress(this.getEmailAddress());
329                 contact.setContactPhoneNumber(phone);
330                 contact.setContactFaxNumber(fax);
331                 contact.setContactCellphoneNumber(cellphone);
332                 contact.setContactBirthday(this.getBirthday());
333                 contact.setContactComment(this.getComment());
334
335                 // Created timestamp and ownContact
336                 contact.setContactCreated(new GregorianCalendar());
337                 contact.setContactOwnContact(Boolean.TRUE);
338
339                 // Set contact in user
340                 user.setUserContact(contact);
341                 user.setUserCreated(new GregorianCalendar());
342
343                 // Trace message
344                 //this.getLogger().logTrace(MessageFormat.format("createUserInstance: user={0} - EXIT!", user));
345                 // Return it
346                 return user;
347         }
348
349         @Override
350         public Date getBirthday () {
351                 return this.birthday;
352         }
353
354         @Override
355         public void setBirthday (final Date birthday) {
356                 this.birthday = birthday;
357         }
358
359         @Override
360         public SmsProvider getCellphoneCarrier () {
361                 return this.cellphoneCarrier;
362         }
363
364         @Override
365         public void setCellphoneCarrier (final SmsProvider cellphoneCarrier) {
366                 this.cellphoneCarrier = cellphoneCarrier;
367         }
368
369         @Override
370         public Long getCellphoneNumber () {
371                 return this.cellphoneNumber;
372         }
373
374         @Override
375         public void setCellphoneNumber (Long cellphoneNumber) {
376                 this.cellphoneNumber = cellphoneNumber;
377         }
378
379         @Override
380         public String getCity () {
381                 return this.city;
382         }
383
384         @Override
385         public void setCity (final String city) {
386                 this.city = city;
387         }
388
389         @Override
390         public String getComment () {
391                 return this.comment;
392         }
393
394         @Override
395         public void setComment (final String comment) {
396                 this.comment = comment;
397         }
398
399         @Override
400         public Country getCountry () {
401                 return this.country;
402         }
403
404         @Override
405         public void setCountry (final Country country) {
406                 this.country = country;
407         }
408
409         @Override
410         public String getEmailAddress () {
411                 return this.emailAddress;
412         }
413
414         @Override
415         public void setEmailAddress (final String emailAddress) {
416                 this.emailAddress = emailAddress;
417         }
418
419         @Override
420         public String getEmailAddressRepeat () {
421                 return this.emailAddressRepeat;
422         }
423
424         @Override
425         public void setEmailAddressRepeat (final String emailAddressRepeat) {
426                 this.emailAddressRepeat = emailAddressRepeat;
427         }
428
429         @Override
430         public String getFamilyName () {
431                 return this.familyName;
432         }
433
434         @Override
435         public void setFamilyName (final String familyName) {
436                 this.familyName = familyName;
437         }
438
439         @Override
440         public Integer getFaxAreaCode () {
441                 return this.faxAreaCode;
442         }
443
444         @Override
445         public void setFaxAreaCode (final Integer faxAreaCode) {
446                 this.faxAreaCode = faxAreaCode;
447         }
448
449         @Override
450         public Country getFaxCountry () {
451                 return this.faxCountry;
452         }
453
454         @Override
455         public void setFaxCountry (final Country faxCountry) {
456                 this.faxCountry = faxCountry;
457         }
458
459         @Override
460         public Long getFaxNumber () {
461                 return this.faxNumber;
462         }
463
464         @Override
465         public void setFaxNumber (final Long faxNumber) {
466                 this.faxNumber = faxNumber;
467         }
468
469         @Override
470         public String getFirstName () {
471                 return this.firstName;
472         }
473
474         @Override
475         public void setFirstName (final String firstName) {
476                 this.firstName = firstName;
477         }
478
479         @Override
480         public Gender getGender () {
481                 return this.gender;
482         }
483
484         @Override
485         public void setGender (final Gender gender) {
486                 this.gender = gender;
487         }
488
489         @Override
490         public Short getHouseNumber () {
491                 return this.houseNumber;
492         }
493
494         @Override
495         public void setHouseNumber (final Short houseNumber) {
496                 this.houseNumber = houseNumber;
497         }
498
499         @Override
500         public Integer getPhoneAreaCode () {
501                 return this.phoneAreaCode;
502         }
503
504         @Override
505         public void setPhoneAreaCode (final Integer phoneAreaCode) {
506                 this.phoneAreaCode = phoneAreaCode;
507         }
508
509         @Override
510         public Country getPhoneCountry () {
511                 return this.phoneCountry;
512         }
513
514         @Override
515         public void setPhoneCountry (final Country phoneCountry) {
516                 this.phoneCountry = phoneCountry;
517         }
518
519         @Override
520         public Long getPhoneNumber () {
521                 return this.phoneNumber;
522         }
523
524         @Override
525         public void setPhoneNumber (final Long phoneNumber) {
526                 this.phoneNumber = phoneNumber;
527         }
528
529         @Override
530         public String getStreet () {
531                 return this.street;
532         }
533
534         @Override
535         public void setStreet (final String street) {
536                 this.street = street;
537         }
538
539         @Override
540         public Long getUserId () {
541                 return this.userId;
542         }
543
544         @Override
545         public void setUserId (final Long userId) {
546                 this.userId = userId;
547         }
548
549         @Override
550         public String getUserName () {
551                 return this.userName;
552         }
553
554         @Override
555         public void setUserName (final String userName) {
556                 this.userName = userName;
557         }
558
559         @Override
560         public String getUserPassword () {
561                 return this.userPassword;
562         }
563
564         @Override
565         public void setUserPassword (final String userPassword) {
566                 this.userPassword = userPassword;
567         }
568
569         @Override
570         public String getUserPasswordRepeat () {
571                 return this.userPasswordRepeat;
572         }
573
574         @Override
575         public void setUserPasswordRepeat (final String userPasswordRepeat) {
576                 this.userPasswordRepeat = userPasswordRepeat;
577         }
578
579         @Override
580         public Integer getZipCode () {
581                 return this.zipCode;
582         }
583
584         @Override
585         public void setZipCode (final Integer zipCode) {
586                 this.zipCode = zipCode;
587         }
588
589         @PostConstruct
590         public void init () {
591                 // Get full user name list for reducing EJB calls
592                 this.userNameList = this.userBean.getUserNameList();
593
594                 // Get full email address list for reducing EJB calls
595                 this.emailAddressList = this.userBean.getEmailAddressList();
596         }
597
598         @Override
599         public boolean isEmailAddressRegistered (final User user) {
600                 return ((this.emailAddressList instanceof List) && (this.emailAddressList.contains(user.getUserContact().getContactEmailAddress())));
601         }
602
603         @Override
604         public boolean isRequiredPersonalDataSet () {
605                 return ((this.getUserName() != null)
606                                 && (this.getGender() != null)
607                                 && (this.getFirstName() != null)
608                                 && (this.getFamilyName() != null)
609                                 && (this.getStreet() != null)
610                                 && (this.getHouseNumber() != null)
611                                 && (this.getZipCode() != null)
612                                 && (this.getCity() != null)
613                                 && (this.getEmailAddress() != null)
614                                 && (this.getEmailAddressRepeat() != null)
615                                 && (this.getUserPassword() != null)
616                                 && (this.getUserPasswordRepeat() != null));
617         }
618
619         @Override
620         public boolean isSameEmailAddressEntered () {
621                 return (Objects.equals(this.getEmailAddress(), this.getEmailAddressRepeat()));
622         }
623
624         @Override
625         public boolean isSamePasswordEntered () {
626                 return (Objects.equals(this.getUserPassword(), this.getUserPasswordRepeat()));
627         }
628
629         @Override
630         public boolean isUserNameRegistered (final User user) {
631                 return ((this.userNameList instanceof List) && (this.userNameList.contains(user.getUserName())));
632         }
633 }