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