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 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.mobileprovider;
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.jphone.phonenumbers.mobileprovider.MobileProvider;
32 import org.mxchange.jphone.phonenumbers.mobileprovider.MobileProviderSingletonBeanRemote;
35 * Converter for SMS provider instance
37 * @author Roland Haeder<roland@mxchange.org>
39 @FacesConverter (value = "MobileProviderConverter")
40 public class PizzaMobileProviderConverter implements Converter {
46 private LoggerBeanLocal loggerBeanLocal;
49 * Mobile provider bean
51 private MobileProviderSingletonBeanRemote mobileRemoteBean;
54 * Initialization of this converter
56 public PizzaMobileProviderConverter () {
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 SMS provider controller
66 this.mobileRemoteBean = (MobileProviderSingletonBeanRemote) context.lookup("java:global/PizzaService-ejb/mobileprovider!org.mxchange.jphone.phonenumbers.mobileprovider.MobileProviderSingletonBeanRemote"); //NOI18N
67 } catch (final NamingException ex) {
68 // Continue to throw it
69 throw new RuntimeException(MessageFormat.format("context.lookup() failed: {0}", ex.getMessage()), ex); //NOI18N
74 public Object getAsObject (final FacesContext context, final UIComponent component, final String submittedValue) {
76 // NOISY-DEBUG: this.loggerBeanLocal.logTrace(MessageFormat.format("getAsObject: context={0},component={1},submittedValue={2} - CALLED!", context, component, submittedValue)); //NOI18N
78 // Is the value null or empty?
79 if ((null == submittedValue) || (submittedValue.trim().isEmpty())) {
81 this.loggerBeanLocal.logWarning("getAsObject: submittedValue is null or empty - EXIT!"); //NOI18N
88 List<MobileProvider> providerList = this.mobileRemoteBean.allMobileProvider();
91 MobileProvider provider = null;
96 Long providerId = Long.parseLong(submittedValue);
98 // Category id should not be below 1
99 assert (providerId > 0) : "providerId is smaller than one: " + providerId; //NOI18N
102 // NOISY-DEBUG: this.loggerBeanLocal.logDebug(MessageFormat.format("getAsObject: providerId={0}", providerId)); //NOI18N
105 for (final MobileProvider prov : providerList) {
106 // Is the id the same? (null-safe)
107 if (Objects.equals(prov.getProviderId(), providerId)) {
113 } catch (final NumberFormatException ex) {
114 // Log exception (maybe to much?)
115 this.loggerBeanLocal.logException(ex);
119 // NOISY-DEBUG: this.loggerBeanLocal.logTrace(MessageFormat.format("getAsObject: provider={0} - EXIT!", provider)); //NOI18N
126 public String getAsString (final FacesContext context, final UIComponent component, final Object value) {
127 // Is the object null?
128 if ((null == value) || ((String.valueOf(value)).isEmpty())) {
131 } else if (!(value instanceof MobileProvider)) {
132 // Not same interface
133 throw new IllegalArgumentException(MessageFormat.format("value {0} does not implement MobileProvider.", value)); //NOI18N
136 // Return category id
137 return String.valueOf(((MobileProvider) value).getProviderId());