]> 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.ejb.EJB;
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 org.mxchange.jphone.exceptions.PhoneEntityNotFoundException;
26 import org.mxchange.jphone.model.phonenumbers.fax.DialableFaxNumber;
27 import org.mxchange.jphone.model.phonenumbers.phone.PhoneSessionBeanRemote;
28
29 /**
30  * Converter for fax id <-> valid fax number instance
31  * <p>
32  * @author Roland Häder<roland@mxchange.org>
33  */
34 @FacesConverter ("FaxNumberConverter")
35 public class PizzaFaxNumberConverter implements Converter<DialableFaxNumber> {
36
37         /**
38          * Phone EJB
39          */
40         @EJB(lookup = "java:global/jfinancials-ejb/phone!org.mxchange.jphone.phonenumbers.phone.PhoneSessionBeanRemote")
41         private PhoneSessionBeanRemote phoneBean;
42
43         /**
44          * Default constructor
45          */
46         public PizzaFaxNumberConverter () {
47         }
48
49         @Override
50         public DialableFaxNumber getAsObject (final FacesContext context, final UIComponent component, final String submittedValue) {
51                 // Log message
52                 // @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
53
54                 // Is the value null or empty?
55                 if ((null == submittedValue) || (submittedValue.trim().isEmpty())) {
56                         // Warning message
57                         // @TODO Not possible here: this.loggerBeanLocal.logWarning(MessageFormat.format("{0}.getAsObject(): submittedValue is null or empty - EXIT!", this.getClass().getSimpleName())); //NOI18N
58
59                         // Return null
60                         return null;
61                 }
62
63                 // Init instance
64                 DialableFaxNumber faxNumber = null;
65
66                 try {
67                         // Try to parse the value as long
68                         Long faxNumberId = Long.valueOf(submittedValue);
69
70                         // Log message
71                         // @TODO Not possible here: this.loggerBeanLocal.logDebug(MessageFormat.format("{0}.getAsObject: faxNumberId={1}", this.getClass().getSimpleName(), faxNumberId)); //NOI18N
72                         // Try to get mobile instance from it
73                         faxNumber = this.phoneBean.findFaxNumberById(faxNumberId);
74                 } catch (final NumberFormatException ex) {
75                         // Throw again
76                         throw new ConverterException(ex);
77                 } catch (final PhoneEntityNotFoundException ex) {
78                         // Debug message
79                         // @TODO Not possible here: this.loggerBeanLocal.logDebug(MessageFormat.format("{0}.getAsObject(): Exception: {1} - Returning null ...", this.getClass().getSimpleName(), ex)); //NOI18N
80                 }
81
82                 // Log message
83                 // @TODO Not possible here: this.loggerBeanLocal.logTrace(MessageFormat.format("{0}.getAsObject: faxNumber={1} - EXIT!", this.getClass().getSimpleName(), faxNumber)); //NOI18N
84                 // Return it
85                 return faxNumber;
86         }
87
88         @Override
89         public String getAsString (final FacesContext context, final UIComponent component, final DialableFaxNumber value) {
90                 // Is the object null?
91                 if ((null == value) || (String.valueOf(value).isEmpty())) {
92                         // Is null
93                         return ""; //NOI18N
94                 }
95
96                 // Return id number
97                 return String.valueOf(value.getPhoneId());
98         }
99
100 }