2 * Copyright (C) 2016, 2017 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.category;
19 import java.text.MessageFormat;
20 import java.util.List;
21 import java.util.Objects;
22 import javax.faces.component.UIComponent;
23 import javax.faces.context.FacesContext;
24 import javax.faces.convert.Converter;
25 import javax.faces.convert.FacesConverter;
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.jproduct.model.category.Category;
32 import org.mxchange.jproduct.model.category.CategorySessionBeanRemote;
35 * A converter for transferring category objects
37 * @author Roland Häder<roland@mxchange.org>
39 @FacesConverter ("CategoryConverter")
40 public class PizzaCategoryConverter implements Converter {
45 private CategorySessionBeanRemote categoryBean;
51 private LoggerBeanLocal loggerBeanLocal;
56 public PizzaCategoryConverter () {
59 // Get initial context
60 Context context = new InitialContext();
62 // Lookup category bean
63 this.categoryBean = (CategorySessionBeanRemote) context.lookup("java:global/jshop-ejb/category!org.mxchange.jshopcore.model.category.CategorySessionBeanRemote"); //NOI18N
66 this.loggerBeanLocal = (LoggerBeanLocal) context.lookup("java:global/jcore-logger-ejb/logger!org.mxchange.jcoreeelogger.beans.local.logger.LoggerBeanLocal"); //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
74 public Object getAsObject (final FacesContext context, final UIComponent component, final String submittedValue) {
76 // NOISY-DEBUG: this.loggerBeanLocal.logTrace(MessageFormat.format("getAsObject: context={0},component={1},submittedValue={2} - CALLED!", context, component, submittedValue)); //NOI18N
79 List<Category> categoryList = this.categoryBean.allCategories();
81 // Is the value null or empty?
82 if ((null == submittedValue) || (submittedValue.trim().isEmpty())) {
84 this.loggerBeanLocal.logWarning("getAsObject: submittedValue is null or empty - EXIT!"); //NOI18N
91 Category category = null;
96 Long categoryId = Long.parseLong(submittedValue);
98 // Category id should not be below 1
99 assert (categoryId > 0) : "categoryId is smaller than one: " + categoryId; //NOI18N
102 // NOISY-DEBUG: this.loggerBeanLocal.logDebug(MessageFormat.format("getAsObject: categoryId={0}", categoryId)); //NOI18N
105 for (final Category cat : categoryList) {
106 // Is the id the same? (null-safe)
107 if (Objects.equals(cat.getCategoryId(), categoryId)) {
115 // NOISY-DEBUG: this.loggerBeanLocal.logDebug(MessageFormat.format("getAsObject: category={0}", category)); //NOI18N
116 } catch (final NumberFormatException ex) {
117 // Log exception (maybe to much?)
118 this.loggerBeanLocal.logException(ex);
122 // NOISY-DEBUG: this.loggerBeanLocal.logTrace(MessageFormat.format("getAsObject: category={0} - EXIT!", category)); //NOI18N
129 public String getAsString (final FacesContext context, final UIComponent component, final Object value) {
131 this.loggerBeanLocal.logTrace(MessageFormat.format("getAsString: context={0},component={1},value={2} - CALLED!", context, component, value)); //NOI18N
133 // Is the object null?
137 } else if (!(value instanceof Category)) {
138 // Not same interface
139 throw new IllegalArgumentException(MessageFormat.format("value {0} does not implement Category.", value)); //NOI18N
142 // Return category id
143 String str = String.valueOf(((Category) value).getCategoryId());
146 this.loggerBeanLocal.logTrace(MessageFormat.format("getAsString: str={0} - EXIT!", str)); //NOI18N