]> git.mxchange.org Git - addressbook-war.git/blob - src/java/org/mxchange/addressbook/beans/user/UserWebBean.java
Compare against null
[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) && (phone.getPhoneAreaCode() > 0)) {
284                         this.setPhoneCountry(phone.getPhoneCountry());
285                         this.setPhoneAreaCode(phone.getPhoneAreaCode());
286                         this.setPhoneNumber(phone.getPhoneNumber());
287                 }
288                 if ((cellphone instanceof DialableCellphoneNumber) && (cellphone.getCellphoneProvider() instanceof SmsProvider)) {
289                         this.setCellphoneCarrier(cellphone.getCellphoneProvider());
290                         this.setCellphoneNumber(cellphone.getPhoneNumber());
291                 }
292                 if ((fax instanceof DialableFaxNumber) && (fax.getPhoneAreaCode() > 0)) {
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
330                 // Don't set null or wrong references
331                 if ((phone instanceof DialableLandLineNumber) && (phone.getPhoneCountry() instanceof Country) && (this.getPhoneAreaCode() != null) && (this.getPhoneNumber() != null) && (this.getPhoneAreaCode() > 0) &&(this.getPhoneNumber() > 0)) {
332                         // Now the number must be given
333                         if (phone.getPhoneAreaCode() == null) {
334                                 // Is null
335                                 throw new NullPointerException("phone.phoneAreaCode is null");
336                         } else if (phone.getPhoneAreaCode() < 1) {
337                                 // Abort here
338                                 throw new IllegalArgumentException("phone.phoneAreaCode is zero or below.");
339                         } else if (phone.getPhoneNumber() == null) {
340                                 // Is null
341                                 throw new NullPointerException("phone.phoneNumber is null");
342                         } else if (phone.getPhoneNumber() < 1) {
343                                 // Abort here
344                                 throw new IllegalArgumentException("phone.phoneNumber is zero or below.");
345                         }
346
347                         // Set phone number
348                         contact.setContactPhoneNumber(phone);
349                 }
350
351                 // Don't set null or wrong references
352                 if ((fax instanceof DialableFaxNumber) && (fax.getPhoneCountry() instanceof Country) && (this.getFaxAreaCode() != null) && (this.getFaxNumber() != null) && (this.getFaxAreaCode() > 0) && (this.getFaxNumber() > 0)) {
353                         // Now the number must be given
354                         if (fax.getPhoneAreaCode() == null) {
355                                 // Is null
356                                 throw new NullPointerException("fax.phoneAreaCode is null");
357                         } else if (fax.getPhoneAreaCode() < 1) {
358                                 // Abort here
359                                 throw new IllegalArgumentException("fax.phoneAreaCode is zero or below.");
360                         } else if (fax.getPhoneNumber() == null) {
361                                 // Is null
362                                 throw new NullPointerException("fax.phoneNumber is null");
363                         } else if (fax.getPhoneNumber() < 1) {
364                                 // Abort here
365                                 throw new IllegalArgumentException("fax.phoneNumber is zero or below.");
366                         }
367
368                         // Set fax number
369                         contact.setContactFaxNumber(fax);
370                 }
371
372                 // Is the provider set?
373                 if ((cellphone instanceof DialableCellphoneNumber) && (this.getCellphoneCarrier() instanceof SmsProvider) && (this.getCellphoneNumber() != null) && (this.getCellphoneNumber() > 0)) {
374                         // Is the number set?
375                         if (cellphone.getPhoneNumber() == null) {
376                                 // Is null
377                                 throw new NullPointerException("cellphone.phoneNumber is null");
378                         } else if (cellphone.getPhoneNumber() < 1) {
379                                 // Abort here
380                                 throw new IllegalArgumentException("cellphone.phoneNumber is zero or below.");
381                         }
382
383                         // Set cellphone number
384                         contact.setContactCellphoneNumber(cellphone);
385                 }
386
387                 contact.setContactBirthday(this.getBirthday());
388                 contact.setContactComment(this.getComment());
389
390                 // Created timestamp and ownContact
391                 contact.setContactCreated(new GregorianCalendar());
392                 contact.setContactOwnContact(Boolean.TRUE);
393
394                 // Set contact in user
395                 user.setUserContact(contact);
396                 user.setUserCreated(new GregorianCalendar());
397
398                 // Trace message
399                 //this.getLogger().logTrace(MessageFormat.format("createUserInstance: user={0} - EXIT!", user));
400                 // Return it
401                 return user;
402         }
403
404         @Override
405         public Date getBirthday () {
406                 return this.birthday;
407         }
408
409         @Override
410         public void setBirthday (final Date birthday) {
411                 this.birthday = birthday;
412         }
413
414         @Override
415         public SmsProvider getCellphoneCarrier () {
416                 return this.cellphoneCarrier;
417         }
418
419         @Override
420         public void setCellphoneCarrier (final SmsProvider cellphoneCarrier) {
421                 this.cellphoneCarrier = cellphoneCarrier;
422         }
423
424         @Override
425         public Long getCellphoneNumber () {
426                 return this.cellphoneNumber;
427         }
428
429         @Override
430         public void setCellphoneNumber (Long cellphoneNumber) {
431                 this.cellphoneNumber = cellphoneNumber;
432         }
433
434         @Override
435         public String getCity () {
436                 return this.city;
437         }
438
439         @Override
440         public void setCity (final String city) {
441                 this.city = city;
442         }
443
444         @Override
445         public String getComment () {
446                 return this.comment;
447         }
448
449         @Override
450         public void setComment (final String comment) {
451                 this.comment = comment;
452         }
453
454         @Override
455         public Country getCountry () {
456                 return this.country;
457         }
458
459         @Override
460         public void setCountry (final Country country) {
461                 this.country = country;
462         }
463
464         @Override
465         public String getEmailAddress () {
466                 return this.emailAddress;
467         }
468
469         @Override
470         public void setEmailAddress (final String emailAddress) {
471                 this.emailAddress = emailAddress;
472         }
473
474         @Override
475         public String getEmailAddressRepeat () {
476                 return this.emailAddressRepeat;
477         }
478
479         @Override
480         public void setEmailAddressRepeat (final String emailAddressRepeat) {
481                 this.emailAddressRepeat = emailAddressRepeat;
482         }
483
484         @Override
485         public String getFamilyName () {
486                 return this.familyName;
487         }
488
489         @Override
490         public void setFamilyName (final String familyName) {
491                 this.familyName = familyName;
492         }
493
494         @Override
495         public Integer getFaxAreaCode () {
496                 return this.faxAreaCode;
497         }
498
499         @Override
500         public void setFaxAreaCode (final Integer faxAreaCode) {
501                 this.faxAreaCode = faxAreaCode;
502         }
503
504         @Override
505         public Country getFaxCountry () {
506                 return this.faxCountry;
507         }
508
509         @Override
510         public void setFaxCountry (final Country faxCountry) {
511                 this.faxCountry = faxCountry;
512         }
513
514         @Override
515         public Long getFaxNumber () {
516                 return this.faxNumber;
517         }
518
519         @Override
520         public void setFaxNumber (final Long faxNumber) {
521                 this.faxNumber = faxNumber;
522         }
523
524         @Override
525         public String getFirstName () {
526                 return this.firstName;
527         }
528
529         @Override
530         public void setFirstName (final String firstName) {
531                 this.firstName = firstName;
532         }
533
534         @Override
535         public Gender getGender () {
536                 return this.gender;
537         }
538
539         @Override
540         public void setGender (final Gender gender) {
541                 this.gender = gender;
542         }
543
544         @Override
545         public Short getHouseNumber () {
546                 return this.houseNumber;
547         }
548
549         @Override
550         public void setHouseNumber (final Short houseNumber) {
551                 this.houseNumber = houseNumber;
552         }
553
554         @Override
555         public Integer getPhoneAreaCode () {
556                 return this.phoneAreaCode;
557         }
558
559         @Override
560         public void setPhoneAreaCode (final Integer phoneAreaCode) {
561                 this.phoneAreaCode = phoneAreaCode;
562         }
563
564         @Override
565         public Country getPhoneCountry () {
566                 return this.phoneCountry;
567         }
568
569         @Override
570         public void setPhoneCountry (final Country phoneCountry) {
571                 this.phoneCountry = phoneCountry;
572         }
573
574         @Override
575         public Long getPhoneNumber () {
576                 return this.phoneNumber;
577         }
578
579         @Override
580         public void setPhoneNumber (final Long phoneNumber) {
581                 this.phoneNumber = phoneNumber;
582         }
583
584         @Override
585         public String getStreet () {
586                 return this.street;
587         }
588
589         @Override
590         public void setStreet (final String street) {
591                 this.street = street;
592         }
593
594         @Override
595         public Long getUserId () {
596                 return this.userId;
597         }
598
599         @Override
600         public void setUserId (final Long userId) {
601                 this.userId = userId;
602         }
603
604         @Override
605         public String getUserName () {
606                 return this.userName;
607         }
608
609         @Override
610         public void setUserName (final String userName) {
611                 this.userName = userName;
612         }
613
614         @Override
615         public String getUserPassword () {
616                 return this.userPassword;
617         }
618
619         @Override
620         public void setUserPassword (final String userPassword) {
621                 this.userPassword = userPassword;
622         }
623
624         @Override
625         public String getUserPasswordRepeat () {
626                 return this.userPasswordRepeat;
627         }
628
629         @Override
630         public void setUserPasswordRepeat (final String userPasswordRepeat) {
631                 this.userPasswordRepeat = userPasswordRepeat;
632         }
633
634         @Override
635         public Integer getZipCode () {
636                 return this.zipCode;
637         }
638
639         @Override
640         public void setZipCode (final Integer zipCode) {
641                 this.zipCode = zipCode;
642         }
643
644         @PostConstruct
645         public void init () {
646                 // Get full user name list for reducing EJB calls
647                 this.userNameList = this.userBean.getUserNameList();
648
649                 // Get full email address list for reducing EJB calls
650                 this.emailAddressList = this.userBean.getEmailAddressList();
651         }
652
653         @Override
654         public boolean isEmailAddressRegistered (final User user) {
655                 return ((this.emailAddressList instanceof List) && (this.emailAddressList.contains(user.getUserContact().getContactEmailAddress())));
656         }
657
658         @Override
659         public boolean isRequiredPersonalDataSet () {
660                 return ((this.getUserName() != null)
661                                 && (this.getGender() != null)
662                                 && (this.getFirstName() != null)
663                                 && (this.getFamilyName() != null)
664                                 && (this.getStreet() != null)
665                                 && (this.getHouseNumber() != null)
666                                 && (this.getZipCode() != null)
667                                 && (this.getCity() != null)
668                                 && (this.getEmailAddress() != null)
669                                 && (this.getEmailAddressRepeat() != null)
670                                 && (this.getUserPassword() != null)
671                                 && (this.getUserPasswordRepeat() != null));
672         }
673
674         @Override
675         public boolean isSameEmailAddressEntered () {
676                 return (Objects.equals(this.getEmailAddress(), this.getEmailAddressRepeat()));
677         }
678
679         @Override
680         public boolean isSamePasswordEntered () {
681                 return (Objects.equals(this.getUserPassword(), this.getUserPasswordRepeat()));
682         }
683
684         @Override
685         public boolean isUserNameRegistered (final User user) {
686                 return ((this.userNameList instanceof List) && (this.userNameList.contains(user.getUserName())));
687         }
688 }