]> git.mxchange.org Git - pizzaservice-war.git/blobdiff - src/java/org/mxchange/pizzaapplication/converter/user/PizzaUserConverter.java
Please cherry-pick:
[pizzaservice-war.git] / src / java / org / mxchange / pizzaapplication / converter / user / PizzaUserConverter.java
index e1749402145006018ad5f8ed96802a7912fe53ea..bcdee373c67f4d9c298367eacc9b98a1cddf69e1 100644 (file)
@@ -16,7 +16,7 @@
  */
 package org.mxchange.pizzaapplication.converter.user;
 
-import java.text.MessageFormat;
+import javax.faces.application.FacesMessage;
 import javax.faces.component.UIComponent;
 import javax.faces.context.FacesContext;
 import javax.faces.convert.Converter;
@@ -34,22 +34,30 @@ import org.mxchange.jusercore.model.user.UserSessionBeanRemote;
  * <p>
  * @author Roland Häder<roland@mxchange.org>
  */
-@FacesConverter (value = "UserConverter")
-public class PizzaUserConverter implements Converter {
+@FacesConverter ("UserConverter")
+public class PizzaUserConverter implements Converter<User> {
 
        /**
         * User EJB
         */
-       private UserSessionBeanRemote userBean;
-
-       /**
-        * Initialization of this converter
-        */
-       public PizzaUserConverter () {
-       }
+       private static UserSessionBeanRemote USER_BEAN;
 
        @Override
-       public Object getAsObject (final FacesContext context, final UIComponent component, final String submittedValue) {
+       public User getAsObject (final FacesContext context, final UIComponent component, final String submittedValue) {
+               // Is the instance there?
+               if (USER_BEAN == null) {
+                       try {
+                               // Not yet, attempt lookup
+                               Context initial = new InitialContext();
+
+                               // Lookup EJB
+                               USER_BEAN = (UserSessionBeanRemote) initial.lookup("java:global/jfinancials-ejb/user!org.mxchange.jusercore.model.user.UserSessionBeanRemote");
+                       } catch (final NamingException ex) {
+                               // Throw it again
+                               throw new ConverterException(new FacesMessage(FacesMessage.SEVERITY_ERROR, "Cannot lookup EJB", ex.getMessage()), ex);
+                       }
+               }
+
                // Is the value null or empty?
                if ((null == submittedValue) || (submittedValue.trim().isEmpty())) {
                        // Warning message
@@ -59,23 +67,6 @@ public class PizzaUserConverter implements Converter {
                        return null;
                }
 
-               synchronized (this) {
-                       // Is the EJB instanciated?
-                       if (null == this.userBean) {
-                               // Try to get it
-                               try {
-                                       // Get initial context
-                                       Context initialContext = new InitialContext();
-
-                                       // ... and user controller
-                                       this.userBean = (UserSessionBeanRemote) initialContext.lookup("java:global/pizzaservice-ejb/user!org.mxchange.jusercore.model.user.UserSessionBeanRemote"); //NOI18N
-                               } catch (final NamingException ex) {
-                                       // Continue to throw it
-                                       throw new RuntimeException(MessageFormat.format("initialContext.lookup() failed: {0}", ex.getMessage()), ex); //NOI18N
-                               }
-                       }
-               }
-
                // Init instance
                User user = null;
 
@@ -84,7 +75,7 @@ public class PizzaUserConverter implements Converter {
                        Long userId = Long.valueOf(submittedValue);
 
                        // Try to get user instance from it
-                       user = this.userBean.findUserById(userId);
+                       user = USER_BEAN.findUserById(userId);
                } catch (final NumberFormatException ex) {
                        // Throw again
                        throw new ConverterException(ex);
@@ -98,18 +89,15 @@ public class PizzaUserConverter implements Converter {
        }
 
        @Override
-       public String getAsString (final FacesContext context, final UIComponent component, final Object value) {
+       public String getAsString (final FacesContext context, final UIComponent component, final User value) {
                // Is the object null?
-               if ((null == value) || ((String.valueOf(value)).isEmpty())) {
+               if ((null == value) || (String.valueOf(value).isEmpty())) {
                        // Is null
                        return ""; //NOI18N
-               } else if (!(value instanceof User)) {
-                       // Not same interface
-                       throw new IllegalArgumentException(MessageFormat.format("value {0} does not implement User.", value)); //NOI18N
                }
 
-               // Return category id
-               return String.valueOf(((User) value).getUserId());
+               // Return id number
+               return String.valueOf(value.getUserId());
        }
 
 }