2 * Copyright (C) 2015 Roland Haeder
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.
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.
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/>.
17 package org.mxchange.addressbook.beans.user;
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;
47 * A user bean (controller)
49 * @author Roland Haeder<roland@mxchange.org>
51 @Named ("userController")
53 public class UserWebBean implements UserWebController {
58 private static final long serialVersionUID = 542_145_347_916L;
60 /////////////////////// Properties /////////////////////
64 private Date birthday;
67 * Cellphone number's carrier
69 private SmsProvider cellphoneCarrier;
74 private Long cellphoneNumber;
84 private String comment;
89 private Country country;
94 private String emailAddress;
99 private List<String> emailAddressList;
102 * Email address repeated
104 private String emailAddressRepeat;
109 private String familyName;
112 * Fax number's area code
114 private Integer faxAreaCode;
117 * Country instance for fax number
119 private Country faxCountry;
124 private Long faxNumber;
129 private String firstName;
134 private Gender gender;
139 private Short houseNumber;
142 * Phone number area code
144 private Integer phoneAreaCode;
147 * Country instance for phone number
149 private Country phoneCountry;
154 private Long phoneNumber;
159 private String street;
164 private final UserSessionBeanRemote userBean;
174 private String userName;
179 private List<String> userNameList;
182 * User password (unencrypted from web form)
184 private String userPassword;
187 * User password repeated (unencrypted from web form)
189 private String userPasswordRepeat;
194 private Integer zipCode;
197 * Default constructor
199 public UserWebBean () {
200 // Set gender to UNKNOWN
201 this.gender = Gender.UNKNOWN;
205 // Get initial context
206 Context context = new InitialContext();
209 this.userBean = (UserSessionBeanRemote) context.lookup("ejb/stateless-user"); //NOI18N
210 } catch (final NamingException e) {
212 throw new FaceletException(e);
217 public void addUserNameEmailAddress (final User user) {
218 // Make sure the entry is not added yet
219 if (this.userNameList.contains(user.getUserName())) {
221 throw new IllegalArgumentException(MessageFormat.format("User name {0} already added.", user.getUserName()));
222 } else if (this.emailAddressList.contains(user.getUserContact().getContactEmailAddress())) {
224 throw new IllegalArgumentException(MessageFormat.format("Email address {0} already added.", user.getUserContact().getContactEmailAddress()));
228 this.userNameList.add(user.getUserName());
231 this.emailAddressList.add(user.getUserContact().getContactEmailAddress());
235 public void clearData () {
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);
246 this.setCountry(null);
249 this.setEmailAddress(null);
250 this.setEmailAddressRepeat(null);
251 this.setPhoneAreaCode(null);
252 this.setCellphoneCarrier(null);
253 this.setFaxAreaCode(null);
256 this.setBirthday(null);
257 this.setComment(null);
258 this.setUserName(null);
259 this.setUserPassword(null);
260 this.setUserPasswordRepeat(null);
264 public void copyUser (final User user) {
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());
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();
283 if ((phone instanceof DialableLandLineNumber) && (phone.getPhoneAreaCode() > 0)) {
284 this.setPhoneCountry(phone.getPhoneCountry());
285 this.setPhoneAreaCode(phone.getPhoneAreaCode());
286 this.setPhoneNumber(phone.getPhoneNumber());
288 if ((cellphone instanceof DialableCellphoneNumber) && (cellphone.getCellphoneProvider() instanceof SmsProvider)) {
289 this.setCellphoneCarrier(cellphone.getCellphoneProvider());
290 this.setCellphoneNumber(cellphone.getPhoneNumber());
292 if ((fax instanceof DialableFaxNumber) && (fax.getPhoneAreaCode() > 0)) {
293 this.setFaxCountry(fax.getPhoneCountry());
294 this.setFaxAreaCode(fax.getPhoneAreaCode());
295 this.setFaxNumber(fax.getPhoneNumber());
297 this.setEmailAddress(user.getUserContact().getContactEmailAddress());
300 this.setBirthday(user.getUserContact().getContactBirthday());
301 this.setComment(user.getUserContact().getContactComment());
305 public User createUserInstance () {
307 //this.getLogger().logTrace("createUserInstance: CALLED!");
309 // Required personal data must be set
310 assert (this.isRequiredPersonalDataSet()) : "not all personal data is set"; //NOI18N
312 // Create new user instance
313 User user = new LoginUser();
314 user.setUserName(this.getUserName());
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());
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());
330 // Don't set null or wrong references
331 if ((phone instanceof DialableLandLineNumber) && (phone.getPhoneCountry() instanceof Country) && (this.getPhoneAreaCode() > 0) &&(this.getPhoneNumber() > 0)) {
332 // Now the number must be given
333 if (phone.getPhoneAreaCode() == null) {
335 throw new NullPointerException("phone.phoneAreaCode is null");
336 } else if (phone.getPhoneAreaCode() < 1) {
338 throw new IllegalArgumentException("phone.phoneAreaCode is zero or below.");
339 } else if (phone.getPhoneNumber() == null) {
341 throw new NullPointerException("phone.phoneNumber is null");
342 } else if (phone.getPhoneNumber() < 1) {
344 throw new IllegalArgumentException("phone.phoneNumber is zero or below.");
348 contact.setContactPhoneNumber(phone);
351 // Don't set null or wrong references
352 if ((fax instanceof DialableFaxNumber) && (fax.getPhoneCountry() instanceof Country) && (this.getFaxAreaCode() > 0) && (this.getFaxNumber() > 0)) {
353 // Now the number must be given
354 if (fax.getPhoneAreaCode() == null) {
356 throw new NullPointerException("fax.phoneAreaCode is null");
357 } else if (fax.getPhoneAreaCode() < 1) {
359 throw new IllegalArgumentException("fax.phoneAreaCode is zero or below.");
360 } else if (fax.getPhoneNumber() == null) {
362 throw new NullPointerException("fax.phoneNumber is null");
363 } else if (fax.getPhoneNumber() < 1) {
365 throw new IllegalArgumentException("fax.phoneNumber is zero or below.");
369 contact.setContactFaxNumber(fax);
372 // Is the provider set?
373 if ((cellphone instanceof DialableCellphoneNumber) && (this.getCellphoneCarrier() instanceof SmsProvider) && (this.getCellphoneNumber() > 0)) {
374 // Is the number set?
375 if (cellphone.getPhoneNumber() == null) {
377 throw new NullPointerException("cellphone.phoneNumber is null");
378 } else if (cellphone.getPhoneNumber() < 1) {
380 throw new IllegalArgumentException("cellphone.phoneNumber is zero or below.");
383 // Set cellphone number
384 contact.setContactCellphoneNumber(cellphone);
387 contact.setContactBirthday(this.getBirthday());
388 contact.setContactComment(this.getComment());
390 // Created timestamp and ownContact
391 contact.setContactCreated(new GregorianCalendar());
392 contact.setContactOwnContact(Boolean.TRUE);
394 // Set contact in user
395 user.setUserContact(contact);
396 user.setUserCreated(new GregorianCalendar());
399 //this.getLogger().logTrace(MessageFormat.format("createUserInstance: user={0} - EXIT!", user));
405 public Date getBirthday () {
406 return this.birthday;
410 public void setBirthday (final Date birthday) {
411 this.birthday = birthday;
415 public SmsProvider getCellphoneCarrier () {
416 return this.cellphoneCarrier;
420 public void setCellphoneCarrier (final SmsProvider cellphoneCarrier) {
421 this.cellphoneCarrier = cellphoneCarrier;
425 public Long getCellphoneNumber () {
426 return this.cellphoneNumber;
430 public void setCellphoneNumber (Long cellphoneNumber) {
431 this.cellphoneNumber = cellphoneNumber;
435 public String getCity () {
440 public void setCity (final String city) {
445 public String getComment () {
450 public void setComment (final String comment) {
451 this.comment = comment;
455 public Country getCountry () {
460 public void setCountry (final Country country) {
461 this.country = country;
465 public String getEmailAddress () {
466 return this.emailAddress;
470 public void setEmailAddress (final String emailAddress) {
471 this.emailAddress = emailAddress;
475 public String getEmailAddressRepeat () {
476 return this.emailAddressRepeat;
480 public void setEmailAddressRepeat (final String emailAddressRepeat) {
481 this.emailAddressRepeat = emailAddressRepeat;
485 public String getFamilyName () {
486 return this.familyName;
490 public void setFamilyName (final String familyName) {
491 this.familyName = familyName;
495 public Integer getFaxAreaCode () {
496 return this.faxAreaCode;
500 public void setFaxAreaCode (final Integer faxAreaCode) {
501 this.faxAreaCode = faxAreaCode;
505 public Country getFaxCountry () {
506 return this.faxCountry;
510 public void setFaxCountry (final Country faxCountry) {
511 this.faxCountry = faxCountry;
515 public Long getFaxNumber () {
516 return this.faxNumber;
520 public void setFaxNumber (final Long faxNumber) {
521 this.faxNumber = faxNumber;
525 public String getFirstName () {
526 return this.firstName;
530 public void setFirstName (final String firstName) {
531 this.firstName = firstName;
535 public Gender getGender () {
540 public void setGender (final Gender gender) {
541 this.gender = gender;
545 public Short getHouseNumber () {
546 return this.houseNumber;
550 public void setHouseNumber (final Short houseNumber) {
551 this.houseNumber = houseNumber;
555 public Integer getPhoneAreaCode () {
556 return this.phoneAreaCode;
560 public void setPhoneAreaCode (final Integer phoneAreaCode) {
561 this.phoneAreaCode = phoneAreaCode;
565 public Country getPhoneCountry () {
566 return this.phoneCountry;
570 public void setPhoneCountry (final Country phoneCountry) {
571 this.phoneCountry = phoneCountry;
575 public Long getPhoneNumber () {
576 return this.phoneNumber;
580 public void setPhoneNumber (final Long phoneNumber) {
581 this.phoneNumber = phoneNumber;
585 public String getStreet () {
590 public void setStreet (final String street) {
591 this.street = street;
595 public Long getUserId () {
600 public void setUserId (final Long userId) {
601 this.userId = userId;
605 public String getUserName () {
606 return this.userName;
610 public void setUserName (final String userName) {
611 this.userName = userName;
615 public String getUserPassword () {
616 return this.userPassword;
620 public void setUserPassword (final String userPassword) {
621 this.userPassword = userPassword;
625 public String getUserPasswordRepeat () {
626 return this.userPasswordRepeat;
630 public void setUserPasswordRepeat (final String userPasswordRepeat) {
631 this.userPasswordRepeat = userPasswordRepeat;
635 public Integer getZipCode () {
640 public void setZipCode (final Integer zipCode) {
641 this.zipCode = zipCode;
645 public void init () {
646 // Get full user name list for reducing EJB calls
647 this.userNameList = this.userBean.getUserNameList();
649 // Get full email address list for reducing EJB calls
650 this.emailAddressList = this.userBean.getEmailAddressList();
654 public boolean isEmailAddressRegistered (final User user) {
655 return ((this.emailAddressList instanceof List) && (this.emailAddressList.contains(user.getUserContact().getContactEmailAddress())));
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));
675 public boolean isSameEmailAddressEntered () {
676 return (Objects.equals(this.getEmailAddress(), this.getEmailAddressRepeat()));
680 public boolean isSamePasswordEntered () {
681 return (Objects.equals(this.getUserPassword(), this.getUserPasswordRepeat()));
685 public boolean isUserNameRegistered (final User user) {
686 return ((this.userNameList instanceof List) && (this.userNameList.contains(user.getUserName())));