]> git.mxchange.org Git - pizzaservice-war.git/blob - src/java/org/mxchange/pizzaapplication/converter/customer/PizzaCustomerConverter.java
e24e767b921522667fb9b6c9ca0efd1b54bd3939
[pizzaservice-war.git] / src / java / org / mxchange / pizzaapplication / converter / customer / PizzaCustomerConverter.java
1 /*
2  * Copyright (C) 2016 - 2022 Free Software Foundation
3  *
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.
8  *
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.
13  *
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/>.
16  */
17 package org.mxchange.pizzaapplication.converter.customer;
18
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.jcustomercore.exceptions.CustomerNotFoundException;
29 import org.mxchange.jcustomercore.model.customer.Customer;
30 import org.mxchange.pizzaapplication.model.customer.PizzaCustomerSessionBeanRemote;
31
32 /**
33  * Converter for customer id <-> valid customer instance
34  * <p>
35  * @author Roland Häder<roland@mxchange.org>
36  */
37 @FacesConverter (value = "CustomerConverter")
38 public class PizzaCustomerConverter implements Converter {
39
40         /**
41          * User EJB
42          */
43         private PizzaCustomerSessionBeanRemote customerBean;
44
45         /**
46          * Initialization of this converter
47          */
48         public PizzaCustomerConverter () {
49                 // Try to get it
50                 try {
51                         // Get initial context
52                         final Context context = new InitialContext();
53
54                         // ... and user controller
55                         this.customerBean = (PizzaCustomerSessionBeanRemote) context.lookup("java:global/pizzaservice-ejb/customer!org.mxchange.pizzaservice.model.customer.PizzaCustomerSessionBeanRemote"); //NOI18N
56                 } catch (final NamingException ex) {
57                         // Continue to throw it
58                         throw new RuntimeException(MessageFormat.format("context.lookup() failed: {0}", ex.getMessage()), ex); //NOI18N
59                 }
60         }
61
62         @Override
63         public Object getAsObject (final FacesContext context, final UIComponent component, final String submittedValue) {
64                 // Trace message
65                 // NOISY-DEBUG: this.loggerBeanLocal.logTrace(MessageFormat.format("getAsObject: context={0},component={1},submittedValue={2} - CALLED!", context, component, submittedValue)); //NOI18N
66
67                 // Is the value null or empty?
68                 if ((null == submittedValue) || (submittedValue.trim().isEmpty())) {
69                         // Warning message
70                         // @TODO this.loggerBeanLocal.logWarning(MessageFormat.format("{0}.getAsObject(): submittedValue is null or empty - EXIT!", this.getClass().getSimpleName())); //NOI18N
71
72                         // Return null
73                         return null;
74                 }
75
76                 // Init instance
77                 final Customer customer;
78
79                 try {
80                         // Try to parse the value as long
81                         Long customerId = Long.valueOf(submittedValue);
82
83                         // Debug message
84                         // NOISY-DEBUG: this.loggerBeanLocal.logDebug(MessageFormat.format("getAsObject: customerId{0}", customerId)); //NOI18N
85
86                         // Try to get user instance from it
87                         customer = this.customerBean.findCustomerById(customerId);
88
89                         // Debug message
90                         // NOISY-DEBUG: this.loggerBeanLocal.logDebug(MessageFormat.format("getAsObject: customer={0}", customer)); //NOI18N
91                 } catch (final NumberFormatException | CustomerNotFoundException ex) {
92                         // Throw again
93                         throw new ConverterException(ex);
94                 }
95                 // Throw again
96
97                 // Trace message
98                 // NOISY-DEBUG: this.loggerBeanLocal.logTrace(MessageFormat.format("getAsObject: contact={0} - EXIT!", contact)); //NOI18N
99
100                 // Return it
101                 return customer;
102         }
103
104         @Override
105         public String getAsString (final FacesContext context, final UIComponent component, final Object value) {
106                 // Is the object null?
107                 if ((null == value) || ((String.valueOf(value)).isEmpty())) {
108                         // Is null
109                         return ""; //NOI18N
110                 } else if (!(value instanceof Customer)) {
111                         // Not same interface
112                         throw new IllegalArgumentException(MessageFormat.format("value {0} does not implement Customer.", value)); //NOI18N
113                 }
114
115                 // Return category id
116                 return String.valueOf(((Customer) value).getCustomerId());
117         }
118
119 }