]> git.mxchange.org Git - pizzaservice-war.git/blob - src/java/org/mxchange/pizzaapplication/converter/country/PizzaCountryConverter.java
The exception's message now contains the thrown exception's message, too.
[pizzaservice-war.git] / src / java / org / mxchange / pizzaapplication / converter / country / PizzaCountryConverter.java
1 /*
2  * Copyright (C) 2016 Roland Haeder
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.country;
18
19 import java.text.MessageFormat;
20 import java.util.List;
21 import java.util.Objects;
22 import javax.faces.component.UIComponent;
23 import javax.faces.context.FacesContext;
24 import javax.faces.convert.Converter;
25 import javax.faces.convert.FacesConverter;
26 import javax.naming.Context;
27 import javax.naming.InitialContext;
28 import javax.naming.NamingException;
29 import org.mxchange.jcoreeelogger.beans.local.logger.Log;
30 import org.mxchange.jcoreeelogger.beans.local.logger.LoggerBeanLocal;
31 import org.mxchange.jcountry.data.Country;
32 import org.mxchange.jcountry.data.CountrySingletonBeanRemote;
33
34 /**
35  * Converter for country instance
36  * <p>
37  * @author Roland Haeder<roland@mxchange.org>
38  */
39 @FacesConverter (value = "country")
40 public class PizzaCountryConverter implements Converter {
41
42         /**
43          * Country bean
44          */
45         private CountrySingletonBeanRemote countryBean;
46
47         /**
48          * Logger instance
49          */
50         @Log
51         private LoggerBeanLocal loggerBeanLocal;
52
53         /**
54          * Initialization of this converter
55          */
56         public PizzaCountryConverter () {
57                 // Try to get it
58                 try {
59                         // Get initial context
60                         Context context = new InitialContext();
61
62                         // Lookup logger
63                         this.loggerBeanLocal = (LoggerBeanLocal) context.lookup("java:global/jcore-logger-ejb/logger!org.mxchange.jcoreeelogger.beans.local.logger.LoggerBeanLocal"); //NOI18N
64
65                         // ... and country bean
66                         this.countryBean = (CountrySingletonBeanRemote) context.lookup("java:global/PizzaService-ejb/country!org.mxchange.jcountry.data.CountrySingletonBeanRemote"); //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
70                 }
71         }
72
73         @Override
74         public Object getAsObject (final FacesContext context, final UIComponent component, final String submittedValue) {
75                 // Trace message
76                 this.loggerBeanLocal.logTrace(MessageFormat.format("getAsObject: context={0},component={1},submittedValue={2},this.countryBean={3} - CALLED!", context, component, submittedValue, this.countryBean)); //NOI18N
77
78                 // Is the value null or empty?
79                 if ((null == submittedValue) || (submittedValue.trim().isEmpty())) {
80                         // Trace message
81                         this.loggerBeanLocal.logTrace("getAsObject: submittedValue is null or empty - EXIT!"); //NOI18N
82
83                         // Return null
84                         return null;
85                 }
86
87                 // Get full list
88                 List<Country> countryList = this.countryBean.allCountries();
89
90                 // Init value
91                 Country country = null;
92
93                 // Try this better
94                 try {
95                         // Convert it to long
96                         Long countryId = Long.parseLong(submittedValue);
97
98                         // Category id should not be below 1
99                         assert (countryId > 0) : "countryId is smaller than one: " + countryId; //NOI18N
100
101                         // Debug message
102                         this.loggerBeanLocal.logDebug(MessageFormat.format("getAsObject: countryId={0}", countryId)); //NOI18N
103
104                         // Try to find it
105                         for (final Country cntry : countryList) {
106                                 // Is the id the same? (null-safe)
107                                 if (Objects.equals(cntry.getCountryId(), countryId)) {
108                                         // Found it
109                                         country = cntry;
110                                         break;
111                                 }
112                         }
113                 } catch (final NumberFormatException ex) {
114                         // Log exception (maybe to much?)
115                         this.loggerBeanLocal.logException(ex);
116                 }
117
118                 // Trace message
119                 this.loggerBeanLocal.logTrace(MessageFormat.format("getAsObject: country={0} - EXIT!", country)); //NOI18N
120
121                 // Return it
122                 return country;
123         }
124
125         @Override
126         public String getAsString (final FacesContext context, final UIComponent component, final Object value) {
127                 // Is the object null?
128                 if ((null == value) || ((String.valueOf(value)).isEmpty())) {
129                         // Is null
130                         return ""; //NOI18N
131                 } else if (!(value instanceof Country)) {
132                         // Not same interface
133                         throw new IllegalArgumentException(MessageFormat.format("value {0} does not implement Country.", value)); //NOI18N
134                 }
135
136                 // Return category id
137                 return String.valueOf(((Country) value).getCountryId());
138         }
139 }