]> git.mxchange.org Git - pizzaservice-war.git/commitdiff
Continued with project:
authorRoland Haeder <roland@mxchange.org>
Fri, 14 Aug 2015 10:05:07 +0000 (12:05 +0200)
committerRoland Haeder <roland@mxchange.org>
Fri, 14 Aug 2015 10:05:07 +0000 (12:05 +0200)
- Added new class BaseCategory and ProductCategory
- Implemented newly required method emptyStringToNull() in category database frontend
- Half-implemented newly required method emptyStringToNull() in product database frontend (unfinished)
- Category is now Comparable and asks for compareTo() method as well
- Added method addCategory() to database frontend
- doAdminAddCategory() should be "basicly finished" by now
- Better implement generateLinkForParent() to generate a HTML link for parent id. This links to product.jsp?category_id=x
Signed-off-by:Roland Häder <roland@mxchange.org>

src/java/org/mxchange/pizzaapplication/application/PizzaServiceApplication.java
src/java/org/mxchange/pizzaapplication/category/BaseCategory.java [new file with mode: 0644]
src/java/org/mxchange/pizzaapplication/category/Category.java
src/java/org/mxchange/pizzaapplication/category/product/ProductCategory.java [new file with mode: 0644]
src/java/org/mxchange/pizzaapplication/database/frontend/category/CategoryFrontend.java
src/java/org/mxchange/pizzaapplication/database/frontend/category/PizzaCategoryDatabaseFrontend.java
src/java/org/mxchange/pizzaapplication/database/frontend/product/PizzaProductDatabaseFrontend.java
web/admin/category.jsp

index 2de42a69ba23c0e6efd9034916229faf90346fdc..b8926b114bd186ee9044918da1e941a6db346cd8 100644 (file)
@@ -1209,6 +1209,18 @@ public class PizzaServiceApplication extends BasePizzaServiceSystem implements P
                        }
                }
 
+               // Get all data from it
+               String title = request.getParameter(PizzaCategoryDatabaseConstants.COLUMN_TITLE);
+               String parent = request.getParameter(PizzaCategoryDatabaseConstants.COLUMN_PARENT);
+
+               try {
+                       // The category is not found, so add it to database
+                       this.categoryFrontend.addCategory(title, parent);
+               } catch (final SQLException ex) {
+                       // Continue to throw it
+                       throw new ServletException(ex);
+               }
+
                // Trace message
                this.getLogger().trace("EXIT!"); //NOI18N
        }
diff --git a/src/java/org/mxchange/pizzaapplication/category/BaseCategory.java b/src/java/org/mxchange/pizzaapplication/category/BaseCategory.java
new file mode 100644 (file)
index 0000000..859edd2
--- /dev/null
@@ -0,0 +1,142 @@
+/*
+ * 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.pizzaapplication.category;
+
+import java.text.MessageFormat;
+import java.util.Objects;
+import org.mxchange.jcore.BaseFrameworkSystem;
+
+/**
+ * A general product category class
+ *
+ * @author Roland Haeder
+ */
+public class BaseCategory extends BaseFrameworkSystem implements Category {
+       /**
+        * Id number of category
+        */
+       private Integer id;
+
+       /**
+        * Parent category id
+        */
+       private Integer parent;
+
+       /**
+        * Title of category
+        */
+       private String title;
+
+       /**
+        * Constructor which accepts all database fields
+        * @param id Id number of database record
+        * @param title Category title
+        * @param parent Parent id
+        */
+       protected BaseCategory (final Integer id, final String title, final Integer parent) {
+               // Set all here
+               this.setId(id);
+               this.setTitle(title);
+               this.setParent(parent);
+       }
+
+       /**
+        * Compares two categories with each other
+        *
+        * @param category Category comparator
+        * @return Comparison value
+        */
+       @Override
+       public int compareTo (final Category category) {
+               // Trace message
+               this.getLogger().trace(MessageFormat.format("category={0} - CALLED!", category));
+               
+               // category should not be null
+               if (category == null) {
+                       throw new NullPointerException("category is null");
+               }
+
+               // Debug message
+               this.getLogger().debug(MessageFormat.format("this.id={0},category.id={1}", this.getId(), category.getId()));
+
+               // Is the id the same?
+               if (Objects.equals(this.getId(), category.getId())) {
+                       // Same id, means same category
+                       return 0;
+               } else if (this.getId() > category.getId()) {
+                       // This id is larger than compared to
+                       return -1;
+               }
+
+               // The other id is larger
+               return 1;
+       }
+
+       /**
+        * Id number of category
+        * @return the id
+        */
+       @Override
+       public final Integer getId () {
+               return this.id;
+       }
+
+       /**
+        * Id number of category
+        * @param id the id to set
+        */
+       @Override
+       public final void setId (final Integer id) {
+               this.id = id;
+       }
+
+       /**
+        * Parent category id
+        * @return the parent
+        */
+       @Override
+       public final Integer getParent () {
+               return this.parent;
+       }
+
+       /**
+        * Parent category id
+        * @param parent the parent to set
+        */
+       @Override
+       public final void setParent (final Integer parent) {
+               this.parent = parent;
+       }
+
+       /**
+        * Title of category
+        * @return the title
+        */
+       @Override
+       public final String getTitle () {
+               return this.title;
+       }
+
+       /**
+        * Title of category
+        * @param title the title to set
+        */
+       @Override
+       public final void setTitle (final String title) {
+               this.title = title;
+       }
+}
index 6e1483625f6038137b1b38aa1ffcee9295c42b83..02b30d8fa898fdd2075ac89511b1e16d086bdf72 100644 (file)
@@ -23,5 +23,48 @@ import org.mxchange.jcore.database.storage.Storeable;
  *
  * @author Roland Haeder
  */
