2 * Copyright (C) 2017 Roland Häder
\r
4 * This program is free software: you can redistribute it and/or modify
\r
5 * it under the terms of the GNU Affero General Public License as
\r
6 * published by the Free Software Foundation, either version 3 of the
\r
7 * License, or (at your option) any later version.
\r
9 * This program is distributed in the hope that it will be useful,
\r
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
12 * GNU Affero General Public License for more details.
\r
14 * You should have received a copy of the GNU Affero General Public License
\r
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
\r
17 package org.mxchange.pizzaapplication.converter.fax;
\r
19 import java.text.MessageFormat;
\r
20 import javax.faces.component.UIComponent;
\r
21 import javax.faces.context.FacesContext;
\r
22 import javax.faces.convert.Converter;
\r
23 import javax.faces.convert.ConverterException;
\r
24 import javax.faces.convert.FacesConverter;
\r
25 import javax.naming.Context;
\r
26 import javax.naming.InitialContext;
\r
27 import javax.naming.NamingException;
\r
28 import org.mxchange.jcoreeelogger.beans.local.logger.Log;
\r
29 import org.mxchange.jcoreeelogger.beans.local.logger.LoggerBeanLocal;
\r
30 import org.mxchange.jphone.exceptions.PhoneEntityNotFoundException;
\r
31 import org.mxchange.jphone.phonenumbers.DialableNumber;
\r
32 import org.mxchange.jphone.phonenumbers.fax.DialableFaxNumber;
\r
33 import org.mxchange.jphone.phonenumbers.phone.PhoneSessionBeanRemote;
\r
36 * Converter for fax id <-> valid fax number instance
\r
38 * @author Roland Häder<roland@mxchange.org>
\r
40 @FacesConverter (value = "FaxNumberConverter")
\r
41 public class PizzaFaxNumberConverter implements Converter {
\r
47 private LoggerBeanLocal loggerBeanLocal;
\r
52 private PhoneSessionBeanRemote phoneBean;
\r
55 * Initialization of this converter
\r
57 public PizzaFaxNumberConverter () {
\r
60 // Get initial context
\r
61 Context context = new InitialContext();
\r
64 this.loggerBeanLocal = (LoggerBeanLocal) context.lookup("java:global/jcore-logger-ejb/logger!org.mxchange.jcoreeelogger.beans.local.logger.LoggerBeanLocal"); //NOI18N
\r
66 // ... and user controller
\r
67 this.phoneBean = (PhoneSessionBeanRemote) context.lookup("java:global/jfinancials-ejb/phone!org.mxchange.jphone.phonenumbers.phone.PhoneSessionBeanRemote"); //NOI18N
\r
68 } catch (final NamingException ex) {
\r
69 // Continue to throw it
\r
70 throw new RuntimeException(MessageFormat.format("context.lookup() failed: {0}", ex.getMessage()), ex); //NOI18N
\r
75 public Object getAsObject (final FacesContext context, final UIComponent component, final String submittedValue) {
\r
77 this.loggerBeanLocal.logTrace(MessageFormat.format("{0}.getAsObject: context={1},component={2},submittedValue={3} - CALLED!", this.getClass().getSimpleName(), context, component, submittedValue)); //NOI18N
\r
79 // Is the value null or empty?
\r
80 if ((null == submittedValue) || (submittedValue.trim().isEmpty())) {
\r
82 this.loggerBeanLocal.logWarning(MessageFormat.format("{0}.getAsObject(): submittedValue is null or empty - EXIT!", this.getClass().getSimpleName())); //NOI18N
\r
89 DialableFaxNumber faxNumber = null;
\r
92 // Try to parse the value as long
\r
93 Long faxNumberId = Long.valueOf(submittedValue);
\r
96 this.loggerBeanLocal.logDebug(MessageFormat.format("{0}.getAsObject: faxNumberId={1}", this.getClass().getSimpleName(), faxNumberId)); //NOI18N
\r
98 // Try to get mobile instance from it
\r
99 faxNumber = this.phoneBean.findFaxNumberById(faxNumberId);
\r
100 } catch (final NumberFormatException ex) {
\r
102 throw new ConverterException(ex);
\r
103 } catch (final PhoneEntityNotFoundException ex) {
\r
105 this.loggerBeanLocal.logDebug(MessageFormat.format("{0}.getAsObject(): Exception: {1} - Returning null ...", this.getClass().getSimpleName(), ex)); //NOI18N
\r
109 this.loggerBeanLocal.logTrace(MessageFormat.format("{0}.getAsObject: faxNumber={1} - EXIT!", this.getClass().getSimpleName(), faxNumber)); //NOI18N
\r
116 public String getAsString (final FacesContext context, final UIComponent component, final Object value) {
\r
117 // Is the object null?
\r
118 if ((null == value) || ((String.valueOf(value)).isEmpty())) {
\r
120 return ""; //NOI18N
\r
121 } else if (!(value instanceof DialableNumber)) {
\r
122 // Not same interface
\r
123 throw new IllegalArgumentException(MessageFormat.format("value {0} does not implement DialableNumber.", value)); //NOI18N
\r
126 // Return category id
\r
127 return String.valueOf(((DialableNumber) value).getPhoneId());
\r