]> git.mxchange.org Git - jjobs-war.git/blob - src/java/org/mxchange/jjobs/converter/user/UserConverter.java
Sharing of address books is not part of this application
[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.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;
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         /**
56          * Constructor of this converter
57          */
58         public UserConverter () {
59                 // Try to get it
60                 try {
61                         // Get initial context
62                         Context context = new InitialContext();
63
64                         // Lookup logger
65                         this.loggerBeanLocal = (LoggerBeanLocal) context.lookup("java:global/jcore-logger-ejb/logger!org.mxchange.jcoreeelogger.beans.local.logger.LoggerBeanLocal"); //NOI18N
66
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
72                 }
73         }
74
75         @Override
76         public Object getAsObject (final FacesContext context, final UIComponent component, final String submittedValue) {
77                 // Trace message
78                 this.loggerBeanLocal.logTrace(MessageFormat.format("getAsObject: contect={0},component={1},submittedValue={2} - CALLED!", context, component, submittedValue)); //NOI18N
79
80                 // Is the value null or empty?
81                 if ((null == submittedValue) || (submittedValue.trim().isEmpty())) {
82                         // Return null
83                         return null;
84                 }
85
86                 // Init instance
87                 User user = null;
88
89                 try {
90                         // Try to parse the value as long
91                         Long userId = Long.valueOf(submittedValue);
92
93                         // Debug message
94                         this.loggerBeanLocal.logDebug(MessageFormat.format("getAsObject: userId{0}", userId));
95
96                         // Try to get user instance from it
97                         user = this.userController.lookupUserById(userId);
98
99                         // Debug message
100                         this.loggerBeanLocal.logDebug(MessageFormat.format("getAsObject: user={0}", user));
101                 } catch (final NumberFormatException ex) {
102                         // Throw again
103                         throw new ConverterException(ex);
104                 } catch (final UserNotFoundException ex) {
105                         // Debug message
106                         this.loggerBeanLocal.logDebug(MessageFormat.format("getAsObject: Exception: {0} - Returning null ...", ex));
107
108                         // Return null
109                         return null;
110                 }
111
112                 // Trace message
113                 this.loggerBeanLocal.logTrace(MessageFormat.format("getAsObject: user={0} - EXIT!", user));
114
115                 // Return it
116                 return user;
117         }
118
119         @Override
120         public String getAsString (final FacesContext context, final UIComponent component, final Object value) {
121                 // Is the object null?
122                 if ((null == value) || ((String.valueOf(value)).isEmpty())) {
123                         // Is null
124                         return ""; //NOI18N
125                 } else if (!(value instanceof User)) {
126                         // Not same interface
127                         throw new IllegalArgumentException(MessageFormat.format("value {0} does not implement User.", value)); //NOI18N
128                 }
129
130                 // Return category id
131                 return String.valueOf(((User) value).getUserId());
132         }
133 }