From: Roland Haeder Date: Thu, 27 Aug 2015 21:52:56 +0000 (+0200) Subject: Continued with project: X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=8b74c6243b2f46a35037f9e5a6881a0dc8e4844e;p=pizzaservice-war.git Continued with project: - added method getProduct() - added method getLast() - added method getAll() (again?) - Used BasketBean instead of Basket - Yepp, JSPs don't support @Inject and @PostConstruct ... :-( - added getLastNumRows() which deligates this call to jcore's method - implemented MiniBasketTag (a bit broken) - added missing language resourced - updated jcore.jar Signed-off-by:Roland Häder --- diff --git a/lib/jcore.jar b/lib/jcore.jar index 11b967e3..d787a200 100644 Binary files a/lib/jcore.jar and b/lib/jcore.jar differ diff --git a/src/java/org/mxchange/localization/bundle_de_DE.properties b/src/java/org/mxchange/localization/bundle_de_DE.properties index 376c5587..35d75a9f 100644 --- a/src/java/org/mxchange/localization/bundle_de_DE.properties +++ b/src/java/org/mxchange/localization/bundle_de_DE.properties @@ -17,3 +17,6 @@ BaseContact.gender.male.text=Herr BaseContact.gender.female.text=Frau BaseContact.gender.company.text=Firma MiniBasketTag.basket_is_empty=Der Warenkorb ist leer. +MiniBasketTag.last_item=Zuletzt hinzugefügt: {0} +MiniBasketTag.additional_items=Es befinden sich noch {0} weitere Produkte im Warenkorb. +MiniBasketTag.to_basket=Zum Warenkorb diff --git a/src/java/org/mxchange/localization/bundle_en_US.properties b/src/java/org/mxchange/localization/bundle_en_US.properties index d61b69f4..07e8210f 100644 --- a/src/java/org/mxchange/localization/bundle_en_US.properties +++ b/src/java/org/mxchange/localization/bundle_en_US.properties @@ -17,3 +17,6 @@ BaseContact.gender.male.text=Mr. BaseContact.gender.female.text=Mrs. BaseContact.gender.company.text=Company MiniBasketTag.basket_is_empty=Der Warenkorb ist leer. +MiniBasketTag.last_item=Zuletzt hinzugefügt: {0} +MiniBasketTag.additional_items=Es befinden sich noch {0} weitere Produkte im Warenkorb. +MiniBasketTag.to_basket=Zum Warenkorb diff --git a/src/java/org/mxchange/pizzaapplication/application/PizzaApplication.java b/src/java/org/mxchange/pizzaapplication/application/PizzaApplication.java index dcb6060f..8515db3f 100644 --- a/src/java/org/mxchange/pizzaapplication/application/PizzaApplication.java +++ b/src/java/org/mxchange/pizzaapplication/application/PizzaApplication.java @@ -30,6 +30,7 @@ import org.mxchange.jcore.exceptions.UnsupportedDatabaseBackendException; import org.mxchange.pizzaapplication.category.Category; import org.mxchange.pizzaapplication.exceptions.CategoryTitleAlreadyUsedException; import org.mxchange.pizzaapplication.exceptions.ProductTitleAlreadyUsedException; +import org.mxchange.pizzaapplication.item.AddableBasketItem; import org.mxchange.pizzaapplication.product.Product; /** @@ -341,4 +342,12 @@ public interface PizzaApplication extends Application { * @throws ServletException If something unexpected happened */ public void doAdminHandleCategoryForms (final HttpServletRequest request, final HttpServletResponse response) throws ServletException; + + /** + * Some "getter" for a Product instance from given item + * @param item Item instance + * @return A Product instance + * @throws ServletException If something unexpected happened + */ + public Product getProduct (final AddableBasketItem item) throws ServletException; } diff --git a/src/java/org/mxchange/pizzaapplication/application/PizzaServiceApplication.java b/src/java/org/mxchange/pizzaapplication/application/PizzaServiceApplication.java index b7f057c0..ff05279f 100644 --- a/src/java/org/mxchange/pizzaapplication/application/PizzaServiceApplication.java +++ b/src/java/org/mxchange/pizzaapplication/application/PizzaServiceApplication.java @@ -38,6 +38,7 @@ import org.mxchange.pizzaapplication.database.frontend.product.PizzaProductDatab import org.mxchange.pizzaapplication.database.frontend.product.ProductFrontend; import org.mxchange.pizzaapplication.exceptions.CategoryTitleAlreadyUsedException; import org.mxchange.pizzaapplication.exceptions.ProductTitleAlreadyUsedException; +import org.mxchange.pizzaapplication.item.AddableBasketItem; import org.mxchange.pizzaapplication.product.Product; /** @@ -1577,4 +1578,36 @@ public class PizzaServiceApplication extends BasePizzaServiceSystem implements P // Return it return isUsed; } + + @Override + public Product getProduct (final AddableBasketItem item) throws ServletException { + // Trace message + this.getLogger().trace("item=" + item + " - CALLED!"); + + // item should not be null + if (null == item) { + // Abort here + throw new NullPointerException("item is null"); + } else if (null == this.productFrontend) { + // Abort here + throw new NullPointerException("productFrontend is null"); + } + + // Init product instance + Product product = null; + + try { + // Call frontend + product = this.productFrontend.getProduct(item); + } catch (final SQLException | IOException | BadTokenException | CorruptedDatabaseFileException | NoSuchMethodException | IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) { + // Continue to throw + throw new ServletException(ex); + } + + // Trace message + this.getLogger().trace("product=" + product + " - EXIT!"); + + // Return it + return product; + } } diff --git a/src/java/org/mxchange/pizzaapplication/basket/BaseBasket.java b/src/java/org/mxchange/pizzaapplication/basket/BaseBasket.java index 3645d04c..58ff9cf6 100644 --- a/src/java/org/mxchange/pizzaapplication/basket/BaseBasket.java +++ b/src/java/org/mxchange/pizzaapplication/basket/BaseBasket.java @@ -21,6 +21,7 @@ import java.io.Serializable; import java.lang.reflect.InvocationTargetException; import java.sql.SQLException; import java.text.MessageFormat; +import java.util.Map; import javax.servlet.ServletContext; import javax.servlet.http.HttpSession; import org.mxchange.jcore.exceptions.BadTokenException; @@ -163,4 +164,32 @@ public class BaseBasket extends BasePizzaServiceSys // Return it return isAdded; } + + @Override + public Map getAll () throws IOException, SQLException, BadTokenException, CorruptedDatabaseFileException, NoSuchMethodException, IllegalAccessException, InvocationTargetException { + // Trace message + this.getLogger().trace("CALLED!"); + + // Init map + @SuppressWarnings("unchecked") + Map map = (Map) ((BasketFrontend) this.getFrontend()).getAll(); + + // Trace message + this.getLogger().trace("map=" + map); + + // Return it + return map; + } + + @Override + public AddableBasketItem getLast () throws IOException, SQLException, BadTokenException, CorruptedDatabaseFileException, NoSuchMethodException, IllegalAccessException, InvocationTargetException { + // Deligate to frontend + return ((BasketFrontend) this.getFrontend()).getLast(); + } + + @Override + public int getLastNumRows () { + // Deligate to frontend + return this.getFrontend().getLastNumRows(); + } } diff --git a/src/java/org/mxchange/pizzaapplication/basket/Basket.java b/src/java/org/mxchange/pizzaapplication/basket/Basket.java index b583b530..462d4668 100644 --- a/src/java/org/mxchange/pizzaapplication/basket/Basket.java +++ b/src/java/org/mxchange/pizzaapplication/basket/Basket.java @@ -20,6 +20,7 @@ import java.io.IOException; import java.io.Serializable; import java.lang.reflect.InvocationTargetException; import java.sql.SQLException; +import java.util.Map; import javax.servlet.ServletContext; import javax.servlet.http.HttpSession; import org.mxchange.jcore.FrameworkInterface; @@ -107,4 +108,39 @@ public interface Basket extends Serializable, Frame * @throws org.mxchange.jcore.exceptions.BadTokenException If a bad token was found */ public void init (final ServletContext context, final HttpSession session) throws UnsupportedDatabaseBackendException, SQLException, IOException, BadTokenException; + + /** + * Some "getter" for all entries in this basket + * + * @return Map on all basket items + * @throws java.io.IOException If an IO error occurs + * @throws java.sql.SQLException If an SQL error occurs + * @throws org.mxchange.jcore.exceptions.BadTokenException If a bad token was found + * @throws org.mxchange.jcore.exceptions.CorruptedDatabaseFileException If a corrupted database file was found + * @throws java.lang.NoSuchMethodException If a method was not found + * @throws java.lang.IllegalAccessException If the invoked method is not public + * @throws java.lang.reflect.InvocationTargetException If anything else happened? + */ + public Map getAll () throws IOException, SQLException, BadTokenException, CorruptedDatabaseFileException, NoSuchMethodException, IllegalAccessException, InvocationTargetException; + + /** + * Getter for last entry + * + * @return Last added item in basket + * @throws java.io.IOException If an IO error occurs + * @throws java.sql.SQLException If an SQL error occurs + * @throws org.mxchange.jcore.exceptions.BadTokenException If a bad token was found + * @throws org.mxchange.jcore.exceptions.CorruptedDatabaseFileException If a corrupted database file was found + * @throws java.lang.NoSuchMethodException If a method was not found + * @throws java.lang.IllegalAccessException If the invoked method is not public + * @throws java.lang.reflect.InvocationTargetException If anything else happened? + */ + public AddableBasketItem getLast () throws IOException, SQLException, BadTokenException, CorruptedDatabaseFileException, NoSuchMethodException, IllegalAccessException, InvocationTargetException; + + /** + * Getter for last num rows + * + * @return Last num rows + */ + public int getLastNumRows (); } diff --git a/src/java/org/mxchange/pizzaapplication/beans/PizzaBean.java b/src/java/org/mxchange/pizzaapplication/beans/PizzaBean.java index 539b0e7b..84a35600 100644 --- a/src/java/org/mxchange/pizzaapplication/beans/PizzaBean.java +++ b/src/java/org/mxchange/pizzaapplication/beans/PizzaBean.java @@ -29,6 +29,7 @@ import org.mxchange.pizzaapplication.beans.basket.BasketBean; import org.mxchange.pizzaapplication.category.Category; import org.mxchange.pizzaapplication.exceptions.CategoryTitleAlreadyUsedException; import org.mxchange.pizzaapplication.exceptions.ProductTitleAlreadyUsedException; +import org.mxchange.pizzaapplication.item.AddableBasketItem; import org.mxchange.pizzaapplication.product.Product; /** @@ -334,4 +335,12 @@ public interface PizzaBean extends Serializable { * @param application the application to set */ public void setApplication (final ServletContext application); + + /** + * Some "getter" for a product from given item + * @param item Item instance + * @return A Product instance + * @throws javax.servlet.ServletException If something bad happens + */ + public Product getProduct (final AddableBasketItem item) throws ServletException; } diff --git a/src/java/org/mxchange/pizzaapplication/beans/PizzaServiceBean.java b/src/java/org/mxchange/pizzaapplication/beans/PizzaServiceBean.java index fbbb338f..d234d668 100644 --- a/src/java/org/mxchange/pizzaapplication/beans/PizzaServiceBean.java +++ b/src/java/org/mxchange/pizzaapplication/beans/PizzaServiceBean.java @@ -39,6 +39,7 @@ import org.mxchange.pizzaapplication.beans.basket.BasketBean; import org.mxchange.pizzaapplication.category.Category; import org.mxchange.pizzaapplication.exceptions.CategoryTitleAlreadyUsedException; import org.mxchange.pizzaapplication.exceptions.ProductTitleAlreadyUsedException; +import org.mxchange.pizzaapplication.item.AddableBasketItem; import org.mxchange.pizzaapplication.product.Product; /** @@ -418,7 +419,7 @@ public class PizzaServiceBean implements PizzaBean { @Override public BasketBean getBasket () { // Trace message - this.getLogger().trace("basked=" + this.basket + " - EXIT!"); + this.getLogger().trace("basket=" + this.basket + " - EXIT!"); // Return it return this.basket; @@ -430,7 +431,7 @@ public class PizzaServiceBean implements PizzaBean { @Override public void setBasket (final BasketBean basket) { // Trace message - this.getLogger().trace("basked=" + basket + " - CALLED!"); + this.getLogger().trace("basket=" + basket + " - CALLED!"); // Set it here this.basket = basket; @@ -459,4 +460,10 @@ public class PizzaServiceBean implements PizzaBean { // Set it here this.application = application; } + + @Override + public Product getProduct (final AddableBasketItem item) throws ServletException { + // Deligate to application + return this.app.getProduct(item); + } } diff --git a/src/java/org/mxchange/pizzaapplication/beans/basket/BasketBean.java b/src/java/org/mxchange/pizzaapplication/beans/basket/BasketBean.java index c37e6322..490740f7 100644 --- a/src/java/org/mxchange/pizzaapplication/beans/basket/BasketBean.java +++ b/src/java/org/mxchange/pizzaapplication/beans/basket/BasketBean.java @@ -20,6 +20,7 @@ import java.io.IOException; import java.io.Serializable; import java.lang.reflect.InvocationTargetException; import java.sql.SQLException; +import java.util.Map; import javax.faces.FacesException; import javax.servlet.ServletContext; import javax.servlet.http.HttpSession; @@ -135,4 +136,39 @@ public interface BasketBean extends Serializable { * @param session the session to set */ public void setSession (final HttpSession session); + + /** + * Some "getter" for all entries in this basket + * + * @return Map on all basket items + * @throws java.io.IOException If an IO error occurs + * @throws java.sql.SQLException If an SQL error occurs + * @throws org.mxchange.jcore.exceptions.BadTokenException If a bad token was found + * @throws org.mxchange.jcore.exceptions.CorruptedDatabaseFileException If a corrupted database file was found + * @throws java.lang.NoSuchMethodException If a method was not found + * @throws java.lang.IllegalAccessException If the invoked method is not public + * @throws java.lang.reflect.InvocationTargetException If anything else happened? + */ + public Map getAll () throws IOException, SQLException, BadTokenException, CorruptedDatabaseFileException, NoSuchMethodException, IllegalAccessException, InvocationTargetException; + + /** + * Getter for last entry + * + * @return Last added item in basket + * @throws java.io.IOException If an IO error occurs + * @throws java.sql.SQLException If an SQL error occurs + * @throws org.mxchange.jcore.exceptions.BadTokenException If a bad token was found + * @throws org.mxchange.jcore.exceptions.CorruptedDatabaseFileException If a corrupted database file was found + * @throws java.lang.NoSuchMethodException If a method was not found + * @throws java.lang.IllegalAccessException If the invoked method is not public + * @throws java.lang.reflect.InvocationTargetException If anything else happened? + */ + public AddableBasketItem getLast () throws IOException, SQLException, BadTokenException, CorruptedDatabaseFileException, NoSuchMethodException, IllegalAccessException, InvocationTargetException; + + /** + * Getter for last num rows + * + * @return Last num rows + */ + public int getLastNumRows (); } diff --git a/src/java/org/mxchange/pizzaapplication/beans/basket/ItemBasketBean.java b/src/java/org/mxchange/pizzaapplication/beans/basket/ItemBasketBean.java index 1a8f7663..ce19d151 100644 --- a/src/java/org/mxchange/pizzaapplication/beans/basket/ItemBasketBean.java +++ b/src/java/org/mxchange/pizzaapplication/beans/basket/ItemBasketBean.java @@ -19,6 +19,7 @@ package org.mxchange.pizzaapplication.beans.basket; import java.io.IOException; import java.lang.reflect.InvocationTargetException; import java.sql.SQLException; +import java.util.Map; import javax.annotation.PostConstruct; import javax.enterprise.context.SessionScoped; import javax.faces.FacesException; @@ -174,4 +175,22 @@ public class ItemBasketBean implements BasketBean { public void setSession (final HttpSession session) { this.session = session; } + + @Override + public Map getAll () throws IOException, SQLException, BadTokenException, CorruptedDatabaseFileException, NoSuchMethodException, IllegalAccessException, InvocationTargetException { + // Deligate to basket instance + return this.basket.getAll(); + } + + @Override + public AddableBasketItem getLast () throws IOException, SQLException, BadTokenException, CorruptedDatabaseFileException, NoSuchMethodException, IllegalAccessException, InvocationTargetException { + // Deligate to basket instance + return this.basket.getLast(); + } + + @Override + public int getLastNumRows () { + // Delegate to basket + return this.basket.getLastNumRows(); + } } diff --git a/src/java/org/mxchange/pizzaapplication/database/frontend/basket/BasketDatabaseFrontend.java b/src/java/org/mxchange/pizzaapplication/database/frontend/basket/BasketDatabaseFrontend.java index fb563d84..28d4033b 100644 --- a/src/java/org/mxchange/pizzaapplication/database/frontend/basket/BasketDatabaseFrontend.java +++ b/src/java/org/mxchange/pizzaapplication/database/frontend/basket/BasketDatabaseFrontend.java @@ -21,7 +21,9 @@ import java.lang.reflect.InvocationTargetException; import java.sql.SQLException; import java.text.MessageFormat; import java.util.Iterator; +import java.util.LinkedHashMap; import java.util.Map; +import java.util.Set; import org.mxchange.jcore.criteria.logical.and.AndLogicalMatcher; import org.mxchange.jcore.criteria.searchable.SearchCriteria; import org.mxchange.jcore.criteria.searchable.SearchableCriteria; @@ -409,4 +411,137 @@ public class BasketDatabaseFrontend extends BaseDatabaseFrontend implements Bask // Return it return result; } + + @Override + public Map getAll () throws IOException, SQLException, BadTokenException, CorruptedDatabaseFileException, NoSuchMethodException, IllegalAccessException, InvocationTargetException { + // Trace message + this.getLogger().trace("CALLED!"); + + // Session should be set here + if (this.getSessionId() == null) { + // Abort here + throw new NullPointerException("sessionId is null"); + } + + // Init seaerch instance + SearchableCriteria criteria = new SearchCriteria(); + + // Add only session id + criteria.addCriteria(BasketDatabaseConstants.COLUMN_SESSION_ID, this.getSessionId()); + + // Now run it on backend + Result result = this.getBackend().doSelectByCriteria(criteria); + + // Now convert it to a map + Set set = result.resultSet(); + + // Debug message + this.getLogger().debug("set=" + set); + + // Init map + Map map = new LinkedHashMap<>(set.size()); + + // Add all entries + for (final Storeable storeable : set) { + // Debug message + this.getLogger().debug("storeable=" + storeable); + + // Check on AddableBasketItem + if (!(storeable instanceof AddableBasketItem)) { + // Not correct instance + throw new IllegalStateException("storeable=" + storeable + " does not implement AddableBasketItem."); + } + + // Get id + Long id = ((AddableBasketItem) storeable).getItemId(); + + // Debug message + this.getLogger().debug("id=" + id); + + // Add it + map.put(id, (AddableBasketItem) storeable); + } + + // Trace message + this.getLogger().trace("map=" + map + " - EXIT!"); + + // Return it + return map; + } + + @Override + public AddableBasketItem getLast () throws IOException, SQLException, BadTokenException, CorruptedDatabaseFileException, NoSuchMethodException, IllegalAccessException, InvocationTargetException { + // Trace message + this.getLogger().trace("CALLED!"); + + // Session should be set here + if (this.getSessionId() == null) { + // Abort here + throw new NullPointerException("sessionId is null"); + } + + // Init seaerch instance + SearchableCriteria criteria = new SearchCriteria(); + + // Add only session id + criteria.addCriteria(BasketDatabaseConstants.COLUMN_SESSION_ID, this.getSessionId()); + + // Get number of rows + int rows = this.getBackend().numRows(criteria); + + // Debug message + this.getLogger().debug("rows=" + rows); + + // Nothing found? + if (rows == 0) { + // Debug message + this.getLogger().debug("Nothing found, returning null ... - EXIT!"); + + // Return null + return null; + } + + // Debug message + this.getLogger().debug("rows=" + rows); + + // Set last num rows + this.setLastNumRows(rows); + + // Now set this -1 as skip value and limit to 1 + criteria.setSkip(rows); + criteria.setLimit(1); + + // And run it ... + Result result = this.getBackend().doSelectByCriteria(criteria); + + // Debug message + this.getLogger().debug("result=" + result); + + // Init instance + AddableBasketItem item; + + // There should be something! + if (!result.hasNext()) { + // Something isn't working here ... + throw new IllegalStateException("result has zero entries, but rows=" + rows); + } + + // Get next element + Storeable storeable = result.next(); + + // Is it still castable? + if (!(storeable instanceof AddableBasketItem)) { + // Opps! + throw new IllegalStateException("storeable=" + storeable + " does not implement AddableBasketItem."); + } + + // Cast it + item = (AddableBasketItem) storeable; + + // Trace message + this.getLogger().trace("item=" + item + " - EXIT!"); + + // Return it + return item; + } } diff --git a/src/java/org/mxchange/pizzaapplication/database/frontend/basket/BasketFrontend.java b/src/java/org/mxchange/pizzaapplication/database/frontend/basket/BasketFrontend.java index 45d53260..75391e81 100644 --- a/src/java/org/mxchange/pizzaapplication/database/frontend/basket/BasketFrontend.java +++ b/src/java/org/mxchange/pizzaapplication/database/frontend/basket/BasketFrontend.java @@ -19,6 +19,7 @@ package org.mxchange.pizzaapplication.database.frontend.basket; import java.io.IOException; import java.lang.reflect.InvocationTargetException; import java.sql.SQLException; +import java.util.Map; import org.mxchange.jcore.database.frontend.DatabaseFrontend; import org.mxchange.jcore.exceptions.BadTokenException; import org.mxchange.jcore.exceptions.CorruptedDatabaseFileException; @@ -87,4 +88,32 @@ public interface BasketFrontend extends DatabaseFrontend { * @throws java.lang.reflect.InvocationTargetException If anything else happened? */ public boolean isItemAdded (final AddableBasketItem item, final String sessionId) throws IOException, SQLException, BadTokenException, CorruptedDatabaseFileException, NoSuchMethodException, IllegalAccessException, InvocationTargetException; + + /** + * Some "getter" for all entries in this basket + * + * @return Map on all basket items + * @throws java.io.IOException If an IO error occurs + * @throws java.sql.SQLException If an SQL error occurs + * @throws org.mxchange.jcore.exceptions.BadTokenException If a bad token was found + * @throws org.mxchange.jcore.exceptions.CorruptedDatabaseFileException If a corrupted database file was found + * @throws java.lang.NoSuchMethodException If a method was not found + * @throws java.lang.IllegalAccessException If the invoked method is not public + * @throws java.lang.reflect.InvocationTargetException If anything else happened? + */ + public Map getAll () throws IOException, SQLException, BadTokenException, CorruptedDatabaseFileException, NoSuchMethodException, IllegalAccessException, InvocationTargetException; + + /** + * Getter for last entry + * + * @return Last added item in basket + * @throws java.io.IOException If an IO error occurs + * @throws java.sql.SQLException If an SQL error occurs + * @throws org.mxchange.jcore.exceptions.BadTokenException If a bad token was found + * @throws org.mxchange.jcore.exceptions.CorruptedDatabaseFileException If a corrupted database file was found + * @throws java.lang.NoSuchMethodException If a method was not found + * @throws java.lang.IllegalAccessException If the invoked method is not public + * @throws java.lang.reflect.InvocationTargetException If anything else happened? + */ + public AddableBasketItem getLast () throws IOException, SQLException, BadTokenException, CorruptedDatabaseFileException, NoSuchMethodException, IllegalAccessException, InvocationTargetException; } 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 1c9008a9..768757dd 100644 --- a/src/java/org/mxchange/pizzaapplication/database/frontend/product/PizzaProductDatabaseFrontend.java +++ b/src/java/org/mxchange/pizzaapplication/database/frontend/product/PizzaProductDatabaseFrontend.java @@ -32,6 +32,7 @@ import org.mxchange.jcore.database.storage.Storeable; import org.mxchange.jcore.exceptions.BadTokenException; import org.mxchange.jcore.exceptions.CorruptedDatabaseFileException; import org.mxchange.jcore.exceptions.UnsupportedDatabaseBackendException; +import org.mxchange.pizzaapplication.item.AddableBasketItem; import org.mxchange.pizzaapplication.product.Product; import org.mxchange.pizzaapplication.product.pizza.PizzaProduct; @@ -44,7 +45,9 @@ public class PizzaProductDatabaseFrontend extends BaseDatabaseFrontend implement /** * Default constrcutor - * @throws org.mxchange.jcore.exceptions.UnsupportedDatabaseBackendException If the configured backend is not supported + * + * @throws org.mxchange.jcore.exceptions.UnsupportedDatabaseBackendException + * If the configured backend is not supported * @throws java.sql.SQLException If any SQL error occurs */ public PizzaProductDatabaseFrontend () throws UnsupportedDatabaseBackendException, SQLException { @@ -60,6 +63,7 @@ public class PizzaProductDatabaseFrontend extends BaseDatabaseFrontend implement /** * Adds product to database by given title, price and category id + * * @param title Product title * @param price Product price * @param category Product category id @@ -70,7 +74,7 @@ public class PizzaProductDatabaseFrontend extends BaseDatabaseFrontend implement public void addProduct (final String title, final Float price, final Long category, final Boolean available) throws SQLException, IOException { // Trace message this.getLogger().trace(MessageFormat.format("title={0},price={1},category={2} - CALLED!", title, price, category)); //NOI18N - + // Title should not be null if (null == title) { // Abort here @@ -85,30 +89,30 @@ public class PizzaProductDatabaseFrontend extends BaseDatabaseFrontend implement // Abort here throw new NullPointerException("available is null"); //NOI18N } - + // Clear dataset from previous usage this.clearDataSet(); - + // Add title and parent this.addToDataSet(ProductFrontend.COLUMN_TITLE, title); this.addToDataSet(ProductFrontend.COLUMN_PRICE, price); this.addToDataSet(ProductFrontend.COLUMN_CATEGORY, category); this.addToDataSet(ProductFrontend.COLUMN_AVAILABLE, available); - + // Handle this over to the backend // @todo Nothing is done yet! Result result = this.doInsertDataSet(); - + // Debug message this.getLogger().debug(MessageFormat.format("result={0}", result)); //NOI18N - + // Trace message this.getLogger().trace("EXIT!"); //NOI18N } /** * Shuts down the database layer - * + * * @throws java.sql.SQLException If an SQL error occurs * @throws java.io.IOException If any IO error occurs */ @@ -158,11 +162,12 @@ public class PizzaProductDatabaseFrontend extends BaseDatabaseFrontend implement * * @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 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") + @SuppressWarnings("unchecked") public Iterator getAllProducts () throws IOException, BadTokenException, SQLException, CorruptedDatabaseFileException, NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException { // Trace message this.getLogger().trace("CALLED!"); //NOI18N @@ -187,7 +192,7 @@ public class PizzaProductDatabaseFrontend extends BaseDatabaseFrontend implement } @Override - @SuppressWarnings ("unchecked") + @SuppressWarnings("unchecked") public Iterator getAvailableProducts () throws IOException, BadTokenException, SQLException, CorruptedDatabaseFileException, NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException { // Trace message this.getLogger().trace("CALLED!"); //NOI18N @@ -197,7 +202,7 @@ public class PizzaProductDatabaseFrontend extends BaseDatabaseFrontend implement // Add criteria criteria.addCriteria(ProductFrontend.COLUMN_AVAILABLE, true); - + // Run the query Result result = this.getBackend().doSelectByCriteria(criteria); @@ -278,43 +283,46 @@ public class PizzaProductDatabaseFrontend extends BaseDatabaseFrontend implement * @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 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, CorruptedDatabaseFileException, NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException { // Trace message this.getLogger().trace(MessageFormat.format("title={0} - CALLED!", title)); //NOI18N - + // Get search criteria SearchableCriteria criteria = new SearchCriteria(); - + // Add criteria criteria.addCriteria(ProductFrontend.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())); //NOI18N - + // Now check size of the result boolean isFound = (result.size() == 1); - + // Trace message this.getLogger().trace(MessageFormat.format("isFound={0} - EXIT!", isFound)); //NOI18N - + // Return it return isFound; } /** - * Converts the given map into a Storeable instance, depending on which class implements it. All - * keys are being interpreted as class fields/attributes and their respective setters are being searched for. As - * this method may fail to find one or access it, this method throws some exception. + * Converts the given map into a Storeable instance, depending on which + * class implements it. All keys are being interpreted as class + * fields/attributes and their respective setters are being searched for. As + * this method may fail to find one or access it, this method throws some + * exception. * * @param map Map instance to convert to Storeable * @return An instance of a Storeable implementation @@ -360,4 +368,55 @@ public class PizzaProductDatabaseFrontend extends BaseDatabaseFrontend implement // Return it return instance; } + + @Override + public Product getProduct (final AddableBasketItem item) throws SQLException, IOException, BadTokenException, CorruptedDatabaseFileException, NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException { + // Trace message + this.getLogger().trace("item=" + item + " - CALLED!"); + + // item should not be null + if (null == item) { + // Abort here + throw new NullPointerException("item is null"); + } + + // Get search criteria + SearchableCriteria criteria = new SearchCriteria(); + + // Add criteria + criteria.addCriteria(ProductFrontend.COLUMN_ITEM_ID, item.getItemId()); + + // 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())); //NOI18N + + // Init product instance + Product product = null; + + // Is there one entry? + if (result.hasNext()) { + // Get item + Storeable storeable = result.next(); + + // Is it Product? + if (!(storeable instanceof Product)) { + // Cannot cast! + throw new IllegalStateException("storeable=" + storeable + " does not implement Product."); + } + + // Cast it + product = (Product) storeable; + } + + // Trace message + this.getLogger().trace("product=" + product + " - EXIT!"); + + // Return it + return product; + } } diff --git a/src/java/org/mxchange/pizzaapplication/database/frontend/product/ProductFrontend.java b/src/java/org/mxchange/pizzaapplication/database/frontend/product/ProductFrontend.java index 6cc26149..6e71d77c 100644 --- a/src/java/org/mxchange/pizzaapplication/database/frontend/product/ProductFrontend.java +++ b/src/java/org/mxchange/pizzaapplication/database/frontend/product/ProductFrontend.java @@ -23,6 +23,7 @@ import java.util.Iterator; import org.mxchange.jcore.database.frontend.DatabaseFrontend; import org.mxchange.jcore.exceptions.BadTokenException; import org.mxchange.jcore.exceptions.CorruptedDatabaseFileException; +import org.mxchange.pizzaapplication.item.AddableBasketItem; import org.mxchange.pizzaapplication.product.Product; /** @@ -105,4 +106,19 @@ public interface ProductFrontend extends DatabaseFrontend { * @throws java.lang.reflect.InvocationTargetException Any other problems? */ public boolean isProductTitleUsed (String title) throws IOException, SQLException, BadTokenException, CorruptedDatabaseFileException, NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException; + + /** + * Some "getter" for a Product instance from given item + * + * @param item Item instance + * @return A Product instance + * @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 + * @throws org.mxchange.jcore.exceptions.CorruptedDatabaseFileException If the database file is damaged + * @throws java.lang.NoSuchMethodException If a method was not found + * @throws java.lang.IllegalAccessException If the method cannot be accessed + * @throws java.lang.reflect.InvocationTargetException Any other problems? + */ + public Product getProduct (final AddableBasketItem item) throws SQLException, IOException, BadTokenException, CorruptedDatabaseFileException, NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException; } diff --git a/src/java/org/mxchange/pizzaapplication/filter/servlet/basket/BasketItemAddedFilter.java b/src/java/org/mxchange/pizzaapplication/filter/servlet/basket/BasketItemAddedFilter.java index 46ff6fa2..367a904b 100644 --- a/src/java/org/mxchange/pizzaapplication/filter/servlet/basket/BasketItemAddedFilter.java +++ b/src/java/org/mxchange/pizzaapplication/filter/servlet/basket/BasketItemAddedFilter.java @@ -29,7 +29,8 @@ import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpSession; import org.mxchange.jcore.exceptions.BadTokenException; import org.mxchange.jcore.exceptions.CorruptedDatabaseFileException; -import org.mxchange.pizzaapplication.basket.Basket; +import org.mxchange.pizzaapplication.beans.PizzaBean; +import org.mxchange.pizzaapplication.beans.basket.BasketBean; import org.mxchange.pizzaapplication.filter.servlet.BaseServletFilter; import org.mxchange.pizzaapplication.item.AddableBasketItem; @@ -83,7 +84,7 @@ public class BasketItemAddedFilter extends BaseServletFilter implements Filter { this.getLogger().debug(MessageFormat.format("item.id={0},item.itemId={1},item.itemType={2},item.amount={3}", item.getId(), item.getItemId(), item.getItemType(), item.getAmount())); //NOI18N // Init instance - Basket basket; + BasketBean basket; try { // Get session instance HttpSession session = ((HttpServletRequest) request).getSession(); @@ -97,8 +98,17 @@ public class BasketItemAddedFilter extends BaseServletFilter implements Filter { throw new NullPointerException("session is null"); //NOI18N } + // Get man controller + PizzaBean bean = (PizzaBean) session.getAttribute("controller"); //NOI18N + + // Debug message + this.getLogger().debug("bean=" + bean); + // Get basket instance - basket = (Basket) session.getAttribute("basket"); //NOI18N + basket = bean.getBasket(); + + // Debug message + this.getLogger().debug("basket=" + basket); // Is the item already added? if (item.getItemId() == null) { diff --git a/src/java/org/mxchange/pizzaapplication/tags/basket/MiniBasketTag.java b/src/java/org/mxchange/pizzaapplication/tags/basket/MiniBasketTag.java index b0d84b9e..a2b9440b 100644 --- a/src/java/org/mxchange/pizzaapplication/tags/basket/MiniBasketTag.java +++ b/src/java/org/mxchange/pizzaapplication/tags/basket/MiniBasketTag.java @@ -19,13 +19,18 @@ package org.mxchange.pizzaapplication.tags.basket; import java.io.IOException; import java.lang.reflect.InvocationTargetException; import java.sql.SQLException; +import java.text.MessageFormat; +import javax.servlet.ServletException; import javax.servlet.jsp.JspException; import javax.servlet.jsp.tagext.BodyTagSupport; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.mxchange.jcore.exceptions.BadTokenException; import org.mxchange.jcore.exceptions.CorruptedDatabaseFileException; +import org.mxchange.pizzaapplication.beans.PizzaBean; import org.mxchange.pizzaapplication.beans.basket.BasketBean; +import org.mxchange.pizzaapplication.item.AddableBasketItem; +import org.mxchange.pizzaapplication.product.Product; /** * A basket tag that outputs a small basket and a link to the full basket website. @@ -80,14 +85,57 @@ public class MiniBasketTag extends BodyTagSupport implements BasketTag { // Some entries found? if (this.getBasket().isEmpty()) { // Empty basket - out.append("
\n"); + out.append("
\n"); out.append(this.getBasket().getMessageStringFromKey("MiniBasketTag.basket_is_empty")).append("\n"); out.append("
\n"); } else { - // Some times were found - out.append("FOUND!"); + // Get all items + AddableBasketItem item = this.getBasket().getLast(); + + // item cannot be null here + if (null == item) { + // Abort here + throw new NullPointerException("item is null"); + } + + // Get application bean from session + PizzaBean bean = (PizzaBean) this.getBasket().getSession().getAttribute("controller"); + + // Debug log + this.LOG.debug("bean=" + bean); + + // Should not be null + if (null == bean) { + // Abort here + throw new NullPointerException("bean is null"); + } + + // Get product instance + Product product = bean.getProduct(item); + + // Debug message + this.LOG.debug("product=" + product); + + // Get last num rows + int lastNumRows = this.getBasket().getLastNumRows(); + + // Debug message + this.LOG.debug("lastNumRows=" + lastNumRows); + + // Output all + out.append("
\n"); + out.append("
\n"); + out.append(" ").append(MessageFormat.format(this.getBasket().getMessageStringFromKey("MiniBasketTag.last_item"), product.getTitle())); + out.append("
\n"); + out.append("
\n"); + out.append(" ").append(MessageFormat.format(this.getBasket().getMessageStringFromKey("MiniBasketTag.additional_items"), (lastNumRows - 1))); + out.append("
\n"); + out.append(" \n"); + out.append("
\n"); } - } catch (final IOException | SQLException | BadTokenException | CorruptedDatabaseFileException | NoSuchMethodException | IllegalAccessException | InvocationTargetException ex) { + } catch (final ServletException | IOException | SQLException | BadTokenException | CorruptedDatabaseFileException | NoSuchMethodException | IllegalAccessException | InvocationTargetException ex) { // Continue to throw throw new JspException(ex); } @@ -116,7 +164,7 @@ public class MiniBasketTag extends BodyTagSupport implements BasketTag { @Override public BasketBean getBasket () { // Trace message - this.LOG.trace("basket=" + this.basket + " - EXIT!"); + //* NOISY-DEBUG: */ this.LOG.trace("basket=" + this.basket + " - EXIT!"); // Return it return this.basket; @@ -128,7 +176,7 @@ public class MiniBasketTag extends BodyTagSupport implements BasketTag { @Override public void setBasket (final BasketBean basket) { // Trace message - this.LOG.trace("basked=" + basket + " - CALLED!"); + //* NOISY-DEBUG: */ this.LOG.trace("basked=" + basket + " - CALLED!"); // Set it here this.basket = basket; diff --git a/web/form_handler/add_item.jsp b/web/form_handler/add_item.jsp index aa07ebf6..d5638079 100644 --- a/web/form_handler/add_item.jsp +++ b/web/form_handler/add_item.jsp @@ -13,6 +13,10 @@ + + +<%controller.init();%> +