*/
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 UserSessionBeanRemote userBean;
+ private static UserSessionBeanRemote USER_BEAN;
- /**
- * Initialization of this converter
- */
- public PizzaUserConverter () {
- // Try to get it
- try {
- // Get initial context
- Context context = new InitialContext();
+ @Override
+ 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();
- // ... and user controller
- this.userBean = (UserSessionBeanRemote) context.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("context.lookup() failed: {0}", ex.getMessage()), ex); //NOI18N
+ // 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);
+ }
}
- }
- @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
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);
}
@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());
}
}