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