]> git.mxchange.org Git - pizzaservice-war.git/blobdiff - src/java/org/mxchange/pizzaapplication/database/frontend/product/PizzaProductDatabaseFrontend.java
Added important TODO to handle insert result
[pizzaservice-war.git] / src / java / org / mxchange / pizzaapplication / database / frontend / product / PizzaProductDatabaseFrontend.java
index 1fc6fe5f760d40694677f2a996f95024de270410..8ac757b2c3340bfcc2b1ea9ceb444a16be313684 100644 (file)
 package org.mxchange.pizzaapplication.database.frontend.product;
 
 import java.io.IOException;
+import java.lang.reflect.InvocationTargetException;
+import java.sql.ResultSet;
 import java.sql.SQLException;
 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;
 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.database.product.PizzaProductDatabaseConstants;
 import org.mxchange.pizzaapplication.product.Product;
+import org.mxchange.pizzaapplication.product.pizza.PizzaProduct;
 
 /**
  * Stores and retrieves Contact instances
@@ -53,6 +59,54 @@ public class PizzaProductDatabaseFrontend extends BaseDatabaseFrontend implement
                this.initBackend();
        }
 
+       /**
+        * Adds product to database by given title, price and category id
+        * @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 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 (title == null) {
+                       // Abort here
+                       throw new NullPointerException("title is null"); //NOI18N
+               } else if (price == null) {
+                       // Abort here
+                       throw new NullPointerException("price is null"); //NOI18N
+               } else if (category == null) {
+                       // Abort here
+                       throw new NullPointerException("category is null"); //NOI18N
+               } else if (available == null) {
+                       // Abort here
+                       throw new NullPointerException("available is null"); //NOI18N
+               }
+               
+               // Clear dataset from previous usage
+               this.clearDataSet();
+               
+               // Add title and parent
+               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
+               // @todo Nothing is done yet!
+               Result<? extends Storeable> 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
         * 
@@ -71,25 +125,80 @@ public class PizzaProductDatabaseFrontend extends BaseDatabaseFrontend implement
                this.getLogger().trace("EXIT!"); //NOI18N
        }
 
+       /**
+        * Depending on column, an empty value may be converted to null
+        *
+        * @param key Key to check
+        * @param value Value to check
+        * @return Possible previous value or null
+        */
+       @Override
+       public Object emptyStringToNull (final String key, final Object value) {
+               // Trace message
+               this.getLogger().trace(MessageFormat.format("key={0},value={1} - CALLED!", key, value)); //NOI18N
+
+               // Copy value
+               Object v = value;
+
+               // Is the value empty?
+               if ((value instanceof String) && ("".equals(value))) { //NOI18N
+                       // This value may need to be changed
+                       switch (key) {
+                       }
+               }
+
+               // Trace message
+               this.getLogger().trace(MessageFormat.format("v={0} - EXIT!", v)); //NOI18N
+
+               // Return it
+               return v;
+       }
+
        /**
         * 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
+        * @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> getProducts () throws IOException, BadTokenException {
+       public Iterator<Product> getAllProducts () throws IOException, BadTokenException, SQLException, CorruptedDatabaseFileException, NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
+               // Trace message
+               this.getLogger().trace("CALLED!"); //NOI18N
+
+               // Instance search criteria
+               SearchableCriteria 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;
+       }
+
+       @Override
+       @SuppressWarnings ("unchecked")
+       public Iterator<Product> getAvailableProducts () throws IOException, BadTokenException, SQLException, CorruptedDatabaseFileException, NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
                // Trace message
                this.getLogger().trace("CALLED!"); //NOI18N
 
                // Instance search criteria
-               SearchableCritera critera = new SearchCriteria();
+               SearchableCriteria critera = new SearchCriteria();
 
                // Add criteria
                critera.addCriteria(PizzaProductDatabaseConstants.COLUMN_AVAILABLE, true);
-
+               
                // Run the query
                Result<? extends Storeable> result = this.getBackend().doSelectByCriteria(critera);
 
@@ -106,35 +215,150 @@ public class PizzaProductDatabaseFrontend extends BaseDatabaseFrontend implement
                return (Iterator<Product>) iterator;
        }
 
+       @Override
+       public String getIdName () {
+               // Return column id
+               return PizzaProductDatabaseConstants.COLUMN_ID;
+       }
+
        /**
-        * Parses given line from database backend into a Storeable instance. Please
-        * note that not all backends need this.
+        * Gets a Result back from given ResultSet instance
         *
-        * @param line Line from database backend
-        * @return A Storeable instance
+        * @param resultSet ResultSet instance from SQL driver
+        * @return A typorized Result instance
+        * @throws java.sql.SQLException If any SQL error occurs
         */
        @Override
