From: Roland Häder <roland@mxchange.org>
Date: Thu, 28 Apr 2016 09:15:29 +0000 (+0200)
Subject: Continued with customer:
X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=61c7ad8eb569de12dbad99ce9e37b78de291ec3b;p=pizzaservice-war.git

Continued with customer:
- added converter for customerId <-> Customer object
- used this in administrative customer pages
- added method copyCustomerToController()
- added getter/setter for customer in helper bean
- some messages fixed (copy-paste)

Signed-off-by: Roland Häder <roland@mxchange.org>
---

diff --git a/src/java/org/mxchange/pizzaapplication/beans/customer/PizzaAdminCustomerWebRequestBean.java b/src/java/org/mxchange/pizzaapplication/beans/customer/PizzaAdminCustomerWebRequestBean.java
index d530b3e4..a8bc117c 100644
--- a/src/java/org/mxchange/pizzaapplication/beans/customer/PizzaAdminCustomerWebRequestBean.java
+++ b/src/java/org/mxchange/pizzaapplication/beans/customer/PizzaAdminCustomerWebRequestBean.java
@@ -16,6 +16,7 @@
  */
 package org.mxchange.pizzaapplication.beans.customer;
 
+import java.text.MessageFormat;
 import javax.annotation.PostConstruct;
 import javax.enterprise.context.RequestScoped;
 import javax.enterprise.event.Event;
@@ -153,6 +154,32 @@ public class PizzaAdminCustomerWebRequestBean implements PizzaAdminCustomerWebRe
 		return "admin_list_customer"; //NOI18N
 	}
 
+	@Override
+	public void copyCustomerToController (final Customer customer) {
+		// Parameters must be valid
+		if (null == customer) {
+			// Throw NPE
+			throw new NullPointerException("customer is null");
+		} else if (customer.getCustomerId() == null) {
+			// Throw again ...
+			throw new NullPointerException("customer.customerId is null");
+		} else if (customer.getCustomerId() < 1) {
+			// Invalid id number
+			throw new IllegalArgumentException(MessageFormat.format("customer.customerId={0} is not valid", customer.getCustomerId()));
+		} else if (customer.getCustomerContact() == null) {
+			// Throw NPE again
+			throw new NullPointerException("customer.customerContact is null");
+		} else if (customer.getCustomerContact().getContactId() == null) {
+			// .. and again
+			throw new NullPointerException("customer.customerContact.contactId is null");
+		} else if (customer.getCustomerContact().getContactId() < 1) {
+			// Invalid id
+			throw new IllegalArgumentException(MessageFormat.format("customer.customerContact.contactId={0} is not valid", customer.getCustomerContact().getContactId()));
+		}
+
+		// @TODO Set all data
+	}
+
 	/**
 	 * Post-initialization of this class
 	 */
diff --git a/src/java/org/mxchange/pizzaapplication/beans/customer/PizzaAdminCustomerWebRequestController.java b/src/java/org/mxchange/pizzaapplication/beans/customer/PizzaAdminCustomerWebRequestController.java
index 3bdcd59a..b068b415 100644
--- a/src/java/org/mxchange/pizzaapplication/beans/customer/PizzaAdminCustomerWebRequestController.java
+++ b/src/java/org/mxchange/pizzaapplication/beans/customer/PizzaAdminCustomerWebRequestController.java
@@ -17,6 +17,7 @@
 package org.mxchange.pizzaapplication.beans.customer;
 
 import java.io.Serializable;
+import org.mxchange.jcustomercore.model.customer.Customer;
 
 /**
  * An interface for user beans
@@ -25,6 +26,8 @@ import java.io.Serializable;
  */
 public interface PizzaAdminCustomerWebRequestController extends Serializable {
 
+	public void copyCustomerToController (Customer customer);
+
 	/**
 	 * Adds customer to database if not already added. This method should return
 	 * a redirect outcome on success.
diff --git a/src/java/org/mxchange/pizzaapplication/beans/helper/PizzaAdminWebRequestController.java b/src/java/org/mxchange/pizzaapplication/beans/helper/PizzaAdminWebRequestController.java
index d867fd8a..a89e6205 100644
--- a/src/java/org/mxchange/pizzaapplication/beans/helper/PizzaAdminWebRequestController.java
+++ b/src/java/org/mxchange/pizzaapplication/beans/helper/PizzaAdminWebRequestController.java
@@ -18,6 +18,7 @@ package org.mxchange.pizzaapplication.beans.helper;
 
 import java.io.Serializable;
 import org.mxchange.jcontacts.contact.Contact;
+import org.mxchange.jcustomercore.model.customer.Customer;
 import org.mxchange.jusercore.model.user.User;
 
 /**
@@ -70,9 +71,28 @@ public interface PizzaAdminWebRequestController extends Serializable {
 	 */
 	void setContact (final Contact contact);
 
+	/**
+	 * Getter for customer instance
+	 * <p>
+	 * @return Customer instance
+	 */
+	Customer getCustomer ();
+
+	/**
+	 * Setter for customer instance
+	 * <p>
+	 * @param customer Contact instance
+	 */
+	void setCustomer (final Customer customer);
+
 	/**
 	 * Copies currently set contact instance's data to adminContactController
 	 */
 	void copyContactToController ();
 
+	/**
+	 * Copies currently set customer instance's data to adminCustomerController
+	 */
+	void copyCustomerToController ();
+
 }
