]> git.mxchange.org Git - jjobs-war.git/blob - src/java/org/mxchange/jjobs/converter/user/UserConverter.java
renamed packages (addressbook import) + added event for logging in
[jjobs-war.git] / src / java / org / mxchange / jjobs / converter / user / UserConverter.java
1 /*
2  * Copyright (C) 2016 Roland Haeder
3  *
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.
8  *
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.
13  *
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/>.
16  */
17 package org.mxchange.jjobs.converter.user;
18
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.jjobs.beans.user.UserWebSessionController;
30 import org.mxchange.jcoreeelogger.beans.local.logger.Log;
31 import org.mxchange.jcoreeelogger.beans.local.logger.LoggerBeanLocal;
32 import org.mxchange.jusercore.exceptions.UserNotFoundException;
33 import org.mxchange.jusercore.model.user.User;
34
35 /**
36  * Converter for user id <-> valid user instance
37  * <p>
38  * @author Roland Haeder
39  */
40 @FacesConverter (value = "UserConverter")
41 public class UserConverter implements Converter {
42
43         /**
44          * Logger instance
45          */
46         @Log
47         private LoggerBeanLocal loggerBeanLocal;
48
49         /**
50          * User bean
51          */
52         @Inject
53         private UserWebSessionController userController;
54
55         @Override
56         public Object getAsObject (final FacesContext context, final UIComponent component, final String submittedValue) {
57                 // Trace message
58                 this.loggerBeanLocal.logTrace(MessageFormat.format("getAsObject: contect={0},component={1},submittedValue={2} - CALLED!", context, component, submittedValue)); //NOI18N
59
60                 // Is the value null or empty?
61                 if ((null == submittedValue) || (submittedValue.trim().isEmpty())) {
62                         // Return null
63                         return null;
64                 }
65
66                 // Init instance
67                 User user = null;
68
69                 try {
70                         // Try to parse the value as long
71                         Long userId = Long.valueOf(submittedValue);
72
73                         // Debug message
74                         this.loggerBeanLocal.logDebug(MessageFormat.format("getAsObject: userId{0}", userId));
75
76                         // Try to get user instance from it
77                         user = this.userController.lookupUserById(userId);
78
79                         // Debug message
80                         this.loggerBeanLocal.logDebug(MessageFormat.format("getAsObject: user={0}", user));
81                 } catch (final NumberFormatException ex) {
82                         // Throw again
83                         throw new ConverterException(ex);
84                 } catch (final UserNotFoundException ex) {
85                         // Debug message
86                         this.loggerBeanLocal.logDebug(MessageFormat.format("getAsObject: Exception: {0} - Returning null ...", ex));
87
88                         // Return null
89                         return null;
90                 }
91
92                 // Trace message
93                 this.loggerBeanLocal.logTrace(MessageFormat.format("getAsObject: user={0} - EXIT!", user));
94
95                 // Return it
96                 return user;
97         }
98
99         @Override
100         public String getAsString (final FacesContext context, final UIComponent component, final Object value) {
101                 // Is the object null?
102                 if ((null == value) || ((String.valueOf(value)).isEmpty())) {
103                         // Is null
104                         return ""; //NOI18N
105                 } else if (!(value instanceof User)) {
106                         // Not same interface
107                         throw new IllegalArgumentException(MessageFormat.format("value {0} does not implement User.", value)); //NOI18N
108                 }
109
110                 // Return category id
111                 return String.valueOf(((User) value).getUserId());
112         }
113
114         /**
115          * Initialization of this converter
116          */
117         public UserConverter () {
118                 // Try to get it
119                 try {
120                         // Get initial context
121                         Context context = new InitialContext();
122
123                         // Lookup logger
124                         this.loggerBeanLocal = (LoggerBeanLocal) context.lookup("java:global/jcore-logger-ejb/logger!org.mxchange.jcoreeelogger.beans.local.logger.LoggerBeanLocal"); //NOI18N
125
126                         // ... and user controller
127                         this.userController = (UserWebSessionController) context.lookup("java:global/juser-ejb/user!org.mxchange.jusercore.model.user.UserSessionBeanRemote"); //NOI18N
128                 } catch (final NamingException ex) {
129                         // Continue to throw it
130                         throw new RuntimeException("context.lookup() failed.", ex); //NOI18N
131                 }
132         }
133 }