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.AddressbookWebRequestController;
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.AdminContactAddedEvent;
43 import org.mxchange.jcontacts.events.contact.update.AdminContactUpdatedEvent;
44 import org.mxchange.jcontacts.events.contact.update.AdminUpdatedContactEvent;
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<AdminAddedContactEvent> addedContactEvent;
78 * Administrative contact EJB
80 private AdminContactSessionBeanRemote adminContactBean;
83 * Admin helper instance
86 private AddressbookWebRequestController beanHelper;
91 private Date birthday;
101 private String comment;
104 * Remote contact bean
106 private final 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<AdminUpdatedContactEvent> updatedContactEvent;
244 private Integer zipCode;
247 * Default constructor
249 public AddressbookAdminContactWebRequestBean () {
252 // Get initial context
253 Context context = new InitialContext();
256 this.contactBean = (ContactSessionBeanRemote) context.lookup("java:global/addressbook-ejb/contact!org.mxchange.jcontacts.contact.ContactSessionBeanRemote"); //NOI18N
257 } catch (final NamingException e) {
259 throw new FaceletException(e);
264 public String addContact () {
265 // Are all minimum fields set?
266 if (this.getGender() == null) {
268 throw new NullPointerException("gender is null"); //NOI18N
269 } else if (this.getFirstName() == null) {
271 throw new NullPointerException("firstName is null"); //NOI18N
272 } else if (this.getFirstName().isEmpty()) {
274 throw new IllegalStateException("firstName is empty"); //NOI18N
275 } else if (this.getFamilyName() == null) {
277 throw new NullPointerException("familyName is null"); //NOI18N
278 } else if (this.getFamilyName().isEmpty()) {
280 throw new IllegalStateException("familyName is empty"); //NOI18N
283 // Create new contact instance
284 Contact contact = this.createContactInstance();
286 // Default is not same contact
287 if (this.isSameContactFound(contact)) {
288 // Already registered
289 throw new FaceletException(new ContactAlreadyAddedException(contact));
293 Contact updatedContact;
298 updatedContact = this.adminContactBean.addContact(contact);
299 } catch (final ContactAlreadyAddedException ex) {
301 throw new FaceletException(ex);
305 this.addedContactEvent.fire(new AdminContactAddedEvent(updatedContact));
311 return "admin_list_contact"; //NOI18N
315 public void copyContactToController (final Contact contact) {
316 // The contact instance must be valid
317 if (null == contact) {
319 throw new NullPointerException("contact is null"); //NOI18N
320 } else if (contact.getContactId() == null) {
322 throw new NullPointerException("contact.contactId is null"); //NOI18N //NOI18N
323 } else if (contact.getContactId() < 1) {
325 throw new IllegalStateException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N
328 // Set all fields: contact
329 this.setContactId(contact.getContactId());
330 this.setTitle(contact.getContactTitle());
331 this.setBirthday(contact.getContactBirthday());
332 this.setCity(contact.getContactCity());
333 this.setComment(contact.getContactComment());
334 this.setCountry(contact.getContactCountry());
335 this.setEmailAddress(contact.getContactEmailAddress());
336 this.setFamilyName(contact.getContactFamilyName());
337 this.setFirstName(contact.getContactFirstName());
338 this.setGender(contact.getContactGender());
339 this.setHouseNumber(contact.getContactHouseNumber());
340 this.setHouseNumberExtension(contact.getContactHouseNumberExtension());
341 this.setStreet(contact.getContactStreet());
342 this.setZipCode(contact.getContactZipCode());
345 if (contact.getContactMobileNumber() instanceof DialableMobileNumber) {
346 this.setMobileId(contact.getContactMobileNumber().getPhoneId());
347 this.setMobileCarrier(contact.getContactMobileNumber().getMobileProvider());
348 this.setMobileNumber(contact.getContactMobileNumber().getPhoneNumber());
352 if (contact.getContactFaxNumber() instanceof DialableFaxNumber) {
353 this.setFaxId(contact.getContactFaxNumber().getPhoneId());
354 this.setFaxAreaCode(contact.getContactFaxNumber().getPhoneAreaCode());
355 this.setFaxCountry(contact.getContactFaxNumber().getPhoneCountry());
356 this.setFaxNumber(contact.getContactFaxNumber().getPhoneNumber());
360 if (contact.getContactLandLineNumber() instanceof DialableLandLineNumber) {
361 this.setLandLineId(contact.getContactLandLineNumber().getPhoneId());
362 this.setPhoneAreaCode(contact.getContactLandLineNumber().getPhoneAreaCode());
363 this.setPhoneCountry(contact.getContactLandLineNumber().getPhoneCountry());
364 this.setPhoneNumber(contact.getContactLandLineNumber().getPhoneNumber());
369 public Contact createContactInstance () {
370 // Generate phone number
371 DialableLandLineNumber phone = new LandLineNumber(this.getPhoneCountry(), this.getPhoneAreaCode(), this.getPhoneNumber());
372 DialableMobileNumber mobile = new MobileNumber(this.getMobileCarrier(), this.getMobileNumber());
373 DialableFaxNumber fax = new FaxNumber(this.getFaxCountry(), this.getFaxAreaCode(), this.getFaxNumber());
375 // Create new instance
376 Contact contact = new UserContact(this.getGender(), this.getFirstName(), this.getFamilyName());
378 // Check if contact instance is in helper and valid
379 if (null == contact) {
381 throw new NullPointerException("beanHelper.contact is null"); //NOI18N
382 } else if (contact.getContactId() == null) {
384 throw new NullPointerException("beanHelper.contact.contactId is null"); //NOI18N //NOI18N
385 } else if (contact.getContactId() < 1) {
387 throw new IllegalStateException(MessageFormat.format("beanHelper.contact.contactId={0} is invalid", contact.getContactId())); //NOI18N
390 // Update all data in contact
391 this.updateContactData(contact);
393 // Call EJB for updating contact data
394 Contact updatedContact = this.contactBean.updateContactData(contact, this.isMobileUnlinked, this.isLandLineUnlinked, this.isFaxUnlinked);
397 this.updatedContactEvent.fire(new AdminContactUpdatedEvent(updatedContact));
407 public String editContactData () {
408 // Get contact instance
409 Contact contact = this.beanHelper.getContact();
411 // Check if contact instance is in helper and valid
412 if (null == contact) {
414 throw new NullPointerException("beanHelper.contact is null"); //NOI18N
415 } else if (contact.getContactId() == null) {
417 throw new NullPointerException("beanHelper.contact.contactId is null"); //NOI18N //NOI18N
418 } else if (contact.getContactId() < 1) {
420 throw new IllegalStateException(MessageFormat.format("beanHelper.contact.contactId={0} is invalid", contact.getContactId())); //NOI18N
423 // Update all data in contact
424 this.updateContactData(contact);
426 // Call EJB for updating contact data
427 Contact updatedContact = this.contactBean.updateContactData(contact, this.isMobileUnlinked, this.isLandLineUnlinked, this.isFaxUnlinked);
430 this.updatedContactEvent.fire(new AdminContactUpdatedEvent(updatedContact));
435 // Return to contact list (for now)
436 return "admin_list_contact"; //NOI18N
440 public String generateMobileNumber (final DialableMobileNumber mobileNumber) {
442 if (null == mobileNumber) {
448 String number = String.format(
450 mobileNumber.getMobileProvider().getProviderCountry().getCountryExternalDialPrefix(),
451 mobileNumber.getMobileProvider().getProviderDialPrefix(),
452 mobileNumber.getPhoneNumber()
460 public String generatePhoneNumber (final DialableNumber phoneNumber) {
462 if (null == phoneNumber) {
468 String number = String.format(
470 phoneNumber.getPhoneCountry().getCountryExternalDialPrefix(),
471 phoneNumber.getPhoneAreaCode(),
472 phoneNumber.getPhoneNumber()
480 @SuppressWarnings ("ReturnOfDateField")
481 public Date getBirthday () {
482 return this.birthday;
486 @SuppressWarnings ("AssignmentToDateFieldFromParameter")
487 public void setBirthday (final Date birthday) {
488 this.birthday = birthday;
492 public String getCity () {
497 public void setCity (final String city) {
502 public String getComment () {
507 public void setComment (final String comment) {
508 this.comment = comment;
512 public Long getContactId () {
513 return this.contactId;
517 public void setContactId (final Long contactId) {
518 this.contactId = contactId;
522 public Country getCountry () {
527 public void setCountry (final Country country) {
528 this.country = country;
532 public String getEmailAddress () {
533 return this.emailAddress;
537 public void setEmailAddress (final String emailAddress) {
538 this.emailAddress = emailAddress;
542 public String getFamilyName () {
543 return this.familyName;
547 public void setFamilyName (final String familyName) {
548 this.familyName = familyName;
552 public Integer getFaxAreaCode () {
553 return this.faxAreaCode;
557 public void setFaxAreaCode (final Integer faxAreaCode) {
558 this.faxAreaCode = faxAreaCode;
562 public Country getFaxCountry () {
563 return this.faxCountry;
567 public void setFaxCountry (final Country faxCountry) {
568 this.faxCountry = faxCountry;
572 public Long getFaxId () {
577 public void setFaxId (final Long faxId) {
582 public Long getFaxNumber () {
583 return this.faxNumber;
587 public void setFaxNumber (final Long faxNumber) {
588 this.faxNumber = faxNumber;
592 public String getFirstName () {
593 return this.firstName;
597 public void setFirstName (final String firstName) {
598 this.firstName = firstName;
602 public Gender getGender () {
607 public void setGender (final Gender gender) {
608 this.gender = gender;
612 public Short getHouseNumber () {
613 return this.houseNumber;
617 public void setHouseNumber (final Short houseNumber) {
618 this.houseNumber = houseNumber;
622 public String getHouseNumberExtension () {
623 return this.houseNumberExtension;
627 public void setHouseNumberExtension (final String houseNumberExtension) {
628 this.houseNumberExtension = houseNumberExtension;
632 public Long getLandLineId () {
633 return this.landLineId;
637 public void setLandLineId (final Long landLineId) {
638 this.landLineId = landLineId;
642 public MobileProvider getMobileCarrier () {
643 return this.mobileCarrier;
647 public void setMobileCarrier (final MobileProvider mobileCarrier) {
648 this.mobileCarrier = mobileCarrier;
652 public Long getMobileId () {
653 return this.mobileId;
657 public void setMobileId (final Long mobileId) {
658 this.mobileId = mobileId;
662 public Long getMobileNumber () {
663 return this.mobileNumber;
667 public void setMobileNumber (Long mobileNumber) {
668 this.mobileNumber = mobileNumber;
672 public Integer getPhoneAreaCode () {
673 return this.phoneAreaCode;
677 public void setPhoneAreaCode (final Integer phoneAreaCode) {
678 this.phoneAreaCode = phoneAreaCode;
682 public Country getPhoneCountry () {
683 return this.phoneCountry;
687 public void setPhoneCountry (final Country phoneCountry) {
688 this.phoneCountry = phoneCountry;
692 public Long getPhoneNumber () {
693 return this.phoneNumber;
697 public void setPhoneNumber (final Long phoneNumber) {
698 this.phoneNumber = phoneNumber;
702 public String getStreet () {
707 public void setStreet (final String street) {
708 this.street = street;
712 public String getTitle () {
717 public void setTitle (final String title) {
722 public Integer getZipCode () {
727 public void setZipCode (final Integer zipCode) {
728 this.zipCode = zipCode;
732 * Post-initialization of this class
735 public void init () {
739 public boolean isGenderRequired () {
740 // Get context parameter
741 String contextParameter = FacesContext.getCurrentInstance().getExternalContext().getInitParameter("is_admin_gender_enabled"); //NOI18N
744 boolean isRequired = ((contextParameter instanceof String) && (contextParameter.toLowerCase().equals("true"))); //NOI18N
753 private void clear () {
756 this.setGender(null);
758 this.setFirstName(null);
759 this.setFamilyName(null);
760 this.setStreet(null);
761 this.setHouseNumber(null);
762 this.setHouseNumberExtension(null);
763 this.setZipCode(null);
765 this.setCountry(null);
768 this.setEmailAddress(null);
769 this.setPhoneCountry(null);
770 this.setPhoneAreaCode(null);
771 this.setPhoneNumber(null);
772 this.setMobileCarrier(null);
773 this.setMobileNumber(null);
774 this.setFaxCountry(null);
775 this.setFaxAreaCode(null);
776 this.setFaxNumber(null);
779 this.setBirthday(null);
780 this.setComment(null);
784 * Checks whether the given contact is found
786 * @param contact Contact inastance
788 * @return Wether contact has been found
790 private boolean isSameContactFound (final Contact contact) {
791 // Default is not found
792 boolean IsFound = false;
795 Iterator<Contact> iterator = this.contactController.allContacts().iterator();
798 while (iterator.hasNext()) {
800 Contact next = iterator.next();
803 if (ContactUtils.isSameContact(contact, next)) {
804 // Yes, then abort loop
815 * Updates all data in contact instance.
817 * @param contact Contact instance
819 private void updateContactData (final Contact contact) {
820 // Contact instance should be valid
821 if (null == contact) {
823 throw new NullPointerException("contact is null"); //NOI18N
824 } else if (contact.getContactId() == null) {
826 throw new NullPointerException("contact.contactId is null"); //NOI18N //NOI18N
827 } else if (contact.getContactId() < 1) {
829 throw new IllegalStateException(MessageFormat.format("contact.contactId={0} is invalid", contact.getContactId())); //NOI18N
833 contact.setContactGender(this.getGender());
834 contact.setContactTitle(this.getTitle());
835 contact.setContactFirstName(this.getFirstName());
836 contact.setContactFamilyName(this.getFamilyName());
837 contact.setContactStreet(this.getStreet());
838 contact.setContactHouseNumber(this.getHouseNumber());
839 contact.setContactHouseNumberExtension(this.getHouseNumberExtension());
840 contact.setContactZipCode(this.getZipCode());
841 contact.setContactCity(this.getCity());
842 contact.setContactCountry(this.getCountry());
844 // Update contact's mobile number
845 this.isMobileUnlinked = ContactUtils.updateCellPhoneNumber(contact, this.getMobileCarrier(), this.getMobileNumber());
847 // Update contact's land-line number
848 this.isLandLineUnlinked = ContactUtils.updateLandLineNumber(contact, this.getPhoneCountry(), this.getPhoneAreaCode(), this.getPhoneNumber());
850 // Update contact's fax number
851 this.isFaxUnlinked = ContactUtils.updateFaxNumber(contact, this.getFaxCountry(), this.getFaxAreaCode(), this.getFaxNumber());