2 * Copyright (C) 2016 Roland Haeder
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (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 General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 package org.mxchange.addressbook.converter.country;
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.AddressbookCountrySingletonBeanRemote;
32 import org.mxchange.jcountry.data.Country;
35 * Converter for country instance
37 * @author Roland Haeder
39 @FacesConverter (value = "country")
40 public class CountryConverter implements Converter {
45 private AddressbookCountrySingletonBeanRemote countryController;
51 private LoggerBeanLocal loggerBeanLocal;
54 * Initialization of this converter
56 public CountryConverter () {
59 // Get initial context
60 Context context = new InitialContext();
63 this.loggerBeanLocal = (LoggerBeanLocal) context.lookup("java:global/jcore-logger-ejb/logger!org.mxchange.jcoreeelogger.beans.local.logger.LoggerBeanLocal"); //NOI18N
65 // ... and country controller
66 this.countryController = (AddressbookCountrySingletonBeanRemote) context.lookup("java:global/addressbook-ejb/country!org.mxchange.jcountry.data.AddressbookCountrySingletonBeanRemote"); //NOI18N
67 } catch (final NamingException ex) {
68 // Continue to throw it
69 throw new RuntimeException("context.lookup() failed.", ex); //NOI18N
74 public Object getAsObject (final FacesContext context, final UIComponent component, final String submittedValue) {
76 this.loggerBeanLocal.logTrace(MessageFormat.format("getAsObject: context={0},component={1},submittedValue={2} - CALLED!", context, component, submittedValue)); //NOI18N
79 List<Country> countryList = this.countryController.allCountries();
81 // Is the value null or empty?
82 if ((null == submittedValue) || (submittedValue.trim().isEmpty())) {
88 Country country = null;
93 Long countryId = Long.parseLong(submittedValue);
95 // Category id should not be below 1
96 assert (countryId > 0) : "countryId is smaller than one: " + countryId; //NOI18N
99 this.loggerBeanLocal.logDebug(MessageFormat.format("getAsObject: countryId={0}", countryId)); //NOI18N
102 for (final Country cntry : countryList) {
103 // Is the id the same? (null-safe)
104 if (Objects.equals(cntry.getCountryId(), countryId)) {
110 } catch (final NumberFormatException ex) {
111 // Log exception (maybe to much?)
112 this.loggerBeanLocal.logException(ex);
116 this.loggerBeanLocal.logTrace(MessageFormat.format("getAsObject: country={0} - EXIT!", country)); //NOI18N
123 public String getAsString (final FacesContext context, final UIComponent component, final Object value) {
124 // Is the object null?
125 if ((null == value) || ((String.valueOf(value)).isEmpty())) {
128 } else if (!(value instanceof Country)) {
129 // Not same interface
130 throw new IllegalArgumentException(MessageFormat.format("value {0} does not implement Country.", value)); //NOI18N
133 // Return category id
134 return String.valueOf(((Country) value).getCountryId());