]> git.mxchange.org Git - pizzaservice-war.git/blobdiff - src/java/org/mxchange/pizzaapplication/beans/contact/PizzaAdminContactWebRequestBean.java
Please cherry-pick:
[pizzaservice-war.git] / src / java / org / mxchange / pizzaapplication / beans / contact / PizzaAdminContactWebRequestBean.java
index f8ce3f5a4dda8358849db401d2091f32e64396ac..4e2496f9e62528f0f9e524aef526f6197d9fe50e 100644 (file)
@@ -19,39 +19,36 @@ package org.mxchange.pizzaapplication.beans.contact;
 import java.text.MessageFormat;
 import java.util.Date;
 import java.util.Iterator;
-import javax.annotation.PostConstruct;
+import javax.ejb.EJB;
 import javax.enterprise.context.RequestScoped;
 import javax.enterprise.event.Event;
+import javax.enterprise.event.Observes;
 import javax.enterprise.inject.Any;
-import javax.faces.context.FacesContext;
 import javax.faces.view.facelets.FaceletException;
 import javax.inject.Inject;
 import javax.inject.Named;
-import javax.naming.Context;
-import javax.naming.InitialContext;
-import javax.naming.NamingException;
-import org.mxchange.jcontacts.contact.AdminContactSessionBeanRemote;
-import org.mxchange.jcontacts.contact.Contact;
-import org.mxchange.jcontacts.contact.ContactSessionBeanRemote;
-import org.mxchange.jcontacts.contact.ContactUtils;
-import org.mxchange.jcontacts.contact.UserContact;
-import org.mxchange.jcontacts.contact.title.PersonalTitle;
 import org.mxchange.jcontacts.events.contact.add.AdminAddedContactEvent;
 import org.mxchange.jcontacts.events.contact.add.ObservableAdminAddedContactEvent;
+import org.mxchange.jcontacts.events.contact.created.ObservableCreatedContactEvent;
 import org.mxchange.jcontacts.events.contact.update.AdminUpdatedContactEvent;
 import org.mxchange.jcontacts.events.contact.update.ObservableAdminUpdatedContactEvent;
 import org.mxchange.jcontacts.exceptions.ContactAlreadyAddedException;
-import org.mxchange.jcountry.data.Country;
-import org.mxchange.jphone.phonenumbers.DialableNumber;
-import org.mxchange.jphone.phonenumbers.fax.DialableFaxNumber;
-import org.mxchange.jphone.phonenumbers.fax.FaxNumber;
-import org.mxchange.jphone.phonenumbers.landline.DialableLandLineNumber;
-import org.mxchange.jphone.phonenumbers.landline.LandLineNumber;
-import org.mxchange.jphone.phonenumbers.mobile.DialableMobileNumber;
-import org.mxchange.jphone.phonenumbers.mobile.MobileNumber;
-import org.mxchange.jphone.phonenumbers.mobileprovider.MobileProvider;
+import org.mxchange.jcontacts.model.contact.AdminContactSessionBeanRemote;
+import org.mxchange.jcontacts.model.contact.Contact;
+import org.mxchange.jcontacts.model.contact.ContactSessionBeanRemote;
+import org.mxchange.jcontacts.model.contact.ContactUtils;
+import org.mxchange.jcontacts.model.contact.UserContact;
+import org.mxchange.jcontacts.model.contact.title.PersonalTitle;
+import org.mxchange.jcountry.model.data.Country;
+import org.mxchange.jphone.model.phonenumbers.DialableNumber;
+import org.mxchange.jphone.model.phonenumbers.fax.DialableFaxNumber;
+import org.mxchange.jphone.model.phonenumbers.fax.FaxNumber;
+import org.mxchange.jphone.model.phonenumbers.landline.DialableLandLineNumber;
+import org.mxchange.jphone.model.phonenumbers.landline.LandLineNumber;
+import org.mxchange.jphone.model.phonenumbers.mobile.DialableMobileNumber;
+import org.mxchange.jphone.model.phonenumbers.mobile.MobileNumber;
+import org.mxchange.jphone.model.phonenumbers.mobileprovider.MobileProvider;
 import org.mxchange.pizzaapplication.beans.BasePizzaController;
