X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=src%2Fjava%2Forg%2Fmxchange%2Fpizzaapplication%2Fdatabase%2Ffrontend%2Fproduct%2FPizzaProductDatabaseFrontend.java;h=8ac757b2c3340bfcc2b1ea9ceb444a16be313684;hb=d938b35b79ae531eaa7412c9f4818c0596c72f03;hp=633e44a491f7fccc8e09f0a4e9cee3ef45002f11;hpb=55a79f8336b44a108d217e3394d78ee16adb6e5d;p=pizzaservice-war.git 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 633e44a4..8ac757b2 100644 --- a/src/java/org/mxchange/pizzaapplication/database/frontend/product/PizzaProductDatabaseFrontend.java +++ b/src/java/org/mxchange/pizzaapplication/database/frontend/product/PizzaProductDatabaseFrontend.java @@ -17,20 +17,24 @@ 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 @@ -55,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 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 * @@ -83,37 +135,70 @@ public class PizzaProductDatabaseFrontend extends BaseDatabaseFrontend implement @Override public Object emptyStringToNull (final String key, final Object value) { // Trace message - this.getLogger().trace(MessageFormat.format("key={0},value={1} - CALLED!", key, value)); + 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))) { + 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)); + 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 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 getProducts () throws IOException, BadTokenException, SQLException { + public Iterator getAllProducts () 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(); + + // Run the query + Result result = this.getBackend().doSelectByCriteria(critera); + + // Debug message + this.getLogger().debug(MessageFormat.format("result={0}", result)); //NOI18N + + // Get iterator + Iterator iterator = result.iterator(); + + // Trace message + this.getLogger().trace(MessageFormat.format("iterator={0} - EXIT!", iterator)); //NOI18N + + // Get iterator and return it + return (Iterator) iterator; + } + + @Override + @SuppressWarnings ("unchecked") + public Iterator getAvailableProducts () throws IOException, BadTokenException, SQLException, CorruptedDatabaseFileException, NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException { + // Trace message + this.getLogger().trace("CALLED!"); //NOI18N + + // Instance search criteria + SearchableCriteria critera = new SearchCriteria(); // Add criteria critera.addCriteria(PizzaProductDatabaseConstants.COLUMN_AVAILABLE, true); - + // Run the query Result result = this.getBackend().doSelectByCriteria(critera); @@ -130,6 +215,12 @@ public class PizzaProductDatabaseFrontend extends BaseDatabaseFrontend implement return (Iterator) iterator; } + @Override + public String getIdName () { + // Return column id + return PizzaProductDatabaseConstants.COLUMN_ID; + } + /** * Gets a Result back from given ResultSet instance * @@ -140,7 +231,7 @@ public class PizzaProductDatabaseFrontend extends BaseDatabaseFrontend implement @Override public Result getResultFromSet (final ResultSet resultSet) throws SQLException { // Trace message - this.getLogger().trace(MessageFormat.format("resultSet={0} - CALLED!", resultSet)); + this.getLogger().trace(MessageFormat.format("resultSet={0} - CALLED!", resultSet)); //NOI18N // Init result instance Result result = new DatabaseResult(); @@ -150,23 +241,38 @@ public class PizzaProductDatabaseFrontend extends BaseDatabaseFrontend implement // "Walk" through all entries while (resultSet.next()) { - // Unwrap whole object - Product product = resultSet.unwrap(Product.class); + // 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)); + 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)); + 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. * @@ -177,12 +283,12 @@ public class PizzaProductDatabaseFrontend extends BaseDatabaseFrontend implement * @throws java.sql.SQLException If any SQL errors occur */ @Override - public boolean isProductTitleUsed (String title) throws IOException, SQLException, BadTokenException { + 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)); + this.getLogger().trace(MessageFormat.format("title={0} - CALLED!", title)); //NOI18N // Get search criteria - SearchableCritera criteria = new SearchCriteria(); + SearchableCriteria criteria = new SearchCriteria(); // Add criteria criteria.addCriteria(PizzaProductDatabaseConstants.COLUMN_TITLE, title); @@ -194,89 +300,65 @@ public class PizzaProductDatabaseFrontend extends BaseDatabaseFrontend implement Result 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())); //NOI18N // Now check size of the result boolean isFound = (result.size() == 1); // Trace message - this.getLogger().trace(MessageFormat.format("isFound={0} - EXIT!", isFound)); + this.getLogger().trace(MessageFormat.format("isFound={0} - EXIT!", isFound)); //NOI18N // Return it return isFound; } /** - * Parses given line from database backend into a Storeable instance. Please - * note that not all backends need this. + * 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 Line from database backend - * @return A Storeable instance + * @param map Map instance to convert to Storeable + * @return An instance of a Storeable implementation */ @Override - public Storeable parseLineToStoreable (final String line) throws BadTokenException { + public Storeable toStoreable (final Map map) throws NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException { // Trace message - this.getLogger().trace(MessageFormat.format("line={0} - CALLED!", line)); //NOI18N - - // Call inner method - Product product = this.parseLineToProduct(line); + 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().trace(MessageFormat.format("product={0} - EXIT!", product)); //NOI18N - - // Return it - return product; - } - - /** - * Parses given line to a Product instance - * - * @param line - * @return A Product instance from given line - */ - private Product parseLineToProduct (final String line) { - throw new UnsupportedOperationException(MessageFormat.format("Not supported yet: line={0}", line)); //NOI18N - } - - /** - * Adds product to database by given title, price and category id - * @param title Product title - * @param price Product price - * @param category Product category id - * @throws java.sql.SQLException If any SQL errors occur - */ - @Override - public void addProduct (final String title, final Float price, final Integer category) throws SQLException { - // Trace message - this.getLogger().trace(MessageFormat.format("title={0},price={1},category={2} - CALLED!", title, price, category)); + this.getLogger().debug(MessageFormat.format("Has to handle {0} entries", map.size())); //NOI18N - // Title should not be null - if (title == null) { - // Abort here - throw new NullPointerException("title is null"); - } else if (price == null) { - // Abort here - throw new NullPointerException("price is null"); - } else if (category == null) { - // Abort here - throw new NullPointerException("category is null"); - } + // Get iterator on all entries + Iterator> iterator = map.entrySet().iterator(); - // Clear dataset from previous usage - this.clearDataSet(); + // Init object instance + Storeable instance = new PizzaProduct(); - // Add title and parent - this.addToDataSet(PizzaProductDatabaseConstants.COLUMN_TITLE, title); - this.addToDataSet(PizzaProductDatabaseConstants.COLUMN_PRICE, price); - this.addToDataSet(PizzaProductDatabaseConstants.COLUMN_CATEGORY, category); + // Iterate over all + while (iterator.hasNext()) { + // Get next entry + Map.Entry entry = iterator.next(); - // Handle this over to the backend - Result result = this.doInsertDataSet(); + // Debug message + this.getLogger().debug(MessageFormat.format("entry:{0}={1}", entry.getKey(), entry.getValue())); //NOI18N - // Debug message - this.getLogger().debug(MessageFormat.format("result={0}", result)); + // Try to set value + instance.setValueFromColumn(entry.getKey(), entry.getValue()); + } // Trace message - this.getLogger().trace("EXIT!"); + this.getLogger().trace(MessageFormat.format("instance={0} - EXIT!", instance)); //NOI18N + + // Return it + return instance; } }