]> git.mxchange.org Git - pizzaservice-war.git/blob - src/java/org/mxchange/pizzaapplication/converter/country/PizzaCountryConverter.java
Please cherry-pick (Massive rewrite):
[pizzaservice-war.git] / src / java / org / mxchange / pizzaapplication / converter / country / PizzaCountryConverter.java
1 /*
2  * Copyright (C) 2016, 2017 Roland Häder
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.ConverterException;
26 import javax.faces.convert.FacesConverter;
27 import javax.naming.Context;
28 import javax.naming.InitialContext;
29 import javax.naming.NamingException;
30 import org.mxchange.jcountry.data.Country;
31 import org.mxchange.jcountry.data.CountrySingletonBeanRemote;
32
33 /**
34  * Converter for country instance
35  * <p>
36  * @author Roland Häder<roland@mxchange.org>
37  */
38 @FacesConverter (value = "CountryConverter")
39 public class PizzaCountryConverter implements Converter {
40
41         /**
42          * Country bean
43          */
44         private static CountrySingletonBeanRemote COUNTRY_BEAN;
45
46         /**
47          * Initialization of this converter
48          */
49         public PizzaCountryConverter () {
50         }
51
52         @Override
53         public Object getAsObject (final FacesContext context, final UIComponent component, final String submittedValue) {
54                 // Is the value null or empty?
55                 if ((null == submittedValue) || (submittedValue.trim().isEmpty())) {
56                         // Warning message
57                         // @TODO Not working with JNDI (no remote interface) this.loggerBeanLocal.logWarning(MessageFormat.format("{0}.getAsObject(): submittedValue is null or empty - EXIT!", this.getClass().getSimpleName())); //NOI18N
58
59                         // Return null
60                         return null;
61                 }
62
63                 // Is the bean there?
64                 // @TODO Requires this synchronization or is it (sync) confusing the container?
65                 if (null == PizzaCountryConverter.COUNTRY_BEAN) {
66                         // Try to get it
67                         try {
68                                 // Get initial context
69                                 Context initialContext = new InitialContext();
70
71                                 // ... and country bean
72                                 PizzaCountryConverter.COUNTRY_BEAN = (CountrySingletonBeanRemote) initialContext.lookup("java:global/pizzaapplication-ejb/country!org.mxchange.jcountry.data.CountrySingletonBeanRemote"); //NOI18N
73                         } catch (final NamingException ex) {
74                                 // Continue to throw it
75                                 throw new ConverterException(MessageFormat.format("initialContext.lookup() failed: {0}", ex.getMessage()), ex); //NOI18N
76                         }
77                 }
78
79                 // Get full list
80                 List<Country> countryList = PizzaCountryConverter.COUNTRY_BEAN.allCountries();
81
82                 // Init value
83                 Country country = null;
84
85                 // Try this better
86                 try {
87                         // Convert it to long
88                         Long countryId = Long.parseLong(submittedValue);
89
90                         // Category id should not be below 1
91                         assert (countryId > 0) : "countryId is smaller than one: " + countryId; //NOI18N
92
93                         // Try to find it
94                         for (final Country cntry : countryList) {
95                                 // Is the id the same? (null-safe)
96                                 if (Objects.equals(cntry.getCountryId(), countryId)) {
97                                         // Found it
98                                         country = cntry;
99                                         break;
100                                 }
101                         }
102                 } catch (final NumberFormatException ex) {
103                         // Throw again
104                         throw new ConverterException(ex);
105                 }
106
107                 // Return it
108                 return country;
109         }
110
111         @Override
112         public String getAsString (final FacesContext context, final UIComponent component, final Object value) {
113                 // Is the object null?
114                 if ((null == value) || ((String.valueOf(value)).isEmpty())) {
115                         // Is null
116                         return ""; //NOI18N
117                 } else if (!(value instanceof Country)) {
118                         // Not same interface
119                         throw new IllegalArgumentException(MessageFormat.format("value {0} does not implement Country.", value)); //NOI18N
120                 }
121
122                 // Return category id
123                 return String.valueOf(((Country) value).getCountryId());
124         }
125
126 }