-import org.mxchange.pizzaapplication.beans.helper.PizzaWebRequestHelperController;
 
 /**
  * Administrative user bean (controller)
@@ -82,14 +79,9 @@ public class PizzaAdminContactWebRequestBean extends BasePizzaController impleme
        /**
         * Administrative contact EJB
         */
+       @EJB (lookup = "java:global/jfinancials-ejb/adminContact!org.mxchange.jcontacts.model.contact.AdminContactSessionBeanRemote")
        private AdminContactSessionBeanRemote adminContactBean;
 
-       /**
-        * Bean helper instance
-        */
-       @Inject
-       private PizzaWebRequestHelperController beanHelper;
-
        /**
         * Birth day
         */
@@ -106,25 +98,26 @@ public class PizzaAdminContactWebRequestBean extends BasePizzaController impleme
        private String comment;
 
        /**
-        * Remote contact bean
+        * EJB for general contact purposes
         */
+       @EJB (lookup = "java:global/jfinancials-ejb/contact!org.mxchange.jcontacts.model.contact.ContactSessionBeanRemote")
        private ContactSessionBeanRemote contactBean;
 
        /**
         * General contact controller
         */
        @Inject
-       private PizzaContactWebSessionController contactController;
+       private PizzaContactWebRequestController contactController;
 
        /**
-        * Contact id
+        * Country instance
         */
-       private Long contactId;
+       private Country contactCountry;
 
        /**
-        * Country instance
+        * Contact id
         */
-       private Country contactCountry;
+       private Long contactId;
 
        /**
         * Email address
@@ -277,7 +270,7 @@ public class PizzaAdminContactWebRequestBean extends BasePizzaController impleme
                }
 
                // Create new contact instance
-               Contact contact = this.createContactInstance();
+               final Contact contact = this.createContactInstance();
 
                // Default is not same contact
                if (this.isSameContactFound(contact)) {
@@ -286,7 +279,7 @@ public class PizzaAdminContactWebRequestBean extends BasePizzaController impleme
                }
 
                // Init contact
-               Contact updatedContact;
+               final Contact updatedContact;
 
                // Try to call EJB
                try {
@@ -307,23 +300,34 @@ public class PizzaAdminContactWebRequestBean extends BasePizzaController impleme
                return "admin_list_contact"; //NOI18N
        }
 
-       @Override
-       public void copyContactToController (final Contact contact) {
+       /**
+        * Observer for events being fired when a bean helper has successfully
+        * created a contact instance.
+        * <p>
+        * @param event Event being fired
+        */
+       public void afterCreatedContactEvent (@Observes final ObservableCreatedContactEvent event) {
                // Log message
-               //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("AdminContactController::copyContactToController(): contact={0} - CALLED!", contact)); //NOI18N
+               //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("AdminContactController::afterCreatedContactEvent(): contact={0} - CALLED!", contact)); //NOI18N
 
-               // The contact instance must be valid
-               if (null == contact) {
+               // The event instance must be valid
+               if (null == event) {
                        // Throw NPE again
-                       throw new NullPointerException("contact is null"); //NOI18N
-               } else if (contact.getContactId() == null) {
+                       throw new NullPointerException("event is null"); //NOI18N
+               } else if (event.getCreatedContact() == null) {
                        // Throw NPE again
-                       throw new NullPointerException("contact.contactId is null"); //NOI18N //NOI18N
-               } else if (contact.getContactId() < 1) {
+                       throw new NullPointerException("event.createdContact is null"); //NOI18N //NOI18N
+               } else if (event.getCreatedContact().getContactId() == null) {
+                       // Throw NPE again
+                       throw new NullPointerException("event.createdContact.contactId is null"); //NOI18N //NOI18N
+               } else if (event.getCreatedContact().getContactId() < 1) {
                        // Not valid
-                       throw new IllegalStateException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N
+                       throw new IllegalStateException(MessageFormat.format("event.createdContact.contactId={0} is not valid.", event.getCreatedContact().getContactId())); //NOI18N
                }
 
+               // Get contact instance from event
+               final Contact contact = event.getCreatedContact();
+
                // Set all fields: contact
                this.setContactId(contact.getContactId());
                this.setAcademicTitle(contact.getContactTitle());
@@ -367,7 +371,7 @@ public class PizzaAdminContactWebRequestBean extends BasePizzaController impleme
                }
 
                // Log message
-               //* NOISY-DEBUG: */ System.out.println("AdminContactController::copyContactToController(): EXIT!"); //NOI18N
+               //* NOISY-DEBUG: */ System.out.println("AdminContactController::afterCreatedContactEvent(): EXIT!"); //NOI18N
        }
 
        @Override