diff --git a/src/java/org/mxchange/pizzaapplication/beans/helper/PizzaAdminWebRequestHelper.java b/src/java/org/mxchange/pizzaapplication/beans/helper/PizzaAdminWebRequestHelper.java
index 98f3d284..b9aac7be 100644
--- a/src/java/org/mxchange/pizzaapplication/beans/helper/PizzaAdminWebRequestHelper.java
+++ b/src/java/org/mxchange/pizzaapplication/beans/helper/PizzaAdminWebRequestHelper.java
@@ -21,6 +21,7 @@ import javax.enterprise.context.RequestScoped;
 import javax.inject.Inject;
 import javax.inject.Named;
 import org.mxchange.jcontacts.contact.Contact;
+import org.mxchange.jcustomercore.model.customer.Customer;
 import org.mxchange.jusercore.model.user.User;
 import org.mxchange.pizzaapplication.beans.contact.PizzaAdminContactWebRequestController;
 import org.mxchange.pizzaapplication.beans.customer.PizzaAdminCustomerWebRequestController;
@@ -65,6 +66,11 @@ public class PizzaAdminWebRequestHelper implements PizzaAdminWebRequestControlle
 	 */
 	private Contact contact;
 
+	/**
+	 * Contact instance
+	 */
+	private Customer customer;
+
 	/**
 	 * General user controller
 	 */
@@ -112,6 +118,30 @@ public class PizzaAdminWebRequestHelper implements PizzaAdminWebRequestControlle
 		//* NOISY-DEBUG: */ System.out.println("AdminHelper::copyContactToController - EXIT!"); //NOI18N
 	}
 
+	@Override
+	public void copyCustomerToController () {
+		// Log message
+		//* NOISY-DEBUG: */ System.out.println("AdminHelper::copyCustomerToController - CALLED!"); //NOI18N
+
+		// Validate user instance
+		if (this.getCustomer() == null) {
+			// Throw NPE
+			throw new NullPointerException("this.customer is null"); //NOI18N
+		} else if (this.getCustomer().getCustomerId() == null) {
+			// Throw NPE again
+			throw new NullPointerException("this.customer.customerId is null"); //NOI18N
+		} else if (this.getCustomer().getCustomerId() < 1) {
+			// Not valid
+			throw new IllegalStateException(MessageFormat.format("this.customer.customerId={0} is not valid.", this.getCustomer().getCustomerId())); //NOI18N
+		}
+
+		// Set all fields: user
+		this.adminCustomerController.copyCustomerToController(this.getCustomer());
+
+		// Log message
+		//* NOISY-DEBUG: */ System.out.println("AdminHelper::copyCustomerToController - EXIT!"); //NOI18N
+	}
+
 	@Override
 	public void copyUserToController () {
 		// Log message
@@ -146,6 +176,16 @@ public class PizzaAdminWebRequestHelper implements PizzaAdminWebRequestControlle
 		this.contact = contact;
 	}
 
