X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=src%2Fjava%2Forg%2Fmxchange%2Fpizzaapplication%2Fdatabase%2Ffrontend%2Fcategory%2FPizzaCategoryDatabaseFrontend.java;h=6f98dd40a4701beeebfde441bda8f43a53ee8acf;hb=d938b35b79ae531eaa7412c9f4818c0596c72f03;hp=36ecbbe8fae0d48a9c6259b33342de74e6b33951;hpb=d29bcbc50afc0a1884985fa8b6e6c037c8f47e41;p=pizzaservice-war.git diff --git a/src/java/org/mxchange/pizzaapplication/database/frontend/category/PizzaCategoryDatabaseFrontend.java b/src/java/org/mxchange/pizzaapplication/database/frontend/category/PizzaCategoryDatabaseFrontend.java index 36ecbbe8..6f98dd40 100644 --- a/src/java/org/mxchange/pizzaapplication/database/frontend/category/PizzaCategoryDatabaseFrontend.java +++ b/src/java/org/mxchange/pizzaapplication/database/frontend/category/PizzaCategoryDatabaseFrontend.java @@ -17,20 +17,25 @@ package org.mxchange.pizzaapplication.database.frontend.category; 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.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 @@ -38,7 +43,6 @@ import org.mxchange.pizzaapplication.database.category.PizzaCategoryDatabaseCons * @author Roland Haeder */ public class PizzaCategoryDatabaseFrontend extends BaseDatabaseFrontend implements CategoryFrontend { - /** * Default constrcutor * @throws org.mxchange.jcore.exceptions.UnsupportedDatabaseBackendException If the configured backend is not supported @@ -55,6 +59,41 @@ public class PizzaCategoryDatabaseFrontend extends BaseDatabaseFrontend implemen this.initBackend(); } + /** + * Adds given category title as new category, parent may be null if not selected. + * + * @param title Title of category + * @param parent Parent id or null if not selected + */ + @Override + public void addCategory (final String title, final Integer parent) throws SQLException, IOException { + // Trace message + this.getLogger().trace(MessageFormat.format("title={0},parent={1} - CALLED!", title, parent)); //NOI18N + + // Title should not be null + if (title == null) { + // Abort here + throw new NullPointerException("title is null"); //NOI18N + } + + // Clear dataset from previous usage + this.clearDataSet(); + + // Add title and parent + this.addToDataSet(PizzaCategoryDatabaseConstants.COLUMN_TITLE, title); + this.addToDataSet(PizzaCategoryDatabaseConstants.COLUMN_PARENT, parent); + + // 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 @@ -72,14 +111,46 @@ public class PizzaCategoryDatabaseFrontend extends BaseDatabaseFrontend implemen 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) { + case PizzaCategoryDatabaseConstants.COLUMN_PARENT: // Convert this + v = null; + break; + } + } + + // Trace message + this.getLogger().trace(MessageFormat.format("v={0} - EXIT!", v)); //NOI18N + + // Return it + return v; + } + @Override @SuppressWarnings ("unchecked") - public Iterator getCategories () throws IOException, BadTokenException, SQLException { + public Iterator getCategories () 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); @@ -97,6 +168,72 @@ public class PizzaCategoryDatabaseFrontend extends BaseDatabaseFrontend implemen return (Iterator) iterator; } + @Override + public Category getCategory (final Product product) throws IOException, BadTokenException, CorruptedDatabaseFileException, SQLException, NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException { + // Trace message + this.getLogger().trace(MessageFormat.format("product={0} - CALLED!", product)); //NOI18N + + // product must not be null + if (product == null) { + // Abort here + throw new NullPointerException("product is null"); //NOI18N + } + + // Get category id from it + Long id = product.getCategory(); + + // Debug message + this.getLogger().debug(MessageFormat.format("id={0}", id)); //NOI18N + + // It should be >0 here + assert(id > 0) : MessageFormat.format("id={0} must be larger zero", id); //NOI18N + + // 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 result = this.getBackend().doSelectByCriteria(criteria); + + // Debug log + this.getLogger().debug(MessageFormat.format("result({0})={1}", result, result.size())); //NOI18N + + // 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(MessageFormat.format("storeable={0}", storeable)); //NOI18N + + // Is it instance of Category? + if (storeable instanceof Category) { + // Then cast it + category = (Category) storeable; + } + } + + // Trace message + this.getLogger().trace(MessageFormat.format("category={0} - EXIT!", category)); //NOI18N + + // Return it + return category; + } + + @Override + public String getIdName () { + // Return column id + return PizzaCategoryDatabaseConstants.COLUMN_ID; + } + /** * Gets a Result back from given ResultSet instance * @@ -107,7 +244,7 @@ public class PizzaCategoryDatabaseFrontend extends BaseDatabaseFrontend implemen @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(); @@ -117,53 +254,34 @@ public class PizzaCategoryDatabaseFrontend extends BaseDatabaseFrontend implemen // "Walk" through all entries while (resultSet.next()) { - // Unwrap whole object - Category category = resultSet.unwrap(Category.class); + // Get id, title and parent id + Long id = resultSet.getLong(PizzaCategoryDatabaseConstants.COLUMN_ID); + String title = resultSet.getString(PizzaCategoryDatabaseConstants.COLUMN_TITLE); + Long parent = resultSet.getLong(PizzaCategoryDatabaseConstants.COLUMN_PARENT); + + // Debug message + this.getLogger().debug(MessageFormat.format("id={0},title={1},parent={2}", id, title, parent)); //NOI18N + + // Instance new object + Category category = new ProductCategory(id, title, parent); // Debug log - this.getLogger().debug(MessageFormat.format("category={0}", category)); + this.getLogger().debug(MessageFormat.format("category={0}", category)); //NOI18N // Add it to result result.add(category); } // 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; } - /** - * Parses given line from database backend into a Storeable instance. Please - * note that not all backends need this. - * - * @param line Line from database backend - * @return A Storeable instance - */ @Override - public Storeable parseLineToStoreable (final String line) throws BadTokenException { - // Trace message - this.getLogger().trace(MessageFormat.format("line={0} - CALLED!", line)); //NOI18N - - // Call inner method - Category category = this.parseLineToCategory(line); - - // Debug message - this.getLogger().trace(MessageFormat.format("category={0} - EXIT!", category)); //NOI18N - - // Return it - return category; - } - - /** - * Parses given line to a Category instance - * - * @param line Raw, decoded line from a file-based backend - * @return A Category instance from given line - */ - private Category parseLineToCategory (final String line) { - throw new UnsupportedOperationException(MessageFormat.format("Not supported yet: line={0}", line)); //NOI18N + public Storeable getStoreableAtRow (final int rowIndex) { + throw new UnsupportedOperationException("Not supported yet: rowIndex=" + rowIndex); } /** @@ -173,12 +291,12 @@ public class PizzaCategoryDatabaseFrontend extends BaseDatabaseFrontend implemen * @return Whether the title has been used */ @Override - public boolean isCategoryTitleUsed (final String title) throws IOException, SQLException, BadTokenException { + public boolean isCategoryTitleUsed (final 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(PizzaCategoryDatabaseConstants.COLUMN_TITLE, title); @@ -190,15 +308,65 @@ public class PizzaCategoryDatabaseFrontend extends BaseDatabaseFrontend implemen 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; } + + /** + * 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 + */ + @Override + public Storeable toStoreable (final Map 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> iterator = map.entrySet().iterator(); + + // Init object instance + Storeable instance = new ProductCategory(); + + // Iterate over all + while (iterator.hasNext()) { + // Get next entry + Map.Entry 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; + } }