@@ -391,12 +395,18 @@ public class PizzaAdminContactWebRequestBean extends BasePizzaController impleme
                }
 
                // Generate phone number
-               DialableLandLineNumber landLine = new LandLineNumber(this.getLandLineCountry(), this.getLandLineAreaCode(), this.getLandLineNumber());
-               DialableMobileNumber mobile = new MobileNumber(this.getMobileProvider(), this.getMobileNumber());
-               DialableFaxNumber fax = new FaxNumber(this.getFaxCountry(), this.getFaxAreaCode(), this.getFaxNumber());
+               final DialableLandLineNumber landLine = new LandLineNumber(this.getLandLineCountry(), this.getLandLineAreaCode(), this.getLandLineNumber());
+               final DialableMobileNumber mobile = new MobileNumber(this.getMobileProvider(), this.getMobileNumber());
+               final DialableFaxNumber fax = new FaxNumber(this.getFaxCountry(), this.getFaxAreaCode(), this.getFaxNumber());
 
                // Create new instance
-               Contact contact = new UserContact(this.getPersonalTitle(), this.getFirstName(), this.getFamilyName());
+               final Contact contact = new UserContact(this.getPersonalTitle(), this.getFirstName(), this.getFamilyName());
+
+               // Is contact id set?
+               if (this.getContactId() instanceof Long) {
+                       // Set it, too
+                       contact.setContactId(this.getContactId());
+               }
 
                // Add all others
                contact.setContactTitle(this.getAcademicTitle());
@@ -481,7 +491,7 @@ public class PizzaAdminContactWebRequestBean extends BasePizzaController impleme
         */
        public String editContactData () {
                // Get contact instance
-               Contact contact = this.beanHelper.getContact();
+               final Contact contact = this.createContactInstance();
 
                // Check if contact instance is in helper and valid
                if (null == contact) {
@@ -499,7 +509,7 @@ public class PizzaAdminContactWebRequestBean extends BasePizzaController impleme
                this.updateContactData(contact);
 
                // Call EJB for updating contact data
-               Contact updatedContact = this.contactBean.updateContactData(contact, this.isMobileNumberUnlinked, this.isLandLineUnlinked, this.isFaxUnlinked);
+               final Contact updatedContact = this.contactBean.updateContactData(contact, this.isMobileNumberUnlinked, this.isLandLineUnlinked, this.isFaxUnlinked);
 
                // Fire event
                this.updatedContactEvent.fire(new AdminUpdatedContactEvent(updatedContact));
@@ -526,12 +536,12 @@ public class PizzaAdminContactWebRequestBean extends BasePizzaController impleme
                }
 
                // Get all data
-               String number = String.format(
-                          "%s%d%d", //NOI18N
-                          mobileNumber.getMobileProvider().getProviderCountry().getCountryExternalDialPrefix(),
-                          mobileNumber.getMobileProvider().getProviderDialPrefix(),
-                          mobileNumber.getPhoneNumber()
-          );
+               final String number = String.format(
+                                        "%s%d%d", //NOI18N
+                                        mobileNumber.getMobileProvider().getProviderCountry().getCountryExternalDialPrefix(),
+                                        mobileNumber.getMobileProvider().getProviderDialPrefix(),
+                                        mobileNumber.getPhoneNumber()
+                        );
 
                // Return it
                return number;
@@ -553,12 +563,12 @@ public class PizzaAdminContactWebRequestBean extends BasePizzaController impleme
                }
 
                // Generate it
-               String number = String.format(
-                          "%s%d%d", //NOI18N
-                          phoneNumber.getPhoneCountry().getCountryExternalDialPrefix(),
-                          phoneNumber.getPhoneAreaCode(),
-                          phoneNumber.getPhoneNumber()
-          );
+               final String number = String.format(
+                                        "%s%d%d", //NOI18N
+                                        phoneNumber.getPhoneCountry().getCountryExternalDialPrefix(),
+                                        phoneNumber.getPhoneAreaCode(),
+                                        phoneNumber.getPhoneNumber()
+                        );
 
                // Return it
                return number;
