/*
- * Copyright (C) 2016 Roland Haeder
+ * Copyright (C) 2016, 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
*/
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;
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.jusercore.exceptions.UserNotFoundException;
import org.mxchange.jusercore.model.user.User;
-import org.mxchange.pizzaapplication.beans.user.PizzaUserWebSessionController;
+import org.mxchange.jusercore.model.user.UserSessionBeanRemote;
/**
* Converter for user id <-> valid user instance
* <p>
- * @author Roland Haeder<roland@mxchange.org>
+ * @author Roland Häder<roland@mxchange.org>
*/
-@FacesConverter (value = "UserConverter")
-public class PizzaUserConverter implements Converter {
+@FacesConverter ("UserConverter")
+public class PizzaUserConverter implements Converter<User> {
/**
- * Logger instance
+ * User EJB
*/
- @Log
- private LoggerBeanLocal loggerBeanLocal;
-
- /**
- * User bean
- */
- private PizzaUserWebSessionController userController;
-
- /**
- * Initialization of this converter
- */
- public PizzaUserConverter () {
- // 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.userController = (PizzaUserWebSessionController) 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("context.lookup() failed.", ex); //NOI18N
- }
- }
+ private static UserSessionBeanRemote USER_BEAN;
@Override
- public Object getAsObject (final FacesContext context, final UIComponent component, final String submittedValue) {
- // Trace message
- this.loggerBeanLocal.logTrace(MessageFormat.format("getAsObject: context={0},component={1},submittedValue={2} - CALLED!", context, component, submittedValue)); //NOI18N
+ 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())) {
- // Trace message
- this.loggerBeanLocal.logTrace("getAsObject: submittedValue is null or empty - EXIT!"); //NOI18N
+ // Warning message
+ // @TODO Not working with JNDI (no remote interface) this.loggerBeanLocal.logWarning(MessageFormat.format("{0}.getAsObject(): submittedValue is null or empty - EXIT!", this.getClass().getSimpleName())); //NOI18N
// Return null
return null;
// Try to parse the value as long
Long userId = Long.valueOf(submittedValue);
- // Debug message
- this.loggerBeanLocal.logDebug(MessageFormat.format("getAsObject: userId{0}", userId));
-
// Try to get user instance from it
- user = this.userController.lookupUserById(userId);
-
- // Debug message
- this.loggerBeanLocal.logDebug(MessageFormat.format("getAsObject: user={0}", user));
+ user = USER_BEAN.findUserById(userId);
} catch (final NumberFormatException ex) {
// Throw again
throw new ConverterException(ex);
} catch (final UserNotFoundException ex) {
// Debug message
- this.loggerBeanLocal.logDebug(MessageFormat.format("getAsObject: Exception: {0} - Returning null ...", ex));
-
- // Return null
- return null;
+ // @TODO Not working with JNDI (no remote interface) this.loggerBeanLocal.logDebug(MessageFormat.format("getAsObject: Exception: {0} - Returning null ...", ex)); //NOI18N
}
- // Trace message
- this.loggerBeanLocal.logTrace(MessageFormat.format("getAsObject: user={0} - EXIT!", user));
-
// Return it
return user;
}
@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());
}
}