]> git.mxchange.org Git - pizzaservice-war.git/blob - src/java/org/mxchange/pizzaapplication/converter/fax/PizzaFaxNumberConverter.java
Please cherry-pick:
[pizzaservice-war.git] / src / java / org / mxchange / pizzaapplication / converter / fax / PizzaFaxNumberConverter.java
1 /*
2  * Copyright (C) 2017 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.fax;
18
19 import javax.faces.application.FacesMessage;
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.faces.validator.ValidatorException;
26 import javax.naming.Context;
27 import javax.naming.InitialContext;
28 import javax.naming.NamingException;
29 import org.mxchange.jphone.exceptions.PhoneEntityNotFoundException;
30 import org.mxchange.jphone.model.phonenumbers.fax.DialableFaxNumber;
31 import org.mxchange.jphone.model.phonenumbers.phone.PhoneSessionBeanRemote;
32
33 /**
34  * Converter for fax id <-> valid fax number instance
35  * <p>
36  * @author Roland Häder<roland@mxchange.org>
37  */
38 @FacesConverter ("FaxNumberConverter")
39 public class PizzaFaxNumberConverter implements Converter<DialableFaxNumber> {
40
41         /**
42          * Phone EJB
43          */
44         private static PhoneSessionBeanRemote PHONE_BEAN;
45
46         @Override
47         public DialableFaxNumber getAsObject (final FacesContext context, final UIComponent component, final String submittedValue) {
48                 // Is the instance there?
49                 if (PHONE_BEAN == null) {
50                         try {
51                                 // Not yet, attempt lookup
52                                 Context initial = new InitialContext();
53
54                                 // Lookup EJB
55                                 PHONE_BEAN = (PhoneSessionBeanRemote) initial.lookup("java:global/jfinancials-ejb/phone!org.mxchange.jphone.phonenumbers.phone.PhoneSessionBeanRemote");
56                         } catch (final NamingException ex) {
57                                 // Throw it again
58                                 throw new ValidatorException(new FacesMessage(FacesMessage.SEVERITY_ERROR, "Cannot lookup EJB", ex.getMessage()), ex);
59                         }
60                 }
61
62                 // Log message
63                 // @TODO Not possible here: this.loggerBeanLocal.logTrace(MessageFormat.format("{0}.getAsObject: context={1},component={2},submittedValue={3} - CALLED!", this.getClass().getSimpleName(), context, component, submittedValue)); //NOI18N
64
65                 // Is the value null or empty?
66                 if ((null == submittedValue) || (submittedValue.trim().isEmpty())) {
67                         // Warning message
68                         // @TODO Not possible here: this.loggerBeanLocal.logWarning(MessageFormat.format("{0}.getAsObject(): submittedValue is null or empty - EXIT!", this.getClass().getSimpleName())); //NOI18N
69
70                         // Return null
71                         return null;
72                 }
73
74                 // Init instance
75                 DialableFaxNumber faxNumber = null;
76
77                 try {
78                         // Try to parse the value as long
79                         Long faxNumberId = Long.valueOf(submittedValue);
80
81                         // Log message
82                         // @TODO Not possible here: this.loggerBeanLocal.logDebug(MessageFormat.format("{0}.getAsObject: faxNumberId={1}", this.getClass().getSimpleName(), faxNumberId)); //NOI18N
83                         // Try to get mobile instance from it
84                         faxNumber = PHONE_BEAN.findFaxNumberById(faxNumberId);
85                 } catch (final NumberFormatException ex) {
86                         // Throw again
87                         throw new ConverterException(ex);
88                 } catch (final PhoneEntityNotFoundException ex) {
89                         // Debug message
90                         // @TODO Not possible here: this.loggerBeanLocal.logDebug(MessageFormat.format("{0}.getAsObject(): Exception: {1} - Returning null ...", this.getClass().getSimpleName(), ex)); //NOI18N
91                 }
92
93                 // Log message
94                 // @TODO Not possible here: this.loggerBeanLocal.logTrace(MessageFormat.format("{0}.getAsObject: faxNumber={1} - EXIT!", this.getClass().getSimpleName(), faxNumber)); //NOI18N
95                 // Return it
96                 return faxNumber;
97         }
98
99         @Override
100         public String getAsString (final FacesContext context, final UIComponent component, final DialableFaxNumber value) {
101                 // Is the object null?
102                 if ((null == value) || (String.valueOf(value).isEmpty())) {
103                         // Is null
104                         return ""; //NOI18N
105                 }
106
107                 // Return id number
108                 return String.valueOf(value.getPhoneId());
109         }
110
111 }