-public interface Category extends Storeable {
+public interface Category extends Storeable, Comparable<Category> {
+
+       /**
+        * Id number of category
+        * @return the id
+        */
+       public Integer getId ();
+
+       /**
+        * Id number of category
+        * @param id the id to set
+        */
+       public void setId (final Integer id);
+
+       /**
+        * Parent category id
+        * @return the parent
+        */
+       public Integer getParent ();
+
+       /**
+        * Parent category id
+        * @param parent the parent to set
+        */
+       public void setParent (final Integer parent);
+
+       /**
+        * Title of category
+        * @return the title
+        */
+       public String getTitle ();
+
+       /**
+        * Title of category
+        * @param title the title to set
+        */
+       public void setTitle (final String title);
+
+       /**
+        * Compare method
+        * @param category Category to compare to
+        */
+       @Override
+       public int compareTo (final Category category);
 }
diff --git a/src/java/org/mxchange/pizzaapplication/category/product/ProductCategory.java b/src/java/org/mxchange/pizzaapplication/category/product/ProductCategory.java
new file mode 100644 (file)
index 0000000..dd89d34
--- /dev/null
@@ -0,0 +1,36 @@
+/*
+ * 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.pizzaapplication.category.product;
+
+import org.mxchange.pizzaapplication.category.BaseCategory;
+
+/**
+ * A product category
+ * @author Roland Haeder
+ */
+public class ProductCategory extends BaseCategory {
+       /**
+        * Constructor which accepts all database fields
+        * @param id Id number of database record
+        * @param title Category title
+        * @param parent Parent id
+        */
+       public ProductCategory (final Integer id, final String title, final Integer parent) {
+               // Call parent constructor
+               super(id, title, parent);
+       }
+}
index e32c7bc999ce945e60c9a9097d3017c9c381c0c4..b638db51cbdafa297ed5a4a81e29c6e9b0a8785d 100644 (file)
@@ -30,6 +30,15 @@ import org.mxchange.pizzaapplication.category.Category;
  */
 public interface CategoryFrontend extends DatabaseFrontend {
 
+       /**
+        * Adds given category title as new category, parent may be null if not selected.
+        *
+        * @param title Title of category
+        * @param parent Parent id or null if not selected
+        * @throws java.sql.SQLException If any SQL error occurs
+        */
+       public void addCategory (final String title, final String parent) throws SQLException;
+
        /**
         * An iterator on all categories
         *
index 36ecbbe8fae0d48a9c6259b33342de74e6b33951..72b84ce860a63b19000b9870dac5c62f3b7968d7 100644 (file)
@@ -16,6 +16,7 @@
  */
 package org.mxchange.pizzaapplication.database.frontend.category;
 
+import org.mxchange.pizzaapplication.category.product.ProductCategory;
 import java.io.IOException;
 import java.sql.ResultSet;
 import java.sql.SQLException;
@@ -38,7 +39,6 @@ import org.mxchange.pizzaapplication.database.category.PizzaCategoryDatabaseCons
  * @author Roland Haeder
  */
 public class PizzaCategoryDatabaseFrontend extends BaseDatabaseFrontend implements CategoryFrontend {
-
        /**
         * Default constrcutor
         * @throws org.mxchange.jcore.exceptions.UnsupportedDatabaseBackendException If the configured backend is not supported
@@ -55,6 +55,40 @@ public class PizzaCategoryDatabaseFrontend extends BaseDatabaseFrontend implemen
                this.initBackend();
        }
 
+       /**
+        * Adds given category title as new category, parent may be null if not selected.
+        *
+        * @param title Title of category
+        * @param parent Parent id or null if not selected
+        */
+       @Override
+       public void addCategory (final String title, final String parent) throws SQLException {
+               // Trace message
+               this.getLogger().trace(MessageFormat.format("title={0},parent={1} - CALLED!", title, parent));
+
+               // Title should not be null
+               if (title == null) {
+                       // Abort here
+                       throw new NullPointerException("title is null");
+               }
+
+               // Clear dataset from previous usage
+               this.clearDataSet();
+
+               // Add title and parent
+               this.addToDataSet(PizzaCategoryDatabaseConstants.COLUMN_TITLE, title);
+               this.addToDataSet(PizzaCategoryDatabaseConstants.COLUMN_PARENT, parent);
+
+               // Handle this over to the backend
+               Result<? extends Storeable> result = this.doInsertDataSet();
+
+               // Debug message
+               this.getLogger().debug(MessageFormat.format("result={0}", result));
+
+               // Trace message
+               this.getLogger().trace("EXIT!");
+       }
+
        /**
         * Shuts down the database layer
         * @throws java.sql.SQLException If an SQL error occurs
@@ -72,6 +106,38 @@ public class PizzaCategoryDatabaseFrontend extends BaseDatabaseFrontend implemen
                this.getLogger().trace("EXIT!"); //NOI18N
        }
 
+       /**
+        * Depending on column, an empty value may be converted to null
+        *
+        * @param key Key to check
+        * @param value Value to check
+        * @return Possible previous value or null
+        */
+       @Override
+       public Object emptyStringToNull (final String key, final Object value) {
+               // Trace message
+               this.getLogger().trace(MessageFormat.format("key={0},value={1} - CALLED!", key, value));
+
+               // Copy value
+               Object v = value;
+
+               // Is the value empty?
+               if ((value instanceof String) && ("".equals(value))) {
+                       // This value may need to be changed
+                       switch (key) {
+                               case PizzaCategoryDatabaseConstants.COLUMN_PARENT: // Convert this
+                                       v = null;
+                                       break;
+                       }
+               }
+
+               // Trace message
+               this.getLogger().trace(MessageFormat.format("v={0} - EXIT!", v));
+
+               // Return it
+               return v;
+       }
+
        @Override
        @SuppressWarnings ("unchecked")
        public Iterator<Category> getCategories () throws IOException, BadTokenException, SQLException {
@@ -117,8 +183,16 @@ public class PizzaCategoryDatabaseFrontend extends BaseDatabaseFrontend implemen
 
                // "Walk" through all entries
                while (resultSet.next()) {
-                       // Unwrap whole object
-                       Category category = resultSet.unwrap(Category.class);
+                       // Get id, title and parent id
+                       Integer id = resultSet.getInt(PizzaCategoryDatabaseConstants.COLUMN_ID);
+                       String title = resultSet.getString(PizzaCategoryDatabaseConstants.COLUMN_PARENT);
+                       Integer parent = resultSet.getInt(PizzaCategoryDatabaseConstants.COLUMN_PARENT);
+
+                       // Debug message
+                       this.getLogger().debug(MessageFormat.format("id={0},title={1},parent={2}", id, title, parent));
+
+                       // Instance new object
+                       Category category = new ProductCategory(id, title, parent);
 
                        // Debug log
                        this.getLogger().debug(MessageFormat.format("category={0}", category));
@@ -134,6 +208,42 @@ public class PizzaCategoryDatabaseFrontend extends BaseDatabaseFrontend implemen
                return result;
        }
 
+       /**
+        * Checks if given category title is already used
+        *
+        * @param title Title to check
+        * @return Whether the title has been used
+        */
+       @Override
+       public boolean isCategoryTitleUsed (final String title) throws IOException, SQLException, BadTokenException {
+               // Trace message
+               this.getLogger().trace(MessageFormat.format("title={0} - CALLED!", title));
+               
+               // Get search criteria
+               SearchableCritera criteria = new SearchCriteria();
+               
+               // Add criteria
+               criteria.addCriteria(PizzaCategoryDatabaseConstants.COLUMN_TITLE, title);
+               
+               // Only one entry is find
+               criteria.setLimit(1);
+               
+               // Run it on backend
+               Result<? extends Storeable> result = this.getBackend().doSelectByCriteria(criteria);
+               
+               // Debug log
+               this.getLogger().debug(MessageFormat.format("result({0}={1}", result, result.size()));
+               
+               // Now check size of the result
+               boolean isFound = (result.size() == 1);
+               
+               // Trace message
+               this.getLogger().trace(MessageFormat.format("isFound={0} - EXIT!", isFound));
+               
+               // Return it
+               return isFound;
+       }
+
        /**
         * Parses given line from database backend into a Storeable instance. Please
         * note that not all backends need this.
@@ -165,40 +275,4 @@ public class PizzaCategoryDatabaseFrontend extends BaseDatabaseFrontend implemen
        private Category parseLineToCategory (final String line) {
                throw new UnsupportedOperationException(MessageFormat.format("Not supported yet: line={0}", line)); //NOI18N
        }
-
-       /**
-        * Checks if given category title is already used
-        *
-        * @param title Title to check
-        * @return Whether the title has been used
-        */
-       @Override
-       public boolean isCategoryTitleUsed (final String title) throws IOException, SQLException, BadTokenException {
-               // Trace message
-               this.getLogger().trace(MessageFormat.format("title={0} - CALLED!", title));
-
-               // Get search criteria
-               SearchableCritera criteria = new SearchCriteria();
-
-               // Add criteria
-               criteria.addCriteria(PizzaCategoryDatabaseConstants.COLUMN_TITLE, title);
-
-               // Only one entry is find
-               criteria.setLimit(1);
-
-               // Run it on backend
-               Result<? extends Storeable> result = this.getBackend().doSelectByCriteria(criteria);
-
-               // Debug log
-               this.getLogger().debug(MessageFormat.format("result({0}={1}", result, result.size()));
-
-               // Now check size of the result
-               boolean isFound = (result.size() == 1);
-
-               // Trace message
-               this.getLogger().trace(MessageFormat.format("isFound={0} - EXIT!", isFound));
-
-               // Return it
-               return isFound;
-       }
 }
index 5f5820a2588559731623500f8a503b084fea82e2..7076a74980332389b6cc475ca9ba5db413fd6466 100644 (file)
@@ -73,6 +73,35 @@ public class PizzaProductDatabaseFrontend extends BaseDatabaseFrontend implement
                this.getLogger().trace("EXIT!"); //NOI18N
        }
 
+       /**
+        * Depending on column, an empty value may be converted to null
+        *
+        * @param key Key to check
+        * @param value Value to check
+        * @return Possible previous value or null
+        */
+       @Override
+       public Object emptyStringToNull (final String key, final Object value) {
+               // Trace message
+               this.getLogger().trace(MessageFormat.format("key={0},value={1} - CALLED!", key, value));
+
+               // Copy value
+               Object v = value;
+
+               // Is the value empty?
+               if ((value instanceof String) && ("".equals(value))) {
+                       // This value may need to be changed
+                       switch (key) {
+                       }
+               }
+
+               // Trace message
+               this.getLogger().trace(MessageFormat.format("v={0} - EXIT!", v));
+
+               // Return it
+               return v;
+       }
+
        @Override
        @SuppressWarnings ("unchecked")
        public Iterator<Product> getProducts () throws IOException, BadTokenException, SQLException {
index 246fd5b5a19aa8ac24a0b521ab5ad11da94c7cfa..516fceca7ec20e5506e40dae2d81b0cb61c4a9f0 100644 (file)
@@ -65,7 +65,7 @@
                                                                        ${category.getTitle()}
                                                                </td>
                                                                <td>
-                                                                       ${category.getPrintableParent()}
+                                                                       ${app.generateLinkForParent(category)}
                                                                </td>
                                                        </tr>
                                                        </c:forEach>