+	@Override
+	public Customer getCustomer () {
+		return this.customer;
+	}
+
+	@Override
+	public void setCustomer (final Customer customer) {
+		this.customer = customer;
+	}
+
 	@Override
 	public User getUser () {
 		return this.user;
diff --git a/src/java/org/mxchange/pizzaapplication/converter/contact/PizzaContactConverter.java b/src/java/org/mxchange/pizzaapplication/converter/contact/PizzaContactConverter.java
index 5e4df62d..c0ae8ce1 100644
--- a/src/java/org/mxchange/pizzaapplication/converter/contact/PizzaContactConverter.java
+++ b/src/java/org/mxchange/pizzaapplication/converter/contact/PizzaContactConverter.java
@@ -98,7 +98,7 @@ public class PizzaContactConverter implements Converter {
 			contact = this.contactBean.findContactById(contactId);
 
 			// Debug message
-			// NOISY-DEBUG: this.loggerBeanLocal.logDebug(MessageFormat.format("getAsObject: user={0}", user)); //NOI18N
+			// NOISY-DEBUG: this.loggerBeanLocal.logDebug(MessageFormat.format("getAsObject: contact={0}", contact)); //NOI18N
 		} catch (final NumberFormatException ex) {
 			// Throw again
 			throw new ConverterException(ex);
diff --git a/src/java/org/mxchange/pizzaapplication/converter/customer/PizzaCustomerConverter.java b/src/java/org/mxchange/pizzaapplication/converter/customer/PizzaCustomerConverter.java
new file mode 100644
index 00000000..6ecb2bda
--- /dev/null
+++ b/src/java/org/mxchange/pizzaapplication/converter/customer/PizzaCustomerConverter.java
@@ -0,0 +1,132 @@
+/*
+ * Copyright (C) 2016 Roland Haeder
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+package org.mxchange.pizzaapplication.converter.customer;
+
+import java.text.MessageFormat;
+import javax.faces.component.UIComponent;
+import javax.faces.context.FacesContext;
+import javax.faces.convert.Converter;
+import javax.faces.convert.ConverterException;
+import javax.faces.convert.FacesConverter;
+import javax.naming.Context;
+import javax.naming.InitialContext;
+import javax.naming.NamingException;
+import org.mxchange.jcoreeelogger.beans.local.logger.Log;
+import org.mxchange.jcoreeelogger.beans.local.logger.LoggerBeanLocal;
+import org.mxchange.jcustomercore.exceptions.CustomerNotFoundException;
+import org.mxchange.jcustomercore.model.customer.Customer;
+import org.mxchange.pizzaapplication.model.customer.PizzaCustomerSessionBeanRemote;
+
+/**
+ * Converter for customer id <-> valid customer instance
+ * <p>
+ * @author Roland Haeder<roland@mxchange.org>
+ */
+@FacesConverter (value = "CustomerConverter")
+public class PizzaCustomerConverter implements Converter {
+
+	/**
+	 * User EJB
+	 */
+	private PizzaCustomerSessionBeanRemote customerBean;
+
+	/**
+	 * Logger instance
+	 */
+	@Log
+	private LoggerBeanLocal loggerBeanLocal;
+
+	/**
+	 * Initialization of this converter
+	 */
+	public PizzaCustomerConverter () {
+		// Try to get it
+		try {
+			// Get initial context
+			Context context = new InitialContext();
+
+			// Lookup logger
+			this.loggerBeanLocal = (LoggerBeanLocal) context.lookup("java:global/jcore-logger-ejb/logger!org.mxchange.jcoreeelogger.beans.local.logger.LoggerBeanLocal"); //NOI18N
+
+			// ... and user controller
+			this.customerBean = (PizzaCustomerSessionBeanRemote) context.lookup("java:global/jratecalc-ejb/customer!de.chotime.jratecalc.model.customer.PizzaCustomerSessionBeanRemote"); //NOI18N
+		} catch (final NamingException ex) {
+			// Continue to throw it
+			throw new RuntimeException(MessageFormat.format("context.lookup() failed: {0}", ex.getMessage()), ex); //NOI18N
+		}
+	}
+
+	@Override
+	public Object getAsObject (final FacesContext context, final UIComponent component, final String submittedValue) {
+		// Trace message
+		// NOISY-DEBUG: this.loggerBeanLocal.logTrace(MessageFormat.format("getAsObject: context={0},component={1},submittedValue={2} - CALLED!", context, component, submittedValue)); //NOI18N
+
+		// Is the value null or empty?
+		if ((null == submittedValue) || (submittedValue.trim().isEmpty())) {
+			// Warning message
+			this.loggerBeanLocal.logWarning(MessageFormat.format("{0}.getAsObject(): submittedValue is null or empty - EXIT!", this.getClass().getSimpleName())); //NOI18N
+
+			// Return null
+			return null;
+		}
+
+		// Init instance
+		Customer customer = null;
+
+		try {
+			// Try to parse the value as long
+			Long customerId = Long.valueOf(submittedValue);
+
+			// Debug message
+			// NOISY-DEBUG: this.loggerBeanLocal.logDebug(MessageFormat.format("getAsObject: customerId{0}", customerId)); //NOI18N
+
+			// Try to get user instance from it
+			customer = this.customerBean.findCustomerById(customerId);
+
+			// Debug message
+			// NOISY-DEBUG: this.loggerBeanLocal.logDebug(MessageFormat.format("getAsObject: customer={0}", customer)); //NOI18N
+		} catch (final NumberFormatException ex) {
+			// Throw again
+			throw new ConverterException(ex);
+		} catch (final CustomerNotFoundException ex) {
+			// Debug message
+			this.loggerBeanLocal.logDebug(MessageFormat.format("{0}.getAsObject(): Exception: {1} - Returning null ...", this.getClass().getSimpleName(), ex)); //NOI18N
+		}
+
+		// Trace message
+		// NOISY-DEBUG: this.loggerBeanLocal.logTrace(MessageFormat.format("getAsObject: contact={0} - EXIT!", contact)); //NOI18N
+
+		// Return it
+		return customer;
+	}
+
+	@Override
+	public String getAsString (final FacesContext context, final UIComponent component, final Object value) {
+		// Is the object null?
+		if ((null == value) || ((String.valueOf(value)).isEmpty())) {
+			// Is null
+			return ""; //NOI18N
+		} else if (!(value instanceof Customer)) {
+			// Not same interface
+			throw new IllegalArgumentException(MessageFormat.format("value {0} does not implement Customer.", value)); //NOI18N
+		}
+
+		// Return category id
+		return String.valueOf(((Customer) value).getCustomerId());
+	}
+
+}
diff --git a/web/admin/customer/admin_customer_delete.xhtml b/web/admin/customer/admin_customer_delete.xhtml
index 2339a9e9..8dcdbfb1 100644
--- a/web/admin/customer/admin_customer_delete.xhtml
+++ b/web/admin/customer/admin_customer_delete.xhtml
@@ -8,6 +8,11 @@
 	xmlns:f="http://xmlns.jcp.org/jsf/core"
 	>
 
+	<f:metadata>
+		<f:viewParam name="customerId" value="#{adminHelper.customer}" converter="CustomerConverter" required="true" requiredMessage="#{msg.ERROR_PARAMETER_CUSTOMER_ID_NOT_SET}" />
+		<f:viewAction action="#{adminHelper.copyCustomerToController()}" />
+	</f:metadata>
+
 	<ui:composition template="/WEB-INF/templates/admin/admin_base.tpl">
 		<ui:define name="admin_title">#{msg.PAGE_TITLE_ADMIN_DELETE_CUSTOMER}</ui:define>
 
diff --git a/web/admin/customer/admin_customer_edit.xhtml b/web/admin/customer/admin_customer_edit.xhtml
index 9d920d04..e70fe72d 100644
--- a/web/admin/customer/admin_customer_edit.xhtml
+++ b/web/admin/customer/admin_customer_edit.xhtml
@@ -8,6 +8,11 @@
 	xmlns:f="http://xmlns.jcp.org/jsf/core"
 	>
 
+	<f:metadata>
+		<f:viewParam name="customerId" value="#{adminHelper.customer}" converter="CustomerConverter" required="true" requiredMessage="#{msg.ERROR_PARAMETER_CUSTOMER_ID_NOT_SET}" />
+		<f:viewAction action="#{adminHelper.copyCustomerToController()}" />
+	</f:metadata>
+
 	<ui:composition template="/WEB-INF/templates/admin/admin_base.tpl">
 		<ui:define name="admin_title">#{msg.PAGE_TITLE_ADMIN_EDIT_CUSTOMER}</ui:define>
 
diff --git a/web/admin/customer/admin_customer_show.xhtml b/web/admin/customer/admin_customer_show.xhtml
index 89362878..61a68401 100644
--- a/web/admin/customer/admin_customer_show.xhtml
+++ b/web/admin/customer/admin_customer_show.xhtml
@@ -8,6 +8,11 @@
 	xmlns:f="http://xmlns.jcp.org/jsf/core"
 	>
 
+	<f:metadata>
+		<f:viewParam name="customerId" value="#{adminHelper.customer}" converter="CustomerConverter" required="true" requiredMessage="#{msg.ERROR_PARAMETER_CUSTOMER_ID_NOT_SET}" />
+		<f:viewAction action="#{adminHelper.copyCustomerToController()}" />
+	</f:metadata>
+
 	<ui:composition template="/WEB-INF/templates/admin/admin_base.tpl">
 		<ui:define name="admin_title">#{msg.PAGE_TITLE_ADMIN_SHOW_CUSTOMER}</ui:define>