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.jjobs.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.jcontacts.contact.AdminContactSessionBeanRemote;
34 import org.mxchange.jcontacts.contact.Contact;
35 import org.mxchange.jcontacts.contact.ContactSessionBeanRemote;
36 import org.mxchange.jcontacts.contact.ContactUtils;
37 import org.mxchange.jcontacts.contact.UserContact;
38 import org.mxchange.jcontacts.contact.title.PersonalTitle;
39 import org.mxchange.jcontacts.events.contact.add.AdminAddedContactEvent;
40 import org.mxchange.jcontacts.events.contact.add.ObservableAdminAddedContactEvent;
41 import org.mxchange.jcontacts.events.contact.update.AdminUpdatedContactEvent;
42 import org.mxchange.jcontacts.events.contact.update.ObservableAdminUpdatedContactEvent;
43 import org.mxchange.jcontacts.exceptions.ContactAlreadyAddedException;
44 import org.mxchange.jcountry.data.Country;
45 import org.mxchange.jjobs.beans.BaseJobsController;
46 import org.mxchange.jjobs.beans.helper.JobsWebRequestHelperController;
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 controller (bean)
59 * @author Roland Häder<roland@mxchange.org>
61 @Named ("adminContactController")
63 public class JobsAdminContactWebRequestBean extends BaseJobsController implements JobsAdminContactWebRequestController {
68 private static final long serialVersionUID = 542_145_347_916L;
70 * Academic academicTitle
72 private String academicTitle;
75 * An event fired when the administrator has added a new contact
79 private Event<ObservableAdminAddedContactEvent> addedContactEvent;
82 * Administrative contact EJB
84 private AdminContactSessionBeanRemote adminContactBean;
87 * Bean helper instance
90 private JobsWebRequestHelperController beanHelper;
95 private Date birthday;
105 private String comment;
108 * Remote contact bean
110 private ContactSessionBeanRemote contactBean;
113 * General contact controller
116 private JobsContactWebSessionController contactController;
121 private Long contactId;
126 private Country country;
131 private String emailAddress;
136 private String familyName;
139 * Fax number's area code
141 private Integer faxAreaCode;
144 * Country instance for fax number
146 private Country faxCountry;
156 private Long faxNumber;
161 private String firstName;
166 private Short houseNumber;
169 * House number extension
171 private String houseNumberExtension;
174 * Whether a fax entry has been unlinked
176 private boolean isFaxUnlinked;
179 * Whether a land-line number has been unlinked
181 private boolean isLandLineUnlinked;
184 * Whether a cellphone entry has been unlinked
186 private boolean isMobileNumberUnlinked;
189 * Phone number area code
191 private Integer landLineAreaCode;
194 * Country instance for phone number
196 private Country landLineCountry;
199 * Land-line id number
201 private Long landLineId;
206 private Long landLineNumber;
211 private Long mobileId;
216 private Long mobileNumber;
219 * Mobile number's provider
221 private MobileProvider mobileProvider;
224 * PersonalTitle instance
226 private PersonalTitle personalTitle;
231 private String street;
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 JobsAdminContactWebRequestBean () {
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()
545 public String getAcademicTitle () {
546 return this.academicTitle;
549 public void setAcademicTitle (final String academicTitle) {
550 this.academicTitle = academicTitle;
554 @SuppressWarnings ("ReturnOfDateField")
555 public Date getBirthday () {
556 return this.birthday;
560 @SuppressWarnings ("AssignmentToDateFieldFromParameter")
561 public void setBirthday (final Date birthday) {
562 this.birthday = birthday;
566 public String getCity () {
571 public void setCity (final String city) {
576 public String getComment () {
581 public void setComment (final String comment) {
582 this.comment = comment;
586 public Long getContactId () {
587 return this.contactId;
591 public void setContactId (final Long contactId) {
592 this.contactId = contactId;
596 public String getControllerType () {
597 return "admin"; //NOI18N
602 public void setControllerType (final String controllerType) {
603 throw new UnsupportedOperationException("Setting controller type is not supported."); //NOI18N
607 public Country getCountry () {
612 public void setCountry (final Country country) {
613 this.country = country;
617 public String getEmailAddress () {
618 return this.emailAddress;
622 public void setEmailAddress (final String emailAddress) {
623 this.emailAddress = emailAddress;
627 public String getFamilyName () {
628 return this.familyName;
632 public void setFamilyName (final String familyName) {
633 this.familyName = familyName;
637 public Integer getFaxAreaCode () {
638 return this.faxAreaCode;
642 public void setFaxAreaCode (final Integer faxAreaCode) {
643 this.faxAreaCode = faxAreaCode;
647 public Country getFaxCountry () {
648 return this.faxCountry;
652 public void setFaxCountry (final Country faxCountry) {
653 this.faxCountry = faxCountry;
657 public Long getFaxId () {
662 public void setFaxId (final Long faxId) {
667 public Long getFaxNumber () {
668 return this.faxNumber;
672 public void setFaxNumber (final Long faxNumber) {
673 this.faxNumber = faxNumber;
677 public String getFirstName () {
678 return this.firstName;
682 public void setFirstName (final String firstName) {
683 this.firstName = firstName;
687 public Short getHouseNumber () {
688 return this.houseNumber;
692 public void setHouseNumber (final Short houseNumber) {
693 this.houseNumber = houseNumber;
697 public String getHouseNumberExtension () {
698 return this.houseNumberExtension;
702 public void setHouseNumberExtension (final String houseNumberExtension) {
703 this.houseNumberExtension = houseNumberExtension;
707 public Integer getLandLineAreaCode () {
708 return this.landLineAreaCode;
712 public void setLandLineAreaCode (final Integer landLineAreaCode) {
713 this.landLineAreaCode = landLineAreaCode;
717 public Country getLandLineCountry () {
718 return this.landLineCountry;
722 public void setLandLineCountry (final Country landLineCountry) {
723 this.landLineCountry = landLineCountry;
727 public Long getLandLineId () {
728 return this.landLineId;
732 public void setLandLineId (final Long landLineId) {
733 this.landLineId = landLineId;
737 public Long getLandLineNumber () {
738 return this.landLineNumber;
742 public void setLandLineNumber (final Long landLineNumber) {
743 this.landLineNumber = landLineNumber;
747 public Long getMobileId () {
748 return this.mobileId;
752 public void setMobileId (final Long mobileId) {
753 this.mobileId = mobileId;
757 public Long getMobileNumber () {
758 return this.mobileNumber;
762 public void setMobileNumber (Long mobileNumber) {
763 this.mobileNumber = mobileNumber;
767 public MobileProvider getMobileProvider () {
768 return this.mobileProvider;
772 public void setMobileProvider (final MobileProvider mobileProvider) {
773 this.mobileProvider = mobileProvider;
777 public PersonalTitle getPersonalTitle () {
778 return this.personalTitle;
782 public void setPersonalTitle (final PersonalTitle personalTitle) {
783 this.personalTitle = personalTitle;
787 public String getStreet () {
792 public void setStreet (final String street) {
793 this.street = street;
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/jjobs-ejb/contact!org.mxchange.jcontacts.contact.ContactSessionBeanRemote"); //NOI18N
820 // Try to lookup (administative)
821 this.adminContactBean = (AdminContactSessionBeanRemote) context.lookup("java:global/jjobs-ejb/adminContact!org.mxchange.jcontacts.contact.AdminContactSessionBeanRemote"); //NOI18N
822 } catch (final NamingException e) {
824 throw new FaceletException(e);
829 public boolean isPersonalTitleRequired () {
830 // Get context parameter
831 String contextParameter = FacesContext.getCurrentInstance().getExternalContext().getInitParameter("is_admin_personal_title_enabled"); //NOI18N
834 boolean isRequired = ((contextParameter instanceof String) && (contextParameter.toLowerCase().equals("true"))); //NOI18N
843 private void clear () {
846 this.setAcademicTitle(null);
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.setContactPersonalTitle(this.getPersonalTitle());
923 contact.setContactTitle(this.getAcademicTitle());
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());