X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=src%2Fjava%2Forg%2Fmxchange%2Fpizzaapplication%2Fconverter%2Fcountry%2FPizzaCountryConverter.java;h=6a23d3aa98469bd954b8e4b5a5de4179df739c23;hb=HEAD;hp=77bf14906fb60854a1c17a31f729311fbb9d8c51;hpb=1f81ac3d6c26cd00eb68e6719a4e62f9ed350d3c;p=pizzaservice-war.git diff --git a/src/java/org/mxchange/pizzaapplication/converter/country/PizzaCountryConverter.java b/src/java/org/mxchange/pizzaapplication/converter/country/PizzaCountryConverter.java index 77bf1490..6a23d3aa 100644 --- a/src/java/org/mxchange/pizzaapplication/converter/country/PizzaCountryConverter.java +++ b/src/java/org/mxchange/pizzaapplication/converter/country/PizzaCountryConverter.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2016 Roland Haeder + * Copyright (C) 2016 - 2024 Free Software Foundation * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License as @@ -16,76 +16,61 @@ */ package org.mxchange.pizzaapplication.converter.country; -import java.text.MessageFormat; import java.util.List; import java.util.Objects; +import javax.faces.application.FacesMessage; import javax.faces.component.UIComponent; import javax.faces.context.FacesContext; import javax.faces.convert.Converter; +import javax.faces.convert.ConverterException; import javax.faces.convert.FacesConverter; +import javax.faces.validator.ValidatorException; import javax.naming.Context; import javax.naming.InitialContext; import javax.naming.NamingException; -import org.mxchange.jcoreeelogger.beans.local.logger.Log; -import org.mxchange.jcoreeelogger.beans.local.logger.LoggerBeanLocal; -import org.mxchange.jcountry.data.Country; -import org.mxchange.jcountry.data.CountrySingletonBeanRemote; +import org.mxchange.jcountry.model.data.Country; +import org.mxchange.jcountry.model.data.CountrySingletonBeanRemote; /** * Converter for country instance *

- * @author Roland Haeder + * @author Roland Häder */ -@FacesConverter (value = "country") -public class PizzaCountryConverter implements Converter { +@FacesConverter ("CountryConverter") +public class PizzaCountryConverter implements Converter { /** * Country bean */ - private CountrySingletonBeanRemote countryBean; - - /** - * Logger instance - */ - @Log - private LoggerBeanLocal loggerBeanLocal; - - /** - * Initialization of this converter - */ - public PizzaCountryConverter () { - // Try to get it - try { - // Get initial context - Context context = new InitialContext(); - - // Lookup logger - this.loggerBeanLocal = (LoggerBeanLocal) context.lookup("java:global/jcore-logger-ejb/logger!org.mxchange.jcoreeelogger.beans.local.logger.LoggerBeanLocal"); //NOI18N - - // ... and country bean - this.countryBean = (CountrySingletonBeanRemote) context.lookup("java:global/PizzaService-ejb/country!org.mxchange.jcountry.data.CountrySingletonBeanRemote"); //NOI18N - } catch (final NamingException ex) { - // Continue to throw it - throw new RuntimeException(MessageFormat.format("context.lookup() failed: {0}", ex.getMessage()), ex); //NOI18N - } - } + private static CountrySingletonBeanRemote COUNTRY_BEAN; @Override - public Object getAsObject (final FacesContext context, final UIComponent component, final String submittedValue) { - // Trace message - // NOISY-DEBUG: this.loggerBeanLocal.logTrace(MessageFormat.format("getAsObject: context={0},component={1},submittedValue={2},this.countryBean={3} - CALLED!", context, component, submittedValue, this.countryBean)); //NOI18N + public Country getAsObject (final FacesContext context, final UIComponent component, final String submittedValue) { + // Is the instance there? + if (COUNTRY_BEAN == null) { + try { + // Not yet, attempt lookup + final Context initial = new InitialContext(); + + // Lookup EJB + COUNTRY_BEAN = (CountrySingletonBeanRemote) initial.lookup("java:global/pizzaservice-ejb/country!org.mxchange.jcountry.model.data.CountrySingletonBeanRemote"); + } catch (final NamingException ex) { + // Throw it again + throw new ValidatorException(new FacesMessage(FacesMessage.SEVERITY_ERROR, "Cannot lookup EJB", ex.getMessage()), ex); + } + } // Is the value null or empty? if ((null == submittedValue) || (submittedValue.trim().isEmpty())) { // Warning message - this.loggerBeanLocal.logWarning("getAsObject: submittedValue is null or empty - EXIT!"); //NOI18N + // @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 // Return null return null; } // Get full list - List countryList = this.countryBean.allCountries(); + final List countryList = COUNTRY_BEAN.allCountries(); // Init value Country country = null; @@ -93,14 +78,11 @@ public class PizzaCountryConverter implements Converter { // Try this better try { // Convert it to long - Long countryId = Long.parseLong(submittedValue); + final Long countryId = Long.valueOf(submittedValue); // Category id should not be below 1 assert (countryId > 0) : "countryId is smaller than one: " + countryId; //NOI18N - // Debug message - // NOISY-DEBUG: this.loggerBeanLocal.logDebug(MessageFormat.format("getAsObject: countryId={0}", countryId)); //NOI18N - // Try to find it for (final Country cntry : countryList) { // Is the id the same? (null-safe) @@ -111,30 +93,24 @@ public class PizzaCountryConverter implements Converter { } } } catch (final NumberFormatException ex) { - // Log exception (maybe to much?) - this.loggerBeanLocal.logException(ex); + // Throw again + throw new ConverterException(ex); } - // Trace message - // NOISY-DEBUG: this.loggerBeanLocal.logTrace(MessageFormat.format("getAsObject: country={0} - EXIT!", country)); //NOI18N - // Return it return country; } @Override - public String getAsString (final FacesContext context, final UIComponent component, final Object value) { + public String getAsString (final FacesContext context, final UIComponent component, final Country value) { // Is the object null? - if ((null == value) || ((String.valueOf(value)).isEmpty())) { + if ((null == value) || (String.valueOf(value).isEmpty())) { // Is null return ""; //NOI18N - } else if (!(value instanceof Country)) { - // Not same interface - throw new IllegalArgumentException(MessageFormat.format("value {0} does not implement Country.", value)); //NOI18N } - // Return category id - return String.valueOf(((Country) value).getCountryId()); + // Return id number + return String.valueOf(value.getCountryId()); } }