@@ -638,6 +648,24 @@ public class PizzaAdminContactWebRequestBean extends BasePizzaController impleme
                this.comment = comment;
        }
 
+       /**
+        * Getter for contactCountry instance
+        * <p>
+        * @return Country instance
+        */
+       public Country getContactCountry () {
+               return this.contactCountry;
+       }
+
+       /**
+        * Setter for contactCountry instance
+        * <p>
+        * @param contactCountry Country instance
+        */
+       public void setContactCountry (final Country contactCountry) {
+               this.contactCountry = contactCountry;
+       }
+
        /**
         * Getter for contact id
         * <p>
@@ -667,24 +695,6 @@ public class PizzaAdminContactWebRequestBean extends BasePizzaController impleme
                throw new UnsupportedOperationException("Setting controller type is not supported."); //NOI18N
        }
 
-       /**
-        * Getter for contactCountry instance
-        * <p>
-        * @return Country instance
-        */
-       public Country getContactCountry () {
-               return this.contactCountry;
-       }
-
-       /**
-        * Setter for contactCountry instance
-        * <p>
-        * @param contactCountry Country instance
-        */
-       public void setContactCountry (final Country contactCountry) {
-               this.contactCountry = contactCountry;
-       }
-
        /**
         * Getter for email address
         * <p>
@@ -1028,38 +1038,16 @@ public class PizzaAdminContactWebRequestBean extends BasePizzaController impleme
                this.zipCode = zipCode;
        }
 
-       /**
-        * Post-initialization of this class
-        */
-       @PostConstruct
-       public void init () {
-               // Try it
-               try {
-                       // Get initial context
-                       Context context = new InitialContext();
-
-                       // Try to lookup
-                       this.contactBean = (ContactSessionBeanRemote) context.lookup("java:global/pizzaservice-ejb/contact!org.mxchange.jcontacts.contact.ContactSessionBeanRemote"); //NOI18N
-
-                       // Try to lookup (administative)
-                       this.adminContactBean = (AdminContactSessionBeanRemote) context.lookup("java:global/pizzaservice-ejb/adminContact!org.mxchange.jcontacts.contact.AdminContactSessionBeanRemote"); //NOI18N
-               } catch (final NamingException e) {
-                       // Throw again
-                       throw new FaceletException(e);
-               }
-       }
-
        @Override
-       @Deprecated
-       public boolean isPersonalTitleRequired () {
-               // Get context parameter
-               String contextParameter = FacesContext.getCurrentInstance().getExternalContext().getInitParameter("is_admin_personal_title_enabled"); //NOI18N
-
-               // Is it set?
-               boolean isRequired = ((contextParameter instanceof String) && (contextParameter.toLowerCase().equals("true"))); //NOI18N
-
-               // Return value
-               return isRequired;
+       public boolean isRequiredPersonalDataSet () {
+               return ((this.getPersonalTitle() != null) &&
+                               (this.getFirstName() != null) &&
+                               (this.getFamilyName() != null) &&
+                               (this.getStreet() != null) &&
+                               (this.getHouseNumber() != null) &&
+                               (this.getZipCode() != null) &&
+                               (this.getCity() != null) &&
+                               (this.getEmailAddress() != null));
        }
 
        @Override
@@ -1123,21 +1111,21 @@ public class PizzaAdminContactWebRequestBean extends BasePizzaController impleme
        /**
         * Checks whether the given contact is found
         * <p>
-        * @param contact Contact inastance
-        *
-        * @return Wether contact has been found
+        * @param contact Contact instance
+        * <p>
+        * @return Whether contact has been found
         */
        private boolean isSameContactFound (final Contact contact) {
                // Default is not found
                boolean IsFound = false;
 
                // Get iterator
-               Iterator<Contact> iterator = this.contactController.allContacts().iterator();
+               final Iterator<Contact> iterator = this.contactController.allContacts().iterator();
 
                // Loop through all
                while (iterator.hasNext()) {
                        // Get next contact
-                       Contact next = iterator.next();
+                       final Contact next = iterator.next();
 
                        // Is the same?
                        if (ContactUtils.isSameContact(contact, next)) {