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