2 * Copyright (C) 2016 Roland Häder
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.
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.
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/>.
17 package org.mxchange.pizzaapplication.converter.contact;
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;
33 * Converter for contact id <-> valid contact instance
35 * @author Roland Häder<roland@mxchange.org>
37 @FacesConverter (value = "ContactConverter")
38 public class PizzaContactConverter implements Converter {
43 private ContactSessionBeanRemote contactBean;
46 * Initialization of this converter
48 public PizzaContactConverter () {
51 // Get initial context
52 Context context = new InitialContext();
54 // ... and user controller
55 this.contactBean = (ContactSessionBeanRemote) context.lookup("java:global/pizzaservice-ejb/contact!org.mxchange.jcontacts.contact.ContactSessionBeanRemote"); //NOI18N
56 } catch (final NamingException ex) {
57 // Continue to throw it
58 throw new ConverterException(MessageFormat.format("context.lookup() failed: {0}", ex.getMessage()), ex); //NOI18N
63 public Object getAsObject (final FacesContext context, final UIComponent component, final String submittedValue) {
65 // NOISY-DEBUG: this.loggerBeanLocal.logTrace(MessageFormat.format("getAsObject: context={0},component={1},submittedValue={2} - CALLED!", context, component, submittedValue)); //NOI18N
67 // Is the value null or empty?
68 if ((null == submittedValue) || (submittedValue.trim().isEmpty())) {
70 // @TODO Not working with JNDI (no remote interface) this.loggerBeanLocal.logWarning(MessageFormat.format("{0}.getAsObject(): submittedValue is null or empty - EXIT!", this.getClass().getSimpleName())); //NOI18N
77 Contact contact = null;
80 // Try to parse the value as long
81 Long contactId = Long.valueOf(submittedValue);
84 // NOISY-DEBUG: this.loggerBeanLocal.logDebug(MessageFormat.format("getAsObject: contactId{0}", contactId)); //NOI18N
86 // Try to get user instance from it
87 contact = this.contactBean.findContactById(contactId);
90 // NOISY-DEBUG: this.loggerBeanLocal.logDebug(MessageFormat.format("getAsObject: contact={0}", contact)); //NOI18N
91 } catch (final NumberFormatException ex) {
93 throw new ConverterException(ex);
94 } catch (final ContactNotFoundException ex) {
96 // @TODO Not working with JNDI (no remote interface) this.loggerBeanLocal.logDebug(MessageFormat.format("{0}.getAsObject(): Exception: {1} - Returning null ...", this.getClass().getSimpleName(), ex)); //NOI18N
100 // NOISY-DEBUG: this.loggerBeanLocal.logTrace(MessageFormat.format("getAsObject: contact={0} - EXIT!", contact)); //NOI18N
107 public String getAsString (final FacesContext context, final UIComponent component, final Object value) {
108 // Is the object null?
109 if ((null == value) || ((String.valueOf(value)).isEmpty())) {
112 } else if (!(value instanceof Contact)) {
113 // Not same interface
114 throw new IllegalArgumentException(MessageFormat.format("value {0} does not implement Contact.", value)); //NOI18N
117 // Return category id
118 return String.valueOf(((Contact) value).getContactId());