]> git.mxchange.org Git - pizzaservice-war.git/blobdiff - src/java/org/mxchange/pizzaapplication/beans/business/basicdata/PizzaAdminBusinessDataWebRequestBean.java
Don't cherry-pick:
[pizzaservice-war.git] / src / java / org / mxchange / pizzaapplication / beans / business / basicdata / PizzaAdminBusinessDataWebRequestBean.java
index dfe472bf752657e3a14f74d5666b9aec1af34709..8aa667d93d5d59160d5e51a44f65b97c944d962f 100644 (file)
  */
 package org.mxchange.pizzaapplication.beans.business.basicdata;
 
-import javax.annotation.PostConstruct;
-import javax.enterprise.context.SessionScoped;
-import javax.faces.view.facelets.FaceletException;
+import javax.ejb.EJB;
+import javax.enterprise.context.RequestScoped;
+import javax.enterprise.event.Event;
+import javax.enterprise.inject.Any;
+import javax.faces.FacesException;
+import javax.inject.Inject;
 import javax.inject.Named;
-import javax.naming.Context;
-import javax.naming.InitialContext;
-import javax.naming.NamingException;
-import org.mxchange.jcontactsbusiness.basicdata.AdminBusinessDataSessionBeanRemote;
-import org.mxchange.jcontactsbusiness.employee.Employee;
-import org.mxchange.jcontactsbusiness.headquarters.HeadQuartersData;
-import org.mxchange.jcountry.data.Country;
+import org.mxchange.jcontactsbusiness.model.basicdata.AdminBasicCompanyDataSessionBeanRemote;
+import org.mxchange.jcontactsbusiness.model.basicdata.BusinessBasicData;
+import org.mxchange.jcontactsbusiness.model.basicdata.CompanyBasicData;
+import org.mxchange.jcontactsbusiness.model.employee.Employee;
+import org.mxchange.jcontactsbusiness.events.basicdata.added.AdminAddedBusinessBasicDataEvent;
+import org.mxchange.jcontactsbusiness.events.basicdata.added.ObservableAdminAddedBusinessBasicDataEvent;
+import org.mxchange.jcontactsbusiness.exceptions.basicdata.BasicCompanyDataAlreadyAddedException;
+import org.mxchange.jcontactsbusiness.model.headquarters.HeadquartersData;
+import org.mxchange.jcountry.model.data.Country;
+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.jusercore.model.user.User;
 import org.mxchange.pizzaapplication.beans.BasePizzaController;
 
@@ -35,8 +44,8 @@ import org.mxchange.pizzaapplication.beans.BasePizzaController;
  * <p>
  * @author Roland Häder<roland@mxchange.org>
  */
