2 * Copyright (C) 2016, 2017 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.title.PersonalTitle;
40 import org.mxchange.jcontacts.contact.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 Short houseNumber;
165 * House number extension
167 private String houseNumberExtension;
170 * Whether a fax entry has been unlinked
172 private boolean isFaxUnlinked;
175 * Whether a land-line number has been unlinked
177 private boolean isLandLineUnlinked;
180 * Whether a cellphone entry has been unlinked
182 private boolean isMobileNumberUnlinked;
185 * Phone number area code
187 private Integer landLineAreaCode;
190 * Country instance for phone number
192 private Country landLineCountry;
195 * Land-line id number
197 private Long landLineId;
202 private Long landLineNumber;
207 private Long mobileId;
212 private Long mobileNumber;
215 * Mobile number's provider
217 private MobileProvider mobileProvider;
220 * PersonalTitle instance
222 private PersonalTitle personalTitle;
227 private String street;
230 * Academic academicTitle
232 private String academicTitle;
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.getPersonalTitle() == null) {
259 throw new NullPointerException("personalTitle 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.setAcademicTitle(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.setPersonalTitle(contact.getContactPersonalTitle());
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.getPersonalTitle() == null) {
373 throw new NullPointerException("personalTitle 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.getPersonalTitle(), this.getFirstName(), this.getFamilyName());
397 contact.setContactTitle(this.getAcademicTitle());
398 contact.setContactBirthday(this.getBirthday());
399 contact.setContactStreet(this.getStreet());
400 contact.setContactHouseNumber(this.getHouseNumber());
401 contact.setContactZipCode(this.getZipCode());
402 contact.setContactCity(this.getCity());
403 contact.setContactCountry(this.getCountry());
404 contact.setContactEmailAddress(this.getEmailAddress());
405 contact.setContactBirthday(this.getBirthday());
406 contact.setContactComment(this.getComment());
409 contact.setContactOwnContact(Boolean.TRUE);
411 // Don't set null or wrong references
412 if ((landLine instanceof DialableLandLineNumber) && (landLine.getPhoneCountry() instanceof Country) && (this.getLandLineAreaCode() != null) && (this.getLandLineNumber() != null) && (this.getLandLineAreaCode() > 0) && (this.getLandLineNumber() > 0)) {
413 // Now the number must be given
414 if (landLine.getPhoneAreaCode() == null) {
416 throw new NullPointerException("phone.phoneAreaCode is null"); //NOI18N
417 } else if (landLine.getPhoneAreaCode() < 1) {
419 throw new IllegalArgumentException("phone.phoneAreaCode is zero or below."); //NOI18N
420 } else if (landLine.getPhoneNumber() == null) {
422 throw new NullPointerException("phone.phoneNumber is null"); //NOI18N
423 } else if (landLine.getPhoneNumber() < 1) {
425 throw new IllegalArgumentException("phone.phoneNumber is zero or below."); //NOI18N
429 contact.setContactLandLineNumber(landLine);
432 // Don't set null or wrong references
433 if ((fax instanceof DialableFaxNumber) && (fax.getPhoneCountry() instanceof Country) && (this.getFaxAreaCode() != null) && (this.getFaxNumber() != null) && (this.getFaxAreaCode() > 0) && (this.getFaxNumber() > 0)) {
434 // Now the number must be given
435 if (fax.getPhoneAreaCode() == null) {
437 throw new NullPointerException("fax.phoneAreaCode is null"); //NOI18N
438 } else if (fax.getPhoneAreaCode() < 1) {
440 throw new IllegalArgumentException("fax.phoneAreaCode is zero or below."); //NOI18N
441 } else if (fax.getPhoneNumber() == null) {
443 throw new NullPointerException("fax.phoneNumber is null"); //NOI18N
444 } else if (fax.getPhoneNumber() < 1) {
446 throw new IllegalArgumentException("fax.phoneNumber is zero or below."); //NOI18N
450 contact.setContactFaxNumber(fax);
453 // Is the provider set?
454 if ((mobile instanceof DialableMobileNumber) && (this.getMobileProvider() instanceof MobileProvider) && (this.getMobileNumber() != null) && (this.getMobileNumber() > 0)) {
455 // Is the number set?
456 if (mobile.getPhoneNumber() == null) {
458 throw new NullPointerException("cellphone.phoneNumber is null"); //NOI18N
459 } else if (mobile.getPhoneNumber() < 1) {
461 throw new IllegalArgumentException("cellphone.phoneNumber is zero or below."); //NOI18N
464 // Set cellphone number
465 contact.setContactMobileNumber(mobile);
473 public String editContactData () {
474 // Get contact instance
475 Contact contact = this.beanHelper.getContact();
477 // Check if contact instance is in helper and valid
478 if (null == contact) {
480 throw new NullPointerException("beanHelper.contact is null"); //NOI18N
481 } else if (contact.getContactId() == null) {
483 throw new NullPointerException("beanHelper.contact.contactId is null"); //NOI18N //NOI18N
484 } else if (contact.getContactId() < 1) {
486 throw new IllegalStateException(MessageFormat.format("beanHelper.contact.contactId={0} is invalid", contact.getContactId())); //NOI18N
489 // Update all data in contact
490 this.updateContactData(contact);
492 // Call EJB for updating contact data
493 Contact updatedContact = this.contactBean.updateContactData(contact, this.isMobileNumberUnlinked, this.isLandLineUnlinked, this.isFaxUnlinked);
496 this.updatedContactEvent.fire(new AdminUpdatedContactEvent(updatedContact));
501 // Return to contact list (for now)
502 return "admin_list_contact"; //NOI18N
506 public String generateMobileNumber (final DialableMobileNumber mobileNumber) {
508 if (null == mobileNumber) {
514 String number = String.format(
516 mobileNumber.getMobileProvider().getProviderCountry().getCountryExternalDialPrefix(),
517 mobileNumber.getMobileProvider().getProviderDialPrefix(),
518 mobileNumber.getPhoneNumber()
526 public String generatePhoneNumber (final DialableNumber phoneNumber) {
528 if (null == phoneNumber) {
534 String number = String.format(
536 phoneNumber.getPhoneCountry().getCountryExternalDialPrefix(),
537 phoneNumber.getPhoneAreaCode(),
538 phoneNumber.getPhoneNumber()
546 @SuppressWarnings ("ReturnOfDateField")
547 public Date getBirthday () {
548 return this.birthday;
552 @SuppressWarnings ("AssignmentToDateFieldFromParameter")
553 public void setBirthday (final Date birthday) {
554 this.birthday = birthday;
558 public String getCity () {
563 public void setCity (final String city) {
568 public String getComment () {
573 public void setComment (final String comment) {
574 this.comment = comment;
578 public Long getContactId () {
579 return this.contactId;
583 public void setContactId (final Long contactId) {
584 this.contactId = contactId;
588 public String getControllerType () {
589 return "admin"; //NOI18N
594 public void setControllerType (final String controllerType) {
595 throw new UnsupportedOperationException("Setting controller type is not supported."); //NOI18N
599 public Country getCountry () {
604 public void setCountry (final Country country) {
605 this.country = country;
609 public String getEmailAddress () {
610 return this.emailAddress;
614 public void setEmailAddress (final String emailAddress) {
615 this.emailAddress = emailAddress;
619 public String getFamilyName () {
620 return this.familyName;
624 public void setFamilyName (final String familyName) {
625 this.familyName = familyName;
629 public Integer getFaxAreaCode () {
630 return this.faxAreaCode;
634 public void setFaxAreaCode (final Integer faxAreaCode) {
635 this.faxAreaCode = faxAreaCode;
639 public Country getFaxCountry () {
640 return this.faxCountry;
644 public void setFaxCountry (final Country faxCountry) {
645 this.faxCountry = faxCountry;
649 public Long getFaxId () {
654 public void setFaxId (final Long faxId) {
659 public Long getFaxNumber () {
660 return this.faxNumber;
664 public void setFaxNumber (final Long faxNumber) {
665 this.faxNumber = faxNumber;
669 public String getFirstName () {
670 return this.firstName;
674 public void setFirstName (final String firstName) {
675 this.firstName = firstName;
679 public Short getHouseNumber () {
680 return this.houseNumber;
684 public void setHouseNumber (final Short houseNumber) {
685 this.houseNumber = houseNumber;
689 public String getHouseNumberExtension () {
690 return this.houseNumberExtension;
694 public void setHouseNumberExtension (final String houseNumberExtension) {
695 this.houseNumberExtension = houseNumberExtension;
699 public Integer getLandLineAreaCode () {
700 return this.landLineAreaCode;
704 public void setLandLineAreaCode (final Integer landLineAreaCode) {
705 this.landLineAreaCode = landLineAreaCode;
709 public Country getLandLineCountry () {
710 return this.landLineCountry;
714 public void setLandLineCountry (final Country landLineCountry) {
715 this.landLineCountry = landLineCountry;
719 public Long getLandLineId () {
720 return this.landLineId;
724 public void setLandLineId (final Long landLineId) {
725 this.landLineId = landLineId;
729 public Long getLandLineNumber () {
730 return this.landLineNumber;
734 public void setLandLineNumber (final Long landLineNumber) {
735 this.landLineNumber = landLineNumber;
739 public Long getMobileId () {
740 return this.mobileId;
744 public void setMobileId (final Long mobileId) {
745 this.mobileId = mobileId;
749 public Long getMobileNumber () {
750 return this.mobileNumber;
754 public void setMobileNumber (Long mobileNumber) {
755 this.mobileNumber = mobileNumber;
759 public MobileProvider getMobileProvider () {
760 return this.mobileProvider;
764 public void setMobileProvider (final MobileProvider mobileProvider) {
765 this.mobileProvider = mobileProvider;
769 public PersonalTitle getPersonalTitle () {
770 return this.personalTitle;
774 public void setPersonalTitle (final PersonalTitle personalTitle) {
775 this.personalTitle = personalTitle;
779 public String getStreet () {
784 public void setStreet (final String street) {
785 this.street = street;
789 public String getAcademicTitle () {
790 return this.academicTitle;
794 public void setAcademicTitle (final String academicTitle) {
795 this.academicTitle = academicTitle;
799 public Integer getZipCode () {
804 public void setZipCode (final Integer zipCode) {
805 this.zipCode = zipCode;
809 * Post-initialization of this class
812 public void init () {
815 // Get initial context
816 Context context = new InitialContext();
819 this.contactBean = (ContactSessionBeanRemote) context.lookup("java:global/addressbook-ejb/contact!org.mxchange.jcontacts.contact.ContactSessionBeanRemote"); //NOI18N
821 // Try to lookup (administative)
822 this.adminContactBean = (AdminContactSessionBeanRemote) context.lookup("java:global/addressbook-ejb/adminContact!org.mxchange.jcontacts.contact.AdminContactSessionBeanRemote"); //NOI18N
823 } catch (final NamingException e) {
825 throw new FaceletException(e);
830 public boolean isPersonalTitleRequired () {
831 // Get context parameter
832 String contextParameter = FacesContext.getCurrentInstance().getExternalContext().getInitParameter("is_admin_personal_title_enabled"); //NOI18N
835 boolean isRequired = ((contextParameter instanceof String) && (contextParameter.toLowerCase().equals("true"))); //NOI18N
844 private void clear () {
847 this.setAcademicTitle(null);
848 this.setFirstName(null);
849 this.setFamilyName(null);
850 this.setStreet(null);
851 this.setHouseNumber(null);
852 this.setHouseNumberExtension(null);
853 this.setZipCode(null);
855 this.setCountry(null);
858 this.setEmailAddress(null);
859 this.setLandLineCountry(null);
860 this.setLandLineAreaCode(null);
861 this.setLandLineNumber(null);
862 this.setMobileProvider(null);
863 this.setMobileNumber(null);
864 this.setFaxCountry(null);
865 this.setFaxAreaCode(null);
866 this.setFaxNumber(null);
869 this.setBirthday(null);
870 this.setComment(null);
874 * Checks whether the given contact is found
876 * @param contact Contact inastance
878 * @return Wether contact has been found
880 private boolean isSameContactFound (final Contact contact) {
881 // Default is not found
882 boolean IsFound = false;
885 Iterator<Contact> iterator = this.contactController.allContacts().iterator();
888 while (iterator.hasNext()) {
890 Contact next = iterator.next();
893 if (ContactUtils.isSameContact(contact, next)) {
894 // Yes, then abort loop
905 * Updates all data in contact instance.
907 * @param contact Contact instance
909 private void updateContactData (final Contact contact) {
910 // Contact instance should be valid
911 if (null == contact) {
913 throw new NullPointerException("contact is null"); //NOI18N
914 } else if (contact.getContactId() == null) {
916 throw new NullPointerException("contact.contactId is null"); //NOI18N //NOI18N
917 } else if (contact.getContactId() < 1) {
919 throw new IllegalStateException(MessageFormat.format("contact.contactId={0} is invalid", contact.getContactId())); //NOI18N
923 contact.setContactPersonalTitle(this.getPersonalTitle());
924 contact.setContactTitle(this.getAcademicTitle());
925 contact.setContactFirstName(this.getFirstName());
926 contact.setContactFamilyName(this.getFamilyName());
927 contact.setContactStreet(this.getStreet());
928 contact.setContactHouseNumber(this.getHouseNumber());
929 contact.setContactHouseNumberExtension(this.getHouseNumberExtension());
930 contact.setContactZipCode(this.getZipCode());
931 contact.setContactCity(this.getCity());
932 contact.setContactCountry(this.getCountry());
934 // Update contact's cellphone number
935 this.isMobileNumberUnlinked = ContactUtils.updateMobileNumber(contact, this.getMobileProvider(), this.getMobileNumber());
937 // Update contact's land-line number
938 this.isLandLineUnlinked = ContactUtils.updateLandLineNumber(contact, this.getLandLineCountry(), this.getLandLineAreaCode(), this.getLandLineNumber());
940 // Update contact's fax number
941 this.isFaxUnlinked = ContactUtils.updateFaxNumber(contact, this.getFaxCountry(), this.getFaxAreaCode(), this.getFaxNumber());