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.contact.utils.ContactUtils;
38 import org.mxchange.jcontacts.events.contact.update.AdminUpdatedContactEvent;
39 import org.mxchange.jcontacts.exceptions.ContactNotFoundException;
40 import org.mxchange.jcountry.data.Country;
41 import org.mxchange.jphone.phonenumbers.cellphone.CellphoneNumber;
42 import org.mxchange.jphone.phonenumbers.cellphone.DialableCellphoneNumber;
43 import org.mxchange.jphone.phonenumbers.fax.DialableFaxNumber;
44 import org.mxchange.jphone.phonenumbers.fax.FaxNumber;
45 import org.mxchange.jphone.phonenumbers.landline.DialableLandLineNumber;
46 import org.mxchange.jphone.phonenumbers.landline.LandLineNumber;
47 import org.mxchange.jphone.phonenumbers.mobileprovider.MobileProvider;
48 import org.mxchange.jusercore.events.login.UserLoggedInEvent;
49 import org.mxchange.jusercore.events.registration.UserRegisteredEvent;
50 import org.mxchange.jusercore.exceptions.UserPasswordMismatchException;
51 import org.mxchange.pizzaapplication.beans.login.PizzaUserLoginWebSessionController;
54 * A general contact bean (controller)
56 * @author Roland Haeder<roland@mxchange.org>
58 @Named ("contactController")
60 public class PizzaContactWebSessionBean implements PizzaContactWebSessionController {
65 private static final long serialVersionUID = 542_145_347_916L;
70 private Date birthday;
73 * Cellphone number's carrier
75 private MobileProvider cellphoneCarrier;
80 private Long cellphoneNumber;
90 private String comment;
95 private final ContactSessionBeanRemote contactBean;
100 private List<Contact> contactList;
105 private Country country;
110 private String emailAddress;
115 private List<String> emailAddressList;
118 * Email address repeated
120 private String emailAddressRepeat;
125 private String familyName;
128 * Fax number's area code
130 private Integer faxAreaCode;
133 * Country instance for fax number
135 private Country faxCountry;
140 private Long faxNumber;
145 private String firstName;
150 private Gender gender;
155 private Short houseNumber;
158 * Whether a cellphone entry has been unlinked
160 private boolean isCellphoneUnlinked;
163 * Whether a fax entry has been unlinked
165 private boolean isFaxUnlinked;
168 * Whether a land-line number has been unlinked
170 private boolean isLandLineUnlinked;
173 * Login bean (controller)
176 private PizzaUserLoginWebSessionController loginController;
179 * Phone number area code
181 private Integer phoneAreaCode;
184 * Country instance for phone number
186 private Country phoneCountry;
191 private Long phoneNumber;
196 private String street;
201 private Integer zipCode;
204 * Default constructor
206 public PizzaContactWebSessionBean () {
207 // Set gender to UNKNOWN
208 this.gender = Gender.UNKNOWN;
212 // Get initial context
213 Context context = new InitialContext();
216 this.contactBean = (ContactSessionBeanRemote) context.lookup("java:global/PizzaService-ejb/contact!org.mxchange.jcontacts.contact.ContactSessionBeanRemote"); //NOI18N
217 } catch (final NamingException e) {
219 throw new FaceletException(e);
224 public void addEmailAddress (final String contactEmailAddress) {
226 this.emailAddressList.add(contactEmailAddress);
230 public void afterAdminUpdatedContactDataEvent (@Observes final AdminUpdatedContactEvent event) {
232 System.out.println(MessageFormat.format("ContactWebBean:afterAdminUpdatedContactDataEvent: event={0} - CALLED!", event)); //NOI18N
234 // event should not be null
237 throw new NullPointerException("event is null"); //NOI18N
238 } else if (event.getUpdatedContact()== null) {
240 throw new NullPointerException("event.updatedUser is null"); //NOI18N
241 } else if (event.getUpdatedContact().getContactId() == null) {
243 throw new NullPointerException("event.updatedUser.userId is null"); //NOI18N
244 } else if (event.getUpdatedContact().getContactId() < 1) {
246 throw new IllegalArgumentException(MessageFormat.format("contactId of contact={0} is not valid: {1}", event.getUpdatedContact(), event.getUpdatedContact().getContactId())); //NOI18N
249 // Get iterator from list
250 Iterator<Contact> iterator = this.contactList.iterator();
252 // "Walk" through all entries
253 while (iterator.hasNext()) {
255 Contact next = iterator.next();
257 // Is id number the same?
258 if (Objects.equals(event.getUpdatedContact().getContactId(), next.getContactId())) {
259 // Found entry, so remove it and abort
260 this.contactList.remove(next);
262 // Remove also email from list
263 this.emailAddressList.remove(next.getContactEmailAddress());
268 // Add contact to list
269 this.contactList.add(event.getUpdatedContact());
271 // Add email address to list
272 this.emailAddressList.add(event.getUpdatedContact().getContactEmailAddress());
276 public void afterRegistrationEvent (final @Observes UserRegisteredEvent event) {
278 System.out.println(MessageFormat.format("ContactWebBean:afterRegistration: event={0} - CALLED!", event)); //NOI18N
280 // event should not be null
283 throw new NullPointerException("event is null"); //NOI18N
284 } else if (event.getRegisteredUser() == null) {
286 throw new NullPointerException("event.user is null"); //NOI18N
287 } else if (event.getRegisteredUser().getUserId() == null) {
289 throw new NullPointerException("event.user.userId is null"); //NOI18N
290 } else if (event.getRegisteredUser().getUserId() < 1) {
292 throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getRegisteredUser(), event.getRegisteredUser().getUserId())); //NOI18N
296 Contact registeredContact = event.getRegisteredUser().getUserContact();
299 System.out.println(MessageFormat.format("ContactWebBean:afterRegistration: registeredContact={0}", registeredContact)); //NOI18N
301 // Copy all data from registered->user
302 this.copyContact(registeredContact);
304 // Add user name and email address
305 this.addUserNameEmailAddress(registeredContact);
311 System.out.println("ContactWebBean:afterRegistration: EXIT!"); //NOI18N
315 public void afterUserLogin (final @Observes UserLoggedInEvent event) {
317 System.out.println(MessageFormat.format("ContactWebBean:afterUserLogin: event={0} - CALLED!", event)); //NOI18N
319 // event should not be null
322 throw new NullPointerException("event is null"); //NOI18N
323 } else if (event.getLoggedInUser() == null) {
325 throw new NullPointerException("event.user is null"); //NOI18N
326 } else if (event.getLoggedInUser().getUserId() == null) {
328 throw new NullPointerException("event.user.userId is null"); //NOI18N
329 } else if (event.getLoggedInUser().getUserId() < 1) {
331 throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getLoggedInUser(), event.getLoggedInUser().getUserId())); //NOI18N
334 // Copy all data to this bean
335 this.copyContact(event.getLoggedInUser().getUserContact());
338 System.out.println("ContactWebBean:afterUserLogin - EXIT!"); //NOI18N
342 public List<Contact> allContacts () {
343 return this.contactList;
350 public void clear () {
353 this.setGender(Gender.UNKNOWN);
354 this.setFirstName(null);
355 this.setFamilyName(null);
356 this.setStreet(null);
357 this.setHouseNumber(null);
358 this.setZipCode(null);
360 this.setCountry(null);
363 this.setEmailAddress(null);
364 this.setEmailAddressRepeat(null);
365 this.setPhoneAreaCode(null);
366 this.setCellphoneCarrier(null);
367 this.setFaxAreaCode(null);
370 this.setBirthday(null);
371 this.setComment(null);
375 public Contact createContactInstance () {
377 //this.getLogger().logTrace("createContactInstance: CALLED!");
379 // Required personal data must be set
380 assert (this.isRequiredPersonalDataSet()) : "not all personal data is set"; //NOI18N
382 // Generate phone number
383 DialableLandLineNumber phone = new LandLineNumber(this.getPhoneCountry(), this.getPhoneAreaCode(), this.getPhoneNumber());
384 DialableCellphoneNumber cellphone = new CellphoneNumber(this.getCellphoneCarrier(), this.getCellphoneNumber());
385 DialableFaxNumber fax = new FaxNumber(this.getFaxCountry(), this.getFaxAreaCode(), this.getFaxNumber());
387 // Create new contact
388 Contact contact = new UserContact(this.getGender(), this.getFirstName(), this.getFamilyName());
389 contact.setContactStreet(this.getStreet());
390 contact.setContactHouseNumber(this.getHouseNumber());
391 contact.setContactZipCode(this.getZipCode());
392 contact.setContactCity(this.getCity());
393 contact.setContactCountry(this.getCountry());
394 contact.setContactEmailAddress(this.getEmailAddress());
395 contact.setContactBirthday(this.getBirthday());
396 contact.setContactComment(this.getComment());
399 contact.setContactOwnContact(Boolean.TRUE);
401 // Don't set null or wrong references
402 if ((phone instanceof DialableLandLineNumber) && (phone.getPhoneCountry() instanceof Country) && (this.getPhoneAreaCode() != null) && (this.getPhoneNumber() != null) && (this.getPhoneAreaCode() > 0) && (this.getPhoneNumber() > 0)) {
403 // Now the number must be given
404 if (phone.getPhoneAreaCode() == null) {
406 throw new NullPointerException("phone.phoneAreaCode is null"); //NOI18N
407 } else if (phone.getPhoneAreaCode() < 1) {
409 throw new IllegalArgumentException("phone.phoneAreaCode is zero or below."); //NOI18N
410 } else if (phone.getPhoneNumber() == null) {
412 throw new NullPointerException("phone.phoneNumber is null"); //NOI18N
413 } else if (phone.getPhoneNumber() < 1) {
415 throw new IllegalArgumentException("phone.phoneNumber is zero or below."); //NOI18N
419 contact.setContactLandLineNumber(phone);
422 // Don't set null or wrong references
423 if ((fax instanceof DialableFaxNumber) && (fax.getPhoneCountry() instanceof Country) && (this.getFaxAreaCode() != null) && (this.getFaxNumber() != null) && (this.getFaxAreaCode() > 0) && (this.getFaxNumber() > 0)) {
424 // Now the number must be given
425 if (fax.getPhoneAreaCode() == null) {
427 throw new NullPointerException("fax.phoneAreaCode is null"); //NOI18N
428 } else if (fax.getPhoneAreaCode() < 1) {
430 throw new IllegalArgumentException("fax.phoneAreaCode is zero or below."); //NOI18N
431 } else if (fax.getPhoneNumber() == null) {
433 throw new NullPointerException("fax.phoneNumber is null"); //NOI18N
434 } else if (fax.getPhoneNumber() < 1) {
436 throw new IllegalArgumentException("fax.phoneNumber is zero or below."); //NOI18N
440 contact.setContactFaxNumber(fax);
443 // Is the provider set?
444 if ((cellphone instanceof DialableCellphoneNumber) && (this.getCellphoneCarrier() instanceof MobileProvider) && (this.getCellphoneNumber() != null) && (this.getCellphoneNumber() > 0)) {
445 // Is the number set?
446 if (cellphone.getPhoneNumber() == null) {
448 throw new NullPointerException("cellphone.phoneNumber is null"); //NOI18N
449 } else if (cellphone.getPhoneNumber() < 1) {
451 throw new IllegalArgumentException("cellphone.phoneNumber is zero or below."); //NOI18N
454 // Set cellphone number
455 contact.setContactCellphoneNumber(cellphone);
459 //this.getLogger().logTrace(MessageFormat.format("createContactInstance: localContact={0} - EXIT!", localContact));
466 public String doChangePersonalContactData () {
467 // This method shall only be called if the user is logged-in
468 if (!this.loginController.isUserLoggedIn()) {
470 throw new IllegalStateException("User is not logged-in"); //NOI18N
471 } else if (!this.isRequiredChangePersonalDataSet()) {
472 // Not all required fields are set
473 throw new FaceletException("Not all required fields are set."); //NOI18N
474 } else if (!this.loginController.ifCurrentPasswordMatches()) {
475 // Password not matching
476 throw new FaceletException(new UserPasswordMismatchException(this.loginController.getLoggedInUser()));
479 // Get contact instance
480 Contact contact = this.loginController.getLoggedInUser().getUserContact();
482 // It should be there, so run some tests on it
483 assert (contact instanceof Contact) : "Instance loginController.loggedInUser.userContact is null"; //NOI18N
484 assert (contact.getContactId() instanceof Long) : "Instance loginController.userContact.contactId is null"; //NOI18N
485 assert (contact.getContactId() > 0) : MessageFormat.format("Instance loginController.userContact.contactId={0} is invalid", contact.getContactId()); //NOI18N
488 contact.setContactGender(this.getGender());
489 contact.setContactFirstName(this.getFirstName());
490 contact.setContactFamilyName(this.getFamilyName());
491 contact.setContactStreet(this.getStreet());
492 contact.setContactHouseNumber(this.getHouseNumber());
493 contact.setContactZipCode(this.getZipCode());
494 contact.setContactCity(this.getCity());
495 contact.setContactCountry(this.getCountry());
497 // Update contact's cellphone number
498 this.isCellphoneUnlinked = ContactUtils.updateCellPhoneNumber(contact, this.getCellphoneCarrier(), this.getCellphoneNumber());
500 // Update contact's land-line number
501 this.isLandLineUnlinked = ContactUtils.updateLandLineNumber(contact, this.getPhoneCountry(), this.getPhoneAreaCode(), this.getPhoneNumber());
503 // Update contact's fax number
504 this.isFaxUnlinked = ContactUtils.updateFaxNumber(contact, this.getFaxCountry(), this.getFaxAreaCode(), this.getFaxNumber());
506 // Send it to the EJB
507 this.contactBean.updateContactData(contact, this.isCellphoneUnlinked, this.isLandLineUnlinked, this.isFaxUnlinked);
510 return "contact_data_saved"; //NOI18N
514 public Date getBirthday () {
515 return this.birthday;
519 public void setBirthday (final Date birthday) {
520 this.birthday = birthday;
524 public MobileProvider getCellphoneCarrier () {
525 return this.cellphoneCarrier;
529 public void setCellphoneCarrier (final MobileProvider cellphoneCarrier) {
530 this.cellphoneCarrier = cellphoneCarrier;
534 public Long getCellphoneNumber () {
535 return this.cellphoneNumber;
539 public void setCellphoneNumber (Long cellphoneNumber) {
540 this.cellphoneNumber = cellphoneNumber;
544 public String getCity () {
549 public void setCity (final String city) {
554 public String getComment () {
559 public void setComment (final String comment) {
560 this.comment = comment;
564 public Country getCountry () {
569 public void setCountry (final Country country) {
570 this.country = country;
574 public String getEmailAddress () {
575 return this.emailAddress;
579 public void setEmailAddress (final String emailAddress) {
580 this.emailAddress = emailAddress;
584 public String getEmailAddressRepeat () {
585 return this.emailAddressRepeat;
589 public void setEmailAddressRepeat (final String emailAddressRepeat) {
590 this.emailAddressRepeat = emailAddressRepeat;
594 public String getFamilyName () {
595 return this.familyName;
599 public void setFamilyName (final String familyName) {
600 this.familyName = familyName;
604 public Integer getFaxAreaCode () {
605 return this.faxAreaCode;
609 public void setFaxAreaCode (final Integer faxAreaCode) {
610 this.faxAreaCode = faxAreaCode;
614 public Country getFaxCountry () {
615 return this.faxCountry;
619 public void setFaxCountry (final Country faxCountry) {
620 this.faxCountry = faxCountry;
624 public Long getFaxNumber () {
625 return this.faxNumber;
629 public void setFaxNumber (final Long faxNumber) {
630 this.faxNumber = faxNumber;
634 public String getFirstName () {
635 return this.firstName;
639 public void setFirstName (final String firstName) {
640 this.firstName = firstName;
644 public Gender getGender () {
649 public void setGender (final Gender gender) {
650 this.gender = gender;
654 public Short getHouseNumber () {
655 return this.houseNumber;
659 public void setHouseNumber (final Short houseNumber) {
660 this.houseNumber = houseNumber;
664 public Integer getPhoneAreaCode () {
665 return this.phoneAreaCode;
669 public void setPhoneAreaCode (final Integer phoneAreaCode) {
670 this.phoneAreaCode = phoneAreaCode;
674 public Country getPhoneCountry () {
675 return this.phoneCountry;
679 public void setPhoneCountry (final Country phoneCountry) {
680 this.phoneCountry = phoneCountry;
684 public Long getPhoneNumber () {
685 return this.phoneNumber;
689 public void setPhoneNumber (final Long phoneNumber) {
690 this.phoneNumber = phoneNumber;
694 public String getStreet () {
699 public void setStreet (final String street) {
700 this.street = street;
704 public Integer getZipCode () {
709 public void setZipCode (final Integer zipCode) {
710 this.zipCode = zipCode;
714 * Post-initialization of this class
717 public void init () {
718 // Get full email address list for reducing EJB calls
719 this.emailAddressList = this.contactBean.getEmailAddressList();
721 // Get full contact list
722 this.contactList = this.contactBean.getAllContacts();
726 public boolean isEmailAddressRegistered (final Contact contact) {
727 return ((this.emailAddressList instanceof List) && (this.emailAddressList.contains(contact.getContactEmailAddress())));
731 public boolean isRequiredChangePersonalDataSet () {
732 return ((this.getGender() != null) &&
733 (this.getFirstName() != null) &&
734 (this.getFamilyName() != null) &&
735 (this.getStreet() != null) &&
736 (this.getHouseNumber() != null) &&
737 (this.getZipCode() != null) &&
738 (this.getCity() != null));
742 public boolean isRequiredPersonalDataSet () {
743 return ((this.getGender() != null) &&
744 (this.getFirstName() != null) &&
745 (this.getFamilyName() != null) &&
746 (this.getStreet() != null) &&
747 (this.getHouseNumber() != null) &&
748 (this.getZipCode() != null) &&
749 (this.getCity() != null) &&
750 (this.getEmailAddress() != null) &&
751 (this.getEmailAddressRepeat() != null));
755 public boolean isSameEmailAddressEntered () {
756 return (Objects.equals(this.getEmailAddress(), this.getEmailAddressRepeat()));
760 public Contact lookupContactById (final Long contactId) throws ContactNotFoundException {
762 Contact localContact = null;
767 // Try to lookup it in visible user list
768 for (final Iterator<Contact> iterator = this.contactList.iterator(); iterator.hasNext();) {
770 Contact next = iterator.next();
772 // Is the user id found?
773 if (Objects.equals(next.getContactId(), contactId)) {
774 // Copy to other variable
781 if (null == localContact) {
782 // Not visible for the current user
783 throw new ContactNotFoundException(contactId);
786 // Copy all data to this bean
787 this.copyContact(localContact);
794 public void updateContactDataFromController (final Contact userContact) {
795 // Is the instance valid?
796 if (null == userContact) {
798 throw new NullPointerException("userContact is null"); //NOI18N
799 } else if (userContact.getContactId() == null) {
801 throw new NullPointerException("userContact.contactId is null"); //NOI18N
802 } else if (userContact.getContactId() < 1) {
803 // Not valid id number
804 throw new IllegalArgumentException(MessageFormat.format("userContact.contactId={0} is not valid.", userContact.getContactId())); //NOI18N
808 this.copyContact(userContact);
812 * Adds email address to bean's internal list.
814 * @param contact Contact instance
816 private void addUserNameEmailAddress (final Contact contact) {
817 // Make sure the entry is not added yet
818 if (this.emailAddressList.contains(contact.getContactEmailAddress())) {
820 throw new IllegalArgumentException(MessageFormat.format("Email address {0} already added.", contact.getContactEmailAddress())); //NOI18N
824 this.emailAddressList.add(contact.getContactEmailAddress());
828 * Copies given contact into the controller
830 * @param contact Contact instance
832 private void copyContact (final Contact contact) {
835 this.setGender(contact.getContactGender());
836 this.setFirstName(contact.getContactFirstName());
837 this.setFamilyName(contact.getContactFamilyName());
838 this.setStreet(contact.getContactStreet());
839 this.setHouseNumber(contact.getContactHouseNumber());
840 this.setZipCode(contact.getContactZipCode());
841 this.setCity(contact.getContactCity());
842 this.setCountry(contact.getContactCountry());
844 // Get cellphone, phone and fax instance
845 DialableCellphoneNumber cellphone = contact.getContactCellphoneNumber();
846 DialableFaxNumber fax = contact.getContactFaxNumber();
847 DialableLandLineNumber phone = contact.getContactLandLineNumber();
850 if ((phone instanceof DialableLandLineNumber) && (phone.getPhoneAreaCode() > 0)) {
851 this.setPhoneCountry(phone.getPhoneCountry());
852 this.setPhoneAreaCode(phone.getPhoneAreaCode());
853 this.setPhoneNumber(phone.getPhoneNumber());
855 if ((cellphone instanceof DialableCellphoneNumber) && (cellphone.getCellphoneProvider() instanceof MobileProvider)) {
856 this.setCellphoneCarrier(cellphone.getCellphoneProvider());
857 this.setCellphoneNumber(cellphone.getPhoneNumber());
859 if ((fax instanceof DialableFaxNumber) && (fax.getPhoneAreaCode() > 0)) {
860 this.setFaxCountry(fax.getPhoneCountry());
861 this.setFaxAreaCode(fax.getPhoneAreaCode());
862 this.setFaxNumber(fax.getPhoneNumber());
864 this.setEmailAddress(contact.getContactEmailAddress());
867 this.setBirthday(contact.getContactBirthday());
868 this.setComment(contact.getContactComment());