2 * Copyright (C) 2016 - 2020 Free Software Foundation
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.country;
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;
35 * Converter for country instance
37 * @author Roland Häder<roland@mxchange.org>
39 @FacesConverter ("CountryConverter")
40 public class PizzaCountryConverter implements Converter<Country> {
45 private static CountrySingletonBeanRemote COUNTRY_BEAN;
48 public Country getAsObject (final FacesContext context, final UIComponent component, final String submittedValue) {
49 // Is the instance there?
50 if (COUNTRY_BEAN == null) {
52 // Not yet, attempt lookup
53 final Context initial = new InitialContext();
56 COUNTRY_BEAN = (CountrySingletonBeanRemote) initial.lookup("java:global/pizzaservice-ejb/country!org.mxchange.jcountry.model.data.CountrySingletonBeanRemote");
57 } catch (final NamingException ex) {
59 throw new ValidatorException(new FacesMessage(FacesMessage.SEVERITY_ERROR, "Cannot lookup EJB", ex.getMessage()), ex);
63 // Is the value null or empty?
64 if ((null == submittedValue) || (submittedValue.trim().isEmpty())) {
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
73 final List<Country> countryList = COUNTRY_BEAN.allCountries();
76 Country country = null;
81 final Long countryId = Long.parseLong(submittedValue);
83 // Category id should not be below 1
84 assert (countryId > 0) : "countryId is smaller than one: " + countryId; //NOI18N
87 for (final Country cntry : countryList) {
88 // Is the id the same? (null-safe)
89 if (Objects.equals(cntry.getCountryId(), countryId)) {
95 } catch (final NumberFormatException ex) {
97 throw new ConverterException(ex);
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())) {
113 return String.valueOf(value.getCountryId());