]> git.mxchange.org Git - pizzaservice-war.git/blob - src/java/org/mxchange/pizzaapplication/converter/smsprovider/PizzaSmsProviderConverter.java
ab14e6794597259e3efe92bc29c510fc18724ef6
[pizzaservice-war.git] / src / java / org / mxchange / pizzaapplication / converter / smsprovider / PizzaSmsProviderConverter.java
1 /*
2  * Copyright (C) 2016 Roland Haeder
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.smsprovider;
18
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.smsprovider.SmsProvider;
32 import org.mxchange.jphone.phonenumbers.smsprovider.SmsProviderSingletonBeanRemote;
33
34 /**
35  * Converter for SMS provider instance
36  * <p>
37  * @author Roland Haeder<roland@mxchange.org>
38  */
39 @FacesConverter (value = "CellphoneCarrierConverter")
40 public class PizzaSmsProviderConverter implements Converter {
41
42         /**
43          * Logger instance
44          */
45         @Log
46         private LoggerBeanLocal loggerBeanLocal;
47
48         /**
49          * SMS provider bean
50          */
51         private SmsProviderSingletonBeanRemote providerController;
52
53         /**
54          * Initialization of this converter
55          */
56         public PizzaSmsProviderConverter () {
57                 // Try to get it
58                 try {
59                         // Get initial context
60                         Context context = new InitialContext();
61
62                         // Lookup logger
63                         this.loggerBeanLocal = (LoggerBeanLocal) context.lookup("java:global/jcore-logger-ejb/logger!org.mxchange.jcoreeelogger.beans.local.logger.LoggerBeanLocal"); //NOI18N
64
65                         /// and SMS provider controller
66                         this.providerController = (SmsProviderSingletonBeanRemote) context.lookup("java:global/PizzaService-ejb/smsprovider!org.mxchange.jphone.phonenumbers.smsprovider.SmsProviderSingletonBeanRemote"); //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
70                 }
71         }
72
73         @Override
74         public Object getAsObject (final FacesContext context, final UIComponent component, final String submittedValue) {
75                 // Trace message
76                 this.loggerBeanLocal.logTrace(MessageFormat.format("getAsObject: context={0},component={1},submittedValue={2} - CALLED!", context, component, submittedValue)); //NOI18N
77
78                 // Is the value null or empty?
79                 if ((null == submittedValue) || (submittedValue.trim().isEmpty())) {
80                         // Trace message
81                         this.loggerBeanLocal.logTrace("getAsObject: submittedValue is null or empty - EXIT!"); //NOI18N
82
83                         // Return null
84                         return null;
85                 }
86
87                 // Get full list
88                 List<SmsProvider> providerList = this.providerController.allSmsProvider();
89
90                 // Init value
91                 SmsProvider provider = null;
92
93                 // Try this better
94                 try {
95                         // Convert it to long
96                         Long providerId = Long.parseLong(submittedValue);
97
98                         // Category id should not be below 1
99                         assert (providerId > 0) : "providerId is smaller than one: " + providerId; //NOI18N
100
101                         // Debug message
102                         this.loggerBeanLocal.logDebug(MessageFormat.format("getAsObject: providerId={0}", providerId)); //NOI18N
103
104                         // Try to find it
105                         for (final SmsProvider prov : providerList) {
106                                 // Is the id the same? (null-safe)
107                                 if (Objects.equals(prov.getProviderId(), providerId)) {
108                                         // Found it
109                                         provider = prov;
110                                         break;
111                                 }
112                         }
113                 } catch (final NumberFormatException ex) {
114                         // Log exception (maybe to much?)
115                         this.loggerBeanLocal.logException(ex);
116                 }
117
118                 // Trace message
119                 this.loggerBeanLocal.logTrace(MessageFormat.format("getAsObject: provider={0} - EXIT!", provider)); //NOI18N
120
121                 // Return it
122                 return provider;
123         }
124
125         @Override
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())) {
129                         // Is null
130                         return ""; //NOI18N
131                 } else if (!(value instanceof SmsProvider)) {
132                         // Not same interface
133                         throw new IllegalArgumentException(MessageFormat.format("value {0} does not implement SmsProvider.", value)); //NOI18N
134                 }
135
136                 // Return category id
137                 return String.valueOf(((SmsProvider) value).getProviderId());
138         }
139 }