]> git.mxchange.org Git - pizzaservice-war.git/blob - src/java/org/mxchange/pizzaapplication/converter/country/PizzaCountryConverter.java
Updated copyright year
[pizzaservice-war.git] / src / java / org / mxchange / pizzaapplication / converter / country / PizzaCountryConverter.java
1 /*
2  * Copyright (C) 2016 - 2020 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.country;
18
19 import java.util.List;
20 import java.util.Objects;
21 import javax.faces.application.FacesMessage;
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.faces.validator.ValidatorException;
28 import javax.naming.Context;
29 import javax.naming.InitialContext;
30 import javax.naming.NamingException;
31 import org.mxchange.jcountry.model.data.Country;
32 import org.mxchange.jcountry.model.data.CountrySingletonBeanRemote;
33
34 /**
35  * Converter for country instance
36  * <p>
37  * @author Roland Häder<roland@mxchange.org>
38  */
39 @FacesConverter ("CountryConverter")
40 public class PizzaCountryConverter implements Converter<Country> {
41
42         /**
43          * Country bean
44          */
45         private static CountrySingletonBeanRemote COUNTRY_BEAN;
46
47         @Override
48         public Country getAsObject (final FacesContext context, final UIComponent component, final String submittedValue) {
49                 // Is the instance there?
50                 if (COUNTRY_BEAN == null) {
51                         try {
52                                 // Not yet, attempt lookup
53                                 final Context initial = new InitialContext();
54
55                                 // Lookup EJB
56                                 COUNTRY_BEAN = (CountrySingletonBeanRemote) initial.lookup("java:global/pizzaservice-ejb/country!org.mxchange.jcountry.model.data.CountrySingletonBeanRemote");
57                         } catch (final NamingException ex) {
58                                 // Throw it again
59                                 throw new ValidatorException(new FacesMessage(FacesMessage.SEVERITY_ERROR, "Cannot lookup EJB", ex.getMessage()), ex);
60                         }
61                 }
62
63                 // Is the value null or empty?
64                 if ((null == submittedValue) || (submittedValue.trim().isEmpty())) {
65                         // Warning message
66                         // @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
67
68                         // Return null
69                         return null;
70                 }
71
72                 // Get full list
73                 final List<Country> countryList = COUNTRY_BEAN.allCountries();
74
75                 // Init value
76                 Country country = null;
77
78                 // Try this better
79                 try {
80                         // Convert it to long
81                         final Long countryId = Long.parseLong(submittedValue);
82
83                         // Category id should not be below 1
84                         assert (countryId > 0) : "countryId is smaller than one: " + countryId; //NOI18N
85
86                         // Try to find it
87                         for (final Country cntry : countryList) {
88                                 // Is the id the same? (null-safe)
89                                 if (Objects.equals(cntry.getCountryId(), countryId)) {
90                                         // Found it
91                                         country = cntry;
92                                         break;
93                                 }
94                         }
95                 } catch (final NumberFormatException ex) {
96                         // Throw again
97                         throw new ConverterException(ex);
98                 }
99
100                 // Return it
101                 return country;
102         }
103
104         @Override
105         public String getAsString (final FacesContext context, final UIComponent component, final Country value) {
106                 // Is the object null?
107                 if ((null == value) || (String.valueOf(value).isEmpty())) {
108                         // Is null
109                         return ""; //NOI18N
110                 }
111
112                 // Return id number
113                 return String.valueOf(value.getCountryId());
114         }
115
116 }