]> git.mxchange.org Git - pizzaservice-war.git/blob - src/java/org/mxchange/pizzaapplication/converter/contact/PizzaContactConverter.java
5e4df62d64ed88a0ac620935376dbc25cd633589
[pizzaservice-war.git] / src / java / org / mxchange / pizzaapplication / converter / contact / PizzaContactConverter.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.contact;
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.jcontacts.contact.Contact;
29 import org.mxchange.jcontacts.contact.ContactSessionBeanRemote;
30 import org.mxchange.jcontacts.exceptions.ContactNotFoundException;
31 import org.mxchange.jcoreeelogger.beans.local.logger.Log;
32 import org.mxchange.jcoreeelogger.beans.local.logger.LoggerBeanLocal;
33
34 /**
35  * Converter for contact id <-> valid contact instance
36  * <p>
37  * @author Roland Haeder<roland@mxchange.org>
38  */
39 @FacesConverter (value = "ContactConverter")
40 public class PizzaContactConverter implements Converter {
41
42         /**
43          * User EJB
44          */
45         private ContactSessionBeanRemote contactBean;
46
47         /**
48          * Logger instance
49          */
50         @Log
51         private LoggerBeanLocal loggerBeanLocal;
52
53         /**
54          * Initialization of this converter
55          */
56         public PizzaContactConverter () {
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.contactBean = (ContactSessionBeanRemote) context.lookup("java:global/PizzaService-ejb/contact!org.mxchange.jcontacts.contact.ContactSessionBeanRemote"); //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                 // NOISY-DEBUG: 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                         // Warning message
81                         this.loggerBeanLocal.logWarning(MessageFormat.format("{0}.getAsObject(): submittedValue is null or empty - EXIT!", this.getClass().getSimpleName())); //NOI18N
82
83                         // Return null
84                         return null;
85                 }
86
87                 // Init instance
88                 Contact contact = null;
89
90                 try {
91                         // Try to parse the value as long
92                         Long contactId = Long.valueOf(submittedValue);
93
94                         // Debug message
95                         // NOISY-DEBUG: this.loggerBeanLocal.logDebug(MessageFormat.format("getAsObject: contactId{0}", contactId)); //NOI18N
96
97                         // Try to get user instance from it
98                         contact = this.contactBean.findContactById(contactId);
99
100                         // Debug message
101                         // NOISY-DEBUG: this.loggerBeanLocal.logDebug(MessageFormat.format("getAsObject: user={0}", user)); //NOI18N
102                 } catch (final NumberFormatException ex) {
103                         // Throw again
104                         throw new ConverterException(ex);
105                 } catch (final ContactNotFoundException ex) {
106                         // Debug message
107                         this.loggerBeanLocal.logDebug(MessageFormat.format("{0}.getAsObject(): Exception: {1} - Returning null ...", this.getClass().getSimpleName(), ex)); //NOI18N
108                 }
109
110                 // Trace message
111                 // NOISY-DEBUG: this.loggerBeanLocal.logTrace(MessageFormat.format("getAsObject: contact={0} - EXIT!", contact)); //NOI18N
112
113                 // Return it
114                 return contact;
115         }
116
117         @Override
118         public String getAsString (final FacesContext context, final UIComponent component, final Object value) {
119                 // Is the object null?
120                 if ((null == value) || ((String.valueOf(value)).isEmpty())) {
121                         // Is null
122                         return ""; //NOI18N
123                 } else if (!(value instanceof Contact)) {
124                         // Not same interface
125                         throw new IllegalArgumentException(MessageFormat.format("value {0} does not implement Contact.", value)); //NOI18N
126                 }
127
128                 // Return category id
129                 return String.valueOf(((Contact) value).getContactId());
130         }
131
132 }