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