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().getEmailAddress())) {
224 throw new IllegalArgumentException(MessageFormat.format("Email address {0} already added.", user.getUserContact().getEmailAddress()));
228 this.userNameList.add(user.getUserName());
231 this.emailAddressList.add(user.getUserContact().getEmailAddress());
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().getGender());
269 this.setFirstName(user.getUserContact().getFirstName());
270 this.setFamilyName(user.getUserContact().getFamilyName());
271 this.setStreet(user.getUserContact().getStreet());
272 this.setHouseNumber(user.getUserContact().getHouseNumber());
273 this.setZipCode(user.getUserContact().getZipCode());
274 this.setCity(user.getUserContact().getCity());
275 this.setCountry(user.getUserContact().getCountry());
277 // Get cellphone, phone and fax instance
278 DialableCellphoneNumber cellphone = user.getUserContact().getCellphoneNumber();
279 DialableFaxNumber fax = user.getUserContact().getFaxNumber();
280 DialableLandLineNumber phone = user.getUserContact().getPhoneNumber();
283 if (phone instanceof DialableLandLineNumber) {
284 this.setPhoneCountry(phone.getPhoneCountry());
285 this.setPhoneAreaCode(phone.getPhoneAreaCode());
286 this.setPhoneNumber(phone.getPhoneNumber());
288 if (cellphone instanceof DialableCellphoneNumber) {
289 this.setCellphoneCarrier(cellphone.getCellphoneProvider());
290 this.setCellphoneNumber(cellphone.getPhoneNumber());
292 if (fax instanceof DialableFaxNumber) {
293 this.setFaxCountry(fax.getPhoneCountry());
294 this.setFaxAreaCode(fax.getPhoneAreaCode());
295 this.setFaxNumber(fax.getPhoneNumber());
297 this.setEmailAddress(user.getUserContact().getEmailAddress());
300 this.setBirthday(user.getUserContact().getBirthday());
301 this.setComment(user.getUserContact().getComment());
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.setStreet(this.getStreet());
324 contact.setHouseNumber(this.getHouseNumber());
325 contact.setZipCode(this.getZipCode());
326 contact.setCity(this.getCity());
327 contact.setCountry(this.getCountry());
328 contact.setEmailAddress(this.getEmailAddress());
329 contact.setPhoneNumber(phone);
330 contact.setFaxNumber(fax);
331 contact.setCellphoneNumber(cellphone);
332 contact.setBirthday(this.getBirthday());
333 contact.setComment(this.getComment());
335 // Created timestamp and ownContact
336 contact.setCreated(new GregorianCalendar());
337 contact.setOwnContact(Boolean.TRUE);
339 // Set contact in user
340 user.setUserContact(contact);
341 user.setUserCreated(new GregorianCalendar());
344 //this.getLogger().logTrace(MessageFormat.format("createUserInstance: user={0} - EXIT!", user));
350 public Date getBirthday () {
351 return this.birthday;
355 public void setBirthday (final Date birthday) {
356 this.birthday = birthday;
360 public SmsProvider getCellphoneCarrier () {
361 return this.cellphoneCarrier;
365 public void setCellphoneCarrier (final SmsProvider cellphoneCarrier) {
366 this.cellphoneCarrier = cellphoneCarrier;
370 public Long getCellphoneNumber () {
371 return this.cellphoneNumber;
375 public void setCellphoneNumber (Long cellphoneNumber) {
376 this.cellphoneNumber = cellphoneNumber;
380 public String getCity () {
385 public void setCity (final String city) {
390 public String getComment () {
395 public void setComment (final String comment) {
396 this.comment = comment;
400 public Country getCountry () {
405 public void setCountry (final Country country) {
406 this.country = country;
410 public String getEmailAddress () {
411 return this.emailAddress;
415 public void setEmailAddress (final String emailAddress) {
416 this.emailAddress = emailAddress;
420 public String getEmailAddressRepeat () {
421 return this.emailAddressRepeat;
425 public void setEmailAddressRepeat (final String emailAddressRepeat) {
426 this.emailAddressRepeat = emailAddressRepeat;
430 public String getFamilyName () {
431 return this.familyName;
435 public void setFamilyName (final String familyName) {
436 this.familyName = familyName;
440 public Integer getFaxAreaCode () {
441 return this.faxAreaCode;
445 public void setFaxAreaCode (final Integer faxAreaCode) {
446 this.faxAreaCode = faxAreaCode;
450 public Country getFaxCountry () {
451 return this.faxCountry;
455 public void setFaxCountry (final Country faxCountry) {
456 this.faxCountry = faxCountry;
460 public Long getFaxNumber () {
461 return this.faxNumber;
465 public void setFaxNumber (final Long faxNumber) {
466 this.faxNumber = faxNumber;
470 public String getFirstName () {
471 return this.firstName;
475 public void setFirstName (final String firstName) {
476 this.firstName = firstName;
480 public Gender getGender () {
485 public void setGender (final Gender gender) {
486 this.gender = gender;
490 public Short getHouseNumber () {
491 return this.houseNumber;
495 public void setHouseNumber (final Short houseNumber) {
496 this.houseNumber = houseNumber;
500 public Integer getPhoneAreaCode () {
501 return this.phoneAreaCode;
505 public void setPhoneAreaCode (final Integer phoneAreaCode) {
506 this.phoneAreaCode = phoneAreaCode;
510 public Country getPhoneCountry () {
511 return this.phoneCountry;
515 public void setPhoneCountry (final Country phoneCountry) {
516 this.phoneCountry = phoneCountry;
520 public Long getPhoneNumber () {
521 return this.phoneNumber;
525 public void setPhoneNumber (final Long phoneNumber) {
526 this.phoneNumber = phoneNumber;
530 public String getStreet () {
535 public void setStreet (final String street) {
536 this.street = street;
540 public Long getUserId () {
545 public void setUserId (final Long userId) {
546 this.userId = userId;
550 public String getUserName () {
551 return this.userName;
555 public void setUserName (final String userName) {
556 this.userName = userName;
560 public String getUserPassword () {
561 return this.userPassword;
565 public void setUserPassword (final String userPassword) {
566 this.userPassword = userPassword;
570 public String getUserPasswordRepeat () {
571 return this.userPasswordRepeat;
575 public void setUserPasswordRepeat (final String userPasswordRepeat) {
576 this.userPasswordRepeat = userPasswordRepeat;
580 public Integer getZipCode () {
585 public void setZipCode (final Integer zipCode) {
586 this.zipCode = zipCode;
590 public void init () {
591 // Get full user name list for reducing EJB calls
592 this.userNameList = this.userBean.getUserNameList();
594 // Get full email address list for reducing EJB calls
595 this.emailAddressList = this.userBean.getEmailAddressList();
599 public boolean isEmailAddressRegistered (final User user) {
600 return ((this.emailAddressList instanceof List) && (this.emailAddressList.contains(user.getUserContact().getEmailAddress())));
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));
620 public boolean isSameEmailAddressEntered () {
621 return (Objects.equals(this.getEmailAddress(), this.getEmailAddressRepeat()));
625 public boolean isSamePasswordEntered () {
626 return (Objects.equals(this.getUserPassword(), this.getUserPasswordRepeat()));
630 public boolean isUserNameRegistered (final User user) {
631 return ((this.userNameList instanceof List) && (this.userNameList.contains(user.getUserName())));