2 * Copyright (C) 2016 Roland Haeder
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 package org.mxchange.jjobs.converter.user;
19 import java.text.MessageFormat;
20 import javax.faces.component.UIComponent;
21 import javax.faces.context.FacesContext;
22 import javax.faces.convert.Converter;
23 import javax.faces.convert.ConverterException;
24 import javax.faces.convert.FacesConverter;
25 import javax.inject.Inject;
26 import javax.naming.Context;
27 import javax.naming.InitialContext;
28 import javax.naming.NamingException;
29 import org.mxchange.jcoreeelogger.beans.local.logger.Log;
30 import org.mxchange.jcoreeelogger.beans.local.logger.LoggerBeanLocal;
31 import org.mxchange.jjobs.beans.user.UserWebSessionController;
32 import org.mxchange.jusercore.exceptions.UserNotFoundException;
33 import org.mxchange.jusercore.model.user.User;
36 * Converter for user id <-> valid user instance
38 * @author Roland Haeder
40 @FacesConverter (value = "UserConverter")
41 public class UserConverter implements Converter {
47 private LoggerBeanLocal loggerBeanLocal;
53 private UserWebSessionController userController;
56 * Constructor of this converter
58 public UserConverter () {
61 // Get initial context
62 Context context = new InitialContext();
65 this.loggerBeanLocal = (LoggerBeanLocal) context.lookup("java:global/jcore-logger-ejb/logger!org.mxchange.jcoreeelogger.beans.local.logger.LoggerBeanLocal"); //NOI18N
67 // ... and user controller
68 this.userController = (UserWebSessionController) context.lookup("java:global/juser-ejb/user!org.mxchange.jusercore.model.user.UserSessionBeanRemote"); //NOI18N
69 } catch (final NamingException ex) {
70 // Continue to throw it
71 throw new RuntimeException("context.lookup() failed.", ex); //NOI18N
76 public Object getAsObject (final FacesContext context, final UIComponent component, final String submittedValue) {
78 this.loggerBeanLocal.logTrace(MessageFormat.format("getAsObject: context={0},component={1},submittedValue={2} - CALLED!", context, component, submittedValue)); //NOI18N
80 // Is the value null or empty?
81 if ((null == submittedValue) || (submittedValue.trim().isEmpty())) {
83 this.loggerBeanLocal.logTrace("getAsObject: submittedValue is null or empty - EXIT!"); //NOI18N
93 // Try to parse the value as long
94 Long userId = Long.valueOf(submittedValue);
97 this.loggerBeanLocal.logDebug(MessageFormat.format("getAsObject: userId{0}", userId));
99 // Try to get user instance from it
100 user = this.userController.lookupUserById(userId);
103 this.loggerBeanLocal.logDebug(MessageFormat.format("getAsObject: user={0}", user));
104 } catch (final NumberFormatException ex) {
106 throw new ConverterException(ex);
107 } catch (final UserNotFoundException ex) {
109 this.loggerBeanLocal.logDebug(MessageFormat.format("getAsObject: Exception: {0} - Returning null ...", ex));
116 this.loggerBeanLocal.logTrace(MessageFormat.format("getAsObject: user={0} - EXIT!", user));
123 public String getAsString (final FacesContext context, final UIComponent component, final Object value) {
124 // Is the object null?
125 if ((null == value) || ((String.valueOf(value)).isEmpty())) {
128 } else if (!(value instanceof User)) {
129 // Not same interface
130 throw new IllegalArgumentException(MessageFormat.format("value {0} does not implement User.", value)); //NOI18N
133 // Return category id
134 return String.valueOf(((User) value).getUserId());