]> git.mxchange.org Git - pizzaservice-war.git/blob - src/java/org/mxchange/pizzaapplication/converter/category/PizzaCategoryConverter.java
Updated copyright year
[pizzaservice-war.git] / src / java / org / mxchange / pizzaapplication / converter / category / PizzaCategoryConverter.java
1 /*
2  * Copyright (C) 2016 - 2024 Free Software Foundation
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.category;
18
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;
33
34 /**
35  * A converter for transferring category objects
36  * <p>
37  * @author Roland Häder<roland@mxchange.org>
38  */
39 @FacesConverter ("CategoryConverter")
40 public class PizzaCategoryConverter implements Converter {
41
42         /**
43          * Category EJB
44          */
45         private CategorySessionBeanRemote categoryBean;
46
47         /**
48          * Logger instance
49          */
50         @Log
51         private LoggerBeanLocal loggerBeanLocal;
52
53         /**
54          * Default constructor
55          */
56         public PizzaCategoryConverter () {
57                 // Try to get it
58                 try {
59                         // Get initial context
60                         Context context = new InitialContext();
61
62                         // Lookup category bean
63                         this.categoryBean = (CategorySessionBeanRemote) context.lookup("java:global/jshop-ejb/category!org.mxchange.jshopcore.model.category.CategorySessionBeanRemote"); //NOI18N
64
65                         // Lookup logger
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
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                 // Get full list
79                 List<Category> categoryList = this.categoryBean.allCategories();
80
81                 // Is the value null or empty?
82                 if ((null == submittedValue) || (submittedValue.trim().isEmpty())) {
83                         // Warning message
84                         this.loggerBeanLocal.logWarning("getAsObject: submittedValue is null or empty - EXIT!"); //NOI18N
85
86                         // Return null
87                         return null;
88                 }
89
90                 // Init value
91                 Category category = null;
92
93                 // Try this better
94                 try {
95                         // Convert it to long
96                         Long categoryId = Long.parseLong(submittedValue);
97
98                         // Category id should not be below 1
99                         assert (categoryId > 0) : "categoryId is smaller than one: " + categoryId; //NOI18N
100
101                         // Debug message
102                         // NOISY-DEBUG: this.loggerBeanLocal.logDebug(MessageFormat.format("getAsObject: categoryId={0}", categoryId)); //NOI18N
103
104                         // Try to find it
105                         for (final Category cat : categoryList) {
106                                 // Is the id the same? (null-safe)
107                                 if (Objects.equals(cat.getCategoryId(), categoryId)) {
108                                         // Found it
109                                         category = cat;
110                                         break;
111                                 }
112                         }
113
114                         // Debug message
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);
119                 }
120
121                 // Trace message
122                 // NOISY-DEBUG: this.loggerBeanLocal.logTrace(MessageFormat.format("getAsObject: category={0} - EXIT!", category)); //NOI18N
123
124                 // Return it
125                 return category;
126         }
127
128         @Override
129         public String getAsString (final FacesContext context, final UIComponent component, final Object value) {
130                 // Trace message
131                 this.loggerBeanLocal.logTrace(MessageFormat.format("getAsString: context={0},component={1},value={2} - CALLED!", context, component, value)); //NOI18N
132
133                 // Is the object null?
134                 if (null == value) {
135                         // Is null
136                         return ""; //NOI18N
137                 } else if (!(value instanceof Category)) {
138                         // Not same interface
139                         throw new IllegalArgumentException(MessageFormat.format("value {0} does not implement Category.", value)); //NOI18N
140                 }
141
142                 // Return category id
143                 String str = String.valueOf(((Category) value).getCategoryId());
144
145                 // Trace message
146                 this.loggerBeanLocal.logTrace(MessageFormat.format("getAsString: str={0} - EXIT!", str)); //NOI18N
147
148                 // Return it
149                 return str;
150         }
151
152 }