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.addressbook.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.addressbook.beans.login.AddressbookUserLoginWebSessionController;
34 import org.mxchange.jcontacts.contact.Contact;
35 import org.mxchange.jcontacts.contact.ContactSessionBeanRemote;
36 import org.mxchange.jcontacts.contact.UserContact;
37 import org.mxchange.jcontacts.contact.gender.Gender;
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;
53 * A general contact bean (controller)
55 * @author Roland Haeder<roland@mxchange.org>
57 @Named ("contactController")
59 public class AddressbookContactWebSessionBean implements AddressbookContactWebSessionController {
64 private static final long serialVersionUID = 542_145_347_916L;
69 private Date birthday;
72 * Cellphone number's carrier
74 private MobileProvider cellphoneCarrier;
79 private Long cellphoneNumber;
89 private String comment;
94 private final ContactSessionBeanRemote contactBean;
99 private List<Contact> contactList;
104 private Country country;
109 private String emailAddress;
114 private List<String> emailAddressList;
117 * Email address repeated
119 private String emailAddressRepeat;
124 private String familyName;
127 * Fax number's area code
129 private Integer faxAreaCode;
132 * Country instance for fax number
134 private Country faxCountry;
139 private Long faxNumber;
144 private String firstName;
149 private Gender gender;
154 private Short houseNumber;
157 * Login bean (controller)
160 private AddressbookUserLoginWebSessionController loginController;
163 * Phone number area code
165 private Integer phoneAreaCode;
168 * Country instance for phone number
170 private Country phoneCountry;
175 private Long phoneNumber;
180 private String street;
185 private Integer zipCode;
188 * Default constructor
190 public AddressbookContactWebSessionBean () {
191 // Set gender to UNKNOWN
192 this.gender = Gender.UNKNOWN;
196 // Get initial context
197 Context context = new InitialContext();
200 this.contactBean = (ContactSessionBeanRemote) context.lookup("java:global/jratecalc-ejb/contact!org.mxchange.jcontacts.contact.ContactSessionBeanRemote"); //NOI18N
201 } catch (final NamingException e) {
203 throw new FaceletException(e);
208 public void addEmailAddress (final String contactEmailAddress) {
210 this.emailAddressList.add(contactEmailAddress);
214 public void afterAdminUpdatedContactDataEvent (@Observes final AdminUpdatedContactEvent event) {
216 System.out.println(MessageFormat.format("ContactWebBean:afterAdminUpdatedContactDataEvent: event={0} - CALLED!", event)); //NOI18N
218 // event should not be null
221 throw new NullPointerException("event is null"); //NOI18N
222 } else if (event.getUpdatedContact()== null) {
224 throw new NullPointerException("event.user is null"); //NOI18N
225 } else if (event.getUpdatedContact().getContactId() == null) {
227 throw new NullPointerException("event.user.userId is null"); //NOI18N
228 } else if (event.getUpdatedContact().getContactId() < 1) {
230 throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getUpdatedContact(), event.getUpdatedContact().getContactId())); //NOI18N
233 // Get iterator from list
234 Iterator<Contact> iterator = this.contactList.iterator();
236 // "Walk" through all entries
237 while (iterator.hasNext()) {
239 Contact next = iterator.next();
241 // Is id number the same?
242 if (Objects.equals(event.getUpdatedContact().getContactId(), next.getContactId())) {
243 // Found entry, so remove it and abort
244 this.contactList.remove(next);
246 // Remove also email from list
247 this.emailAddressList.remove(next.getContactEmailAddress());
252 // Add contact to list
253 this.contactList.add(event.getUpdatedContact());
255 // Add email address to list
256 this.emailAddressList.add(event.getUpdatedContact().getContactEmailAddress());
260 public void afterRegistrationEvent (final @Observes UserRegisteredEvent event) {
262 System.out.println(MessageFormat.format("ContactWebBean:afterRegistration: event={0} - CALLED!", event)); //NOI18N
264 // event should not be null
267 throw new NullPointerException("event is null"); //NOI18N
268 } else if (event.getRegisteredUser() == null) {
270 throw new NullPointerException("event.user is null"); //NOI18N
271 } else if (event.getRegisteredUser().getUserId() == null) {
273 throw new NullPointerException("event.user.userId is null"); //NOI18N
274 } else if (event.getRegisteredUser().getUserId() < 1) {
276 throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getRegisteredUser(), event.getRegisteredUser().getUserId())); //NOI18N
280 Contact registeredContact = event.getRegisteredUser().getUserContact();
283 System.out.println(MessageFormat.format("ContactWebBean:afterRegistration: registeredContact={0}", registeredContact)); //NOI18N
285 // Copy all data from registered->user
286 this.copyContact(registeredContact);
288 // Add user name and email address
289 this.addUserNameEmailAddress(registeredContact);
295 System.out.println("ContactWebBean:afterRegistration: EXIT!"); //NOI18N
299 public void afterUserLogin (final @Observes UserLoggedInEvent event) {
301 System.out.println(MessageFormat.format("ContactWebBean:afterUserLogin: event={0} - CALLED!", event)); //NOI18N
303 // event should not be null
306 throw new NullPointerException("event is null"); //NOI18N
307 } else if (event.getLoggedInUser() == null) {
309 throw new NullPointerException("event.user is null"); //NOI18N
310 } else if (event.getLoggedInUser().getUserId() == null) {
312 throw new NullPointerException("event.user.userId is null"); //NOI18N
313 } else if (event.getLoggedInUser().getUserId() < 1) {
315 throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getLoggedInUser(), event.getLoggedInUser().getUserId())); //NOI18N
318 // Copy all data to this bean
319 this.copyContact(event.getLoggedInUser().getUserContact());
322 System.out.println("ContactWebBean:afterUserLogin - EXIT!"); //NOI18N
329 public void clear () {
332 this.setGender(Gender.UNKNOWN);
333 this.setFirstName(null);
334 this.setFamilyName(null);
335 this.setStreet(null);
336 this.setHouseNumber(null);
337 this.setZipCode(null);
339 this.setCountry(null);
342 this.setEmailAddress(null);
343 this.setEmailAddressRepeat(null);
344 this.setPhoneAreaCode(null);
345 this.setCellphoneCarrier(null);
346 this.setFaxAreaCode(null);
349 this.setBirthday(null);
350 this.setComment(null);
354 public Contact createContactInstance () {
356 //this.getLogger().logTrace("createContactInstance: CALLED!");
358 // Required personal data must be set
359 assert (this.isRequiredPersonalDataSet()) : "not all personal data is set"; //NOI18N
361 // Create new contact instance
362 Contact localContact = new UserContact();
364 // Generate phone number
365 DialableLandLineNumber phone = new LandLineNumber(this.getPhoneCountry(), this.getPhoneAreaCode(), this.getPhoneNumber());
366 DialableCellphoneNumber cellphone = new CellphoneNumber(this.getCellphoneCarrier(), this.getCellphoneNumber());
367 DialableFaxNumber fax = new FaxNumber(this.getFaxCountry(), this.getFaxAreaCode(), this.getFaxNumber());
369 // Create new contact
370 Contact contact = new UserContact(this.getGender(), this.getFirstName(), this.getFamilyName());
371 contact.setContactStreet(this.getStreet());
372 contact.setContactHouseNumber(this.getHouseNumber());
373 contact.setContactZipCode(this.getZipCode());
374 contact.setContactCity(this.getCity());
375 contact.setContactCountry(this.getCountry());
376 contact.setContactEmailAddress(this.getEmailAddress());
378 // Don't set null or wrong references
379 if ((phone instanceof DialableLandLineNumber) && (phone.getPhoneCountry() instanceof Country) && (this.getPhoneAreaCode() != null) && (this.getPhoneNumber() != null) && (this.getPhoneAreaCode() > 0) && (this.getPhoneNumber() > 0)) {
380 // Now the number must be given
381 if (phone.getPhoneAreaCode() == null) {
383 throw new NullPointerException("phone.phoneAreaCode is null"); //NOI18N
384 } else if (phone.getPhoneAreaCode() < 1) {
386 throw new IllegalArgumentException("phone.phoneAreaCode is zero or below."); //NOI18N
387 } else if (phone.getPhoneNumber() == null) {
389 throw new NullPointerException("phone.phoneNumber is null"); //NOI18N
390 } else if (phone.getPhoneNumber() < 1) {
392 throw new IllegalArgumentException("phone.phoneNumber is zero or below."); //NOI18N
396 contact.setContactLandLineNumber(phone);
399 // Don't set null or wrong references
400 if ((fax instanceof DialableFaxNumber) && (fax.getPhoneCountry() instanceof Country) && (this.getFaxAreaCode() != null) && (this.getFaxNumber() != null) && (this.getFaxAreaCode() > 0) && (this.getFaxNumber() > 0)) {
401 // Now the number must be given
402 if (fax.getPhoneAreaCode() == null) {
404 throw new NullPointerException("fax.phoneAreaCode is null"); //NOI18N
405 } else if (fax.getPhoneAreaCode() < 1) {
407 throw new IllegalArgumentException("fax.phoneAreaCode is zero or below."); //NOI18N
408 } else if (fax.getPhoneNumber() == null) {
410 throw new NullPointerException("fax.phoneNumber is null"); //NOI18N
411 } else if (fax.getPhoneNumber() < 1) {
413 throw new IllegalArgumentException("fax.phoneNumber is zero or below."); //NOI18N
417 contact.setContactFaxNumber(fax);
420 // Is the provider set?
421 if ((cellphone instanceof DialableCellphoneNumber) && (this.getCellphoneCarrier() instanceof MobileProvider) && (this.getCellphoneNumber() != null) && (this.getCellphoneNumber() > 0)) {
422 // Is the number set?
423 if (cellphone.getPhoneNumber() == null) {
425 throw new NullPointerException("cellphone.phoneNumber is null"); //NOI18N
426 } else if (cellphone.getPhoneNumber() < 1) {
428 throw new IllegalArgumentException("cellphone.phoneNumber is zero or below."); //NOI18N
431 // Set cellphone number
432 contact.setContactCellphoneNumber(cellphone);
435 contact.setContactBirthday(this.getBirthday());
436 contact.setContactComment(this.getComment());
438 // Created timestamp and ownContact
439 contact.setContactOwnContact(Boolean.TRUE);
442 //this.getLogger().logTrace(MessageFormat.format("createContactInstance: localContact={0} - EXIT!", localContact));
449 public String doChangePersonalContactData () {
450 // This method shall only be called if the user is logged-in
451 if (!this.loginController.isUserLoggedIn()) {
453 throw new IllegalStateException("User is not logged-in"); //NOI18N
454 } else if (!this.isRequiredChangePersonalDataSet()) {
455 // Not all required fields are set
456 throw new FaceletException("Not all required fields are set."); //NOI18N
457 } else if (!this.loginController.ifCurrentPasswordMatches()) {
458 // Password not matching
459 throw new FaceletException(new UserPasswordMismatchException(this.loginController.getLoggedInUser()));
462 // Get contact instance
463 Contact contact = this.loginController.getLoggedInUser().getUserContact();
465 // It should be there, so run some tests on it
466 assert (contact instanceof Contact) : "Instance loginController.loggedInUser.userContact is null"; //NOI18N
467 assert (contact.getContactId() instanceof Long) : "Instance loginController.userContact.contactId is null"; //NOI18N
468 assert (contact.getContactId() > 0) : MessageFormat.format("Instance loginController.userContact.contactId={0} is invalid", contact.getContactId()); //NOI18N
471 contact.setContactGender(this.getGender());
472 contact.setContactFirstName(this.getFirstName());
473 contact.setContactFamilyName(this.getFamilyName());
474 contact.setContactStreet(this.getStreet());
475 contact.setContactHouseNumber(this.getHouseNumber());
476 contact.setContactZipCode(this.getZipCode());
477 contact.setContactCity(this.getCity());
478 contact.setContactCountry(this.getCountry());
480 // Is there a phone number?
481 if (contact.getContactLandLineNumber() instanceof DialableLandLineNumber) {
483 System.out.println(MessageFormat.format("ContactWebBean:doChangePersonalData: phoneId={0}", contact.getContactLandLineNumber().getPhoneId())); //NOI18N
485 // Yes, then update as well
486 contact.getContactLandLineNumber().setPhoneAreaCode(this.getPhoneAreaCode());
487 contact.getContactLandLineNumber().setPhoneNumber(this.getPhoneNumber());
490 // Is there a fax number?
491 if (contact.getContactFaxNumber() instanceof DialableFaxNumber) {
493 System.out.println(MessageFormat.format("ContactWebBean:doChangePersonalData: faxId={0}", contact.getContactFaxNumber().getPhoneId())); //NOI18N
495 // Yes, then update as well
496 contact.getContactFaxNumber().setPhoneAreaCode(this.getFaxAreaCode());
497 contact.getContactFaxNumber().setPhoneNumber(this.getFaxNumber());
500 // Is there a cellphone number?
501 if (contact.getContactCellphoneNumber() instanceof DialableCellphoneNumber) {
503 System.out.println(MessageFormat.format("ContactWebBean:doChangePersonalData: cellPhoneId={0}", contact.getContactCellphoneNumber().getPhoneId())); //NOI18N
505 // Yes, then update as well
506 contact.getContactCellphoneNumber().setCellphoneProvider(this.getCellphoneCarrier());
507 contact.getContactCellphoneNumber().setPhoneNumber(this.getCellphoneNumber());
510 // Send it to the EJB
511 this.contactBean.updateContactPersonalData(contact);
514 return "contact_data_saved"; //NOI18N
518 public Date getBirthday () {
519 return this.birthday;
523 public void setBirthday (final Date birthday) {
524 this.birthday = birthday;
528 public MobileProvider getCellphoneCarrier () {
529 return this.cellphoneCarrier;
533 public void setCellphoneCarrier (final MobileProvider cellphoneCarrier) {
534 this.cellphoneCarrier = cellphoneCarrier;
538 public Long getCellphoneNumber () {
539 return this.cellphoneNumber;
543 public void setCellphoneNumber (Long cellphoneNumber) {
544 this.cellphoneNumber = cellphoneNumber;
548 public String getCity () {
553 public void setCity (final String city) {
558 public String getComment () {
563 public void setComment (final String comment) {
564 this.comment = comment;
568 public Country getCountry () {
573 public void setCountry (final Country country) {
574 this.country = country;
578 public String getEmailAddress () {
579 return this.emailAddress;
583 public void setEmailAddress (final String emailAddress) {
584 this.emailAddress = emailAddress;
588 public String getEmailAddressRepeat () {
589 return this.emailAddressRepeat;
593 public void setEmailAddressRepeat (final String emailAddressRepeat) {
594 this.emailAddressRepeat = emailAddressRepeat;
598 public String getFamilyName () {
599 return this.familyName;
603 public void setFamilyName (final String familyName) {
604 this.familyName = familyName;
608 public Integer getFaxAreaCode () {
609 return this.faxAreaCode;
613 public void setFaxAreaCode (final Integer faxAreaCode) {
614 this.faxAreaCode = faxAreaCode;
618 public Country getFaxCountry () {
619 return this.faxCountry;
623 public void setFaxCountry (final Country faxCountry) {
624 this.faxCountry = faxCountry;
628 public Long getFaxNumber () {
629 return this.faxNumber;
633 public void setFaxNumber (final Long faxNumber) {
634 this.faxNumber = faxNumber;
638 public String getFirstName () {
639 return this.firstName;
643 public void setFirstName (final String firstName) {
644 this.firstName = firstName;
648 public Gender getGender () {
653 public void setGender (final Gender gender) {
654 this.gender = gender;
658 public Short getHouseNumber () {
659 return this.houseNumber;
663 public void setHouseNumber (final Short houseNumber) {
664 this.houseNumber = houseNumber;
668 public Integer getPhoneAreaCode () {
669 return this.phoneAreaCode;
673 public void setPhoneAreaCode (final Integer phoneAreaCode) {
674 this.phoneAreaCode = phoneAreaCode;
678 public Country getPhoneCountry () {
679 return this.phoneCountry;
683 public void setPhoneCountry (final Country phoneCountry) {
684 this.phoneCountry = phoneCountry;
688 public Long getPhoneNumber () {
689 return this.phoneNumber;
693 public void setPhoneNumber (final Long phoneNumber) {
694 this.phoneNumber = phoneNumber;
698 public String getStreet () {
703 public void setStreet (final String street) {
704 this.street = street;
708 public Integer getZipCode () {
713 public void setZipCode (final Integer zipCode) {
714 this.zipCode = zipCode;
718 * Post-initialization of this class
721 public void init () {
722 // Get full email address list for reducing EJB calls
723 this.emailAddressList = this.contactBean.getEmailAddressList();
725 // Get full contact list
726 this.contactList = this.contactBean.getAllContacts();
730 public boolean isEmailAddressRegistered (final Contact contact) {
731 return ((this.emailAddressList instanceof List) && (this.emailAddressList.contains(contact.getContactEmailAddress())));
735 public boolean isRequiredChangePersonalDataSet () {
736 return ((this.getGender() != null) &&
737 (this.getFirstName() != null) &&
738 (this.getFamilyName() != null) &&
739 (this.getStreet() != null) &&
740 (this.getHouseNumber() != null) &&
741 (this.getZipCode() != null) &&
742 (this.getCity() != null));
746 public boolean isRequiredPersonalDataSet () {
747 return ((this.getGender() != null) &&
748 (this.getFirstName() != null) &&
749 (this.getFamilyName() != null) &&
750 (this.getStreet() != null) &&
751 (this.getHouseNumber() != null) &&
752 (this.getZipCode() != null) &&
753 (this.getCity() != null) &&
754 (this.getEmailAddress() != null) &&
755 (this.getEmailAddressRepeat() != null));
759 public boolean isSameEmailAddressEntered () {
760 return (Objects.equals(this.getEmailAddress(), this.getEmailAddressRepeat()));
764 public Contact lookupContactById (final Long contactId) throws ContactNotFoundException {
766 Contact localContact = null;
771 // Try to lookup it in visible user list
772 for (final Iterator<Contact> iterator = this.contactList.iterator(); iterator.hasNext();) {
774 Contact next = iterator.next();
776 // Is the user id found?
777 if (Objects.equals(next.getContactId(), contactId)) {
778 // Copy to other variable
785 if (null == localContact) {
786 // Not visible for the current user
787 throw new ContactNotFoundException(contactId);
790 // Copy all data to this bean
791 this.copyContact(localContact);
798 public void updateContactDataFromController (final Contact userContact) {
799 // Is the instance valid?
800 if (null == userContact) {
802 throw new NullPointerException("userContact is null"); //NOI18N
803 } else if (userContact.getContactId() == null) {
805 throw new NullPointerException("userContact.contactId is null"); //NOI18N
806 } else if (userContact.getContactId() < 1) {
807 // Not valid id number
808 throw new IllegalArgumentException(MessageFormat.format("userContact.contactId={0} is not valid.", userContact.getContactId())); //NOI18N
812 this.copyContact(userContact);
816 * Adds email address to bean's internal list.
818 * @param contact Contact instance
820 private void addUserNameEmailAddress (final Contact contact) {
821 // Make sure the entry is not added yet
822 if (this.emailAddressList.contains(contact.getContactEmailAddress())) {
824 throw new IllegalArgumentException(MessageFormat.format("Email address {0} already added.", contact.getContactEmailAddress())); //NOI18N
828 this.emailAddressList.add(contact.getContactEmailAddress());
832 * Copies given contact into the controller
834 * @param contact Contact instance
836 private void copyContact (final Contact contact) {
839 this.setGender(contact.getContactGender());
840 this.setFirstName(contact.getContactFirstName());
841 this.setFamilyName(contact.getContactFamilyName());
842 this.setStreet(contact.getContactStreet());
843 this.setHouseNumber(contact.getContactHouseNumber());
844 this.setZipCode(contact.getContactZipCode());
845 this.setCity(contact.getContactCity());
846 this.setCountry(contact.getContactCountry());
848 // Get cellphone, phone and fax instance
849 DialableCellphoneNumber cellphone = contact.getContactCellphoneNumber();
850 DialableFaxNumber fax = contact.getContactFaxNumber();
851 DialableLandLineNumber phone = contact.getContactLandLineNumber();
854 if ((phone instanceof DialableLandLineNumber) && (phone.getPhoneAreaCode() > 0)) {
855 this.setPhoneCountry(phone.getPhoneCountry());
856 this.setPhoneAreaCode(phone.getPhoneAreaCode());
857 this.setPhoneNumber(phone.getPhoneNumber());
859 if ((cellphone instanceof DialableCellphoneNumber) && (cellphone.getCellphoneProvider() instanceof MobileProvider)) {
860 this.setCellphoneCarrier(cellphone.getCellphoneProvider());
861 this.setCellphoneNumber(cellphone.getPhoneNumber());
863 if ((fax instanceof DialableFaxNumber) && (fax.getPhoneAreaCode() > 0)) {
864 this.setFaxCountry(fax.getPhoneCountry());
865 this.setFaxAreaCode(fax.getPhoneAreaCode());
866 this.setFaxNumber(fax.getPhoneNumber());
868 this.setEmailAddress(contact.getContactEmailAddress());
871 this.setBirthday(contact.getContactBirthday());
872 this.setComment(contact.getContactComment());