From d5dd77a1b682baa8f4cf0431323060c70289c93d Mon Sep 17 00:00:00 2001 From: =?utf8?q?Roland=20H=C3=A4der?= Date: Mon, 17 Jul 2017 12:21:31 +0200 Subject: [PATCH] Please cherry-pick: - added missing converter for land-line and fax numbers - renamed converter after naming-convetion, see below Convention: A converter converts mobile number instances from their corresponding id numbers and vise versa, so it should be called MobileNumberConverter for FacesConverter annotation and MobileNumberConverter for class name. --- .../fax/AddressbookFaxNumberConverter.java | 130 ++++++++++++++++++ .../AddressbookLandLineNumberConverter.java | 121 ++++++++++++++++ 2 files changed, 251 insertions(+) create mode 100644 src/java/org/mxchange/addressbook/converter/fax/AddressbookFaxNumberConverter.java create mode 100644 src/java/org/mxchange/addressbook/converter/landline/AddressbookLandLineNumberConverter.java diff --git a/src/java/org/mxchange/addressbook/converter/fax/AddressbookFaxNumberConverter.java b/src/java/org/mxchange/addressbook/converter/fax/AddressbookFaxNumberConverter.java new file mode 100644 index 00000000..afce7036 --- /dev/null +++ b/src/java/org/mxchange/addressbook/converter/fax/AddressbookFaxNumberConverter.java @@ -0,0 +1,130 @@ +/* + * Copyright (C) 2017 Roland Häder + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ +package org.mxchange.addressbook.converter.fax; + +import java.text.MessageFormat; +import javax.faces.component.UIComponent; +import javax.faces.context.FacesContext; +import javax.faces.convert.Converter; +import javax.faces.convert.ConverterException; +import javax.faces.convert.FacesConverter; +import javax.naming.Context; +import javax.naming.InitialContext; +import javax.naming.NamingException; +import org.mxchange.jcoreeelogger.beans.local.logger.Log; +import org.mxchange.jcoreeelogger.beans.local.logger.LoggerBeanLocal; +import org.mxchange.jphone.exceptions.PhoneEntityNotFoundException; +import org.mxchange.jphone.phonenumbers.DialableNumber; +import org.mxchange.jphone.phonenumbers.fax.DialableFaxNumber; +import org.mxchange.jphone.phonenumbers.phone.PhoneSessionBeanRemote; + +/** + * Converter for fax id <-> valid fax number instance + *

+ * @author Roland Häder + */ +@FacesConverter (value = "FaxNumberConverter") +public class AddressbookFaxNumberConverter implements Converter { + + /** + * Logger instance + */ + @Log + private LoggerBeanLocal loggerBeanLocal; + + /** + * Phone EJB + */ + private PhoneSessionBeanRemote phoneBean; + + /** + * Initialization of this converter + */ + public AddressbookFaxNumberConverter () { + // Try to get it + try { + // Get initial context + Context context = new InitialContext(); + + // Lookup logger + this.loggerBeanLocal = (LoggerBeanLocal) context.lookup("java:global/jcore-logger-ejb/logger!org.mxchange.jcoreeelogger.beans.local.logger.LoggerBeanLocal"); //NOI18N + + // ... and user controller + this.phoneBean = (PhoneSessionBeanRemote) context.lookup("java:global/jfinancials-ejb/phone!org.mxchange.jphone.phonenumbers.phone.PhoneSessionBeanRemote"); //NOI18N + } catch (final NamingException ex) { + // Continue to throw it + throw new RuntimeException(MessageFormat.format("context.lookup() failed: {0}", ex.getMessage()), ex); //NOI18N + } + } + + @Override + public Object getAsObject (final FacesContext context, final UIComponent component, final String submittedValue) { + // Log message + this.loggerBeanLocal.logTrace(MessageFormat.format("{0}.getAsObject: context={1},component={2},submittedValue={3} - CALLED!", this.getClass().getSimpleName(), context, component, submittedValue)); //NOI18N + + // Is the value null or empty? + if ((null == submittedValue) || (submittedValue.trim().isEmpty())) { + // Warning message + this.loggerBeanLocal.logWarning(MessageFormat.format("{0}.getAsObject(): submittedValue is null or empty - EXIT!", this.getClass().getSimpleName())); //NOI18N + + // Return null + return null; + } + + // Init instance + DialableFaxNumber faxNumber = null; + + try { + // Try to parse the value as long + Long faxNumberId = Long.valueOf(submittedValue); + + // Log message + this.loggerBeanLocal.logDebug(MessageFormat.format("{0}.getAsObject: faxNumberId={1}", this.getClass().getSimpleName(), faxNumberId)); //NOI18N + + // Try to get mobile instance from it + faxNumber = this.phoneBean.findFaxNumberById(faxNumberId); + } catch (final NumberFormatException ex) { + // Throw again + throw new ConverterException(ex); + } catch (final PhoneEntityNotFoundException ex) { + // Debug message + this.loggerBeanLocal.logDebug(MessageFormat.format("{0}.getAsObject(): Exception: {1} - Returning null ...", this.getClass().getSimpleName(), ex)); //NOI18N + } + + // Log message + this.loggerBeanLocal.logTrace(MessageFormat.format("{0}.getAsObject: faxNumber={1} - EXIT!", this.getClass().getSimpleName(), faxNumber)); //NOI18N + + // Return it + return faxNumber; + } + + @Override + public String getAsString (final FacesContext context, final UIComponent component, final Object value) { + // Is the object null? + if ((null == value) || ((String.valueOf(value)).isEmpty())) { + // Is null + return ""; //NOI18N + } else if (!(value instanceof DialableNumber)) { + // Not same interface + throw new IllegalArgumentException(MessageFormat.format("value {0} does not implement DialableNumber.", value)); //NOI18N + } + + // Return category id + return String.valueOf(((DialableNumber) value).getPhoneId()); + } + +} diff --git a/src/java/org/mxchange/addressbook/converter/landline/AddressbookLandLineNumberConverter.java b/src/java/org/mxchange/addressbook/converter/landline/AddressbookLandLineNumberConverter.java new file mode 100644 index 00000000..2c870d38 --- /dev/null +++ b/src/java/org/mxchange/addressbook/converter/landline/AddressbookLandLineNumberConverter.java @@ -0,0 +1,121 @@ +/* + * Copyright (C) 2017 Roland Häder + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ +package org.mxchange.addressbook.converter.landline; + +import java.text.MessageFormat; +import javax.faces.component.UIComponent; +import javax.faces.context.FacesContext; +import javax.faces.convert.Converter; +import javax.faces.convert.ConverterException; +import javax.faces.convert.FacesConverter; +import javax.naming.Context; +import javax.naming.InitialContext; +import javax.naming.NamingException; +import org.mxchange.jcoreeelogger.beans.local.logger.Log; +import org.mxchange.jcoreeelogger.beans.local.logger.LoggerBeanLocal; +import org.mxchange.jphone.exceptions.PhoneEntityNotFoundException; +import org.mxchange.jphone.phonenumbers.DialableNumber; +import org.mxchange.jphone.phonenumbers.landline.DialableLandLineNumber; +import org.mxchange.jphone.phonenumbers.phone.PhoneSessionBeanRemote; + +/** + * Converter for land-line id <-> valid land-line number instance + *

+ * @author Roland Häder + */ +@FacesConverter (value = "LandLineNumberConverter") +public class AddressbookLandLineNumberConverter implements Converter { + + /** + * Logger instance + */ + @Log + private LoggerBeanLocal loggerBeanLocal; + + /** + * Phone EJB + */ + private PhoneSessionBeanRemote phoneBean; + + /** + * Initialization of this converter + */ + public AddressbookLandLineNumberConverter () { + // Try to get it + try { + // Get initial context + Context context = new InitialContext(); + + // Lookup logger + this.loggerBeanLocal = (LoggerBeanLocal) context.lookup("java:global/jcore-logger-ejb/logger!org.mxchange.jcoreeelogger.beans.local.logger.LoggerBeanLocal"); //NOI18N + + // ... and user controller + this.phoneBean = (PhoneSessionBeanRemote) context.lookup("java:global/jfinancials-ejb/phone!org.mxchange.jphone.phonenumbers.phone.PhoneSessionBeanRemote"); //NOI18N + } catch (final NamingException ex) { + // Continue to throw it + throw new RuntimeException(MessageFormat.format("context.lookup() failed: {0}", ex.getMessage()), ex); //NOI18N + } + } + + @Override + public Object getAsObject (final FacesContext context, final UIComponent component, final String submittedValue) { + // Is the value null or empty? + if ((null == submittedValue) || (submittedValue.trim().isEmpty())) { + // Warning message + this.loggerBeanLocal.logWarning(MessageFormat.format("{0}.getAsObject(): submittedValue is null or empty - EXIT!", this.getClass().getSimpleName())); //NOI18N + + // Return null + return null; + } + + // Init instance + DialableLandLineNumber landLineNumber = null; + + try { + // Try to parse the value as long + Long landLineNumberId = Long.valueOf(submittedValue); + + // Try to get mobile instance from it + landLineNumber = this.phoneBean.findLandLineNumberById(landLineNumberId); + } catch (final NumberFormatException ex) { + // Throw again + throw new ConverterException(ex); + } catch (final PhoneEntityNotFoundException ex) { + // Debug message + this.loggerBeanLocal.logDebug(MessageFormat.format("{0}.getAsObject(): Exception: {1} - Returning null ...", this.getClass().getSimpleName(), ex)); //NOI18N + } + + // Return it + return landLineNumber; + } + + @Override + public String getAsString (final FacesContext context, final UIComponent component, final Object value) { + // Is the object null? + if ((null == value) || ((String.valueOf(value)).isEmpty())) { + // Is null + return ""; //NOI18N + } else if (!(value instanceof DialableNumber)) { + // Not same interface + throw new IllegalArgumentException(MessageFormat.format("value {0} does not implement DialableNumber.", value)); //NOI18N + } + + // Return category id + return String.valueOf(((DialableNumber) value).getPhoneId()); + } + +} -- 2.39.5