]> git.mxchange.org Git - addressbook-war.git/commitdiff
Please cherry-pick:
authorRoland Häder <roland@mxchange.org>
Mon, 17 Jul 2017 10:21:31 +0000 (12:21 +0200)
committerRoland Häder <roland@mxchange.org>
Sat, 22 Jul 2017 22:11:03 +0000 (00:11 +0200)
- 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 <Project>MobileNumberConverter for
class name.

src/java/org/mxchange/addressbook/converter/fax/AddressbookFaxNumberConverter.java [new file with mode: 0644]
src/java/org/mxchange/addressbook/converter/landline/AddressbookLandLineNumberConverter.java [new file with mode: 0644]

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