]> git.mxchange.org Git - pizzaservice-war.git/commitdiff
Continued:
authorRoland Haeder <roland@mxchange.org>
Fri, 25 Sep 2015 13:06:49 +0000 (15:06 +0200)
committerRoland Haeder <roland@mxchange.org>
Fri, 25 Sep 2015 13:06:49 +0000 (15:06 +0200)
- added converter (from other project)
- removed long validator (opps)
Signed-off-by:Roland Häder <roland@mxchange.org>

src/java/org/mxchange/jshopcore/model/category/CategoryConverter.java [new file with mode: 0644]
web/WEB-INF/templates/admin/admin_category_selection_box.tpl

diff --git a/src/java/org/mxchange/jshopcore/model/category/CategoryConverter.java b/src/java/org/mxchange/jshopcore/model/category/CategoryConverter.java
new file mode 100644 (file)
index 0000000..822673e
--- /dev/null
@@ -0,0 +1,133 @@
+/*
+ * Copyright (C) 2015 Roland Haeder
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+package org.mxchange.jshopcore.model.category;
+
+import java.text.MessageFormat;
+import java.util.List;
+import java.util.Objects;
+import javax.annotation.PostConstruct;
+import javax.faces.component.UIComponent;
+import javax.faces.context.FacesContext;
+import javax.faces.convert.Converter;
+import javax.faces.convert.FacesConverter;
+import javax.inject.Inject;
+import javax.naming.Context;
+import javax.naming.InitialContext;
+import javax.naming.NamingException;
+import org.mxchange.jcoreeelogger.beans.local.logger.Log;
+import org.mxchange.jcoreeelogger.beans.local.logger.LoggerBeanLocal;
+import org.mxchange.pizzaapplication.beans.shop.ShopWebController;
+
+/**
+ * A converter for transfering category objects
+ *
+ * @author Roland Haeder
+ */
+@FacesConverter (forClass = ProductCategory.class, value = "category")
+public class CategoryConverter implements Converter {
+
+       /**
+        * Category bean
+        */
+       @Inject
+       private ShopWebController shopController;
+
+       /**
+        * Logger instance
+        */
+       @Log
+       private LoggerBeanLocal loggerBeanLocal;
+
+       @PostConstruct
+       public void init () {
+               // Try to get it
+               try {
+                       // Get initial context
+                       Context context = new InitialContext();
+                       
+                       // Lookup logger
+                       this.loggerBeanLocal = (LoggerBeanLocal) context.lookup("java:global/jcore-ee-logger/logger!org.mxchange.jcoreeelogger.beans.local.logger.LoggerBeanLocal"); //NOI18N
+               } catch (final NamingException ex) {
+                       // Continue to throw it
+                       throw new RuntimeException("context.lookup() failed.", ex); //NOI18N
+               }
+       }
+
+       @Override
+       public Object getAsObject (final FacesContext context, final UIComponent component, final String submittedValue) {
+               // Trace message
+               this.loggerBeanLocal.logTrace(MessageFormat.format("getAsObject: contect={0},component={1},submittedValue={2} - CALLED!", context, component, submittedValue)); //NOI18N
+
+               // Get full list
+               List<Category> categoryList = this.shopController.getAllCategories();
+
+               // Is the value null or empty?
+               if ((null == submittedValue) || (submittedValue.trim().isEmpty())) {
+                       // Return null
+                       return null;
+               }
+
+               // Init value
+               Category category = null;
+
+               // Try this better
+               try {
+                       // Convert it to long
+                       Long categoryId = Long.parseLong(submittedValue);
+
+                       // Category id should not be below 1
+                       assert(categoryId > 0) : "categoryId is smaller than one: " + categoryId; //NOI18N
+
+                       // Debug message
+                       this.loggerBeanLocal.logDebug(MessageFormat.format("getAsObject: categoryId={0}", categoryId)); //NOI18N
+
+                       // Try to find it
+                       for (final Category cat : categoryList) {
+                               // Is the id the same? (null-safe)
+                               if (Objects.equals(cat.getCategoryId(), categoryId)) {
+                                       // Found it
+                                       category = cat;
+                                       break;
+                               }
+                       }
+               } catch (final NumberFormatException ex) {
+                       // Log exception (maybe to much?)
+                       this.loggerBeanLocal.logException(ex);
+               }
+
+               // Trace message
+               this.loggerBeanLocal.logTrace(MessageFormat.format("getAsObject: category={0} - EXIT!", category)); //NOI18N
+
+               // Return it
+               return category;
+       }
+
+       @Override
+       public String getAsString (final FacesContext context, final UIComponent component, final Object value) {
+               // Is the object null?
+               if (null == value) {
+                       // Is null
+                       return ""; //NOI18N
+               } else if (!(value instanceof Category)) {
+                       // Not same interface
+                       throw new IllegalArgumentException(MessageFormat.format("value {0} does not implement Category.", value)); //NOI18N
+               }
+
+               // Return category id
+               return String.valueOf(((Category) value).getCategoryId());
+       }
+}
index 9235e9524fef8dc8e89c8d05807a57f628ee32dd..62a2019ab270ab66096bb58364328c9164c13351 100644 (file)
@@ -6,6 +6,5 @@
 
        <h:selectOneMenu class="select" id="productCategory" value="#{admin_product.productCategory}" required="true" requiredMessage="#{msg.ADMIN_CATEGORY_MUST_BE_SELECTED}" converter="category">
                <f:selectItems value="#{controller.allCategories}" var="cat" itemValue="#{cat}" itemLabel="#{cat.categoryTitle}" />
-               <f:validateLongRange for="productCategory" minimum="0" maximum="1000" />
        </h:selectOneMenu>
 </ui:composition>