-       public Storeable parseLineToStoreable (final String line) throws BadTokenException {
+       public Result<? extends Storeable> getResultFromSet (final ResultSet resultSet) throws SQLException {
                // Trace message
-               this.getLogger().trace(MessageFormat.format("line={0} - CALLED!", line)); //NOI18N
+               this.getLogger().trace(MessageFormat.format("resultSet={0} - CALLED!", resultSet)); //NOI18N
 
-               // Call inner method
-               Product product = this.parseLineToProduct(line);
+               // Init result instance
+               Result<? extends Storeable> result = new DatabaseResult();
 
-               // Debug message
-               this.getLogger().trace(MessageFormat.format("product={0} - EXIT!", product)); //NOI18N
+               // Reset result set before first row
+               resultSet.beforeFirst();
+
+               // "Walk" through all entries
+               while (resultSet.next()) {
+                       // 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)); //NOI18N
 
+                       // Instance new object
+                       Product product = new PizzaProduct(id, title, price, category, available);
+
+                       // Debug log
+                       this.getLogger().debug(MessageFormat.format("product={0}", product)); //NOI18N
+
+                       // Add it to result
+                       result.add(product);
+               }
+
+               // Trace message
+               this.getLogger().trace(MessageFormat.format("result({0})={1} - EXIT!", result.size(), result)); //NOI18N
+
+               // Return result
+               return result;
+       }
+
+       @Override
+       public Storeable getStoreableAtRow (final int rowIndex) {
+               throw new UnsupportedOperationException("Not supported yet: rowIndex=" + rowIndex);
+       }
+
+       /**
+        * Checks wether the given product title is already used.
+        *
+        * @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 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(PizzaProductDatabaseConstants.COLUMN_TITLE, title);
+               
+               // 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())); //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 product;
+               return isFound;
        }
 
        /**
-        * Parses given line to a Product instance
+        * 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 line
-        * @return A Product instance from given line
+        * @param map Map instance to convert to Storeable
+        * @return An instance of a Storeable implementation
         */
-       private Product parseLineToProduct (final String line) {
-               throw new UnsupportedOperationException(MessageFormat.format("Not supported yet: line={0}", line)); //NOI18N
+       @Override
+       public Storeable toStoreable (final Map<String, String> map) throws NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
+               // Trace message
+               this.getLogger().trace(MessageFormat.format("map={0} - CALLED!", map)); //NOI18N
+
+               // Is map null?
+               if (map == null) {
+                       // Is null
+                       throw new NullPointerException("map is null"); //NOI18N
+               } else if (map.isEmpty()) {
+                       // Map is empty
+                       throw new IllegalArgumentException("map is empty."); //NOI18N
+               }
+
+               // Debug message
+               this.getLogger().debug(MessageFormat.format("Has to handle {0} entries", map.size())); //NOI18N
+
+               // Get iterator on all entries
+               Iterator<Map.Entry<String, String>> iterator = map.entrySet().iterator();
+
+               // Init object instance
+               Storeable instance = new PizzaProduct();
+
+               // Iterate over all
+               while (iterator.hasNext()) {
+                       // Get next entry
+                       Map.Entry<String, String> entry = iterator.next();
+
+                       // Debug message
+                       this.getLogger().debug(MessageFormat.format("entry:{0}={1}", entry.getKey(), entry.getValue())); //NOI18N
+
+                       // Try to set value
+                       instance.setValueFromColumn(entry.getKey(), entry.getValue());
+               }
+
+               // Trace message
+               this.getLogger().trace(MessageFormat.format("instance={0} - EXIT!", instance)); //NOI18N
+
+               // Return it
+               return instance;
        }
 }