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.converter.customer;
19 import java.text.MessageFormat;
20 import javax.faces.component.UIComponent;
21 import javax.faces.context.FacesContext;
22 import javax.faces.convert.Converter;
23 import javax.faces.convert.ConverterException;
24 import javax.faces.convert.FacesConverter;
25 import javax.naming.Context;
26 import javax.naming.InitialContext;
27 import javax.naming.NamingException;
28 import org.mxchange.jcoreeelogger.beans.local.logger.Log;
29 import org.mxchange.jcoreeelogger.beans.local.logger.LoggerBeanLocal;
30 import org.mxchange.jcustomercore.exceptions.CustomerNotFoundException;
31 import org.mxchange.jcustomercore.model.customer.Customer;
32 import org.mxchange.pizzaapplication.model.customer.PizzaCustomerSessionBeanRemote;
35 * Converter for customer id <-> valid customer instance
37 * @author Roland Häder<roland@mxchange.org>
39 @FacesConverter (value = "CustomerConverter")
40 public class PizzaCustomerConverter implements Converter {
45 private PizzaCustomerSessionBeanRemote customerBean;
51 private LoggerBeanLocal loggerBeanLocal;
54 * Initialization of this converter
56 public PizzaCustomerConverter () {
59 // Get initial context
60 Context context = new InitialContext();
63 this.loggerBeanLocal = (LoggerBeanLocal) context.lookup("java:global/jcore-logger-ejb/logger!org.mxchange.jcoreeelogger.beans.local.logger.LoggerBeanLocal"); //NOI18N
65 // ... and user controller
66 this.customerBean = (PizzaCustomerSessionBeanRemote) context.lookup("java:global/pizzaservice-ejb/customer!org.mxchange.pizzaservice.model.customer.PizzaCustomerSessionBeanRemote"); //NOI18N
67 } catch (final NamingException ex) {
68 // Continue to throw it
69 throw new RuntimeException(MessageFormat.format("context.lookup() failed: {0}", ex.getMessage()), ex); //NOI18N
74 public Object getAsObject (final FacesContext context, final UIComponent component, final String submittedValue) {
76 // NOISY-DEBUG: this.loggerBeanLocal.logTrace(MessageFormat.format("getAsObject: context={0},component={1},submittedValue={2} - CALLED!", context, component, submittedValue)); //NOI18N
78 // Is the value null or empty?
79 if ((null == submittedValue) || (submittedValue.trim().isEmpty())) {
81 this.loggerBeanLocal.logWarning(MessageFormat.format("{0}.getAsObject(): submittedValue is null or empty - EXIT!", this.getClass().getSimpleName())); //NOI18N
88 Customer customer = null;
91 // Try to parse the value as long
92 Long customerId = Long.valueOf(submittedValue);
95 // NOISY-DEBUG: this.loggerBeanLocal.logDebug(MessageFormat.format("getAsObject: customerId{0}", customerId)); //NOI18N
97 // Try to get user instance from it
98 customer = this.customerBean.findCustomerById(customerId);
101 // NOISY-DEBUG: this.loggerBeanLocal.logDebug(MessageFormat.format("getAsObject: customer={0}", customer)); //NOI18N
102 } catch (final NumberFormatException ex) {
104 throw new ConverterException(ex);
105 } catch (final CustomerNotFoundException ex) {
107 this.loggerBeanLocal.logDebug(MessageFormat.format("{0}.getAsObject(): Exception: {1} - Returning null ...", this.getClass().getSimpleName(), ex)); //NOI18N
111 // NOISY-DEBUG: this.loggerBeanLocal.logTrace(MessageFormat.format("getAsObject: contact={0} - EXIT!", contact)); //NOI18N
118 public String getAsString (final FacesContext context, final UIComponent component, final Object value) {
119 // Is the object null?
120 if ((null == value) || ((String.valueOf(value)).isEmpty())) {
123 } else if (!(value instanceof Customer)) {
124 // Not same interface
125 throw new IllegalArgumentException(MessageFormat.format("value {0} does not implement Customer.", value)); //NOI18N
128 // Return category id
129 return String.valueOf(((Customer) value).getCustomerId());