--- /dev/null
+SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
+SET time_zone = "+00:00";
+
+/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
+/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
+/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
+/*!40101 SET NAMES utf8 */;
+
+
+DROP TABLE IF EXISTS `category`;
+CREATE TABLE IF NOT EXISTS `category` (
+`id` bigint(20) unsigned NOT NULL COMMENT 'Primary key',
+ `title` varchar(255) NOT NULL COMMENT 'Category title',
+ `parent` bigint(20) unsigned DEFAULT NULL COMMENT 'Parent category'
+) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='Categories' AUTO_INCREMENT=3 ;
+
+DROP TABLE IF EXISTS `contacts`;
+CREATE TABLE IF NOT EXISTS `contacts` (
+`id` bigint(20) unsigned NOT NULL COMMENT 'Primary key',
+ `own_contact` tinyint(1) NOT NULL DEFAULT '0' COMMENT 'Whether own contact',
+ `gender` varchar(10) NOT NULL DEFAULT 'UNKNOWN' COMMENT 'Gender',
+ `surname` varchar(100) NOT NULL COMMENT 'Surname',
+ `family_name` varchar(100) NOT NULL COMMENT 'Family name',
+ `company_name` varchar(255) DEFAULT NULL COMMENT 'Company name',
+ `street` varchar(255) DEFAULT NULL COMMENT 'Street name',
+ `house_number` smallint(5) unsigned DEFAULT NULL COMMENT 'House number',
+ `city` varchar(100) DEFAULT NULL COMMENT 'City name',
+ `zip_code` smallint(5) unsigned DEFAULT NULL COMMENT 'ZIP code',
+ `country_code` char(2) DEFAULT NULL COMMENT 'Country code',
+ `phone_number` varchar(100) DEFAULT NULL COMMENT 'Phone number',
+ `cellphone_number` varchar(100) DEFAULT NULL COMMENT 'Cellphone number',
+ `fax_number` varchar(100) DEFAULT NULL COMMENT 'Fax number',
+ `email_address` varchar(100) DEFAULT NULL COMMENT 'Email addres',
+ `birthday` date DEFAULT NULL COMMENT 'Birth day',
+ `comment` tinytext NOT NULL COMMENT 'Comment',
+ `created` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT 'Entry created',
+ `updated` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00' ON UPDATE CURRENT_TIMESTAMP
+) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='Contacts data' AUTO_INCREMENT=1 ;
+
+DROP TABLE IF EXISTS `products`;
+CREATE TABLE IF NOT EXISTS `products` (
+`id` bigint(20) unsigned NOT NULL COMMENT 'Primary key',
+ `category` bigint(20) unsigned DEFAULT NULL COMMENT 'Category id',
+ `title` varchar(255) NOT NULL COMMENT 'Title of product',
+ `price` decimal(20,2) unsigned NOT NULL COMMENT 'Product price',
+ `available` tinyint(1) NOT NULL DEFAULT '0' COMMENT 'Whether product is available'
+) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='Products' AUTO_INCREMENT=2 ;
+
+
+ALTER TABLE `category`
+ ADD PRIMARY KEY (`id`), ADD KEY `parent` (`parent`);
+
+ALTER TABLE `contacts`
+ ADD PRIMARY KEY (`id`);
+
+ALTER TABLE `products`
+ ADD PRIMARY KEY (`id`), ADD KEY `category` (`category`);
+
+
+ALTER TABLE `category`
+MODIFY `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT 'Primary key',AUTO_INCREMENT=3;
+ALTER TABLE `contacts`
+MODIFY `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT 'Primary key';
+ALTER TABLE `products`
+MODIFY `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT 'Primary key',AUTO_INCREMENT=2;
+/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
+/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
+/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
public void setValueInSession (final HttpSession session, final String key, final Object value);
/**
- * Some "getter" for a an array of all products
+ * Some "getter" for a an array of only available products
*
+ * @return Only available products
+ * @throws javax.servlet.ServletException If anything went wrong
+ */
+ public Iterator<Product> getAvailableProducts () throws ServletException;
+
+ /**
+ * Some "getter" for a an array of all products
+ *
* @return All products
* @throws javax.servlet.ServletException If anything went wrong
*/
- public Iterator<Product> getProducts () throws ServletException;
+ public Iterator<Product> getAllProducts () throws ServletException;
/**
* Some "getter" for a an array of all categories
// Init/declare total price and iterator
int totalAmount = 0;
- Iterator<Product> iterator = this.getProducts();
+ Iterator<Product> iterator = this.getAvailableProducts();
// "Walk" over all products
while (iterator.hasNext()) {
float totalPrice = 0.00f;
// Get iterator
- Iterator<Product> iterator = this.getProducts();
+ Iterator<Product> iterator = this.getAvailableProducts();
// "Walk" over all products
while (iterator.hasNext()) {
*/
@Override
public String getPrintableProduktAvailability (final Product product) {
- throw new UnsupportedOperationException(MessageFormat.format("Not supported yet: product={0}", product)); //NOI18N
+ // Trace message
+ this.getLogger().trace(MessageFormat.format("product={0} - CALLED!", product));
+
+ // Is it null?
+ if (product == null) {
+ // Should not be null
+ throw new NullPointerException("product is null");
+ }
+
+ // Get availability
+ if (product.getAvailable() == true) {
+ // Is available
+ return "Ja";
+ } else {
+ // Not, not for public
+ return "Nein";
+ }
}
/**
return this.convertNullToEmpty(value);
}
+ /**
+ * Some "getter" for a an array of only available products
+ *
+ * @return All products
+ */
+ @Override
+ public Iterator<Product> getAvailableProducts () throws ServletException {
+ try {
+ // Ask frontend for a list of products
+ return this.productFrontend.getAvailableProducts();
+ } catch (final IOException | BadTokenException | SQLException ex) {
+ throw new ServletException(ex);
+ }
+ }
+
/**
* Some "getter" for a an array of all products
*
* @return All products
*/
@Override
- public Iterator<Product> getProducts () throws ServletException {
+ public Iterator<Product> getAllProducts () throws ServletException {
try {
// Ask frontend for a list of products
- return this.productFrontend.getProducts();
+ return this.productFrontend.getAllProducts();
} catch (final IOException | BadTokenException | SQLException ex) {
throw new ServletException(ex);
}
this.getLogger().trace(MessageFormat.format("request={0},session={1} - CALLED!", request, session)); //NOI18N
// Init iterator
- Iterator<Product> iterator = this.getProducts();
+ Iterator<Product> iterator = this.getAvailableProducts();
// "Walk" over all products
while (iterator.hasNext()) {
try {
// Get iterator
- iterator = this.getProducts();
+ iterator = this.getAvailableProducts();
} catch (final ServletException ex) {
this.abortProgramWithException(ex);
}
String title = request.getParameter(PizzaProductDatabaseConstants.COLUMN_TITLE);
String price = request.getParameter(PizzaProductDatabaseConstants.COLUMN_PRICE);
String category = request.getParameter(PizzaProductDatabaseConstants.COLUMN_CATEGORY);
- Integer id = null;
+ String available = request.getParameter(PizzaProductDatabaseConstants.COLUMN_AVAILABLE);
+
+ Long id = null;
Float p = null;
+ Boolean a = null;
// Check all fields
if (title == null) {
} 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);
- }
+ } else if (available == null) {
+ // "title" not set
+ throw new IllegalArgumentException(MessageFormat.format("{0} is not set.", PizzaProductDatabaseConstants.COLUMN_AVAILABLE));
+ } else if (available.isEmpty()) {
+ // Is left empty
+ throw new IllegalArgumentException(MessageFormat.format("{0} is empty.", PizzaProductDatabaseConstants.COLUMN_AVAILABLE));
+ } else if ((!"true".equals(available)) && (!"false".equals(available))) {
+ // Invalid value
+ throw new IllegalArgumentException(MessageFormat.format("{0} is invalid: {1}", PizzaProductDatabaseConstants.COLUMN_AVAILABLE, available));
}
+ // Parse numbers
+ try {
+ id = Long.parseLong(category);
+ p = Float.parseFloat(price);
+ } catch (final NumberFormatException e) {
+ // Not valid number
+ throw new IllegalArgumentException(e);
+ }
+
+ // Parse boolean
+ a = Boolean.parseBoolean(available);
+
// Test on product title
try {
// Try to check if title is used already
try {
// The product is not found, so add it to database
- this.productFrontend.addProduct(title, p, id);
+ this.productFrontend.addProduct(title, p, id, a);
} catch (final SQLException ex) {
// Continue to throw it
throw new ServletException(ex);
return 1;
}
+ /**
+ * Decodes the UTF8-encoded title
+ *
+ * @return Decoded title
+ */
+ @Override
+ public final String decodedTitle () throws UnsupportedEncodingException {
+ // Get title
+ byte[] t = this.getTitle().getBytes();
+
+ // Decode it
+ return new String(t, "UTF-8");
+ }
+
/**
* Id number of category
* @return the id
public final void setTitle (final String title) {
this.title = title;
}
-
- /**
- * Decodes the UTF8-encoded title
- *
- * @return Decoded title
- */
- @Override
- public final String decodedTitle () throws UnsupportedEncodingException {
- // Get title
- byte[] t = this.getTitle().getBytes();
-
- // Decode it
- return new String(t, "UTF-8");
- }
}
this.getLogger().trace(MessageFormat.format("line={0} - CALLED!", line)); //NOI18N
// Call inner method
- Category category = this.parseLineToCategory(line);
+ Category category = this.parseLine(line);
// Debug message
this.getLogger().trace(MessageFormat.format("category={0} - EXIT!", category)); //NOI18N
* @param line Raw, decoded line from a file-based backend
* @return A Category instance from given line
*/
- private Category parseLineToCategory (final String line) {
+ private Category parseLine (final String line) {
throw new UnsupportedOperationException(MessageFormat.format("Not supported yet: line={0}", line)); //NOI18N
}
}
import org.mxchange.jcore.exceptions.BadTokenException;
import org.mxchange.jcore.exceptions.UnsupportedDatabaseBackendException;
import org.mxchange.pizzaapplication.database.product.PizzaProductDatabaseConstants;
+import org.mxchange.pizzaapplication.product.PizzaProduct;
import org.mxchange.pizzaapplication.product.Product;
/**
@Override
@SuppressWarnings ("unchecked")
- public Iterator<Product> getProducts () throws IOException, BadTokenException, SQLException {
+ public Iterator<Product> getAvailableProducts () throws IOException, BadTokenException, SQLException {
// Trace message
this.getLogger().trace("CALLED!"); //NOI18N
return (Iterator<Product>) iterator;
}
+ /**
+ * An iterator on all products
+ *
+ * @return Iterator on all products
+ * @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
+ @SuppressWarnings ("unchecked")
+ public Iterator<Product> getAllProducts () throws IOException, BadTokenException, SQLException {
+ // Trace message
+ this.getLogger().trace("CALLED!"); //NOI18N
+
+ // Instance search criteria
+ SearchableCritera critera = new SearchCriteria();
+
+ // Run the query
+ Result<? extends Storeable> result = this.getBackend().doSelectByCriteria(critera);
+
+ // Debug message
+ this.getLogger().debug(MessageFormat.format("result={0}", result)); //NOI18N
+
+ // Get iterator
+ Iterator<?> iterator = result.iterator();
+
+ // Trace message
+ this.getLogger().trace(MessageFormat.format("iterator={0} - EXIT!", iterator)); //NOI18N
+
+ // Get iterator and return it
+ return (Iterator<Product>) iterator;
+ }
+
/**
* Gets a Result back from given ResultSet instance
*
// "Walk" through all entries
while (resultSet.next()) {
- // Unwrap whole object
- Product product = resultSet.unwrap(Product.class);
+ // Get id, title and parent id
+ Long id = resultSet.getLong(PizzaProductDatabaseConstants.COLUMN_ID);
+ String title = resultSet.getString(PizzaProductDatabaseConstants.COLUMN_TITLE);
+ Float price = resultSet.getFloat(PizzaProductDatabaseConstants.COLUMN_PRICE);
+ Long category = resultSet.getLong(PizzaProductDatabaseConstants.COLUMN_CATEGORY);
+ Boolean available = resultSet.getBoolean(PizzaProductDatabaseConstants.COLUMN_AVAILABLE);
+
+ // Debug message
+ this.getLogger().debug(MessageFormat.format("id={0},title={1},category={2},available={3}", id, title, category, available));
+
+ // Instance new object
+ Product product = new PizzaProduct(id, title, price, category, available);
// Debug log
this.getLogger().debug(MessageFormat.format("product={0}", product));
this.getLogger().trace(MessageFormat.format("line={0} - CALLED!", line)); //NOI18N
// Call inner method
- Product product = this.parseLineToProduct(line);
+ Product product = this.parseLine(line);
// Debug message
this.getLogger().trace(MessageFormat.format("product={0} - EXIT!", product)); //NOI18N
* @param line
* @return A Product instance from given line
*/
- private Product parseLineToProduct (final String line) {
+ private Product parseLine (final String line) {
throw new UnsupportedOperationException(MessageFormat.format("Not supported yet: line={0}", line)); //NOI18N
}
* @param title Product title
* @param price Product price
* @param category Product category id
+ * @param available Availability of product (selectable by customer)
* @throws java.sql.SQLException If any SQL errors occur
*/
@Override
- public void addProduct (final String title, final Float price, final Integer category) throws SQLException {
+ public void addProduct (final String title, final Float price, final Long category, final Boolean available) throws SQLException {
// Trace message
this.getLogger().trace(MessageFormat.format("title={0},price={1},category={2} - CALLED!", title, price, category));
} else if (category == null) {
// Abort here
throw new NullPointerException("category is null");
+ } else if (available == null) {
+ // Abort here
+ throw new NullPointerException("available is null");
}
// Clear dataset from previous usage
this.addToDataSet(PizzaProductDatabaseConstants.COLUMN_TITLE, title);
this.addToDataSet(PizzaProductDatabaseConstants.COLUMN_PRICE, price);
this.addToDataSet(PizzaProductDatabaseConstants.COLUMN_CATEGORY, category);
+ this.addToDataSet(PizzaProductDatabaseConstants.COLUMN_AVAILABLE, available);
// Handle this over to the backend
Result<? extends Storeable> result = this.doInsertDataSet();
* @param title Product title
* @param price Product price
* @param category Product category id
+ * @param available Availability of product (selectable by customer)
* @throws java.sql.SQLException If any SQL errors occur
*/
- public void addProduct (final String title, final Float price, final Integer category) throws SQLException;
+ public void addProduct (final String title, final Float price, final Long category, final Boolean available) throws SQLException;
/**
* An iterator on all products
* @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 Iterator<Product> getProducts () throws IOException, BadTokenException, SQLException;
+ public Iterator<Product> getAllProducts () throws IOException, BadTokenException, SQLException;
+
+ /**
+ * An iterator on all products
+ *
+ * @return Iterator on all products
+ * @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 Iterator<Product> getAvailableProducts () throws IOException, BadTokenException, SQLException;
/**
* Checks wether the given product title is already used.
*/
public static final String COLUMN_CATEGORY = "category";
+ /**
+ * Column name for "id"
+ */
+ public static final String COLUMN_ID = "id";
+
/**
* Column name for "price"
*/
*/
package org.mxchange.pizzaapplication.product;
-import org.mxchange.pizzaapplication.category.Category;
+import java.text.MessageFormat;
+import java.util.Objects;
import org.mxchange.jcore.BaseFrameworkSystem;
/**
* @author Roland Haeder
*/
public class PizzaProduct extends BaseFrameworkSystem implements Product {
+ /**
+ * Availability of product
+ */
+ private Boolean available;
+
/**
* Product category
*/
- private Category category;
+ private Long category;
/**
* Id number of product
*/
private String title;
+
/**
* Constructor for products with a name and a price.
*
* @param id Id number of product
* @param title Name of product
* @param price Price
+ * @deprecated Please use constructor with category and available
*/
+ @Deprecated
public PizzaProduct (final Long id, final String title, final float price) {
this.setId(id);
this.setTitle(title);
this.setPrice(price);
}
+ /**
+ * Constructor will all required data
+ *
+ * @param id Id number of product
+ * @param title Name of product
+ * @param price Price
+ * @param category Category id
+ * @param available Availability (selectable by customer)
+ */
+ public PizzaProduct (final Long id, final String title, final Float price, final Long category, final Boolean available) {
+ // Set all here
+ this.setId(id);
+ this.setTitle(title);
+ this.setPrice(price);
+ this.setCategory(category);
+ this.setAvailable(available);
+ }
+
+ /**
+ * Getter for product availability
+ *
+ * @return Product availability
+ */
+ @Override
+ public final Boolean getAvailable () {
+ return this.available;
+ }
+
+ /**
+ * Setter for product availability
+ *
+ * @param available Product availability
+ */
+ @Override
+ public final void setAvailable (final Boolean available) {
+ this.available = available;
+ }
+
/**
* Getter for product category
*
* @return Product category
*/
@Override
- public final Category getCategory () {
+ public final Long getCategory () {
return this.category;
}
* @param category Product category
*/
@Override
- public final void setCategory (final Category category) {
+ public final void setCategory (final Long category) {
this.category = category;
}
public final void setTitle (final String title) {
this.title = title;
}
+
+ /**
+ * Compares two categories with each other
+ *
+ * @param product Product comparator
+ * @return Comparison value
+ */
+ @Override
+ public int compareTo (final Product product) {
+ // Trace message
+ this.getLogger().trace(MessageFormat.format("product={0} - CALLED!", product));
+
+ // category should not be null
+ if (product == null) {
+ throw new NullPointerException("product is null");
+ }
+
+ // Debug message
+ this.getLogger().debug(MessageFormat.format("this.id={0},product.id={1}", this.getId(), product.getId()));
+
+ // Is the id the same?
+ if (Objects.equals(this.getId(), product.getId())) {
+ // Same id, means same category
+ return 0;
+ } else if (this.getId() > product.getId()) {
+ // This id is larger than compared to
+ return -1;
+ }
+
+ // The other id is larger
+ return 1;
+ }
}
*/
package org.mxchange.pizzaapplication.product;
-import org.mxchange.pizzaapplication.category.Category;
import org.mxchange.jcore.database.storage.Storeable;
/**
+ * An interface for in database storeable products
*
* @author Roland Haeder
*/
-public interface Product extends Storeable {
+public interface Product extends Storeable, Comparable<Product> {
/**
* Getter for id number, suitable for form fields.
*
*
* @return Product category
*/
- public Category getCategory ();
+ public Long getCategory ();
/**
* Setter for product category
*
* @param category Product category
*/
- public void setCategory (final Category category);
+ public void setCategory (final Long category);
+
+ /**
+ * Getter for product availability
+ *
+ * @return Product availability
+ */
+ public Boolean getAvailable ();
+
+ /**
+ * Setter for product availability
+ *
+ * @param available Product availability
+ */
+ public void setAvailable (final Boolean available);
+
+ /**
+ * Compare method
+ * @param category Category to compare to
+ */
+ @Override
+ public int compareTo (final Product category);
}
<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%> - Bestellung anzeigen</title>
+ <title><%=PizzaServiceApplication.MAIN_TITLE%> - Produkte</title>
</head>
<body>
<div id="title">
- <h1><%=PizzaServiceApplication.MAIN_TITLE%> - Bestellung anzeigen</h1>
+ <h1><%=PizzaServiceApplication.MAIN_TITLE%> - Produkte</h1>
</div>
<jsp:include page="/static/admin/menu.jsp" flush="true" />
<thead class="table_header">
<tr>
<th class="table_header_column">
- Produktname: (Schlüssel)
+ Produktnummer:
</th>
<th class="table_header_column">
Produktbezeichnung:
</thead>
<tbody class="table_body">
- <c:forEach var="product" items="<%=app.getProducts()%>">
+ <c:forEach var="product" items="<%=app.getAllProducts()%>">
<tr>
<td>
${product.getId()}
<div class="clear"></div>
</div>
+
+ <div class="data_row">
+ <div class="table_left">
+ Verfügbar?
+ </div>
+
+ <div class="table_right">
+ <select name="available" size="1">
+ <option value="true">Ja</option>
+ <option value="false">Nein</option>
+ </select>
+ </div>
+
+ <div class="clear"></div>
+ </div>
</fieldset>
<div class="table_footer">
<tbody class="table_body">
<%
// Get Iterator
- Iterator<Product> iterator = app.getProducts();
+ Iterator<Product> iterator = app.getAvailableProducts();
// "Walk" through all products and unmark them as ordered
while (iterator.hasNext()) {
// Is it post?
if ("POST".equals(request.getMethod())) { //NOI18N
// Get Iterator
- Iterator<Product> iterator = app.getProducts();
+ Iterator<Product> iterator = app.getAvailableProducts();
// "Walk" through all products and unmark them as ordered
while (iterator.hasNext()) {
<tbody class="table_body">
<%
// Get Iterator
- Iterator<Product> iterator = app.getProducts();
+ Iterator<Product> iterator = app.getAvailableProducts();
// "Walk" through all products and unmark them as ordered
while (iterator.hasNext()) {
<tbody class="table_body">
<%
// Get Iterator
- Iterator<Product> iterator = app.getProducts();
+ Iterator<Product> iterator = app.getAvailableProducts();
// "Walk" through all products and unmark them as ordered
while (iterator.hasNext()) {