*/
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;
* <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 static UserSessionBeanRemote USER_BEAN;
- /**
- * Initialization of this converter
- */
- public PizzaUserConverter () {
- }
-
@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
return null;
}
- // Is the bean there?
- // @TODO Requires this synchronization or is it (sync) confusing the container?
- if (null == PizzaUserConverter.USER_BEAN) {
- // Try to get it
- try {
- // Get initial context
- Context initialContext = new InitialContext();
-
- // ... and user controller
- PizzaUserConverter.USER_BEAN = (UserSessionBeanRemote) initialContext.lookup("java:global/pizzaapplication-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;
Long userId = Long.valueOf(submittedValue);
// Try to get user instance from it
- user = PizzaUserConverter.USER_BEAN.findUserById(userId);
+ user = USER_BEAN.findUserById(userId);
} catch (final NumberFormatException ex) {
// Throw again
throw new ConverterException(ex);
}
@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());
}
}