-@Named ("adminBusinessDataController")
-@SessionScoped
+@Named ("adminCompanyDataController")
+@RequestScoped
 public class PizzaAdminBusinessDataWebRequestBean extends BasePizzaController implements PizzaAdminBusinessDataWebRequestController {
 
        /**
@@ -45,9 +54,17 @@ public class PizzaAdminBusinessDataWebRequestBean extends BasePizzaController im
        private static final long serialVersionUID = 56_189_028_928_374L;
 
        /**
-        * Remote contact bean
+        * EJB for administrative purposes
         */
-       private AdminBusinessDataSessionBeanRemote adminBusinessDataBean;
+       @EJB (lookup = "java:global/addressbook-ejb/adminBasicCompanyData!org.mxchange.jcontactsbusiness.model.basicdata.AdminBasicCompanyDataSessionBeanRemote")
+       private AdminBasicCompanyDataSessionBeanRemote adminBasicCompanyDataBean;
+
+       /**
+        * An event being fired when basic business data has been added
+        */
+       @Inject
+       @Any
+       private Event<ObservableAdminAddedBusinessBasicDataEvent> businessDataAddedEvent;
 
        /**
         * Comments for this company
@@ -64,10 +81,15 @@ public class PizzaAdminBusinessDataWebRequestBean extends BasePizzaController im
         */
        private String companyEmailAddress;
 
+       /**
+        * Company founder
+        */
+       private Employee companyFounder;
+
        /**
         * Head quarter data for this company
         */
-       private HeadQuartersData companyHeadQuarters;
+       private HeadquartersData companyHeadQuarters;
 
        /**
         * Company name
@@ -80,14 +102,14 @@ public class PizzaAdminBusinessDataWebRequestBean extends BasePizzaController im
        private String companyTaxNumber;
 
        /**
-        * Web site URL
+        * Owning user instance (which this company is assigned to)
         */
-       private String companyWebsiteUrl;
+       private User companyUserOwner;
 
        /**
-        * Owning user instance (which this company is assigned to)
+        * Web site URL
         */
-       private User contactCompanyUserOwner;
+       private String companyWebsiteUrl;
 
        /**
         * Area code for fax number
@@ -120,13 +142,105 @@ public class PizzaAdminBusinessDataWebRequestBean extends BasePizzaController im
        private Long landLineNumber;
 
        /**
-        * Constructor
+        * Default constructor
         */
        public PizzaAdminBusinessDataWebRequestBean () {
                // Call super constructor
                super();
        }
 
+       /**
+        * Adds a basic business data entry, if not yet found.
+        * <p>
+        * @return Redirect outcome
+        */
+       public String addBusinessBasicData () {
+               // First, validate all parameter
+               if (this.getCompanyName() == null) {
+                       // Is null
+                       throw new NullPointerException("this.companyName is null"); //NOI18N
+               } else if (this.getCompanyName().isEmpty()) {
+                       // Is null
+                       throw new IllegalArgumentException("this.companyName is empty"); //NOI18N
+               }
+
+               // Prepare entity
+               BusinessBasicData basicData = new CompanyBasicData(this.getCompanyName());
+
+               // Set all opther remaining data
+               basicData.setCompanyComments(this.getCompanyComments());
+               basicData.setCompanyContactEmployee(this.getCompanyContactEmployee());
+               basicData.setCompanyEmailAddress(this.getCompanyEmailAddress());
+               basicData.setCompanyFounder(this.getCompanyFounder());
+               basicData.setCompanyHeadQuartersData(this.getCompanyHeadQuarters());
+               basicData.setCompanyTaxNumber(this.getCompanyTaxNumber());
+               basicData.setCompanyUserOwner(this.getCompanyUserOwner());
+               basicData.setCompanyWebsiteUrl(this.getCompanyWebsiteUrl());
+
+               // Set logo instance
+               // @TODO basicData.setCompanyLogo();
+               // Generate phone number
+               DialableLandLineNumber landLine = new LandLineNumber(this.getLandLineCountry(), this.getLandLineAreaCode(), this.getLandLineNumber());
+               DialableFaxNumber fax = new FaxNumber(this.getFaxCountry(), this.getFaxAreaCode(), this.getFaxNumber());
+
+               // Don't set null or wrong references
+               if ((landLine instanceof DialableLandLineNumber) && (landLine.getPhoneCountry() instanceof Country) && (this.getLandLineAreaCode() != null) && (this.getLandLineNumber() != null) && (this.getLandLineAreaCode() > 0) && (this.getLandLineNumber() > 0)) {
+                       // Now the number must be given
+                       if (landLine.getPhoneAreaCode() == null) {
+                               // Is null
+                               throw new NullPointerException("phone.phoneAreaCode is null"); //NOI18N
+                       } else if (landLine.getPhoneAreaCode() < 1) {
+                               // Abort here
+                               throw new IllegalArgumentException("phone.phoneAreaCode is zero or below."); //NOI18N
+                       } else if (landLine.getPhoneNumber() == null) {
+                               // Is null
+                               throw new NullPointerException("phone.phoneNumber is null"); //NOI18N
+                       } else if (landLine.getPhoneNumber() < 1) {
+                               // Abort here
+                               throw new IllegalArgumentException("phone.phoneNumber is zero or below."); //NOI18N
+                       }
+
+                       // Set phone number
+                       basicData.setCompanyLandLineNumber(landLine);
+               }
+
+               // Don't set null or wrong references
+               if ((fax instanceof DialableFaxNumber) && (fax.getPhoneCountry() instanceof Country) && (this.getFaxAreaCode() != null) && (this.getFaxNumber() != null) && (this.getFaxAreaCode() > 0) && (this.getFaxNumber() > 0)) {
+                       // Now the number must be given
+                       if (fax.getPhoneAreaCode() == null) {
+                               // Is null
+                               throw new NullPointerException("fax.phoneAreaCode is null"); //NOI18N
+                       } else if (fax.getPhoneAreaCode() < 1) {
+                               // Abort here
+                               throw new IllegalArgumentException("fax.phoneAreaCode is zero or below."); //NOI18N
+                       } else if (fax.getPhoneNumber() == null) {
+                               // Is null
+                               throw new NullPointerException("fax.phoneNumber is null"); //NOI18N
+                       } else if (fax.getPhoneNumber() < 1) {
+                               // Abort here
+                               throw new IllegalArgumentException("fax.phoneNumber is zero or below."); //NOI18N
+                       }
+
+                       // Set fax number
+                       basicData.setCompanyFaxNumber(fax);
+               }
+
+               // Now try to send to EJB and get an updated version back
+               try {
+                       // Try it
+                       BusinessBasicData updatedBasicData = this.adminBasicCompanyDataBean.addCompanyBasicData(basicData);
+
+                       // Fire event
+                       this.businessDataAddedEvent.fire(new AdminAddedBusinessBasicDataEvent(updatedBasicData));
+               } catch (final BasicCompanyDataAlreadyAddedException e) {
+                       // Does already exist
+                       throw new FacesException(e);
+               }
+
+               // Continue to list again
+               return "admin_list_business_contacts"; //NOI18N
+       }
+
        /**
         * Getter for comments
         * <p>
@@ -181,12 +295,30 @@ public class PizzaAdminBusinessDataWebRequestBean extends BasePizzaController im
                this.companyEmailAddress = companyEmailAddress;
        }
 
+       /**
+        * Getter for company founder
+        * <p>
+        * @return Company founder
+        */
+       public Employee getCompanyFounder () {
+               return this.companyFounder;
+       }
+
+       /**
+        * Setter for company founder
+        * <p>
+        * @param companyFounder Company founder
+        */
+       public void setCompanyFounder (final Employee companyFounder) {
+               this.companyFounder = companyFounder;
+       }
+
        /**
         * Getter for headquarters data
         * <p>
         * @return Headquarters data
         */
-       public HeadQuartersData getCompanyHeadQuarters () {
+       public HeadquartersData getCompanyHeadQuarters () {
                return this.companyHeadQuarters;
        }
 
@@ -195,7 +327,7 @@ public class PizzaAdminBusinessDataWebRequestBean extends BasePizzaController im
         * <p>
         * @param companyHeadQuarters Headquarters data
         */
-       public void setCompanyHeadQuarters (final HeadQuartersData companyHeadQuarters) {
+       public void setCompanyHeadQuarters (final HeadquartersData companyHeadQuarters) {
                this.companyHeadQuarters = companyHeadQuarters;
        }
 
@@ -236,39 +368,39 @@ public class PizzaAdminBusinessDataWebRequestBean extends BasePizzaController im
        }
 
        /**
-        * Getter for company web site URL
+        * Getter for owning user instance
         * <p>
-        * @return Company web site URL
+        * @return Owning user instance
         */
-       public String getCompanyWebsiteUrl () {
-               return this.companyWebsiteUrl;
+       public User getCompanyUserOwner () {
+               return this.companyUserOwner;
        }
 
        /**
-        * Getter for company web site URL
+        * Setter for owning user instance
         * <p>
-        * @param companyWebsiteUrl Company web site URL
+        * @param companyUserOwner Owning user instance
         */
-       public void setCompanyWebsiteUrl (final String companyWebsiteUrl) {
-               this.companyWebsiteUrl = companyWebsiteUrl;
+       public void setCompanyUserOwner (final User companyUserOwner) {
+               this.companyUserOwner = companyUserOwner;
        }
 
        /**
-        * Getter for owning user instance
+        * Getter for company web site URL
         * <p>
-        * @return Owning user instance
+        * @return Company web site URL
         */
-       public User getContactCompanyUserOwner () {
-               return this.contactCompanyUserOwner;
+       public String getCompanyWebsiteUrl () {
+               return this.companyWebsiteUrl;
        }
 
        /**
-        * Setter for owning user instance
+        * Getter for company web site URL
         * <p>
-        * @param contactCompanyUserOwner Owning user instance
+        * @param companyWebsiteUrl Company web site URL
         */
-       public void setContactCompanyUserOwner (final User contactCompanyUserOwner) {
-               this.contactCompanyUserOwner = contactCompanyUserOwner;
+       public void setCompanyWebsiteUrl (final String companyWebsiteUrl) {
+               this.companyWebsiteUrl = companyWebsiteUrl;
        }
 
        /**
@@ -379,22 +511,4 @@ public class PizzaAdminBusinessDataWebRequestBean extends BasePizzaController im
                this.landLineNumber = landLineNumber;
        }
 
-       /**
-        * Post-initialization of this class
-        */
-       @PostConstruct
-       public void init () {
-               // Try it
-               try {
-                       // Get initial context
-                       Context context = new InitialContext();
-
-                       // Try to lookup
-                       this.adminBusinessDataBean = (AdminBusinessDataSessionBeanRemote) context.lookup("java:global/pizzaservice-ejb/adminBusinessData!org.mxchange.jcontactsbusiness.basicdata.BusinessDataAdminSessionBeanRemote"); //NOI18N
-               } catch (final NamingException e) {
-                       // Throw again
-                       throw new FaceletException(e);
-               }
-       }
-
 }