2 * Copyright (C) 2016 Roland Häder
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 javax.annotation.PostConstruct;
23 import javax.enterprise.context.RequestScoped;
24 import javax.enterprise.event.Event;
25 import javax.enterprise.inject.Any;
26 import javax.faces.context.FacesContext;
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.BaseAddressbookController;
34 import org.mxchange.addressbook.beans.helper.AddressbookWebRequestHelperController;
35 import org.mxchange.jcontacts.contact.AdminContactSessionBeanRemote;
36 import org.mxchange.jcontacts.contact.Contact;
37 import org.mxchange.jcontacts.contact.ContactSessionBeanRemote;
38 import org.mxchange.jcontacts.contact.UserContact;
39 import org.mxchange.jcontacts.contact.gender.Gender;
40 import org.mxchange.jcontacts.contact.utils.ContactUtils;
41 import org.mxchange.jcontacts.events.contact.add.AdminAddedContactEvent;
42 import org.mxchange.jcontacts.events.contact.add.ObservableAdminAddedContactEvent;
43 import org.mxchange.jcontacts.events.contact.update.AdminUpdatedContactEvent;
44 import org.mxchange.jcontacts.events.contact.update.ObservableAdminUpdatedContactEvent;
45 import org.mxchange.jcontacts.exceptions.ContactAlreadyAddedException;
46 import org.mxchange.jcountry.data.Country;
47 import org.mxchange.jphone.phonenumbers.DialableNumber;
48 import org.mxchange.jphone.phonenumbers.fax.DialableFaxNumber;
49 import org.mxchange.jphone.phonenumbers.fax.FaxNumber;
50 import org.mxchange.jphone.phonenumbers.landline.DialableLandLineNumber;
51 import org.mxchange.jphone.phonenumbers.landline.LandLineNumber;
52 import org.mxchange.jphone.phonenumbers.mobile.DialableMobileNumber;
53 import org.mxchange.jphone.phonenumbers.mobile.MobileNumber;
54 import org.mxchange.jphone.phonenumbers.mobileprovider.MobileProvider;
57 * An administrative user bean (controller)
59 * @author Roland Häder<roland@mxchange.org>
61 @Named ("adminContactController")
63 public class AddressbookAdminContactWebRequestBean extends BaseAddressbookController implements AddressbookAdminContactWebRequestController {
68 private static final long serialVersionUID = 542_145_347_916L;
71 * An event fired when the administrator has added a new contact
75 private Event<ObservableAdminAddedContactEvent> addedContactEvent;
78 * Administrative contact EJB
80 private AdminContactSessionBeanRemote adminContactBean;
83 * Bean helper instance
86 private AddressbookWebRequestHelperController beanHelper;
91 private Date birthday;
101 private String comment;
104 * Remote contact bean
106 private ContactSessionBeanRemote contactBean;
109 * General contact controller
112 private AddressbookContactWebSessionController contactController;
117 private Long contactId;
122 private Country country;
127 private String emailAddress;
132 private String familyName;
135 * Fax number's area code
137 private Integer faxAreaCode;
140 * Country instance for fax number
142 private Country faxCountry;
152 private Long faxNumber;
157 private String firstName;
162 private Gender gender;
167 private Short houseNumber;
170 * House number extension
172 private String houseNumberExtension;
175 * Whether a fax entry has been unlinked
177 private boolean isFaxUnlinked;
180 * Whether a land-line number has been unlinked
182 private boolean isLandLineUnlinked;
185 * Whether a mobile entry has been unlinked
187 private boolean isMobileUnlinked;
190 * Land-line id number
192 private Long landLineId;
195 * Mobile number's carrier
197 private MobileProvider mobileCarrier;
202 private Long mobileId;
207 private Long mobileNumber;
210 * Phone number area code
212 private Integer phoneAreaCode;
215 * Country instance for phone number
217 private Country phoneCountry;
222 private Long phoneNumber;
227 private String street;
232 private String title;
235 * An event fired when the administrator has updated contact data
239 private Event<ObservableAdminUpdatedContactEvent> updatedContactEvent;
244 private Integer zipCode;
247 * Default constructor
249 public AddressbookAdminContactWebRequestBean () {
250 // Call super constructor
255 public String addContact () {
256 // Are all minimum fields set?
257 if (this.getGender() == null) {
259 throw new NullPointerException("gender is null"); //NOI18N
260 } else if (this.getFirstName() == null) {
262 throw new NullPointerException("firstName is null"); //NOI18N
263 } else if (this.getFirstName().isEmpty()) {
265 throw new IllegalStateException("firstName is empty"); //NOI18N
266 } else if (this.getFamilyName() == null) {
268 throw new NullPointerException("familyName is null"); //NOI18N
269 } else if (this.getFamilyName().isEmpty()) {
271 throw new IllegalStateException("familyName is empty"); //NOI18N
274 // Create new contact instance
275 Contact contact = this.createContactInstance();
277 // Default is not same contact
278 if (this.isSameContactFound(contact)) {
279 // Already registered
280 throw new FaceletException(new ContactAlreadyAddedException(contact));
284 Contact updatedContact;
289 updatedContact = this.adminContactBean.addContact(contact);
290 } catch (final ContactAlreadyAddedException ex) {
292 throw new FaceletException(ex);
296 this.addedContactEvent.fire(new AdminAddedContactEvent(updatedContact));
302 return "admin_list_contact"; //NOI18N
306 public void copyContactToController (final Contact contact) {
307 // The contact instance must be valid
308 if (null == contact) {
310 throw new NullPointerException("contact is null"); //NOI18N
311 } else if (contact.getContactId() == null) {
313 throw new NullPointerException("contact.contactId is null"); //NOI18N //NOI18N
314 } else if (contact.getContactId() < 1) {
316 throw new IllegalStateException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N
319 // Set all fields: contact
320 this.setContactId(contact.getContactId());
321 this.setTitle(contact.getContactTitle());
322 this.setBirthday(contact.getContactBirthday());
323 this.setCity(contact.getContactCity());
324 this.setComment(contact.getContactComment());
325 this.setCountry(contact.getContactCountry());
326 this.setEmailAddress(contact.getContactEmailAddress());
327 this.setFamilyName(contact.getContactFamilyName());
328 this.setFirstName(contact.getContactFirstName());
329 this.setGender(contact.getContactGender());
330 this.setHouseNumber(contact.getContactHouseNumber());
331 this.setHouseNumberExtension(contact.getContactHouseNumberExtension());
332 this.setStreet(contact.getContactStreet());
333 this.setZipCode(contact.getContactZipCode());
336 if (contact.getContactMobileNumber() instanceof DialableMobileNumber) {
337 this.setMobileId(contact.getContactMobileNumber().getPhoneId());
338 this.setMobileCarrier(contact.getContactMobileNumber().getMobileProvider());
339 this.setMobileNumber(contact.getContactMobileNumber().getPhoneNumber());
343 if (contact.getContactFaxNumber() instanceof DialableFaxNumber) {
344 this.setFaxId(contact.getContactFaxNumber().getPhoneId());
345 this.setFaxAreaCode(contact.getContactFaxNumber().getPhoneAreaCode());
346 this.setFaxCountry(contact.getContactFaxNumber().getPhoneCountry());
347 this.setFaxNumber(contact.getContactFaxNumber().getPhoneNumber());
351 if (contact.getContactLandLineNumber() instanceof DialableLandLineNumber) {
352 this.setLandLineId(contact.getContactLandLineNumber().getPhoneId());
353 this.setPhoneAreaCode(contact.getContactLandLineNumber().getPhoneAreaCode());
354 this.setPhoneCountry(contact.getContactLandLineNumber().getPhoneCountry());
355 this.setPhoneNumber(contact.getContactLandLineNumber().getPhoneNumber());
360 public Contact createContactInstance () {
361 // Generate phone number
362 DialableLandLineNumber phone = new LandLineNumber(this.getPhoneCountry(), this.getPhoneAreaCode(), this.getPhoneNumber());
363 DialableMobileNumber mobile = new MobileNumber(this.getMobileCarrier(), this.getMobileNumber());
364 DialableFaxNumber fax = new FaxNumber(this.getFaxCountry(), this.getFaxAreaCode(), this.getFaxNumber());
366 // Create new instance
367 Contact contact = new UserContact(this.getGender(), this.getFirstName(), this.getFamilyName());
369 // Check if contact instance is in helper and valid
370 if (null == contact) {
372 throw new NullPointerException("beanHelper.contact is null"); //NOI18N
373 } else if (contact.getContactId() == null) {
375 throw new NullPointerException("beanHelper.contact.contactId is null"); //NOI18N //NOI18N
376 } else if (contact.getContactId() < 1) {
378 throw new IllegalStateException(MessageFormat.format("beanHelper.contact.contactId={0} is invalid", contact.getContactId())); //NOI18N
381 // Update all data in contact
382 this.updateContactData(contact);
384 // Call EJB for updating contact data
385 Contact updatedContact = this.contactBean.updateContactData(contact, this.isMobileUnlinked, this.isLandLineUnlinked, this.isFaxUnlinked);
388 this.updatedContactEvent.fire(new AdminUpdatedContactEvent(updatedContact));
398 public String editContactData () {
399 // Get contact instance
400 Contact contact = this.beanHelper.getContact();
402 // Check if contact instance is in helper and valid
403 if (null == contact) {
405 throw new NullPointerException("beanHelper.contact is null"); //NOI18N
406 } else if (contact.getContactId() == null) {
408 throw new NullPointerException("beanHelper.contact.contactId is null"); //NOI18N //NOI18N
409 } else if (contact.getContactId() < 1) {
411 throw new IllegalStateException(MessageFormat.format("beanHelper.contact.contactId={0} is invalid", contact.getContactId())); //NOI18N
414 // Update all data in contact
415 this.updateContactData(contact);
417 // Call EJB for updating contact data
418 Contact updatedContact = this.contactBean.updateContactData(contact, this.isMobileUnlinked, this.isLandLineUnlinked, this.isFaxUnlinked);
421 this.updatedContactEvent.fire(new AdminUpdatedContactEvent(updatedContact));
426 // Return to contact list (for now)
427 return "admin_list_contact"; //NOI18N
431 public String generateMobileNumber (final DialableMobileNumber mobileNumber) {
433 if (null == mobileNumber) {
439 String number = String.format(
441 mobileNumber.getMobileProvider().getProviderCountry().getCountryExternalDialPrefix(),
442 mobileNumber.getMobileProvider().getProviderDialPrefix(),
443 mobileNumber.getPhoneNumber()
451 public String generatePhoneNumber (final DialableNumber phoneNumber) {
453 if (null == phoneNumber) {
459 String number = String.format(
461 phoneNumber.getPhoneCountry().getCountryExternalDialPrefix(),
462 phoneNumber.getPhoneAreaCode(),
463 phoneNumber.getPhoneNumber()
471 @SuppressWarnings ("ReturnOfDateField")
472 public Date getBirthday () {
473 return this.birthday;
477 @SuppressWarnings ("AssignmentToDateFieldFromParameter")
478 public void setBirthday (final Date birthday) {
479 this.birthday = birthday;
483 public String getCity () {
488 public void setCity (final String city) {
493 public String getComment () {
498 public void setComment (final String comment) {
499 this.comment = comment;
503 public Long getContactId () {
504 return this.contactId;
508 public void setContactId (final Long contactId) {
509 this.contactId = contactId;
513 public Country getCountry () {
518 public void setCountry (final Country country) {
519 this.country = country;
523 public String getEmailAddress () {
524 return this.emailAddress;
528 public void setEmailAddress (final String emailAddress) {
529 this.emailAddress = emailAddress;
533 public String getFamilyName () {
534 return this.familyName;
538 public void setFamilyName (final String familyName) {
539 this.familyName = familyName;
543 public Integer getFaxAreaCode () {
544 return this.faxAreaCode;
548 public void setFaxAreaCode (final Integer faxAreaCode) {
549 this.faxAreaCode = faxAreaCode;
553 public Country getFaxCountry () {
554 return this.faxCountry;
558 public void setFaxCountry (final Country faxCountry) {
559 this.faxCountry = faxCountry;
563 public Long getFaxId () {
568 public void setFaxId (final Long faxId) {
573 public Long getFaxNumber () {
574 return this.faxNumber;
578 public void setFaxNumber (final Long faxNumber) {
579 this.faxNumber = faxNumber;
583 public String getFirstName () {
584 return this.firstName;
588 public void setFirstName (final String firstName) {
589 this.firstName = firstName;
593 public Gender getGender () {
598 public void setGender (final Gender gender) {
599 this.gender = gender;
603 public Short getHouseNumber () {
604 return this.houseNumber;
608 public void setHouseNumber (final Short houseNumber) {
609 this.houseNumber = houseNumber;
613 public String getHouseNumberExtension () {
614 return this.houseNumberExtension;
618 public void setHouseNumberExtension (final String houseNumberExtension) {
619 this.houseNumberExtension = houseNumberExtension;
623 public Long getLandLineId () {
624 return this.landLineId;
628 public void setLandLineId (final Long landLineId) {
629 this.landLineId = landLineId;
633 public MobileProvider getMobileCarrier () {
634 return this.mobileCarrier;
638 public void setMobileCarrier (final MobileProvider mobileCarrier) {
639 this.mobileCarrier = mobileCarrier;
643 public Long getMobileId () {
644 return this.mobileId;
648 public void setMobileId (final Long mobileId) {
649 this.mobileId = mobileId;
653 public Long getMobileNumber () {
654 return this.mobileNumber;
658 public void setMobileNumber (Long mobileNumber) {
659 this.mobileNumber = mobileNumber;
663 public Integer getPhoneAreaCode () {
664 return this.phoneAreaCode;
668 public void setPhoneAreaCode (final Integer phoneAreaCode) {
669 this.phoneAreaCode = phoneAreaCode;
673 public Country getPhoneCountry () {
674 return this.phoneCountry;
678 public void setPhoneCountry (final Country phoneCountry) {
679 this.phoneCountry = phoneCountry;
683 public Long getPhoneNumber () {
684 return this.phoneNumber;
688 public void setPhoneNumber (final Long phoneNumber) {
689 this.phoneNumber = phoneNumber;
693 public String getStreet () {
698 public void setStreet (final String street) {
699 this.street = street;
703 public String getTitle () {
708 public void setTitle (final String title) {
713 public Integer getZipCode () {
718 public void setZipCode (final Integer zipCode) {
719 this.zipCode = zipCode;
723 * Post-initialization of this class
726 public void init () {
729 // Get initial context
730 Context context = new InitialContext();
733 this.contactBean = (ContactSessionBeanRemote) context.lookup("java:global/addressbook-ejb/contact!org.mxchange.jcontacts.contact.ContactSessionBeanRemote"); //NOI18N
734 } catch (final NamingException e) {
736 throw new FaceletException(e);
741 public boolean isGenderRequired () {
742 // Get context parameter
743 String contextParameter = FacesContext.getCurrentInstance().getExternalContext().getInitParameter("is_admin_gender_enabled"); //NOI18N
746 boolean isRequired = ((contextParameter instanceof String) && (contextParameter.toLowerCase().equals("true"))); //NOI18N
755 private void clear () {
758 this.setGender(null);
760 this.setFirstName(null);
761 this.setFamilyName(null);
762 this.setStreet(null);
763 this.setHouseNumber(null);
764 this.setHouseNumberExtension(null);
765 this.setZipCode(null);
767 this.setCountry(null);
770 this.setEmailAddress(null);
771 this.setPhoneCountry(null);
772 this.setPhoneAreaCode(null);
773 this.setPhoneNumber(null);
774 this.setMobileCarrier(null);
775 this.setMobileNumber(null);
776 this.setFaxCountry(null);
777 this.setFaxAreaCode(null);
778 this.setFaxNumber(null);
781 this.setBirthday(null);
782 this.setComment(null);
786 * Checks whether the given contact is found
788 * @param contact Contact inastance
790 * @return Wether contact has been found
792 private boolean isSameContactFound (final Contact contact) {
793 // Default is not found
794 boolean IsFound = false;
797 Iterator<Contact> iterator = this.contactController.allContacts().iterator();
800 while (iterator.hasNext()) {
802 Contact next = iterator.next();
805 if (ContactUtils.isSameContact(contact, next)) {
806 // Yes, then abort loop
817 * Updates all data in contact instance.
819 * @param contact Contact instance
821 private void updateContactData (final Contact contact) {
822 // Contact instance should be valid
823 if (null == contact) {
825 throw new NullPointerException("contact is null"); //NOI18N
826 } else if (contact.getContactId() == null) {
828 throw new NullPointerException("contact.contactId is null"); //NOI18N //NOI18N
829 } else if (contact.getContactId() < 1) {
831 throw new IllegalStateException(MessageFormat.format("contact.contactId={0} is invalid", contact.getContactId())); //NOI18N
835 contact.setContactGender(this.getGender());
836 contact.setContactTitle(this.getTitle());
837 contact.setContactFirstName(this.getFirstName());
838 contact.setContactFamilyName(this.getFamilyName());
839 contact.setContactStreet(this.getStreet());
840 contact.setContactHouseNumber(this.getHouseNumber());
841 contact.setContactHouseNumberExtension(this.getHouseNumberExtension());
842 contact.setContactZipCode(this.getZipCode());
843 contact.setContactCity(this.getCity());
844 contact.setContactCountry(this.getCountry());
846 // Update contact's mobile number
847 this.isMobileUnlinked = ContactUtils.updateMobileNumber(contact, this.getMobileCarrier(), this.getMobileNumber());
849 // Update contact's land-line number
850 this.isLandLineUnlinked = ContactUtils.updateLandLineNumber(contact, this.getPhoneCountry(), this.getPhoneAreaCode(), this.getPhoneNumber());
852 // Update contact's fax number
853 this.isFaxUnlinked = ContactUtils.updateFaxNumber(contact, this.getFaxCountry(), this.getFaxAreaCode(), this.getFaxNumber());