From: Roland Haeder Date: Fri, 14 Aug 2015 10:05:07 +0000 (+0200) Subject: Continued with project: X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=ba67f3d50d3788c464578ec0b7cb9dfb32111a9b;p=pizzaservice-war.git Continued with project: - 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 --- diff --git a/src/java/org/mxchange/pizzaapplication/application/PizzaServiceApplication.java b/src/java/org/mxchange/pizzaapplication/application/PizzaServiceApplication.java index 2de42a69..b8926b11 100644 --- a/src/java/org/mxchange/pizzaapplication/application/PizzaServiceApplication.java +++ b/src/java/org/mxchange/pizzaapplication/application/PizzaServiceApplication.java @@ -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 index 00000000..859edd23 --- /dev/null +++ b/src/java/org/mxchange/pizzaapplication/category/BaseCategory.java @@ -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 . + */ +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; + } +} diff --git a/src/java/org/mxchange/pizzaapplication/category/Category.java b/src/java/org/mxchange/pizzaapplication/category/Category.java index 6e148362..02b30d8f 100644 --- a/src/java/org/mxchange/pizzaapplication/category/Category.java +++ b/src/java/org/mxchange/pizzaapplication/category/Category.java @@ -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 { + + /** + * 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 index 00000000..dd89d346 --- /dev/null +++ b/src/java/org/mxchange/pizzaapplication/category/product/ProductCategory.java @@ -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 . + */ +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); + } +} diff --git a/src/java/org/mxchange/pizzaapplication/database/frontend/category/CategoryFrontend.java b/src/java/org/mxchange/pizzaapplication/database/frontend/category/CategoryFrontend.java index e32c7bc9..b638db51 100644 --- a/src/java/org/mxchange/pizzaapplication/database/frontend/category/CategoryFrontend.java +++ b/src/java/org/mxchange/pizzaapplication/database/frontend/category/CategoryFrontend.java @@ -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 * diff --git a/src/java/org/mxchange/pizzaapplication/database/frontend/category/PizzaCategoryDatabaseFrontend.java b/src/java/org/mxchange/pizzaapplication/database/frontend/category/PizzaCategoryDatabaseFrontend.java index 36ecbbe8..72b84ce8 100644 --- a/src/java/org/mxchange/pizzaapplication/database/frontend/category/PizzaCategoryDatabaseFrontend.java +++ b/src/java/org/mxchange/pizzaapplication/database/frontend/category/PizzaCategoryDatabaseFrontend.java @@ -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 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 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 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 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; - } } diff --git a/src/java/org/mxchange/pizzaapplication/database/frontend/product/PizzaProductDatabaseFrontend.java b/src/java/org/mxchange/pizzaapplication/database/frontend/product/PizzaProductDatabaseFrontend.java index 5f5820a2..7076a749 100644 --- a/src/java/org/mxchange/pizzaapplication/database/frontend/product/PizzaProductDatabaseFrontend.java +++ b/src/java/org/mxchange/pizzaapplication/database/frontend/product/PizzaProductDatabaseFrontend.java @@ -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 getProducts () throws IOException, BadTokenException, SQLException { diff --git a/web/admin/category.jsp b/web/admin/category.jsp index 246fd5b5..516fceca 100644 --- a/web/admin/category.jsp +++ b/web/admin/category.jsp @@ -65,7 +65,7 @@ ${category.getTitle()} - ${category.getPrintableParent()} + ${app.generateLinkForParent(category)}