From: Roland Haeder Date: Thu, 13 Aug 2015 08:41:26 +0000 (+0200) Subject: Continued with project: X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=44730f88dd8248d9d433ec664a7416c194806572;p=pizzaservice-war.git Continued with project: - Added new frontend class and interface for categories + rewritings - Used instead of for-loop where possible, else a while() loop has to be used with iterator.hasNext() and iterator.next() 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 30e1368d..4c4335c9 100644 --- a/src/java/org/mxchange/pizzaapplication/application/PizzaServiceApplication.java +++ b/src/java/org/mxchange/pizzaapplication/application/PizzaServiceApplication.java @@ -34,6 +34,8 @@ import org.mxchange.pizzaapplication.BasePizzaServiceSystem; import org.mxchange.pizzaapplication.category.Category; import org.mxchange.pizzaapplication.customer.Customer; import org.mxchange.pizzaapplication.customer.PizzaServiceCustomer; +import org.mxchange.pizzaapplication.database.frontend.category.CategoryFrontend; +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.product.Product; @@ -54,6 +56,11 @@ public class PizzaServiceApplication extends BasePizzaServiceSystem implements P */ private ProductFrontend productFrontend; + /** + * Frontend for categories + */ + private CategoryFrontend categoryFrontend; + /** * Some singleton getter for this instance. If the instance is not set in * given application, it will be created. @@ -536,8 +543,9 @@ public class PizzaServiceApplication extends BasePizzaServiceSystem implements P * @return All categories */ @Override - public Iterator getCategories () { - throw new UnsupportedOperationException("Needs refacturing ..."); + public Iterator getCategories () throws IOException, BadTokenException { + // Ask frontend for a list of categories + return this.categoryFrontend.getCategories(); } /** @@ -1047,7 +1055,11 @@ public class PizzaServiceApplication extends BasePizzaServiceSystem implements P * Initializes database frontends. */ private void initDatabaseFrontends () throws UnsupportedDatabaseBackendException, SQLException { + // Product frontend this.productFrontend = new PizzaProductDatabaseFrontend(); + + // Category frontend + this.categoryFrontend = new PizzaCategoryDatabaseFrontend(); } /** diff --git a/src/java/org/mxchange/pizzaapplication/category/Category.java b/src/java/org/mxchange/pizzaapplication/category/Category.java index c1b0fdf7..6e148362 100644 --- a/src/java/org/mxchange/pizzaapplication/category/Category.java +++ b/src/java/org/mxchange/pizzaapplication/category/Category.java @@ -16,12 +16,12 @@ */ package org.mxchange.pizzaapplication.category; -import org.mxchange.jcore.FrameworkInterface; +import org.mxchange.jcore.database.storage.Storeable; /** * An interface for categories * * @author Roland Haeder */ -public interface Category extends FrameworkInterface { +public interface Category extends Storeable { } diff --git a/src/java/org/mxchange/pizzaapplication/database/frontend/category/CategoryFrontend.java b/src/java/org/mxchange/pizzaapplication/database/frontend/category/CategoryFrontend.java new file mode 100644 index 00000000..baa6e14f --- /dev/null +++ b/src/java/org/mxchange/pizzaapplication/database/frontend/category/CategoryFrontend.java @@ -0,0 +1,40 @@ +/* + * Copyright (C) 2015 Roland Häder + * + * 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.database.frontend.category; + +import java.io.IOException; +import java.util.Iterator; +import org.mxchange.jcore.database.frontend.DatabaseFrontend; +import org.mxchange.jcore.exceptions.BadTokenException; +import org.mxchange.pizzaapplication.category.Category; + +/** + * An interface for product database frontends + * + * @author Roland Häder + */ +public interface CategoryFrontend extends DatabaseFrontend { + + /** + * An iterator on all categories + * + * @return Iterator on all categories + * @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 ... ;-) + */ + public Iterator getCategories () throws IOException, BadTokenException; +} diff --git a/src/java/org/mxchange/pizzaapplication/database/frontend/category/PizzaCategoryDatabaseFrontend.java b/src/java/org/mxchange/pizzaapplication/database/frontend/category/PizzaCategoryDatabaseFrontend.java new file mode 100644 index 00000000..64ee9609 --- /dev/null +++ b/src/java/org/mxchange/pizzaapplication/database/frontend/category/PizzaCategoryDatabaseFrontend.java @@ -0,0 +1,135 @@ +/* + * 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.database.frontend.category; + +import java.io.IOException; +import java.sql.SQLException; +import java.text.MessageFormat; +import java.util.Iterator; +import org.mxchange.jcore.criteria.searchable.SearchCriteria; +import org.mxchange.jcore.criteria.searchable.SearchableCritera; +import org.mxchange.jcore.database.frontend.BaseDatabaseFrontend; +import org.mxchange.jcore.database.result.Result; +import org.mxchange.jcore.database.storage.Storeable; +import org.mxchange.jcore.exceptions.BadTokenException; +import org.mxchange.jcore.exceptions.UnsupportedDatabaseBackendException; +import org.mxchange.pizzaapplication.category.Category; + +/** + * Stores and retrieves Contact instances + * + * @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 + * @throws java.sql.SQLException If any SQL error occurs + */ + public PizzaCategoryDatabaseFrontend () throws UnsupportedDatabaseBackendException, SQLException { + // Trace message + this.getLogger().trace("CALLED!"); //NOI18N + + // Set "table" name + this.setTableName("category"); //NOI18N + + // Initalize backend + this.initBackend(); + } + + /** + * Shuts down the database layer + * @throws java.sql.SQLException If an SQL error occurs + * @throws java.io.IOException If any IO error occurs + */ + @Override + public void doShutdown () throws SQLException, IOException { + // Trace message + this.getLogger().trace("CALLED!"); //NOI18N + + // Shutdown backend + this.getBackend().doShutdown(); + + // Trace message + this.getLogger().trace("EXIT!"); //NOI18N + } + + /** + * 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 ... ;-) + */ + @Override + @SuppressWarnings ("unchecked") + public Iterator getCategories () throws IOException, BadTokenException { + // Trace message + this.getLogger().trace("CALLED!"); //NOI18N + + // Instance search criteria + SearchableCritera critera = new SearchCriteria(); + + // Run the query + Result 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) iterator; + } + + /** + * Parses given line from database backend into a Storeable instance. Please + * note that not all backends need this. + * + * @param line Line from database backend + * @return A Storeable instance + */ + @Override + public Storeable parseLineToStoreable (final String line) throws BadTokenException { + // Trace message + this.getLogger().trace(MessageFormat.format("line={0} - CALLED!", line)); //NOI18N + + // Call inner method + Category category = this.parseLineToCategory(line); + + // Debug message + this.getLogger().trace(MessageFormat.format("category={0} - EXIT!", category)); //NOI18N + + // Return it + return category; + } + + /** + * Parses given line to a Category instance + * + * @param line Raw, decoded line from a file-based backend + * @return A Category instance from given line + */ + private Category parseLineToCategory (final String line) { + throw new UnsupportedOperationException(MessageFormat.format("Not supported yet: line={0}", line)); //NOI18N + } +} 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 6830a0f8..1fc6fe5f 100644 --- a/src/java/org/mxchange/pizzaapplication/database/frontend/product/PizzaProductDatabaseFrontend.java +++ b/src/java/org/mxchange/pizzaapplication/database/frontend/product/PizzaProductDatabaseFrontend.java @@ -55,6 +55,9 @@ public class PizzaProductDatabaseFrontend extends BaseDatabaseFrontend implement /** * Shuts down the database layer + * + * @throws java.sql.SQLException If an SQL error occurs + * @throws java.io.IOException If any IO error occurs */ @Override public void doShutdown () throws SQLException, IOException { @@ -72,6 +75,8 @@ public class PizzaProductDatabaseFrontend extends BaseDatabaseFrontend implement * An iterator on all products * * @return Iterator on all products + * @throws org.mxchange.jcore.exceptions.BadTokenException + * @throws java.io.IOException If any IO error occurs */ @Override @SuppressWarnings ("unchecked") diff --git a/web/finished.jsp b/web/finished.jsp index c2ca53b6..3a2430b4 100644 --- a/web/finished.jsp +++ b/web/finished.jsp @@ -65,8 +65,13 @@ <% - // "Walk" over all products - for (final Product product : app.getProducts()) { + // Get Iterator + Iterator iterator = app.getProducts(); + + // "Walk" through all products and unmark them as ordered + while (iterator.hasNext()) { + // Get product instance + Product product = iterator.next(); %> diff --git a/web/form_handler/do_preview.jsp b/web/form_handler/do_preview.jsp index f6e35822..a1b20cb0 100644 --- a/web/form_handler/do_preview.jsp +++ b/web/form_handler/do_preview.jsp @@ -20,8 +20,17 @@ // Is it post? if ("POST".equals(request.getMethod())) { //NOI18N + // Get Iterator + Iterator iterator = app.getProducts(); + // "Walk" through all products and unmark them as ordered - for (final Product product : app.getUnmarkedProducts(session)) { + while (iterator.hasNext()) { + // Get product instance + Product product = iterator.next(); + + // Mark product as not ordered + app.unmarkProductAsOrdered(product, session); + // Is it choosen and amount set? if (app.isProductChoosen(product, request, session)) { // Then mark it as choosen @@ -29,7 +38,6 @@ } else { // Unmark it app.unmarkProductAsChoosen(product, session); - app.unmarkProductAsOrdered(product, session); } } // Redirect to proper URL diff --git a/web/index.jsp b/web/index.jsp index 05afdbfb..7dd9458f 100644 --- a/web/index.jsp +++ b/web/index.jsp @@ -58,8 +58,14 @@ <% + // Get Iterator + Iterator iterator = app.getProducts(); + // "Walk" through all products and unmark them as ordered - for (final Product product : app.getProducts()) { + while (iterator.hasNext()) { + // Get product instance + Product product = iterator.next(); + // Unmark as ordered app.unmarkProductAsOrdered(product, session); %> diff --git a/web/preview.jsp b/web/preview.jsp index dd300782..fb93d386 100644 --- a/web/preview.jsp +++ b/web/preview.jsp @@ -65,8 +65,14 @@ <% + // Get Iterator + Iterator iterator = app.getProducts(); + // "Walk" through all products and unmark them as ordered - for (final Product product : app.getProducts()) { + while (iterator.hasNext()) { + // Get product instance + Product product = iterator.next(); + // Unmark it as ordered app.unmarkProductAsOrdered(product, session); %>