]> git.mxchange.org Git - pizzaservice-war.git/commitdiff
Continued with project:
authorRoland Haeder <roland@mxchange.org>
Thu, 13 Aug 2015 10:51:46 +0000 (12:51 +0200)
committerRoland Haeder <roland@mxchange.org>
Thu, 13 Aug 2015 13:36:11 +0000 (15:36 +0200)
- added thrown exceptions
- Very rude implementation of getResultFromSet()
- getProducts() and getCategories() now only throw ServletException
- The above change allows to remove the try-catch block
Signed-off-by:Roland Häder <roland@mxchange.org>

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/database/frontend/product/ProductFrontend.java

index 89f46456a72bf676fcccc36f4b446e5abb2ec52a..22901cab914be13d1075ac80ad6aa90523b067b6 100644 (file)
  */
 package org.mxchange.pizzaapplication.application;
 
-import java.io.IOException;
 import java.util.Iterator;
 import javax.servlet.ServletException;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpSession;
 import org.mxchange.jcore.application.Application;
-import org.mxchange.jcore.exceptions.BadTokenException;
 import org.mxchange.pizzaapplication.category.Category;
 import org.mxchange.pizzaapplication.product.Product;
 
@@ -204,19 +202,17 @@ public interface PizzaApplication extends Application {
         * Some "getter" for a an array of all products
         * 
         * @return All products
-        * @throws java.io.IOException If an IO error occurs
-        * @throws org.mxchange.jcore.exceptions.BadTokenException If a bad token was found
+        * @throws javax.servlet.ServletException If anything went wrong
         */
-       public Iterator<Product> getProducts () throws IOException, BadTokenException;
+       public Iterator<Product> getProducts () throws ServletException;
 
        /**
         * Some "getter" for a an array of all categories
         *
         * @return All categories
-        * @throws java.io.IOException If an IO error occurs
-        * @throws org.mxchange.jcore.exceptions.BadTokenException If a bad token was found
+        * @throws javax.servlet.ServletException If anything went wrong
         */
-       public Iterator<Category> getCategories () throws IOException, BadTokenException;
+       public Iterator<Category> getCategories () throws ServletException;
 
        /**
         * Checks if given Product instance is available and returns a printable
index 4c4335c9ca2366e60771a05de5665db169ccef5b..1603c70f859e9fabcb96429159c6e437db6d3eec 100644 (file)
@@ -164,14 +164,7 @@ public class PizzaServiceApplication extends BasePizzaServiceSystem implements P
 
                // Init/declare total price and iterator
                int totalAmount = 0;
-               Iterator<Product> iterator;
-
-               try {
-                       // Get iterator
-                       iterator = this.getProducts();
-               } catch (final IOException | BadTokenException ex) {
-                       throw new ServletException(ex);
-               }
+               Iterator<Product> iterator = this.getProducts();
 
                // "Walk" over all products
                while (iterator.hasNext()) {
@@ -225,12 +218,7 @@ public class PizzaServiceApplication extends BasePizzaServiceSystem implements P
                float totalPrice = 0.00f;
 
                // Get iterator
-               Iterator<Product> iterator;
-               try {
-                       iterator = this.getProducts();
-               } catch (final IOException | BadTokenException ex) {
-                       throw new ServletException(ex);
-               }
+               Iterator<Product> iterator = this.getProducts();
 
                // "Walk" over all products
                while (iterator.hasNext()) {
@@ -532,9 +520,13 @@ public class PizzaServiceApplication extends BasePizzaServiceSystem implements P
         * @return All products
         */
        @Override
-       public Iterator<Product> getProducts () throws IOException, BadTokenException {
-               // Ask frontend for a list of products
-               return this.productFrontend.getProducts();
+       public Iterator<Product> getProducts () throws ServletException {
+               try {
+                       // Ask frontend for a list of products
+                       return this.productFrontend.getProducts();
+               } catch (final IOException | BadTokenException | SQLException ex) {
+                       throw new ServletException(ex);
+               }
        }
 
        /**
@@ -543,9 +535,13 @@ public class PizzaServiceApplication extends BasePizzaServiceSystem implements P
         * @return All categories
         */
        @Override
-       public Iterator<Category> getCategories () throws IOException, BadTokenException {
-               // Ask frontend for a list of categories
-               return this.categoryFrontend.getCategories();
+       public Iterator<Category> getCategories () throws ServletException {
+               try {
+                       // Ask frontend for a list of categories
+                       return this.categoryFrontend.getCategories();
+               } catch (final IOException | BadTokenException | SQLException ex) {
+                       throw new ServletException(ex);
+               }
        }
 
        /**
@@ -756,14 +752,7 @@ public class PizzaServiceApplication extends BasePizzaServiceSystem implements P
                this.getLogger().trace(MessageFormat.format("request={0},session={1} - CALLED!", request, session));
 
                // Init iterator
-               Iterator<Product> iterator;
-
-               try {
-                       // Get iterator
-                       iterator = this.getProducts();
-               } catch (final IOException | BadTokenException ex) {
-                       throw new ServletException(ex);
-               }
+               Iterator<Product> iterator = this.getProducts();
 
                // "Walk" over all products
                while (iterator.hasNext()) {
@@ -1122,7 +1111,7 @@ public class PizzaServiceApplication extends BasePizzaServiceSystem implements P
                try {
                        // Get iterator
                        iterator = this.getProducts();
-               } catch (final IOException | BadTokenException ex) {
+               } catch (final ServletException ex) {
                        this.abortProgramWithException(ex);
                }
 
index baa6e14f07ff2dad66060d2c7e3b6fe0c84bc636..831e9aec8e274d57d3e683af197528d7499df5d3 100644 (file)
@@ -17,6 +17,7 @@
 package org.mxchange.pizzaapplication.database.frontend.category;
 
 import java.io.IOException;
+import java.sql.SQLException;
 import java.util.Iterator;
 import org.mxchange.jcore.database.frontend.DatabaseFrontend;
 import org.mxchange.jcore.exceptions.BadTokenException;
@@ -35,6 +36,7 @@ public interface CategoryFrontend extends DatabaseFrontend {
         * @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 ... ;-)
+        * @throws java.sql.SQLException If any SQL error occurs
         */
-       public Iterator<Category> getCategories () throws IOException, BadTokenException;
+       public Iterator<Category> getCategories () throws IOException, BadTokenException, SQLException;
 }
index 64ee96096ccdb057a73153daa161b144faa8d9c2..65b3612fa7b7e221a53797fe050af73665df1189 100644 (file)
 package org.mxchange.pizzaapplication.database.frontend.category;
 
 import java.io.IOException;
+import java.sql.ResultSet;
 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.DatabaseResult;
 import org.mxchange.jcore.database.result.Result;
 import org.mxchange.jcore.database.storage.Storeable;
 import org.mxchange.jcore.exceptions.BadTokenException;
@@ -69,16 +71,9 @@ public class PizzaCategoryDatabaseFrontend extends BaseDatabaseFrontend implemen
                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<Category> getCategories () throws IOException, BadTokenException {
+       public Iterator<Category> getCategories () throws IOException, BadTokenException, SQLException {
                // Trace message
                this.getLogger().trace("CALLED!"); //NOI18N
 
@@ -101,6 +96,43 @@ public class PizzaCategoryDatabaseFrontend extends BaseDatabaseFrontend implemen
                return (Iterator<Category>) iterator;
        }
 
+       /**
+        * Gets a Result back from given ResultSet instance
+        *
+        * @param resultSet ResultSet instance from SQL driver
+        * @return A typorized Result instance
+        * @throws java.sql.SQLException If any SQL error occurs
+        */
+       @Override
+       public Result<? extends Storeable> getResultFromSet (final ResultSet resultSet) throws SQLException {
+               // Trace message
+               this.getLogger().trace(MessageFormat.format("resultSet={0} - CALLED!", resultSet));
+
+               // Init result instance
+               Result<? extends Storeable> result = new DatabaseResult();
+
+               // Reset result set before first row
+               resultSet.beforeFirst();
+
+               // "Walk" through all entries
+               while (resultSet.next()) {
+                       // Unwrap whole object
+                       Category category = resultSet.unwrap(Category.class);
+
+                       // Debug log
+                       this.getLogger().debug(MessageFormat.format("category={0}", category));
+
+                       // Add it to result
+                       result.add(category);
+               }
+
+               // Trace message
+               this.getLogger().trace(MessageFormat.format("result({0})={1} - EXIT!", result.size(), result));
+
+               // Return result
+               return result;
+       }
+
        /**
         * Parses given line from database backend into a Storeable instance. Please
         * note that not all backends need this.
index 1fc6fe5f760d40694677f2a996f95024de270410..5f5820a2588559731623500f8a503b084fea82e2 100644 (file)
 package org.mxchange.pizzaapplication.database.frontend.product;
 
 import java.io.IOException;
+import java.sql.ResultSet;
 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.DatabaseResult;
 import org.mxchange.jcore.database.result.Result;
 import org.mxchange.jcore.database.storage.Storeable;
 import org.mxchange.jcore.exceptions.BadTokenException;
@@ -71,16 +73,9 @@ public class PizzaProductDatabaseFrontend extends BaseDatabaseFrontend implement
                this.getLogger().trace("EXIT!"); //NOI18N
        }
 
-       /**
-        * 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")
-       public Iterator<Product> getProducts () throws IOException, BadTokenException {
+       public Iterator<Product> getProducts () throws IOException, BadTokenException, SQLException {
                // Trace message
                this.getLogger().trace("CALLED!"); //NOI18N
 
@@ -106,6 +101,43 @@ public class PizzaProductDatabaseFrontend extends BaseDatabaseFrontend implement
                return (Iterator<Product>) iterator;
        }
 
+       /**
+        * Gets a Result back from given ResultSet instance
+        *
+        * @param resultSet ResultSet instance from SQL driver
+        * @return A typorized Result instance
+        * @throws java.sql.SQLException If any SQL error occurs
+        */
+       @Override
+       public Result<? extends Storeable> getResultFromSet (final ResultSet resultSet) throws SQLException {
+               // Trace message
+               this.getLogger().trace(MessageFormat.format("resultSet={0} - CALLED!", resultSet));
+
+               // Init result instance
+               Result<? extends Storeable> result = new DatabaseResult();
+
+               // Reset result set before first row
+               resultSet.beforeFirst();
+
+               // "Walk" through all entries
+               while (resultSet.next()) {
+                       // Unwrap whole object
+                       Product product = resultSet.unwrap(Product.class);
+
+                       // Debug log
+                       this.getLogger().debug(MessageFormat.format("product={0}", product));
+
+                       // Add it to result
+                       result.add(product);
+               }
+
+               // Trace message
+               this.getLogger().trace(MessageFormat.format("result({0})={1} - EXIT!", result.size(), result));
+
+               // Return result
+               return result;
+       }
+
        /**
         * Parses given line from database backend into a Storeable instance. Please
         * note that not all backends need this.
index c7f8b72a89664c9eafd7032fa26cace3ad19f608..0ceadaa6244d72890789b1a9fe016a0551c2a6e3 100644 (file)
@@ -17,6 +17,7 @@
 package org.mxchange.pizzaapplication.database.frontend.product;
 
 import java.io.IOException;
+import java.sql.SQLException;
 import java.util.Iterator;
 import org.mxchange.jcore.database.frontend.DatabaseFrontend;
 import org.mxchange.jcore.exceptions.BadTokenException;
@@ -35,6 +36,7 @@ public interface ProductFrontend extends DatabaseFrontend {
         * @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> getProducts () throws IOException, BadTokenException;
+       public Iterator<Product> getProducts () throws IOException, BadTokenException, SQLException;
 }