*/
package org.mxchange.pizzaapplication.application;
-import org.mxchange.pizzaapplication.exceptions.CategoryTitleAlreadyUsedException;
import java.util.Iterator;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.mxchange.jcore.application.Application;
import org.mxchange.pizzaapplication.category.Category;
+import org.mxchange.pizzaapplication.exceptions.CategoryTitleAlreadyUsedException;
+import org.mxchange.pizzaapplication.exceptions.ProductTitleAlreadyUsedException;
import org.mxchange.pizzaapplication.product.Product;
/**
*
* @param request Request instance
* @throws javax.servlet.ServletException If something unexpected happened
- * @throws org.mxchange.pizzaapplication.application.CategoryTitleAlreadyUsedException If the given title is already used
+ * @throws org.mxchange.pizzaapplication.exceptions.CategoryTitleAlreadyUsedException If the given title is already used
*/
public void doAdminAddCategory (final HttpServletRequest request) throws ServletException, CategoryTitleAlreadyUsedException;
*
* @param request Request instance
* @throws javax.servlet.ServletException If something unexpected happened
+ * @throws org.mxchange.pizzaapplication.exceptions.ProductTitleAlreadyUsedException If the given product title is already used
*/
- public void doAdminAddProduct (final HttpServletRequest request) throws ServletException;
+ public void doAdminAddProduct (final HttpServletRequest request) throws ServletException, ProductTitleAlreadyUsedException;
/**
* Generates link HTML code for given category's parent id, if set. This
import org.mxchange.pizzaapplication.database.frontend.category.PizzaCategoryDatabaseFrontend;
import org.mxchange.pizzaapplication.database.frontend.product.PizzaProductDatabaseFrontend;
import org.mxchange.pizzaapplication.database.frontend.product.ProductFrontend;
+import org.mxchange.pizzaapplication.database.product.PizzaProductDatabaseConstants;
+import org.mxchange.pizzaapplication.exceptions.ProductTitleAlreadyUsedException;
import org.mxchange.pizzaapplication.product.Product;
/**
/**
* Checks whether given category title is already used
+ *
* @param title Title of category to check
* @return Whether it has been found
*/
- private boolean isCategoryUsed (final String title) throws IOException, SQLException, BadTokenException {
+ private boolean isCategoryTitleUsed (final String title) throws IOException, SQLException, BadTokenException {
// Delegate to frontend
return this.categoryFrontend.isCategoryTitleUsed(title);
}
+ /**
+ * Checks if given product title is already used
+ * @param title Product title to check
+ * @return Whether the product title has already been used
+ */
+ private boolean isProductTitleUsed (final String title) throws IOException, SQLException, BadTokenException {
+ // Delegate to frontend
+ return this.productFrontend.isProductTitleUsed(title);
+ }
+
/**
* Checks if the product ordered?
*
throw new NullPointerException("request is null");
}
- // Check if all fields are given
- if (request.getParameter(PizzaCategoryDatabaseConstants.COLUMN_TITLE) == null) {
+ // Get all fields
+ String title = request.getParameter(PizzaCategoryDatabaseConstants.COLUMN_TITLE);
+ String parent = request.getParameter(PizzaCategoryDatabaseConstants.COLUMN_PARENT);
+ Integer id = null;
+
+ // Check all fields
+ if (title == null) {
// "title" not set
throw new IllegalArgumentException(MessageFormat.format("{0} is not set.", PizzaCategoryDatabaseConstants.COLUMN_TITLE));
- } else if (request.getParameter(PizzaCategoryDatabaseConstants.COLUMN_TITLE).isEmpty()) {
+ } else if (title.isEmpty()) {
// Is left empty
throw new IllegalArgumentException(MessageFormat.format("{0} is empty.", PizzaCategoryDatabaseConstants.COLUMN_TITLE));
- } else if ((request.getParameter(PizzaCategoryDatabaseConstants.COLUMN_PARENT) != null) && (!request.getParameter(PizzaCategoryDatabaseConstants.COLUMN_PARENT).isEmpty())) {
+ } else if ((parent != null) && (!parent.isEmpty())) {
// "parent" is set, so check it
try {
- Integer dummy = Integer.parseInt(request.getParameter(PizzaCategoryDatabaseConstants.COLUMN_PARENT));
+ id = Integer.parseInt(parent);
} catch (final NumberFormatException e) {
// Not valid number
throw new IllegalArgumentException(e);
}
- } else {
- try {
- // Try to check if title is used already
- if (this.isCategoryUsed(request.getParameter(PizzaCategoryDatabaseConstants.COLUMN_TITLE))) {
- // Title already used
- throw new CategoryTitleAlreadyUsedException(request);
- }
- } catch (final IOException | SQLException | BadTokenException ex) {
- throw new ServletException(ex);
+ }
+ try {
+ // Try to check if title is used already
+ if (this.isCategoryTitleUsed(title)) {
+ // Title already used
+ throw new CategoryTitleAlreadyUsedException(request);
}
+ } catch (final IOException | SQLException | BadTokenException ex) {
+ throw new ServletException(ex);
}
- // 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);
+ this.categoryFrontend.addCategory(title, id);
} catch (final SQLException ex) {
// Continue to throw it
throw new ServletException(ex);
* @param request Request instance
*/
@Override
- public void doAdminAddProduct (final HttpServletRequest request) {
+ public void doAdminAddProduct (final HttpServletRequest request) throws ServletException, ProductTitleAlreadyUsedException {
// Trace message
this.getLogger().trace(MessageFormat.format("request={0} - CALLED!", request)); //NOI18N
throw new NullPointerException("request is null");
}
- // Check if all fields are given
+ // Get title, price and category id
+ String title = request.getParameter(PizzaProductDatabaseConstants.COLUMN_TITLE);
+ String price = request.getParameter(PizzaProductDatabaseConstants.COLUMN_PRICE);
+ String category = request.getParameter(PizzaProductDatabaseConstants.COLUMN_CATEGORY);
+ Integer id = null;
+ Float p = null;
+
+ // Check all fields
+ if (title == null) {
+ // "title" not set
+ throw new IllegalArgumentException(MessageFormat.format("{0} is not set.", PizzaProductDatabaseConstants.COLUMN_TITLE));
+ } else if (title.isEmpty()) {
+ // Is left empty
+ throw new IllegalArgumentException(MessageFormat.format("{0} is empty.", PizzaProductDatabaseConstants.COLUMN_TITLE));
+ } else if (price == null) {
+ // "price" not set
+ throw new IllegalArgumentException(MessageFormat.format("{0} is not set.", PizzaProductDatabaseConstants.COLUMN_PRICE));
+ } else if (price.isEmpty()) {
+ // Is left empty
+ throw new IllegalArgumentException(MessageFormat.format("{0} is empty.", PizzaProductDatabaseConstants.COLUMN_PRICE));
+ } else if (category == null) {
+ // "title" not set
+ throw new IllegalArgumentException(MessageFormat.format("{0} is not set.", PizzaProductDatabaseConstants.COLUMN_CATEGORY));
+ } else if (category.isEmpty()) {
+ // Is left empty
+ throw new IllegalArgumentException(MessageFormat.format("{0} is empty.", PizzaProductDatabaseConstants.COLUMN_CATEGORY));
+ } else {
+ // "parent" is set, so check it
+ try {
+ id = Integer.parseInt(category);
+ p = Float.parseFloat(price);
+ } catch (final NumberFormatException e) {
+ // Not valid number
+ throw new IllegalArgumentException(e);
+ }
+ }
+
+ // Test on product title
+ try {
+ // Try to check if title is used already
+ if (this.isProductTitleUsed(title)) {
+ // Title already used
+ throw new ProductTitleAlreadyUsedException(request);
+ }
+ } catch (final IOException | SQLException | BadTokenException ex) {
+ throw new ServletException(ex);
+ }
+
+ try {
+ // The product is not found, so add it to database
+ this.productFrontend.addProduct(title, p, id);
+ } catch (final SQLException ex) {
+ // Continue to throw it
+ throw new ServletException(ex);
+ }
+
// Trace message
this.getLogger().trace("EXIT!"); //NOI18N
}
* @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;
+ public void addCategory (final String title, final Integer parent) throws SQLException;
/**
* An iterator on all categories
* @param parent Parent id or null if not selected
*/
@Override
- public void addCategory (final String title, final String parent) throws SQLException {
+ public void addCategory (final String title, final Integer parent) throws SQLException {
// Trace message
this.getLogger().trace(MessageFormat.format("title={0},parent={1} - CALLED!", title, parent));
return result;
}
+ /**
+ * Checks wether the given product title is already used.
+ *
+ * @param title Product title
+ * @return Whether the product title is already used
+ * @throws java.io.IOException If any IO error occurs
+ * @throws org.mxchange.jcore.exceptions.BadTokenException If a bad token was found in a file-based database backend's file ... ;-)
+ * @throws java.sql.SQLException If any SQL errors occur
+ */
+ @Override
+ public boolean isProductTitleUsed (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(PizzaProductDatabaseConstants.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.
private Product parseLineToProduct (final String line) {
throw new UnsupportedOperationException(MessageFormat.format("Not supported yet: line={0}", line)); //NOI18N
}
+
+ /**
+ * Adds product to database by given title, price and category id
+ * @param title Product title
+ * @param price Product price
+ * @param category Product category id
+ * @throws java.sql.SQLException If any SQL errors occur
+ */
+ @Override
+ public void addProduct (final String title, final Float price, final Integer category) throws SQLException {
+ // Trace message
+ this.getLogger().trace(MessageFormat.format("title={0},price={1},category={2} - CALLED!", title, price, category));
+
+ // Title should not be null
+ if (title == null) {
+ // Abort here
+ throw new NullPointerException("title is null");
+ } else if (price == null) {
+ // Abort here
+ throw new NullPointerException("price is null");
+ } else if (category == null) {
+ // Abort here
+ throw new NullPointerException("category is null");
+ }
+
+ // Clear dataset from previous usage
+ this.clearDataSet();
+
+ // Add title and parent
+ this.addToDataSet(PizzaProductDatabaseConstants.COLUMN_TITLE, title);
+ this.addToDataSet(PizzaProductDatabaseConstants.COLUMN_PRICE, price);
+ this.addToDataSet(PizzaProductDatabaseConstants.COLUMN_CATEGORY, category);
+
+ // 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!");
+ }
}
*/
public interface ProductFrontend extends DatabaseFrontend {
+ /**
+ * Adds product to database by given title, price and category id
+ * @param title Product title
+ * @param price Product price
+ * @param category Product category id
+ * @throws java.sql.SQLException If any SQL errors occur
+ */
+ public void addProduct (final String title, final Float price, final Integer category) throws SQLException;
+
/**
* An iterator on all products
*
* @throws java.sql.SQLException If any SQL errors occur
*/
public Iterator<Product> getProducts () throws IOException, BadTokenException, SQLException;
+
+ /**
+ * Checks wether the given product title is already used.
+ *
+ * @param title Product title
+ * @return Whether the product title is already used
+ * @throws java.io.IOException If any IO error occurs
+ * @throws org.mxchange.jcore.exceptions.BadTokenException If a bad token was found in a file-based database backend's file ... ;-)
+ * @throws java.sql.SQLException If any SQL errors occur
+ */
+ public boolean isProductTitleUsed (String title) throws IOException, SQLException, BadTokenException;
}
*/
public static final String COLUMN_AVAILABLE = "available";
+ /**
+ * Column name for "category"
+ */
+ public static final String COLUMN_CATEGORY = "category";
+
+ /**
+ * Column name for "price"
+ */
+ public static final String COLUMN_PRICE = "price";
+
+ /**
+ * Column name for "title"
+ */
+ public static final String COLUMN_TITLE = "title";
+
/**
* No instance from this class
*/
--- /dev/null
+/*
+ * 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.exceptions;
+
+import java.text.MessageFormat;
+import javax.servlet.http.HttpServletRequest;
+import org.mxchange.pizzaapplication.database.product.PizzaProductDatabaseConstants;
+
+/**
+ * An exception thrown when the given title is already used
+ *
+ * @author Roland Haeder
+ */
+public class ProductTitleAlreadyUsedException extends Exception {
+
+ /**
+ * Constructor with HttpServletRequest instance
+ *
+ * @param request A HttpServletRequest instance
+ */
+ public ProductTitleAlreadyUsedException (final HttpServletRequest request) {
+ // Call super constructor
+ super(MessageFormat.format("Title {0} is already used.", request.getParameter(PizzaProductDatabaseConstants.COLUMN_TITLE)));
+ }
+
+}
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%--<%@page errorPage="errorHandler.jsp" %>--%>
+<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@page import="java.util.Iterator"%>
<%@page import="java.util.Map"%>
<%@page import="org.mxchange.jcore.contact.Gender"%>
<%@page import="org.mxchange.pizzaapplication.product.Product"%>
<%@page import="org.mxchange.pizzaapplication.application.PizzaApplication"%>
<%@page import="org.mxchange.pizzaapplication.application.PizzaServiceApplication"%>
-<%@page contentType="text/html" pageEncoding="UTF-8"%>
+<%@page import="org.mxchange.pizzaapplication.database.product.PizzaProductDatabaseConstants"%>
<%
// Init application instance
</div>
<div class="table_right">
- <input type="text" name="title" size="10" maxlength="255" />
+ <input type="text" name="<%=PizzaProductDatabaseConstants.COLUMN_TITLE%>" size="10" maxlength="255" />
</div>
<div class="clear"></div>
</div>
<div class="table_right">
- <input type="text" name="price" size="10" maxlength="255" />
+ <input type="text" name="<%=PizzaProductDatabaseConstants.COLUMN_PRICE%>" size="10" maxlength="255" />
</div>
<div class="clear"></div>
+++ /dev/null
-<%--
- Document : order
- Created on : 07.08.2015, 14:58:21
- Author : Roland Haeder
---%>
-
-<%--<%@page errorPage="errorHandler.jsp" %>--%>
-<%@page contentType="text/html" pageEncoding="UTF-8"%>
-<%@page import="org.mxchange.pizzaapplication.application.PizzaServiceApplication"%>
-<%@page import="org.mxchange.pizzaapplication.application.PizzaApplication"%>
-<%@page import="org.mxchange.pizzaapplication.beans.CustomerBean" %>
-<%@page import="org.mxchange.pizzaapplication.product.Product"%>
-
-<%
- // Init application instance
- PizzaApplication app = PizzaServiceApplication.getInstance(application);
-
- // Is it post?
- if ("POST".equals(request.getMethod())) { //NOI18N
- // Handle saving customer data and such things
-
- // Redirect to proper URL
- // @TODO Commented out for developing:
- //response.sendRedirect(request.getContextPath() + "/finished.jsp");
- }
-%>
-
-<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
-<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="de" lang="de">
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
- <link rel="stylesheet" href="<%=request.getContextPath()%>/style.css" type="text/css"/>
- <title><%=PizzaServiceApplication.MAIN_TITLE%> - Form-Handler - Administration</title>
- </head>
-
- <body>
- <div id="title">
- <h1><%=PizzaServiceApplication.MAIN_TITLE%> - Form-Handler - Administration</h1>
- </div>
-
- <jsp:include page="/static/admin/menu.jsp" flush="true" />
-
- <div id="content_outer">
- <div id="content_title">
- <h2>Bitte nicht direkt aufrufen:</h2>
- </div>
-
- <div id="content">
- Bitte rufen Sie diese Seite nicht direkt auf.
- </div>
- </div>
- </body>
-</html>
PizzaApplication app = PizzaServiceApplication.getInstance(application);
%>
-<select name="<%=PizzaCategoryDatabaseConstants.COLUMN_ID%>" size="1">
+<select name="category" size="1">
<c:forEach var="category" items="<%=app.getCategories()%>">
<option value="${category.getId()}">${category.decodedTitle()}</option>
</c:forEach>