]> git.mxchange.org Git - jjobs-war.git/blob - src/java/org/mxchange/jjobs/converter/smsprovider/SmsProviderConverter.java
a8ffcb0a33dd942a546ec2e9524a0ea696045205
[jjobs-war.git] / src / java / org / mxchange / jjobs / 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.jjobs.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         /**
54          * Initialization of this converter
55          */
56         public SmsProviderConverter () {
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 = (AddressbookSmsProviderSingletonBeanRemote) context.lookup("java:global/jjobs-ejb/smsprovider!org.mxchange.jphone.phonenumbers.smsprovider.AddressbookSmsProviderSingletonBeanRemote"); //NOI18N
67                 } catch (final NamingException ex) {
68                         // Continue to throw it
69                         throw new RuntimeException("context.lookup() failed.", 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                 // Get full list
79                 List<SmsProvider> providerList = this.providerController.allSmsProvider();
80
81                 // Is the value null or empty?
82                 if ((null == submittedValue) || (submittedValue.trim().isEmpty())) {
83                         // Return null
84                         return null;
85                 }
86
87                 // Init value
88                 SmsProvider provider = null;
89
90                 // Try this better
91                 try {
92                         // Convert it to long
93                         Long providerId = Long.parseLong(submittedValue);
94
95                         // Category id should not be below 1
96                         assert (providerId > 0) : "providerId is smaller than one: " + providerId; //NOI18N
97
98                         // Debug message
99                         this.loggerBeanLocal.logDebug(MessageFormat.format("getAsObject: providerId={0}", providerId)); //NOI18N
100
101                         // Try to find it
102                         for (final SmsProvider prov : providerList) {
103                                 // Is the id the same? (null-safe)
104                                 if (Objects.equals(prov.getProviderId(), providerId)) {
105                                         // Found it
106                                         provider = prov;
107                                         break;
108                                 }
109                         }
110                 } catch (final NumberFormatException ex) {
111                         // Log exception (maybe to much?)
112                         this.loggerBeanLocal.logException(ex);
113                 }
114
115                 // Trace message
116                 this.loggerBeanLocal.logTrace(MessageFormat.format("getAsObject: provider={0} - EXIT!", provider)); //NOI18N
117
118                 // Return it
119                 return provider;
120         }
121
122         @Override
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())) {
126                         // Is null
127                         return ""; //NOI18N
128                 } else if (!(value instanceof SmsProvider)) {
129                         // Not same interface
130                         throw new IllegalArgumentException(MessageFormat.format("value {0} does not implement SmsProvider.", value)); //NOI18N
131                 }
132
133                 // Return category id
134                 return String.valueOf(((SmsProvider) value).getProviderId());
135         }
136 }