]> git.mxchange.org Git - pizzaservice-war.git/commitdiff
Continued with project:
authorRoland Haeder <roland@mxchange.org>
Sat, 15 Aug 2015 10:25:32 +0000 (12:25 +0200)
committerRoland Haeder <roland@mxchange.org>
Sat, 15 Aug 2015 10:25:32 +0000 (12:25 +0200)
- added showing product's category in admin area
- added method getPrintableProduktCategory() for this
- fixed logger XML file
- added method getCategory() of given Product instance to category database frontend
- added method isProductTitleUsed() to pre-check if the title has been used. This avoids an exception thrown in doAdminAddProduct() which expects that the product's title is not yet used.
Signed-off-by:Roland Häder <roland@mxchange.org>

src/java/log4j2.xml
src/java/org/mxchange/pizzaapplication/application/PizzaApplication.java
src/java/org/mxchange/pizzaapplication/application/PizzaServiceApplication.java
src/java/org/mxchange/pizzaapplication/database/frontend/category/CategoryFrontend.java
src/java/org/mxchange/pizzaapplication/database/frontend/category/PizzaCategoryDatabaseFrontend.java
src/java/org/mxchange/pizzaapplication/database/frontend/product/PizzaProductDatabaseFrontend.java
src/java/org/mxchange/pizzaapplication/product/BaseProduct.java
web/admin/product.jsp
web/form_handler/admin/do_product.jsp

index 8860f19ebe29c068e05b6e609157193486556ba0..1ebbd9fd27e84c8140e6466a8b251312657b8931 100644 (file)
@@ -15,7 +15,7 @@ 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/>.
 -->
-<Configuration status="WARN">
+<Configuration status="INFO">
        <Appenders>
                <Console name="STDOUT" target="SYSTEM_OUT">
                        <PatternLayout pattern="%d %-5p [%t] %C{2} (%F:%L) - %m%n"/>
@@ -23,7 +23,6 @@ along with this program.  If not, see <http://www.gnu.org/licenses/>.
        </Appenders>
        <Loggers>
                <Root level="trace">
-                       <AppenderRef ref="STDERR" level="WARNING"/>
                        <AppenderRef ref="STDOUT" level="TRACE"/>
                </Root>
        </Loggers>
index dc0140bb8408606ba7a0536c5dcd936b594215cb..c1ebe25236b649f57b64e7096987071f3c0a4c20 100644 (file)
@@ -233,6 +233,15 @@ public interface PizzaApplication extends Application {
         */
        public String getPrintableProduktAvailability (final Product product);
 
+       /**
+        * Returns a printable (human-readable) string of product's category
+        * 
+        * @param product Product instance to check
+        * @return Human-readable version of product availability
+        * @throws javax.servlet.ServletException If something unexpected happened
+        */
+       public String getPrintableProduktCategory (final Product product) throws ServletException;
+
        /**
         * Marks all choosen products as ordered
         *
@@ -260,6 +269,15 @@ public interface PizzaApplication extends Application {
         */
        public void doAdminAddProduct (final HttpServletRequest request) throws ServletException, ProductTitleAlreadyUsedException;
 
+       /**
+        * Checks if product's title is already used.
+        *
+        * @param request Request instance
+        * @return Whether the product's title has already been used
+        * @throws javax.servlet.ServletException If something unexpected happened
+        */
+       public boolean isProductTitleUsed (final HttpServletRequest request) throws ServletException;
+
        /**
         * Generates link HTML code for given category's parent id, if set. This
         * link then points to products.jsp?category_id=x
index a9a89d2e5f720790d2fddf83c2ee1dc7716844f9..2c389deb7ff149d486bd658d0edde77f3f22eca3 100644 (file)
  */
 package org.mxchange.pizzaapplication.application;
 
-import org.mxchange.pizzaapplication.exceptions.CategoryTitleAlreadyUsedException;
 import java.io.IOException;
+import java.io.UnsupportedEncodingException;
 import java.lang.reflect.Field;
 import java.lang.reflect.InvocationTargetException;
 import java.sql.SQLException;
 import java.text.MessageFormat;
 import java.util.Iterator;
 import java.util.Map;
+import java.util.logging.Level;
+import java.util.logging.Logger;
 import javax.servlet.ServletContext;
 import javax.servlet.ServletException;
 import javax.servlet.http.HttpServletRequest;
@@ -42,6 +44,7 @@ import org.mxchange.pizzaapplication.database.frontend.category.PizzaCategoryDat
 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.CategoryTitleAlreadyUsedException;
 import org.mxchange.pizzaapplication.exceptions.ProductTitleAlreadyUsedException;
 import org.mxchange.pizzaapplication.product.Product;
 
@@ -1401,4 +1404,82 @@ public class PizzaServiceApplication extends BasePizzaServiceSystem implements P
                // No parent set
                return "Keine";
        }
