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 cellphone entry has been unlinked
187 private boolean isMobileNumberUnlinked;
190 * Phone number area code
192 private Integer landLineAreaCode;
195 * Country instance for phone number
197 private Country landLineCountry;
200 * Land-line id number
202 private Long landLineId;
207 private Long landLineNumber;
212 private Long mobileId;
217 private Long mobileNumber;
220 * Mobile number's provider
222 private MobileProvider mobileProvider;
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) {
308 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("AdminContactController::copyContactToController(): contact={0} - CALLED!", contact)); //NOI18N
310 // The contact instance must be valid
311 if (null == contact) {
313 throw new NullPointerException("contact is null"); //NOI18N
314 } else if (contact.getContactId() == null) {
316 throw new NullPointerException("contact.contactId is null"); //NOI18N //NOI18N
317 } else if (contact.getContactId() < 1) {
319 throw new IllegalStateException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N
322 // Set all fields: contact
323 this.setContactId(contact.getContactId());
324 this.setTitle(contact.getContactTitle());
325 this.setBirthday(contact.getContactBirthday());
326 this.setCity(contact.getContactCity());
327 this.setComment(contact.getContactComment());
328 this.setCountry(contact.getContactCountry());
329 this.setEmailAddress(contact.getContactEmailAddress());
330 this.setFamilyName(contact.getContactFamilyName());
331 this.setFirstName(contact.getContactFirstName());
332 this.setGender(contact.getContactGender());
333 this.setHouseNumber(contact.getContactHouseNumber());
334 this.setHouseNumberExtension(contact.getContactHouseNumberExtension());
335 this.setStreet(contact.getContactStreet());
336 this.setZipCode(contact.getContactZipCode());
338 // Is the cell phone set?
339 if (contact.getContactMobileNumber() instanceof DialableMobileNumber) {
340 // ... cellphone data
341 this.setMobileId(contact.getContactMobileNumber().getPhoneId());
342 this.setMobileProvider(contact.getContactMobileNumber().getMobileProvider());
343 this.setMobileNumber(contact.getContactMobileNumber().getPhoneNumber());
347 if (contact.getContactFaxNumber() instanceof DialableFaxNumber) {
349 this.setFaxId(contact.getContactFaxNumber().getPhoneId());
350 this.setFaxAreaCode(contact.getContactFaxNumber().getPhoneAreaCode());
351 this.setFaxCountry(contact.getContactFaxNumber().getPhoneCountry());
352 this.setFaxNumber(contact.getContactFaxNumber().getPhoneNumber());
355 // Is the land-line number set?
356 if (contact.getContactLandLineNumber() instanceof DialableLandLineNumber) {
358 this.setLandLineId(contact.getContactLandLineNumber().getPhoneId());
359 this.setLandLineAreaCode(contact.getContactLandLineNumber().getPhoneAreaCode());
360 this.setLandLineCountry(contact.getContactLandLineNumber().getPhoneCountry());
361 this.setLandLineNumber(contact.getContactLandLineNumber().getPhoneNumber());
365 //* NOISY-DEBUG: */ System.out.println("AdminContactController::copyContactToController(): EXIT!"); //NOI18N
369 public Contact createContactInstance () {
370 // Are all minimum fields set?
371 if (this.getGender() == null) {
373 throw new NullPointerException("gender is null"); //NOI18N
374 } else if (this.getFirstName() == null) {
376 throw new NullPointerException("firstName is null"); //NOI18N
377 } else if (this.getFirstName().isEmpty()) {
379 throw new IllegalStateException("firstName is empty"); //NOI18N
380 } else if (this.getFamilyName() == null) {
382 throw new NullPointerException("familyName is null"); //NOI18N
383 } else if (this.getFamilyName().isEmpty()) {
385 throw new IllegalStateException("familyName is empty"); //NOI18N
388 // Generate phone number
389 DialableLandLineNumber landLine = new LandLineNumber(this.getLandLineCountry(), this.getLandLineAreaCode(), this.getLandLineNumber());
390 DialableMobileNumber mobile = new MobileNumber(this.getMobileProvider(), this.getMobileNumber());
391 DialableFaxNumber fax = new FaxNumber(this.getFaxCountry(), this.getFaxAreaCode(), this.getFaxNumber());
393 // Create new instance
394 Contact contact = new UserContact(this.getGender(), this.getFirstName(), this.getFamilyName());
397 contact.setContactBirthday(this.getBirthday());
398 contact.setContactStreet(this.getStreet());
399 contact.setContactHouseNumber(this.getHouseNumber());
400 contact.setContactZipCode(this.getZipCode());
401 contact.setContactCity(this.getCity());
402 contact.setContactCountry(this.getCountry());
403 contact.setContactEmailAddress(this.getEmailAddress());
404 contact.setContactBirthday(this.getBirthday());
405 contact.setContactComment(this.getComment());
408 contact.setContactOwnContact(Boolean.TRUE);
410 // Don't set null or wrong references
411 if ((landLine instanceof DialableLandLineNumber) && (landLine.getPhoneCountry() instanceof Country) && (this.getLandLineAreaCode() != null) && (this.getLandLineNumber() != null) && (this.getLandLineAreaCode() > 0) && (this.getLandLineNumber() > 0)) {
412 // Now the number must be given
413 if (landLine.getPhoneAreaCode() == null) {
415 throw new NullPointerException("phone.phoneAreaCode is null"); //NOI18N
416 } else if (landLine.getPhoneAreaCode() < 1) {
418 throw new IllegalArgumentException("phone.phoneAreaCode is zero or below."); //NOI18N
419 } else if (landLine.getPhoneNumber() == null) {
421 throw new NullPointerException("phone.phoneNumber is null"); //NOI18N
422 } else if (landLine.getPhoneNumber() < 1) {
424 throw new IllegalArgumentException("phone.phoneNumber is zero or below."); //NOI18N
428 contact.setContactLandLineNumber(landLine);
431 // Don't set null or wrong references
432 if ((fax instanceof DialableFaxNumber) && (fax.getPhoneCountry() instanceof Country) && (this.getFaxAreaCode() != null) && (this.getFaxNumber() != null) && (this.getFaxAreaCode() > 0) && (this.getFaxNumber() > 0)) {
433 // Now the number must be given
434 if (fax.getPhoneAreaCode() == null) {
436 throw new NullPointerException("fax.phoneAreaCode is null"); //NOI18N
437 } else if (fax.getPhoneAreaCode() < 1) {
439 throw new IllegalArgumentException("fax.phoneAreaCode is zero or below."); //NOI18N
440 } else if (fax.getPhoneNumber() == null) {
442 throw new NullPointerException("fax.phoneNumber is null"); //NOI18N
443 } else if (fax.getPhoneNumber() < 1) {
445 throw new IllegalArgumentException("fax.phoneNumber is zero or below."); //NOI18N
449 contact.setContactFaxNumber(fax);
452 // Is the provider set?
453 if ((mobile instanceof DialableMobileNumber) && (this.getMobileProvider() instanceof MobileProvider) && (this.getMobileNumber() != null) && (this.getMobileNumber() > 0)) {
454 // Is the number set?
455 if (mobile.getPhoneNumber() == null) {
457 throw new NullPointerException("cellphone.phoneNumber is null"); //NOI18N
458 } else if (mobile.getPhoneNumber() < 1) {
460 throw new IllegalArgumentException("cellphone.phoneNumber is zero or below."); //NOI18N
463 // Set cellphone number
464 contact.setContactMobileNumber(mobile);
472 public String editContactData () {
473 // Get contact instance
474 Contact contact = this.beanHelper.getContact();
476 // Check if contact instance is in helper and valid
477 if (null == contact) {
479 throw new NullPointerException("beanHelper.contact is null"); //NOI18N
480 } else if (contact.getContactId() == null) {
482 throw new NullPointerException("beanHelper.contact.contactId is null"); //NOI18N //NOI18N
483 } else if (contact.getContactId() < 1) {
485 throw new IllegalStateException(MessageFormat.format("beanHelper.contact.contactId={0} is invalid", contact.getContactId())); //NOI18N
488 // Update all data in contact
489 this.updateContactData(contact);
491 // Call EJB for updating contact data
492 Contact updatedContact = this.contactBean.updateContactData(contact, this.isMobileNumberUnlinked, this.isLandLineUnlinked, this.isFaxUnlinked);
495 this.updatedContactEvent.fire(new AdminUpdatedContactEvent(updatedContact));
500 // Return to contact list (for now)
501 return "admin_list_contact"; //NOI18N
505 public String generateMobileNumber (final DialableMobileNumber mobileNumber) {
507 if (null == mobileNumber) {
513 String number = String.format(
515 mobileNumber.getMobileProvider().getProviderCountry().getCountryExternalDialPrefix(),
516 mobileNumber.getMobileProvider().getProviderDialPrefix(),
517 mobileNumber.getPhoneNumber()
525 public String generatePhoneNumber (final DialableNumber phoneNumber) {
527 if (null == phoneNumber) {
533 String number = String.format(
535 phoneNumber.getPhoneCountry().getCountryExternalDialPrefix(),
536 phoneNumber.getPhoneAreaCode(),
537 phoneNumber.getPhoneNumber()
545 @SuppressWarnings ("ReturnOfDateField")
546 public Date getBirthday () {
547 return this.birthday;
551 @SuppressWarnings ("AssignmentToDateFieldFromParameter")
552 public void setBirthday (final Date birthday) {
553 this.birthday = birthday;
557 public String getCity () {
562 public void setCity (final String city) {
567 public String getComment () {
572 public void setComment (final String comment) {
573 this.comment = comment;
577 public Long getContactId () {
578 return this.contactId;
582 public void setContactId (final Long contactId) {
583 this.contactId = contactId;
587 public String getControllerType () {
588 return "admin"; //NOI18N
593 public void setControllerType (final String controllerType) {
594 throw new UnsupportedOperationException("Setting controller type is not supported."); //NOI18N
598 public Country getCountry () {
603 public void setCountry (final Country country) {
604 this.country = country;
608 public String getEmailAddress () {
609 return this.emailAddress;
613 public void setEmailAddress (final String emailAddress) {
614 this.emailAddress = emailAddress;
618 public String getFamilyName () {
619 return this.familyName;
623 public void setFamilyName (final String familyName) {
624 this.familyName = familyName;
628 public Integer getFaxAreaCode () {
629 return this.faxAreaCode;
633 public void setFaxAreaCode (final Integer faxAreaCode) {
634 this.faxAreaCode = faxAreaCode;
638 public Country getFaxCountry () {
639 return this.faxCountry;
643 public void setFaxCountry (final Country faxCountry) {
644 this.faxCountry = faxCountry;
648 public Long getFaxId () {
653 public void setFaxId (final Long faxId) {
658 public Long getFaxNumber () {
659 return this.faxNumber;
663 public void setFaxNumber (final Long faxNumber) {
664 this.faxNumber = faxNumber;
668 public String getFirstName () {
669 return this.firstName;
673 public void setFirstName (final String firstName) {
674 this.firstName = firstName;
678 public Gender getGender () {
683 public void setGender (final Gender gender) {
684 this.gender = gender;
688 public Short getHouseNumber () {
689 return this.houseNumber;
693 public void setHouseNumber (final Short houseNumber) {
694 this.houseNumber = houseNumber;
698 public String getHouseNumberExtension () {
699 return this.houseNumberExtension;
703 public void setHouseNumberExtension (final String houseNumberExtension) {
704 this.houseNumberExtension = houseNumberExtension;
708 public Integer getLandLineAreaCode () {
709 return this.landLineAreaCode;
713 public void setLandLineAreaCode (final Integer landLineAreaCode) {
714 this.landLineAreaCode = landLineAreaCode;
718 public Country getLandLineCountry () {
719 return this.landLineCountry;
723 public void setLandLineCountry (final Country landLineCountry) {
724 this.landLineCountry = landLineCountry;
728 public Long getLandLineId () {
729 return this.landLineId;
733 public void setLandLineId (final Long landLineId) {
734 this.landLineId = landLineId;
738 public Long getLandLineNumber () {
739 return this.landLineNumber;
743 public void setLandLineNumber (final Long landLineNumber) {
744 this.landLineNumber = landLineNumber;
748 public Long getMobileId () {
749 return this.mobileId;
753 public void setMobileId (final Long mobileId) {
754 this.mobileId = mobileId;
758 public Long getMobileNumber () {
759 return this.mobileNumber;
763 public void setMobileNumber (Long mobileNumber) {
764 this.mobileNumber = mobileNumber;
768 public MobileProvider getMobileProvider () {
769 return this.mobileProvider;
773 public void setMobileProvider (final MobileProvider mobileProvider) {
774 this.mobileProvider = mobileProvider;
778 public String getStreet () {
783 public void setStreet (final String street) {
784 this.street = street;
788 public String getTitle () {
793 public void setTitle (final String title) {
798 public Integer getZipCode () {
803 public void setZipCode (final Integer zipCode) {
804 this.zipCode = zipCode;
808 * Post-initialization of this class
811 public void init () {
814 // Get initial context
815 Context context = new InitialContext();
818 this.contactBean = (ContactSessionBeanRemote) context.lookup("java:global/pizzaservice-ejb/contact!org.mxchange.jcontacts.contact.ContactSessionBeanRemote"); //NOI18N
820 // Try to lookup (administative)
821 this.adminContactBean = (AdminContactSessionBeanRemote) context.lookup("java:global/pizzaservice-ejb/adminContact!org.mxchange.jcontacts.contact.AdminContactSessionBeanRemote"); //NOI18N
822 } catch (final NamingException e) {
824 throw new FaceletException(e);
829 public boolean isGenderRequired () {
830 // Get context parameter
831 String contextParameter = FacesContext.getCurrentInstance().getExternalContext().getInitParameter("is_admin_gender_enabled"); //NOI18N
834 boolean isRequired = ((contextParameter instanceof String) && (contextParameter.toLowerCase().equals("true"))); //NOI18N
843 private void clear () {
847 this.setFirstName(null);
848 this.setFamilyName(null);
849 this.setStreet(null);
850 this.setHouseNumber(null);
851 this.setHouseNumberExtension(null);
852 this.setZipCode(null);
854 this.setCountry(null);
857 this.setEmailAddress(null);
858 this.setLandLineCountry(null);
859 this.setLandLineAreaCode(null);
860 this.setLandLineNumber(null);
861 this.setMobileProvider(null);
862 this.setMobileNumber(null);
863 this.setFaxCountry(null);
864 this.setFaxAreaCode(null);
865 this.setFaxNumber(null);
868 this.setBirthday(null);
869 this.setComment(null);
873 * Checks whether the given contact is found
875 * @param contact Contact inastance
877 * @return Wether contact has been found
879 private boolean isSameContactFound (final Contact contact) {
880 // Default is not found
881 boolean IsFound = false;
884 Iterator<Contact> iterator = this.contactController.allContacts().iterator();
887 while (iterator.hasNext()) {
889 Contact next = iterator.next();
892 if (ContactUtils.isSameContact(contact, next)) {
893 // Yes, then abort loop
904 * Updates all data in contact instance.
906 * @param contact Contact instance
908 private void updateContactData (final Contact contact) {
909 // Contact instance should be valid
910 if (null == contact) {
912 throw new NullPointerException("contact is null"); //NOI18N
913 } else if (contact.getContactId() == null) {
915 throw new NullPointerException("contact.contactId is null"); //NOI18N //NOI18N
916 } else if (contact.getContactId() < 1) {
918 throw new IllegalStateException(MessageFormat.format("contact.contactId={0} is invalid", contact.getContactId())); //NOI18N
922 contact.setContactGender(this.getGender());
923 contact.setContactTitle(this.getTitle());
924 contact.setContactFirstName(this.getFirstName());
925 contact.setContactFamilyName(this.getFamilyName());
926 contact.setContactStreet(this.getStreet());
927 contact.setContactHouseNumber(this.getHouseNumber());
928 contact.setContactHouseNumberExtension(this.getHouseNumberExtension());
929 contact.setContactZipCode(this.getZipCode());
930 contact.setContactCity(this.getCity());
931 contact.setContactCountry(this.getCountry());
933 // Update contact's cellphone number
934 this.isMobileNumberUnlinked = ContactUtils.updateMobileNumber(contact, this.getMobileProvider(), this.getMobileNumber());
936 // Update contact's land-line number
937 this.isLandLineUnlinked = ContactUtils.updateLandLineNumber(contact, this.getLandLineCountry(), this.getLandLineAreaCode(), this.getLandLineNumber());
939 // Update contact's fax number
940 this.isFaxUnlinked = ContactUtils.updateFaxNumber(contact, this.getFaxCountry(), this.getFaxAreaCode(), this.getFaxNumber());