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.pizzaapplication.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.UserContact;
37 import org.mxchange.jcontacts.contact.gender.Gender;
38 import org.mxchange.jcontacts.contact.utils.ContactUtils;
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.jphone.phonenumbers.DialableNumber;
46 import org.mxchange.jphone.phonenumbers.fax.DialableFaxNumber;
47 import org.mxchange.jphone.phonenumbers.fax.FaxNumber;
48 import org.mxchange.jphone.phonenumbers.landline.DialableLandLineNumber;
49 import org.mxchange.jphone.phonenumbers.landline.LandLineNumber;
50 import org.mxchange.jphone.phonenumbers.mobile.DialableMobileNumber;
51 import org.mxchange.jphone.phonenumbers.mobile.MobileNumber;
52 import org.mxchange.jphone.phonenumbers.mobileprovider.MobileProvider;
53 import org.mxchange.pizzaapplication.beans.BasePizzaController;
54 import org.mxchange.pizzaapplication.beans.helper.PizzaWebRequestHelperController;
57 * Administrative user bean (controller)
59 * @author Roland Häder<roland@mxchange.org>
61 @Named ("adminContactController")
63 public class PizzaAdminContactWebRequestBean extends BasePizzaController implements PizzaAdminContactWebRequestController {
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 PizzaWebRequestHelperController beanHelper;
91 private Date birthday;
94 * Cellphone number's carrier
96 private MobileProvider mobileCarrier;
101 private Long mobileId;
106 private Long mobileNumber;
116 private String comment;
119 * Remote contact bean
121 private ContactSessionBeanRemote contactBean;
124 * General contact controller
127 private PizzaContactWebSessionController contactController;
132 private Long contactId;
137 private Country country;
142 private String emailAddress;
147 private String familyName;
150 * Fax number's area code
152 private Integer faxAreaCode;
155 * Country instance for fax number
157 private Country faxCountry;
167 private Long faxNumber;
172 private String firstName;
177 private Gender gender;
182 private Short houseNumber;
185 * House number extension
187 private String houseNumberExtension;
190 * Whether a cellphone entry has been unlinked
192 private boolean isCellphoneUnlinked;
195 * Whether a fax entry has been unlinked
197 private boolean isFaxUnlinked;
200 * Whether a land-line number has been unlinked
202 private boolean isLandLineUnlinked;
205 * Land-line id number
207 private Long landLineId;
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 PizzaAdminContactWebRequestBean () {
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.setMobileCarrier(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.setPhoneAreaCode(contact.getContactLandLineNumber().getPhoneAreaCode());
360 this.setPhoneCountry(contact.getContactLandLineNumber().getPhoneCountry());
361 this.setPhoneNumber(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.getPhoneCountry(), this.getPhoneAreaCode(), this.getPhoneNumber());
390 DialableMobileNumber mobile = new MobileNumber(this.getMobileCarrier(), 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.getPhoneAreaCode() != null) && (this.getPhoneNumber() != null) && (this.getPhoneAreaCode() > 0) && (this.getPhoneNumber() > 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.getMobileCarrier() 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.isCellphoneUnlinked, 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 generateCellphoneNumber (final DialableMobileNumber cellphoneNumber) {
507 if (null == cellphoneNumber) {
513 String number = String.format(
515 cellphoneNumber.getMobileProvider().getProviderCountry().getCountryExternalDialPrefix(),
516 cellphoneNumber.getMobileProvider().getProviderDialPrefix(),
517 cellphoneNumber.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 MobileProvider getMobileCarrier () {
558 return this.mobileCarrier;
562 public void setMobileCarrier (final MobileProvider mobileCarrier) {
563 this.mobileCarrier = mobileCarrier;
567 public Long getMobileId () {
568 return this.mobileId;
572 public void setMobileId (final Long mobileId) {
573 this.mobileId = mobileId;
577 public Long getMobileNumber () {
578 return this.mobileNumber;
582 public void setMobileNumber (Long mobileNumber) {
583 this.mobileNumber = mobileNumber;
587 public String getCity () {
592 public void setCity (final String city) {
597 public String getComment () {
602 public void setComment (final String comment) {
603 this.comment = comment;
607 public Long getContactId () {
608 return this.contactId;
612 public void setContactId (final Long contactId) {
613 this.contactId = contactId;
617 public String getControllerType () {
618 return "admin"; //NOI18N
623 public void setControllerType (final String controllerType) {
624 throw new UnsupportedOperationException("Setting controller type is not supported."); //NOI18N
628 public Country getCountry () {
633 public void setCountry (final Country country) {
634 this.country = country;
638 public String getEmailAddress () {
639 return this.emailAddress;
643 public void setEmailAddress (final String emailAddress) {
644 this.emailAddress = emailAddress;
648 public String getFamilyName () {
649 return this.familyName;
653 public void setFamilyName (final String familyName) {
654 this.familyName = familyName;
658 public Integer getFaxAreaCode () {
659 return this.faxAreaCode;
663 public void setFaxAreaCode (final Integer faxAreaCode) {
664 this.faxAreaCode = faxAreaCode;
668 public Country getFaxCountry () {
669 return this.faxCountry;
673 public void setFaxCountry (final Country faxCountry) {
674 this.faxCountry = faxCountry;
678 public Long getFaxId () {
683 public void setFaxId (final Long faxId) {
688 public Long getFaxNumber () {
689 return this.faxNumber;
693 public void setFaxNumber (final Long faxNumber) {
694 this.faxNumber = faxNumber;
698 public String getFirstName () {
699 return this.firstName;
703 public void setFirstName (final String firstName) {
704 this.firstName = firstName;
708 public Gender getGender () {
713 public void setGender (final Gender gender) {
714 this.gender = gender;
718 public Short getHouseNumber () {
719 return this.houseNumber;
723 public void setHouseNumber (final Short houseNumber) {
724 this.houseNumber = houseNumber;
728 public String getHouseNumberExtension () {
729 return this.houseNumberExtension;
733 public void setHouseNumberExtension (final String houseNumberExtension) {
734 this.houseNumberExtension = houseNumberExtension;
738 public Long getLandLineId () {
739 return this.landLineId;
743 public void setLandLineId (final Long landLineId) {
744 this.landLineId = landLineId;
748 public Integer getPhoneAreaCode () {
749 return this.phoneAreaCode;
753 public void setPhoneAreaCode (final Integer phoneAreaCode) {
754 this.phoneAreaCode = phoneAreaCode;
758 public Country getPhoneCountry () {
759 return this.phoneCountry;
763 public void setPhoneCountry (final Country phoneCountry) {
764 this.phoneCountry = phoneCountry;
768 public Long getPhoneNumber () {
769 return this.phoneNumber;
773 public void setPhoneNumber (final Long phoneNumber) {
774 this.phoneNumber = phoneNumber;
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.setPhoneCountry(null);
859 this.setPhoneAreaCode(null);
860 this.setPhoneNumber(null);
861 this.setMobileCarrier(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.isCellphoneUnlinked = ContactUtils.updateMobileNumber(contact, this.getMobileCarrier(), this.getMobileNumber());
936 // Update contact's land-line number
937 this.isLandLineUnlinked = ContactUtils.updateLandLineNumber(contact, this.getPhoneCountry(), this.getPhoneAreaCode(), this.getPhoneNumber());
939 // Update contact's fax number
940 this.isFaxUnlinked = ContactUtils.updateFaxNumber(contact, this.getFaxCountry(), this.getFaxAreaCode(), this.getFaxNumber());