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.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 AddressbookWebRequestController 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 () {
253 public String addContact () {
254 // Are all minimum fields set?
255 if (this.getGender() == null) {
257 throw new NullPointerException("gender is null"); //NOI18N
258 } else if (this.getFirstName() == null) {
260 throw new NullPointerException("firstName is null"); //NOI18N
261 } else if (this.getFirstName().isEmpty()) {
263 throw new IllegalStateException("firstName is empty"); //NOI18N
264 } else if (this.getFamilyName() == null) {
266 throw new NullPointerException("familyName is null"); //NOI18N
267 } else if (this.getFamilyName().isEmpty()) {
269 throw new IllegalStateException("familyName is empty"); //NOI18N
272 // Create new contact instance
273 Contact contact = this.createContactInstance();
275 // Default is not same contact
276 if (this.isSameContactFound(contact)) {
277 // Already registered
278 throw new FaceletException(new ContactAlreadyAddedException(contact));
282 Contact updatedContact;
287 updatedContact = this.adminContactBean.addContact(contact);
288 } catch (final ContactAlreadyAddedException ex) {
290 throw new FaceletException(ex);
294 this.addedContactEvent.fire(new AdminAddedContactEvent(updatedContact));
300 return "admin_list_contact"; //NOI18N
304 public void copyContactToController (final Contact contact) {
305 // The contact instance must be valid
306 if (null == contact) {
308 throw new NullPointerException("contact is null"); //NOI18N
309 } else if (contact.getContactId() == null) {
311 throw new NullPointerException("contact.contactId is null"); //NOI18N //NOI18N
312 } else if (contact.getContactId() < 1) {
314 throw new IllegalStateException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N
317 // Set all fields: contact
318 this.setContactId(contact.getContactId());
319 this.setTitle(contact.getContactTitle());
320 this.setBirthday(contact.getContactBirthday());
321 this.setCity(contact.getContactCity());
322 this.setComment(contact.getContactComment());
323 this.setCountry(contact.getContactCountry());
324 this.setEmailAddress(contact.getContactEmailAddress());
325 this.setFamilyName(contact.getContactFamilyName());
326 this.setFirstName(contact.getContactFirstName());
327 this.setGender(contact.getContactGender());
328 this.setHouseNumber(contact.getContactHouseNumber());
329 this.setHouseNumberExtension(contact.getContactHouseNumberExtension());
330 this.setStreet(contact.getContactStreet());
331 this.setZipCode(contact.getContactZipCode());
334 if (contact.getContactMobileNumber() instanceof DialableMobileNumber) {
335 this.setMobileId(contact.getContactMobileNumber().getPhoneId());
336 this.setMobileCarrier(contact.getContactMobileNumber().getMobileProvider());
337 this.setMobileNumber(contact.getContactMobileNumber().getPhoneNumber());
341 if (contact.getContactFaxNumber() instanceof DialableFaxNumber) {
342 this.setFaxId(contact.getContactFaxNumber().getPhoneId());
343 this.setFaxAreaCode(contact.getContactFaxNumber().getPhoneAreaCode());
344 this.setFaxCountry(contact.getContactFaxNumber().getPhoneCountry());
345 this.setFaxNumber(contact.getContactFaxNumber().getPhoneNumber());
349 if (contact.getContactLandLineNumber() instanceof DialableLandLineNumber) {
350 this.setLandLineId(contact.getContactLandLineNumber().getPhoneId());
351 this.setPhoneAreaCode(contact.getContactLandLineNumber().getPhoneAreaCode());
352 this.setPhoneCountry(contact.getContactLandLineNumber().getPhoneCountry());
353 this.setPhoneNumber(contact.getContactLandLineNumber().getPhoneNumber());
358 public Contact createContactInstance () {
359 // Generate phone number
360 DialableLandLineNumber phone = new LandLineNumber(this.getPhoneCountry(), this.getPhoneAreaCode(), this.getPhoneNumber());
361 DialableMobileNumber mobile = new MobileNumber(this.getMobileCarrier(), this.getMobileNumber());
362 DialableFaxNumber fax = new FaxNumber(this.getFaxCountry(), this.getFaxAreaCode(), this.getFaxNumber());
364 // Create new instance
365 Contact contact = new UserContact(this.getGender(), this.getFirstName(), this.getFamilyName());
367 // Check if contact instance is in helper and valid
368 if (null == contact) {
370 throw new NullPointerException("beanHelper.contact is null"); //NOI18N
371 } else if (contact.getContactId() == null) {
373 throw new NullPointerException("beanHelper.contact.contactId is null"); //NOI18N //NOI18N
374 } else if (contact.getContactId() < 1) {
376 throw new IllegalStateException(MessageFormat.format("beanHelper.contact.contactId={0} is invalid", contact.getContactId())); //NOI18N
379 // Update all data in contact
380 this.updateContactData(contact);
382 // Call EJB for updating contact data
383 Contact updatedContact = this.contactBean.updateContactData(contact, this.isMobileUnlinked, this.isLandLineUnlinked, this.isFaxUnlinked);
386 this.updatedContactEvent.fire(new AdminUpdatedContactEvent(updatedContact));
396 public String editContactData () {
397 // Get contact instance
398 Contact contact = this.beanHelper.getContact();
400 // Check if contact instance is in helper and valid
401 if (null == contact) {
403 throw new NullPointerException("beanHelper.contact is null"); //NOI18N
404 } else if (contact.getContactId() == null) {
406 throw new NullPointerException("beanHelper.contact.contactId is null"); //NOI18N //NOI18N
407 } else if (contact.getContactId() < 1) {
409 throw new IllegalStateException(MessageFormat.format("beanHelper.contact.contactId={0} is invalid", contact.getContactId())); //NOI18N
412 // Update all data in contact
413 this.updateContactData(contact);
415 // Call EJB for updating contact data
416 Contact updatedContact = this.contactBean.updateContactData(contact, this.isMobileUnlinked, this.isLandLineUnlinked, this.isFaxUnlinked);
419 this.updatedContactEvent.fire(new AdminUpdatedContactEvent(updatedContact));
424 // Return to contact list (for now)
425 return "admin_list_contact"; //NOI18N
429 public String generateMobileNumber (final DialableMobileNumber mobileNumber) {
431 if (null == mobileNumber) {
437 String number = String.format(
439 mobileNumber.getMobileProvider().getProviderCountry().getCountryExternalDialPrefix(),
440 mobileNumber.getMobileProvider().getProviderDialPrefix(),
441 mobileNumber.getPhoneNumber()
449 public String generatePhoneNumber (final DialableNumber phoneNumber) {
451 if (null == phoneNumber) {
457 String number = String.format(
459 phoneNumber.getPhoneCountry().getCountryExternalDialPrefix(),
460 phoneNumber.getPhoneAreaCode(),
461 phoneNumber.getPhoneNumber()
469 @SuppressWarnings ("ReturnOfDateField")
470 public Date getBirthday () {
471 return this.birthday;
475 @SuppressWarnings ("AssignmentToDateFieldFromParameter")
476 public void setBirthday (final Date birthday) {
477 this.birthday = birthday;
481 public String getCity () {
486 public void setCity (final String city) {
491 public String getComment () {
496 public void setComment (final String comment) {
497 this.comment = comment;
501 public Long getContactId () {
502 return this.contactId;
506 public void setContactId (final Long contactId) {
507 this.contactId = contactId;
511 public Country getCountry () {
516 public void setCountry (final Country country) {
517 this.country = country;
521 public String getEmailAddress () {
522 return this.emailAddress;
526 public void setEmailAddress (final String emailAddress) {
527 this.emailAddress = emailAddress;
531 public String getFamilyName () {
532 return this.familyName;
536 public void setFamilyName (final String familyName) {
537 this.familyName = familyName;
541 public Integer getFaxAreaCode () {
542 return this.faxAreaCode;
546 public void setFaxAreaCode (final Integer faxAreaCode) {
547 this.faxAreaCode = faxAreaCode;
551 public Country getFaxCountry () {
552 return this.faxCountry;
556 public void setFaxCountry (final Country faxCountry) {
557 this.faxCountry = faxCountry;
561 public Long getFaxId () {
566 public void setFaxId (final Long faxId) {
571 public Long getFaxNumber () {
572 return this.faxNumber;
576 public void setFaxNumber (final Long faxNumber) {
577 this.faxNumber = faxNumber;
581 public String getFirstName () {
582 return this.firstName;
586 public void setFirstName (final String firstName) {
587 this.firstName = firstName;
591 public Gender getGender () {
596 public void setGender (final Gender gender) {
597 this.gender = gender;
601 public Short getHouseNumber () {
602 return this.houseNumber;
606 public void setHouseNumber (final Short houseNumber) {
607 this.houseNumber = houseNumber;
611 public String getHouseNumberExtension () {
612 return this.houseNumberExtension;
616 public void setHouseNumberExtension (final String houseNumberExtension) {
617 this.houseNumberExtension = houseNumberExtension;
621 public Long getLandLineId () {
622 return this.landLineId;
626 public void setLandLineId (final Long landLineId) {
627 this.landLineId = landLineId;
631 public MobileProvider getMobileCarrier () {
632 return this.mobileCarrier;
636 public void setMobileCarrier (final MobileProvider mobileCarrier) {
637 this.mobileCarrier = mobileCarrier;
641 public Long getMobileId () {
642 return this.mobileId;
646 public void setMobileId (final Long mobileId) {
647 this.mobileId = mobileId;
651 public Long getMobileNumber () {
652 return this.mobileNumber;
656 public void setMobileNumber (Long mobileNumber) {
657 this.mobileNumber = mobileNumber;
661 public Integer getPhoneAreaCode () {
662 return this.phoneAreaCode;
666 public void setPhoneAreaCode (final Integer phoneAreaCode) {
667 this.phoneAreaCode = phoneAreaCode;
671 public Country getPhoneCountry () {
672 return this.phoneCountry;
676 public void setPhoneCountry (final Country phoneCountry) {
677 this.phoneCountry = phoneCountry;
681 public Long getPhoneNumber () {
682 return this.phoneNumber;
686 public void setPhoneNumber (final Long phoneNumber) {
687 this.phoneNumber = phoneNumber;
691 public String getStreet () {
696 public void setStreet (final String street) {
697 this.street = street;
701 public String getTitle () {
706 public void setTitle (final String title) {
711 public Integer getZipCode () {
716 public void setZipCode (final Integer zipCode) {
717 this.zipCode = zipCode;
721 * Post-initialization of this class
724 public void init () {
727 // Get initial context
728 Context context = new InitialContext();
731 this.contactBean = (ContactSessionBeanRemote) context.lookup("java:global/addressbook-ejb/contact!org.mxchange.jcontacts.contact.ContactSessionBeanRemote"); //NOI18N
732 } catch (final NamingException e) {
734 throw new FaceletException(e);
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.updateMobileNumber(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());