2 * Copyright (C) 2016 Roland Haeder
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.
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.
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/>.
17 package org.mxchange.pizzaapplication.beans.contact;
19 import java.text.MessageFormat;
20 import java.util.Date;
21 import java.util.Iterator;
22 import java.util.List;
23 import java.util.Objects;
24 import javax.annotation.PostConstruct;
25 import javax.enterprise.context.SessionScoped;
26 import javax.enterprise.event.Observes;
27 import javax.faces.view.facelets.FaceletException;
28 import javax.inject.Inject;
29 import javax.inject.Named;
30 import javax.naming.Context;
31 import javax.naming.InitialContext;
32 import javax.naming.NamingException;
33 import org.mxchange.jcontacts.contact.Contact;
34 import org.mxchange.jcontacts.contact.ContactSessionBeanRemote;
35 import org.mxchange.jcontacts.contact.UserContact;
36 import org.mxchange.jcontacts.contact.gender.Gender;
37 import org.mxchange.jcontacts.exceptions.ContactNotFoundException;
38 import org.mxchange.jcountry.data.Country;
39 import org.mxchange.jphone.phonenumbers.cellphone.CellphoneNumber;
40 import org.mxchange.jphone.phonenumbers.cellphone.DialableCellphoneNumber;
41 import org.mxchange.jphone.phonenumbers.fax.DialableFaxNumber;
42 import org.mxchange.jphone.phonenumbers.fax.FaxNumber;
43 import org.mxchange.jphone.phonenumbers.landline.DialableLandLineNumber;
44 import org.mxchange.jphone.phonenumbers.landline.LandLineNumber;
45 import org.mxchange.jphone.phonenumbers.mobileprovider.MobileProvider;
46 import org.mxchange.jusercore.events.login.UserLoggedInEvent;
47 import org.mxchange.jusercore.events.registration.UserRegisteredEvent;
48 import org.mxchange.jusercore.exceptions.UserPasswordMismatchException;
49 import org.mxchange.pizzaapplication.beans.login.PizzaUserLoginWebSessionController;
52 * A general contact bean (controller)
54 * @author Roland Haeder<roland@mxchange.org>
56 @Named ("contactController")
58 public class PizzaContactWebSessionBean implements PizzaContactWebSessionController {
63 private static final long serialVersionUID = 542_145_347_916L;
68 private Date birthday;
71 * Cellphone number's carrier
73 private MobileProvider cellphoneCarrier;
78 private Long cellphoneNumber;
88 private String comment;
93 private final ContactSessionBeanRemote contactBean;
98 private List<Contact> contactList;
103 private Country country;
108 private String emailAddress;
113 private List<String> emailAddressList;
116 * Email address repeated
118 private String emailAddressRepeat;
123 private String familyName;
126 * Fax number's area code
128 private Integer faxAreaCode;
131 * Country instance for fax number
133 private Country faxCountry;
138 private Long faxNumber;
143 private String firstName;
148 private Gender gender;
153 private Short houseNumber;
156 * Login bean (controller)
159 private PizzaUserLoginWebSessionController loginController;
162 * Phone number area code
164 private Integer phoneAreaCode;
167 * Country instance for phone number
169 private Country phoneCountry;
174 private Long phoneNumber;
179 private String street;
184 private Integer zipCode;
187 * Default constructor
189 public PizzaContactWebSessionBean () {
190 // Set gender to UNKNOWN
191 this.gender = Gender.UNKNOWN;
195 // Get initial context
196 Context context = new InitialContext();
199 this.contactBean = (ContactSessionBeanRemote) context.lookup("java:global/jratecalc-ejb/contact!org.mxchange.jcontacts.contact.ContactSessionBeanRemote"); //NOI18N
200 } catch (final NamingException e) {
202 throw new FaceletException(e);
207 public void addEmailAddress (final String contactEmailAddress) {
209 this.emailAddressList.add(contactEmailAddress);
213 public void afterRegistrationEvent (final @Observes UserRegisteredEvent event) {
215 System.out.println(MessageFormat.format("ContactWebBean:afterRegistration: event={0} - CALLED!", event)); //NOI18N
217 // event should not be null
220 throw new NullPointerException("event is null"); //NOI18N
221 } else if (event.getRegisteredUser() == null) {
223 throw new NullPointerException("event.user is null"); //NOI18N
224 } else if (event.getRegisteredUser().getUserId() == null) {
226 throw new NullPointerException("event.user.userId is null"); //NOI18N
227 } else if (event.getRegisteredUser().getUserId() < 1) {
229 throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getRegisteredUser(), event.getRegisteredUser().getUserId())); //NOI18N
233 Contact registeredContact = event.getRegisteredUser().getUserContact();
236 System.out.println(MessageFormat.format("ContactWebBean:afterRegistration: registeredContact={0}", registeredContact)); //NOI18N
238 // Copy all data from registered->user
239 this.copyContact(registeredContact);
241 // Add user name and email address
242 this.addUserNameEmailAddress(registeredContact);
248 System.out.println("ContactWebBean:afterRegistration: EXIT!"); //NOI18N
252 public void afterUserLogin (final @Observes UserLoggedInEvent event) {
254 System.out.println(MessageFormat.format("ContactWebBean:afterUserLogin: event={0} - CALLED!", event)); //NOI18N
256 // event should not be null
259 throw new NullPointerException("event is null"); //NOI18N
260 } else if (event.getLoggedInUser() == null) {
262 throw new NullPointerException("event.user is null"); //NOI18N
263 } else if (event.getLoggedInUser().getUserId() == null) {
265 throw new NullPointerException("event.user.userId is null"); //NOI18N
266 } else if (event.getLoggedInUser().getUserId() < 1) {
268 throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getLoggedInUser(), event.getLoggedInUser().getUserId())); //NOI18N
271 // Copy all data to this bean
272 this.copyContact(event.getLoggedInUser().getUserContact());
275 System.out.println("ContactWebBean:afterUserLogin - EXIT!"); //NOI18N
282 public void clear () {
285 this.setGender(Gender.UNKNOWN);
286 this.setFirstName(null);
287 this.setFamilyName(null);
288 this.setStreet(null);
289 this.setHouseNumber(null);
290 this.setZipCode(null);
292 this.setCountry(null);
295 this.setEmailAddress(null);
296 this.setEmailAddressRepeat(null);
297 this.setPhoneAreaCode(null);
298 this.setCellphoneCarrier(null);
299 this.setFaxAreaCode(null);
302 this.setBirthday(null);
303 this.setComment(null);
307 public Contact createContactInstance () {
309 //this.getLogger().logTrace("createContactInstance: CALLED!");
311 // Required personal data must be set
312 assert (this.isRequiredPersonalDataSet()) : "not all personal data is set"; //NOI18N
314 // Create new contact instance
315 Contact localContact = new UserContact();
317 // Generate phone number
318 DialableLandLineNumber phone = new LandLineNumber(this.getPhoneCountry(), this.getPhoneAreaCode(), this.getPhoneNumber());
319 DialableCellphoneNumber cellphone = new CellphoneNumber(this.getCellphoneCarrier(), this.getCellphoneNumber());
320 DialableFaxNumber fax = new FaxNumber(this.getFaxCountry(), this.getFaxAreaCode(), this.getFaxNumber());
322 // Create new contact
323 Contact contact = new UserContact(this.getGender(), this.getFirstName(), this.getFamilyName());
324 contact.setContactStreet(this.getStreet());
325 contact.setContactHouseNumber(this.getHouseNumber());
326 contact.setContactZipCode(this.getZipCode());
327 contact.setContactCity(this.getCity());
328 contact.setContactCountry(this.getCountry());
329 contact.setContactEmailAddress(this.getEmailAddress());
331 // Don't set null or wrong references
332 if ((phone instanceof DialableLandLineNumber) && (phone.getPhoneCountry() instanceof Country) && (this.getPhoneAreaCode() != null) && (this.getPhoneNumber() != null) && (this.getPhoneAreaCode() > 0) && (this.getPhoneNumber() > 0)) {
333 // Now the number must be given
334 if (phone.getPhoneAreaCode() == null) {
336 throw new NullPointerException("phone.phoneAreaCode is null"); //NOI18N
337 } else if (phone.getPhoneAreaCode() < 1) {
339 throw new IllegalArgumentException("phone.phoneAreaCode is zero or below."); //NOI18N
340 } else if (phone.getPhoneNumber() == null) {
342 throw new NullPointerException("phone.phoneNumber is null"); //NOI18N
343 } else if (phone.getPhoneNumber() < 1) {
345 throw new IllegalArgumentException("phone.phoneNumber is zero or below."); //NOI18N
349 contact.setContactLandLineNumber(phone);
352 // Don't set null or wrong references
353 if ((fax instanceof DialableFaxNumber) && (fax.getPhoneCountry() instanceof Country) && (this.getFaxAreaCode() != null) && (this.getFaxNumber() != null) && (this.getFaxAreaCode() > 0) && (this.getFaxNumber() > 0)) {
354 // Now the number must be given
355 if (fax.getPhoneAreaCode() == null) {
357 throw new NullPointerException("fax.phoneAreaCode is null"); //NOI18N
358 } else if (fax.getPhoneAreaCode() < 1) {
360 throw new IllegalArgumentException("fax.phoneAreaCode is zero or below."); //NOI18N
361 } else if (fax.getPhoneNumber() == null) {
363 throw new NullPointerException("fax.phoneNumber is null"); //NOI18N
364 } else if (fax.getPhoneNumber() < 1) {
366 throw new IllegalArgumentException("fax.phoneNumber is zero or below."); //NOI18N
370 contact.setContactFaxNumber(fax);
373 // Is the provider set?
374 if ((cellphone instanceof DialableCellphoneNumber) && (this.getCellphoneCarrier() instanceof MobileProvider) && (this.getCellphoneNumber() != null) && (this.getCellphoneNumber() > 0)) {
375 // Is the number set?
376 if (cellphone.getPhoneNumber() == null) {
378 throw new NullPointerException("cellphone.phoneNumber is null"); //NOI18N
379 } else if (cellphone.getPhoneNumber() < 1) {
381 throw new IllegalArgumentException("cellphone.phoneNumber is zero or below."); //NOI18N
384 // Set cellphone number
385 contact.setContactCellphoneNumber(cellphone);
388 contact.setContactBirthday(this.getBirthday());
389 contact.setContactComment(this.getComment());
391 // Created timestamp and ownContact
392 contact.setContactOwnContact(Boolean.TRUE);
395 //this.getLogger().logTrace(MessageFormat.format("createContactInstance: localContact={0} - EXIT!", localContact));
401 public String doChangePersonalContactData () {
402 // This method shall only be called if the user is logged-in
403 if (!this.loginController.isUserLoggedIn()) {
405 throw new IllegalStateException("User is not logged-in"); //NOI18N
406 } else if (!this.isRequiredChangePersonalDataSet()) {
407 // Not all required fields are set
408 throw new FaceletException("Not all required fields are set."); //NOI18N
409 } else if (!this.loginController.ifCurrentPasswordMatches()) {
410 // Password not matching
411 throw new FaceletException(new UserPasswordMismatchException(this.loginController.getLoggedInUser()));
414 // Get contact instance
415 Contact contact = this.loginController.getLoggedInUser().getUserContact();
417 // It should be there, so run some tests on it
418 assert (contact instanceof Contact) : "Instance loginController.loggedInUser.userContact is null"; //NOI18N
419 assert (contact.getContactId() instanceof Long) : "Instance loginController.userContact.contactId is null"; //NOI18N
420 assert (contact.getContactId() > 0) : MessageFormat.format("Instance loginController.userContact.contactId={0} is invalid", contact.getContactId()); //NOI18N
423 contact.setContactGender(this.getGender());
424 contact.setContactFirstName(this.getFirstName());
425 contact.setContactFamilyName(this.getFamilyName());
426 contact.setContactStreet(this.getStreet());
427 contact.setContactHouseNumber(this.getHouseNumber());
428 contact.setContactZipCode(this.getZipCode());
429 contact.setContactCity(this.getCity());
430 contact.setContactCountry(this.getCountry());
432 // Is there a phone number?
433 if (contact.getContactLandLineNumber() instanceof DialableLandLineNumber) {
435 System.out.println(MessageFormat.format("ContactWebBean:doChangePersonalData: phoneId={0}", contact.getContactLandLineNumber().getPhoneId())); //NOI18N
437 // Yes, then update as well
438 contact.getContactLandLineNumber().setPhoneAreaCode(this.getPhoneAreaCode());
439 contact.getContactLandLineNumber().setPhoneNumber(this.getPhoneNumber());
442 // Is there a fax number?
443 if (contact.getContactFaxNumber() instanceof DialableFaxNumber) {
445 System.out.println(MessageFormat.format("ContactWebBean:doChangePersonalData: faxId={0}", contact.getContactFaxNumber().getPhoneId())); //NOI18N
447 // Yes, then update as well
448 contact.getContactFaxNumber().setPhoneAreaCode(this.getFaxAreaCode());
449 contact.getContactFaxNumber().setPhoneNumber(this.getFaxNumber());
452 // Is there a cellphone number?
453 if (contact.getContactCellphoneNumber() instanceof DialableCellphoneNumber) {
455 System.out.println(MessageFormat.format("ContactWebBean:doChangePersonalData: cellPhoneId={0}", contact.getContactCellphoneNumber().getPhoneId())); //NOI18N
457 // Yes, then update as well
458 contact.getContactCellphoneNumber().setCellphoneProvider(this.getCellphoneCarrier());
459 contact.getContactCellphoneNumber().setPhoneNumber(this.getCellphoneNumber());
462 // Send it to the EJB
463 this.contactBean.updateContactPersonalData(contact);
466 return "contact_data_saved"; //NOI18N
470 public Date getBirthday () {
471 return this.birthday;
475 public void setBirthday (final Date birthday) {
476 this.birthday = birthday;
480 public MobileProvider getCellphoneCarrier () {
481 return this.cellphoneCarrier;
485 public void setCellphoneCarrier (final MobileProvider cellphoneCarrier) {
486 this.cellphoneCarrier = cellphoneCarrier;
490 public Long getCellphoneNumber () {
491 return this.cellphoneNumber;
495 public void setCellphoneNumber (Long cellphoneNumber) {
496 this.cellphoneNumber = cellphoneNumber;
500 public String getCity () {
505 public void setCity (final String city) {
510 public String getComment () {
515 public void setComment (final String comment) {
516 this.comment = comment;
520 public Country getCountry () {
525 public void setCountry (final Country country) {
526 this.country = country;
530 public String getEmailAddress () {
531 return this.emailAddress;
535 public void setEmailAddress (final String emailAddress) {
536 this.emailAddress = emailAddress;
540 public String getEmailAddressRepeat () {
541 return this.emailAddressRepeat;
545 public void setEmailAddressRepeat (final String emailAddressRepeat) {
546 this.emailAddressRepeat = emailAddressRepeat;
550 public String getFamilyName () {
551 return this.familyName;
555 public void setFamilyName (final String familyName) {
556 this.familyName = familyName;
560 public Integer getFaxAreaCode () {
561 return this.faxAreaCode;
565 public void setFaxAreaCode (final Integer faxAreaCode) {
566 this.faxAreaCode = faxAreaCode;
570 public Country getFaxCountry () {
571 return this.faxCountry;
575 public void setFaxCountry (final Country faxCountry) {
576 this.faxCountry = faxCountry;
580 public Long getFaxNumber () {
581 return this.faxNumber;
585 public void setFaxNumber (final Long faxNumber) {
586 this.faxNumber = faxNumber;
590 public String getFirstName () {
591 return this.firstName;
595 public void setFirstName (final String firstName) {
596 this.firstName = firstName;
600 public Gender getGender () {
605 public void setGender (final Gender gender) {
606 this.gender = gender;
610 public Short getHouseNumber () {
611 return this.houseNumber;
615 public void setHouseNumber (final Short houseNumber) {
616 this.houseNumber = houseNumber;
620 public Integer getPhoneAreaCode () {
621 return this.phoneAreaCode;
625 public void setPhoneAreaCode (final Integer phoneAreaCode) {
626 this.phoneAreaCode = phoneAreaCode;
630 public Country getPhoneCountry () {
631 return this.phoneCountry;
635 public void setPhoneCountry (final Country phoneCountry) {
636 this.phoneCountry = phoneCountry;
640 public Long getPhoneNumber () {
641 return this.phoneNumber;
645 public void setPhoneNumber (final Long phoneNumber) {
646 this.phoneNumber = phoneNumber;
650 public String getStreet () {
655 public void setStreet (final String street) {
656 this.street = street;
660 public Integer getZipCode () {
665 public void setZipCode (final Integer zipCode) {
666 this.zipCode = zipCode;
670 * Post-initialization of this class
673 public void init () {
674 // Get full email address list for reducing EJB calls
675 this.emailAddressList = this.contactBean.getEmailAddressList();
677 // Get full contact list
678 this.contactList = this.contactBean.getAllContacts();
682 public boolean isEmailAddressRegistered (final Contact contact) {
683 return ((this.emailAddressList instanceof List) && (this.emailAddressList.contains(contact.getContactEmailAddress())));
687 public boolean isRequiredChangePersonalDataSet () {
688 return ((this.getGender() != null) &&
689 (this.getFirstName() != null) &&
690 (this.getFamilyName() != null) &&
691 (this.getStreet() != null) &&
692 (this.getHouseNumber() != null) &&
693 (this.getZipCode() != null) &&
694 (this.getCity() != null));
698 public boolean isRequiredPersonalDataSet () {
699 return ((this.getGender() != null) &&
700 (this.getFirstName() != null) &&
701 (this.getFamilyName() != null) &&
702 (this.getStreet() != null) &&
703 (this.getHouseNumber() != null) &&
704 (this.getZipCode() != null) &&
705 (this.getCity() != null) &&
706 (this.getEmailAddress() != null) &&
707 (this.getEmailAddressRepeat() != null));
711 public boolean isSameEmailAddressEntered () {
712 return (Objects.equals(this.getEmailAddress(), this.getEmailAddressRepeat()));
716 public Contact lookupContactById (final Long contactId) throws ContactNotFoundException {
718 Contact localContact = null;
723 // Try to lookup it in visible user list
724 for (final Iterator<Contact> iterator = this.contactList.iterator(); iterator.hasNext();) {
726 Contact next = iterator.next();
728 // Is the user id found?
729 if (Objects.equals(next.getContactId(), contactId)) {
730 // Copy to other variable
737 if (null == localContact) {
738 // Not visible for the current user
739 throw new ContactNotFoundException(contactId);
742 // Copy all data to this bean
743 this.copyContact(localContact);
750 public void updateContactDataFromController (final Contact userContact) {
751 // Is the instance valid?
752 if (null == userContact) {
754 throw new NullPointerException("userContact is null"); //NOI18N
755 } else if (userContact.getContactId() == null) {
757 throw new NullPointerException("userContact.contactId is null"); //NOI18N
758 } else if (userContact.getContactId() < 1) {
759 // Not valid id number
760 throw new IllegalArgumentException(MessageFormat.format("userContact.contactId={0} is not valid.", userContact.getContactId())); //NOI18N
764 this.copyContact(userContact);
768 * Adds email address to bean's internal list.
770 * @param contact Contact instance
772 private void addUserNameEmailAddress (final Contact contact) {
773 // Make sure the entry is not added yet
774 if (this.emailAddressList.contains(contact.getContactEmailAddress())) {
776 throw new IllegalArgumentException(MessageFormat.format("Email address {0} already added.", contact.getContactEmailAddress())); //NOI18N
780 this.emailAddressList.add(contact.getContactEmailAddress());
784 * Copies given contact into the controller
786 * @param contact Contact instance
788 private void copyContact (final Contact contact) {
791 this.setGender(contact.getContactGender());
792 this.setFirstName(contact.getContactFirstName());
793 this.setFamilyName(contact.getContactFamilyName());
794 this.setStreet(contact.getContactStreet());
795 this.setHouseNumber(contact.getContactHouseNumber());
796 this.setZipCode(contact.getContactZipCode());
797 this.setCity(contact.getContactCity());
798 this.setCountry(contact.getContactCountry());
800 // Get cellphone, phone and fax instance
801 DialableCellphoneNumber cellphone = contact.getContactCellphoneNumber();
802 DialableFaxNumber fax = contact.getContactFaxNumber();
803 DialableLandLineNumber phone = contact.getContactLandLineNumber();
806 if ((phone instanceof DialableLandLineNumber) && (phone.getPhoneAreaCode() > 0)) {
807 this.setPhoneCountry(phone.getPhoneCountry());
808 this.setPhoneAreaCode(phone.getPhoneAreaCode());
809 this.setPhoneNumber(phone.getPhoneNumber());
811 if ((cellphone instanceof DialableCellphoneNumber) && (cellphone.getCellphoneProvider() instanceof MobileProvider)) {
812 this.setCellphoneCarrier(cellphone.getCellphoneProvider());
813 this.setCellphoneNumber(cellphone.getPhoneNumber());
815 if ((fax instanceof DialableFaxNumber) && (fax.getPhoneAreaCode() > 0)) {
816 this.setFaxCountry(fax.getPhoneCountry());
817 this.setFaxAreaCode(fax.getPhoneAreaCode());
818 this.setFaxNumber(fax.getPhoneNumber());
820 this.setEmailAddress(contact.getContactEmailAddress());
823 this.setBirthday(contact.getContactBirthday());
824 this.setComment(contact.getContactComment());