+
+       @Override
+       public String getPrintableProduktCategory (final Product product) throws ServletException {
+               // Trace message
+               this.getLogger().trace("product=" + product + " - CALLED!");
+
+               // product must not be null
+               if (product == null) {
+                       // Abort here
+                       throw new NullPointerException("product is null");
+               }
+
+               // Declare category
+               Category category;
+
+               try {
+                       // Get Category instance from product over the frontend
+                       category = this.categoryFrontend.getCategory(product);
+               } catch (final IOException | SQLException | BadTokenException | CorruptedDatabaseFileException | NoSuchMethodException | IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) {
+                       throw new ServletException(ex);
+               }
+
+               // Debug message
+               this.getLogger().debug("category=" + category);
+
+               String title = null;
+               try {
+                       // Now get title from it and return it
+                       title = category.decodedTitle();
+               } catch (final UnsupportedEncodingException ex) {
+                       // Continue to throw as cause
+                       throw new ServletException(ex);
+               }
+
+               // Trace message
+               this.getLogger().trace("title=" + title + " - EXIT!");
+
+               // Return it
+               return title;
+       }
+
+       @Override
+       public boolean isProductTitleUsed (final HttpServletRequest request) throws ServletException {
+               // Trace message
+               this.getLogger().trace("request=" + request + " - CALLED!");
+
+               // Init title
+               String title = request.getParameter(PizzaProductDatabaseConstants.COLUMN_TITLE);
+
+               // request must not be null and "title" must be found and non-empty
+               if (request == null) {
+                       // Abort here
+                       throw new NullPointerException("request is null");
+               } else if (title == null) {
+                       // title is not set
+                       throw new IllegalArgumentException(MessageFormat.format("{0} is not set.", PizzaProductDatabaseConstants.COLUMN_TITLE)); //NOI18N
+               } else if (title.isEmpty()) {
+                       // Is left empty
+                       throw new IllegalArgumentException(MessageFormat.format("{0} is empty.", PizzaProductDatabaseConstants.COLUMN_TITLE)); //NOI18N
+               }
+
+               // Default is not used
+               boolean isUsed = false;
+
+               try {
+                       // So that all is tested, try to find the product
+                       isUsed = this.isProductTitleUsed(title);
+               } catch (final IOException | SQLException | BadTokenException | CorruptedDatabaseFileException | NoSuchMethodException | IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) {
+                       // Continue to throw all as cause
+                       throw new ServletException(ex);
+               }
+
+               // Trace message
+               this.getLogger().trace("isUsed=" + isUsed + " - EXIT!");
+
+               // Return it
+               return isUsed;
+       }
 }
index 39007b7c32dcc73a2da0f3cb555eb99952934795..29aa737c62aa64e96d2c87d22c880047dd2373b4 100644 (file)
@@ -24,6 +24,7 @@ import org.mxchange.jcore.database.frontend.DatabaseFrontend;
 import org.mxchange.jcore.exceptions.BadTokenException;
 import org.mxchange.jcore.exceptions.CorruptedDatabaseFileException;
 import org.mxchange.pizzaapplication.category.Category;
+import org.mxchange.pizzaapplication.product.Product;
 
 /**
  * An interface for product database frontends
@@ -70,4 +71,19 @@ public interface CategoryFrontend extends DatabaseFrontend {
         * @throws java.lang.reflect.InvocationTargetException Any other problems?
         */
        public boolean isCategoryTitleUsed (final String title) throws IOException, SQLException, BadTokenException, CorruptedDatabaseFileException, NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException;
