]> git.mxchange.org Git - pizzaservice-war.git/blob - src/java/org/mxchange/pizzaapplication/converter/user/PizzaUserConverter.java
moved to new place
[pizzaservice-war.git] / src / java / org / mxchange / pizzaapplication / converter / user / PizzaUserConverter.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 Affero General Public License as
6  * published by the Free Software Foundation, either version 3 of the
7  * License, or (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 Affero General Public License for more details.
13  *
14  * You should have received a copy of the GNU Affero General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17 package org.mxchange.pizzaapplication.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.naming.Context;
26 import javax.naming.InitialContext;
27 import javax.naming.NamingException;
28 import org.mxchange.jcoreeelogger.beans.local.logger.Log;
29 import org.mxchange.jcoreeelogger.beans.local.logger.LoggerBeanLocal;
30 import org.mxchange.jusercore.exceptions.UserNotFoundException;
31 import org.mxchange.jusercore.model.user.User;
32 import org.mxchange.pizzaapplication.beans.user.PizzaUserWebSessionController;
33
34 /**
35  * Converter for user id <-> valid user instance
36  * <p>
37  * @author Roland Haeder<roland@mxchange.org>
38  */
39 @FacesConverter (value = "UserConverter")
40 public class PizzaUserConverter implements Converter {
41
42         /**
43          * Logger instance
44          */
45         @Log
46         private LoggerBeanLocal loggerBeanLocal;
47
48         /**
49          * User bean
50          */
51         private PizzaUserWebSessionController userController;
52
53         /**
54          * Initialization of this converter
55          */
56         public PizzaUserConverter () {
57                 // Try to get it
58                 try {
59                         // Get initial context
60                         Context context = new InitialContext();
61
62                         // Lookup logger
63                         this.loggerBeanLocal = (LoggerBeanLocal) context.lookup("java:global/jcore-logger-ejb/logger!org.mxchange.jcoreeelogger.beans.local.logger.LoggerBeanLocal"); //NOI18N
64
65                         // ... and user controller
66                         this.userController = (PizzaUserWebSessionController) context.lookup("java:global/PizzaService-ejb/user!org.mxchange.jusercore.model.user.UserSessionBeanRemote"); //NOI18N
67                 } catch (final NamingException ex) {
68                         // Continue to throw it
69                         throw new RuntimeException(MessageFormat.format("context.lookup() failed: {0}", ex.getMessage()), ex); //NOI18N
70                 }
71         }
72
73         @Override
74         public Object getAsObject (final FacesContext context, final UIComponent component, final String submittedValue) {
75                 // Trace message
76                 this.loggerBeanLocal.logTrace(MessageFormat.format("getAsObject: context={0},component={1},submittedValue={2} - CALLED!", context, component, submittedValue)); //NOI18N
77
78                 // Is the value null or empty?
79                 if ((null == submittedValue) || (submittedValue.trim().isEmpty())) {
80                         // Trace message
81                         this.loggerBeanLocal.logTrace("getAsObject: submittedValue is null or empty - EXIT!"); //NOI18N
82
83                         // Return null
84                         return null;
85                 }
86
87                 // Init instance
88                 User user = null;
89
90                 try {
91                         // Try to parse the value as long
92                         Long userId = Long.valueOf(submittedValue);
93
94                         // Debug message
95                         this.loggerBeanLocal.logDebug(MessageFormat.format("getAsObject: userId{0}", userId));
96
97                         // Try to get user instance from it
98                         user = this.userController.lookupUserById(userId);
99
100                         // Debug message
101                         this.loggerBeanLocal.logDebug(MessageFormat.format("getAsObject: user={0}", user));
102                 } catch (final NumberFormatException ex) {
103                         // Throw again
104                         throw new ConverterException(ex);
105                 } catch (final UserNotFoundException ex) {
106                         // Debug message
107                         this.loggerBeanLocal.logDebug(MessageFormat.format("getAsObject: Exception: {0} - Returning null ...", ex));
108
109                         // Return null
110                         return null;
111                 }
112
113                 // Trace message
114                 this.loggerBeanLocal.logTrace(MessageFormat.format("getAsObject: user={0} - EXIT!", user));
115
116                 // Return it
117                 return user;
118         }
119
120         @Override
121         public String getAsString (final FacesContext context, final UIComponent component, final Object value) {
122                 // Is the object null?
123                 if ((null == value) || ((String.valueOf(value)).isEmpty())) {
124                         // Is null
125                         return ""; //NOI18N
126                 } else if (!(value instanceof User)) {
127                         // Not same interface
128                         throw new IllegalArgumentException(MessageFormat.format("value {0} does not implement User.", value)); //NOI18N
129                 }
130
131                 // Return category id
132                 return String.valueOf(((User) value).getUserId());
133         }
134
135 }