]> git.mxchange.org Git - pizzaservice-war.git/blob - src/java/org/mxchange/pizzaapplication/converter/cellphone/PizzaCellphoneConverter.java
more JNDIs fixed
[pizzaservice-war.git] / src / java / org / mxchange / pizzaapplication / converter / cellphone / PizzaCellphoneConverter.java
1 /*
2  * Copyright (C) 2016 Roland Häder
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.cellphone;
18
19 import java.text.MessageFormat;
20 import javax.faces.component.UIComponent;
21 import javax.faces.context.FacesContext;
22 import javax.faces.convert.Converter;
23 import javax.faces.convert.ConverterException;
24 import javax.faces.convert.FacesConverter;
25 import javax.naming.Context;
26 import javax.naming.InitialContext;
27 import javax.naming.NamingException;
28 import org.mxchange.jcoreeelogger.beans.local.logger.Log;
29 import org.mxchange.jcoreeelogger.beans.local.logger.LoggerBeanLocal;
30 import org.mxchange.jphone.exceptions.PhoneEntityNotFoundException;
31 import org.mxchange.jphone.phonenumbers.DialableNumber;
32 import org.mxchange.jphone.phonenumbers.mobile.DialableMobileNumber;
33 import org.mxchange.jphone.phonenumbers.phone.PhoneSessionBeanRemote;
34
35 /**
36  * Converter for cellphone id <-> valid cellphone instance
37  * <p>
38  * @author Roland Häder<roland@mxchange.org>
39  */
40 @FacesConverter (value = "CellphoneConverter")
41 public class PizzaCellphoneConverter implements Converter {
42
43         /**
44          * Logger instance
45          */
46         @Log
47         private LoggerBeanLocal loggerBeanLocal;
48
49         /**
50          * Phone EJB
51          */
52         private PhoneSessionBeanRemote phoneBean;
53
54         /**
55          * Initialization of this converter
56          */
57         public PizzaCellphoneConverter () {
58                 // Try to get it
59                 try {
60                         // Get initial context
61                         Context context = new InitialContext();
62
63                         // Lookup logger
64                         this.loggerBeanLocal = (LoggerBeanLocal) context.lookup("java:global/jcore-logger-ejb/logger!org.mxchange.jcoreeelogger.beans.local.logger.LoggerBeanLocal"); //NOI18N
65
66                         // ... and user controller
67                         this.phoneBean = (PhoneSessionBeanRemote) context.lookup("java:global/pizzaservice-ejb/phone!org.mxchange.jphone.phonenumbers.phone.PhoneSessionBeanRemote"); //NOI18N
68                 } catch (final NamingException ex) {
69                         // Continue to throw it
70                         throw new RuntimeException(MessageFormat.format("context.lookup() failed: {0}", ex.getMessage()), ex); //NOI18N
71                 }
72         }
73
74         @Override
75         public Object getAsObject (final FacesContext context, final UIComponent component, final String submittedValue) {
76                 // Trace message
77                 // NOISY-DEBUG: this.loggerBeanLocal.logTrace(MessageFormat.format("getAsObject: context={0},component={1},submittedValue={2} - CALLED!", context, component, submittedValue)); //NOI18N
78
79                 // Is the value null or empty?
80                 if ((null == submittedValue) || (submittedValue.trim().isEmpty())) {
81                         // Warning message
82                         this.loggerBeanLocal.logWarning(MessageFormat.format("{0}.getAsObject(): submittedValue is null or empty - EXIT!", this.getClass().getSimpleName())); //NOI18N
83
84                         // Return null
85                         return null;
86                 }
87
88                 // Init instance
89                 DialableMobileNumber cellphone = null;
90
91                 try {
92                         // Try to parse the value as long
93                         Long cellphoneId = Long.valueOf(submittedValue);
94
95                         // Debug message
96                         // NOISY-DEBUG: this.loggerBeanLocal.logDebug(MessageFormat.format("getAsObject: userId{0}", userId)); //NOI18N
97
98                         // Try to get cellphone instance from it
99                         cellphone = this.phoneBean.findCellphoneById(cellphoneId);
100
101                         // Debug message
102                         // NOISY-DEBUG: this.loggerBeanLocal.logDebug(MessageFormat.format("getAsObject: cellphone={0}", cellphone)); //NOI18N
103                 } catch (final NumberFormatException ex) {
104                         // Throw again
105                         throw new ConverterException(ex);
106                 } catch (final PhoneEntityNotFoundException ex) {
107                         // Debug message
108                         this.loggerBeanLocal.logDebug(MessageFormat.format("{0}.getAsObject(): Exception: {1} - Returning null ...", this.getClass().getSimpleName(), ex)); //NOI18N
109                 }
110
111                 // Trace message
112                 // NOISY-DEBUG: this.loggerBeanLocal.logTrace(MessageFormat.format("getAsObject: cellphone={0} - EXIT!", cellphone)); //NOI18N
113
114                 // Return it
115                 return cellphone;
116         }
117
118         @Override
119         public String getAsString (final FacesContext context, final UIComponent component, final Object value) {
120                 // Is the object null?
121                 if ((null == value) || ((String.valueOf(value)).isEmpty())) {
122                         // Is null
123                         return ""; //NOI18N
124                 } else if (!(value instanceof DialableNumber)) {
125                         // Not same interface
126                         throw new IllegalArgumentException(MessageFormat.format("value {0} does not implement DialableNumber.", value)); //NOI18N
127                 }
128
129                 // Return category id
130                 return String.valueOf(((DialableNumber) value).getPhoneId());
131         }
132
133 }