+
+       /**
+        * Gets a Category instance for given Product instance.
+        *
+        * @param product Product instance
+        * @return A category 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 error occurs
+        * @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 Category getCategory (final Product product) throws IOException, SQLException, BadTokenException, CorruptedDatabaseFileException, NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException;
 }
index c87f79194bacce76187bfa241e49c228c87cd493..12e16b1d5ba4dbd8bd216a3ff48b838230d3e8f4 100644 (file)
@@ -24,7 +24,7 @@ import java.text.MessageFormat;
 import java.util.Iterator;
 import java.util.Map;
 import org.mxchange.jcore.criteria.searchable.SearchCriteria;
-import org.mxchange.jcore.criteria.searchable.SearchableCritera;
+import org.mxchange.jcore.criteria.searchable.SearchableCriteria;
 import org.mxchange.jcore.database.frontend.BaseDatabaseFrontend;
 import org.mxchange.jcore.database.result.DatabaseResult;
 import org.mxchange.jcore.database.result.Result;
@@ -35,6 +35,7 @@ import org.mxchange.jcore.exceptions.UnsupportedDatabaseBackendException;
 import org.mxchange.pizzaapplication.category.Category;
 import org.mxchange.pizzaapplication.category.product.ProductCategory;
 import org.mxchange.pizzaapplication.database.category.PizzaCategoryDatabaseConstants;
+import org.mxchange.pizzaapplication.product.Product;
 
 /**
  * Stores and retrieves Contact instances
@@ -148,7 +149,7 @@ public class PizzaCategoryDatabaseFrontend extends BaseDatabaseFrontend implemen
                this.getLogger().trace("CALLED!"); //NOI18N
 
                // Instance search criteria
-               SearchableCritera critera = new SearchCriteria();
+               SearchableCriteria critera = new SearchCriteria();
 
                // Run the query
                Result<? extends Storeable> result = this.getBackend().doSelectByCriteria(critera);
@@ -223,7 +224,7 @@ public class PizzaCategoryDatabaseFrontend extends BaseDatabaseFrontend implemen
                this.getLogger().trace(MessageFormat.format("title={0} - CALLED!", title));
 
                // Get search criteria
-               SearchableCritera criteria = new SearchCriteria();
+               SearchableCriteria criteria = new SearchCriteria();
 
                // Add criteria
                criteria.addCriteria(PizzaCategoryDatabaseConstants.COLUMN_TITLE, title);
@@ -235,7 +236,7 @@ public class PizzaCategoryDatabaseFrontend extends BaseDatabaseFrontend implemen
                Result<? extends Storeable> result = this.getBackend().doSelectByCriteria(criteria);
 
                // Debug log
-               this.getLogger().debug(MessageFormat.format("result({0}={1}", result, result.size()));
+               this.getLogger().debug(MessageFormat.format("result({0})={1}", result, result.size()));
 
                // Now check size of the result
                boolean isFound = (result.size() == 1);
@@ -302,4 +303,64 @@ public class PizzaCategoryDatabaseFrontend extends BaseDatabaseFrontend implemen
                // Return column id
                return PizzaCategoryDatabaseConstants.COLUMN_ID;
        }
+
+       @Override
+       public Category getCategory (final Product product) throws IOException, BadTokenException, CorruptedDatabaseFileException, SQLException, NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
+               // Trace message
+               this.getLogger().trace("product=" + product + " - CALLED!");
+
+               // product must not be null
+               if (product == null) {
+                       // Abort here
+                       throw new NullPointerException("product is null");
+               }
+
+               // Get category id from it
+               Long id = product.getCategory();
+
+               // Debug message
+               this.getLogger().debug("id=" + id);
+
+               // It should be >0 here
+               assert(id > 0) : "id=" + id + " must be larger zero";
+
+               // Then construct a search instance
+               SearchableCriteria criteria = new SearchCriteria();
+
+               // Add id to it
+               criteria.addCriteria(PizzaCategoryDatabaseConstants.COLUMN_ID, id);
+
+               // 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()));
+
+               // Init category instance
+               Category category = null;
+
+               // Is there one entry?
+               if (result.hasNext()) {
+                       // Read result from it
+                       Storeable storeable = result.next();
+
+                       // Debug message
+                       this.getLogger().debug("storeable=" + storeable);
+
+                       // Is it instance of Category?
+                       if (storeable instanceof Category) {
+                               // Then cast it
+                               category = (Category) storeable;
+                       }
+               }
+
+               // Trace message
+               this.getLogger().trace("category=" + category + " - EXIT!");
+
+               // Return it
+               return category;
+       }
 }
index 993d10a831a53d185a839ae40a6b4d366b8d2ea4..91d5a9864f7919ce4735e3608dbeda8215b4e11f 100644 (file)
@@ -24,7 +24,7 @@ import java.text.MessageFormat;
 import java.util.Iterator;
 import java.util.Map;
 import org.mxchange.jcore.criteria.searchable.SearchCriteria;
-import org.mxchange.jcore.criteria.searchable.SearchableCritera;
+import org.mxchange.jcore.criteria.searchable.SearchableCriteria;
 import org.mxchange.jcore.database.frontend.BaseDatabaseFrontend;
 import org.mxchange.jcore.database.result.DatabaseResult;
 import org.mxchange.jcore.database.result.Result;
@@ -113,7 +113,7 @@ public class PizzaProductDatabaseFrontend extends BaseDatabaseFrontend implement
                this.getLogger().trace("CALLED!"); //NOI18N
 
                // Instance search criteria
-               SearchableCritera critera = new SearchCriteria();
+               SearchableCriteria critera = new SearchCriteria();
 
                // Add criteria
                critera.addCriteria(PizzaProductDatabaseConstants.COLUMN_AVAILABLE, true);
@@ -149,7 +149,7 @@ public class PizzaProductDatabaseFrontend extends BaseDatabaseFrontend implement
                this.getLogger().trace("CALLED!"); //NOI18N
 
                // Instance search criteria
-               SearchableCritera critera = new SearchCriteria();
+               SearchableCriteria critera = new SearchCriteria();
 
                // Run the query
                Result<? extends Storeable> result = this.getBackend().doSelectByCriteria(critera);
@@ -229,7 +229,7 @@ public class PizzaProductDatabaseFrontend extends BaseDatabaseFrontend implement
                this.getLogger().trace(MessageFormat.format("title={0} - CALLED!", title));
                
                // Get search criteria
-               SearchableCritera criteria = new SearchCriteria();
+               SearchableCriteria criteria = new SearchCriteria();
                
                // Add criteria
                criteria.addCriteria(PizzaProductDatabaseConstants.COLUMN_TITLE, title);
index 60f3623be75207829a68a49cb295be3dddcd2632..f3dbf3c4d87bacdeabf00cbed1ff821d256d35a1 100644 (file)
@@ -159,11 +159,11 @@ public class BaseProduct extends BaseFrameworkSystem implements Product {
                        return 0;
                } else if (this.getId() > product.getId()) {
                        // This id is larger than compared to
-                       return -1;
+                       return 1;
                }
 
                // The other id is larger
-               return 1;
+               return -1;
        }
 
        @Override
index 9f827724d42d5344db85f5d6b5c98ef409098026..b3da16e3ef8be9b8d5d8a943deeddf68361a366b 100644 (file)
@@ -54,6 +54,9 @@
                                                                <th class="table_header_column">
                                                                        Einzelpreis:
                                                                </th>
+                                                               <th class="table_header_column">
+                                                                       Kategorie:
+                                                               </th>
                                                                <th class="table_header_column">
                                                                        Verfügbarkeit:
                                                                </th>
@@ -64,7 +67,8 @@
                                                        <c:forEach var="product" items="<%=app.getAllProducts()%>">
                                                        <tr>
                                                                <td>
-                                                                       ${product.getId()}
+                                                                       ${product.getId()}:
+                                                                       <input type="checkbox" name="product[${product.getId()}]" value="1" />
                                                                </td>
                                                                <td>
                                                                        ${product.getTitle()}
                                                                <td>
                                                                        ${product.getPrice()}
                                                                </td>
+                                                               <td>
+                                                                       ${app.getPrintableProduktCategory(product)}
+                                                               </td>
                                                                <td>
                                                                        ${app.getPrintableProduktAvailability(product)}
                                                                </td>
                                                        </tr>
                                                        </c:forEach>
                                                        <tr>
-                                                               <td colspan="4" class="table_footer">
+                                                               <td colspan="5" class="table_footer">
                                                                        <input type="reset" value="Formular zurücksetzen" />
                                                                        <input type="submit" name="edit" value="Ändern" />
                                                                        <input type="submit" name="delete" value="Löschen" />
index 679f21b2acda0b9e82f7f9301cc886f3aa5470bd..87afa0196ba09b48b06bab22028e886e12032951 100644 (file)
        if ("POST".equals(request.getMethod())) { //NOI18N
                // Is "add/edit/delete" set?
                if (request.getParameter("add") != null) { //NOI18N
-                       // Add new product
-                       app.doAdminAddProduct(request);
+                       // Is it already added?
+                       if (app.isProductTitleUsed(request)) {
+                               // Debug message
+                               app.getLogger().debug("Already used, redirecting ...");
+
+                               // Already added, so redirect here, else a ServletException will be thrown
+                               response.sendRedirect(request.getContextPath() + "/admin/product.jsp?already=1");
+                       } else {
+                               // Add new product
+                               app.doAdminAddProduct(request);
+                       }
                } else if (request.getParameter("edit") != null) { //NOI18N
                        // @TODO
                } else if (request.getParameter("delete") != null) { //NOI18N