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=e9e76f4c4d3293640e2c1d57278103e1d9f65e0c;hpb=8b2aec4791dfe5498f9fff0fc63b55a08ec73340;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 e9e76f4c..6f98dd40 100644 --- a/src/java/org/mxchange/pizzaapplication/database/frontend/category/PizzaCategoryDatabaseFrontend.java +++ b/src/java/org/mxchange/pizzaapplication/database/frontend/category/PizzaCategoryDatabaseFrontend.java @@ -16,22 +16,26 @@ */ package org.mxchange.pizzaapplication.database.frontend.category; -import org.mxchange.pizzaapplication.category.product.ProductCategory; 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 @@ -62,14 +66,14 @@ public class PizzaCategoryDatabaseFrontend extends BaseDatabaseFrontend implemen * @param parent Parent id or null if not selected */ @Override - public void addCategory (final String title, final Integer parent) throws SQLException { + 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)); + 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"); + throw new NullPointerException("title is null"); //NOI18N } // Clear dataset from previous usage @@ -80,13 +84,14 @@ public class PizzaCategoryDatabaseFrontend extends BaseDatabaseFrontend implemen 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)); + this.getLogger().debug(MessageFormat.format("result={0}", result)); //NOI18N // Trace message - this.getLogger().trace("EXIT!"); + this.getLogger().trace("EXIT!"); //NOI18N } /** @@ -116,13 +121,13 @@ public class PizzaCategoryDatabaseFrontend extends BaseDatabaseFrontend implemen @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) { case PizzaCategoryDatabaseConstants.COLUMN_PARENT: // Convert this @@ -132,7 +137,7 @@ public class PizzaCategoryDatabaseFrontend extends BaseDatabaseFrontend implemen } // 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; @@ -140,12 +145,12 @@ public class PizzaCategoryDatabaseFrontend extends BaseDatabaseFrontend implemen @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); @@ -163,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 * @@ -173,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(); @@ -184,30 +255,35 @@ public class PizzaCategoryDatabaseFrontend extends BaseDatabaseFrontend implemen // "Walk" through all entries while (resultSet.next()) { // Get id, title and parent id - Integer id = resultSet.getInt(PizzaCategoryDatabaseConstants.COLUMN_ID); + Long id = resultSet.getLong(PizzaCategoryDatabaseConstants.COLUMN_ID); String title = resultSet.getString(PizzaCategoryDatabaseConstants.COLUMN_TITLE); - Integer parent = resultSet.getInt(PizzaCategoryDatabaseConstants.COLUMN_PARENT); + Long parent = resultSet.getLong(PizzaCategoryDatabaseConstants.COLUMN_PARENT); // Debug message - this.getLogger().debug(MessageFormat.format("id={0},title={1},parent={2}", id, title, parent)); + 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; } + @Override + public Storeable getStoreableAtRow (final int rowIndex) { + throw new UnsupportedOperationException("Not supported yet: rowIndex=" + rowIndex); + } + /** * Checks if given category title is already used * @@ -215,64 +291,82 @@ 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); - + // 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())); - + 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 - Category category = this.parseLine(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("category={0} - EXIT!", category)); //NOI18N + this.getLogger().debug(MessageFormat.format("Has to handle {0} entries", map.size())); //NOI18N - // Return it - return category; - } + // Get iterator on all entries + Iterator> iterator = map.entrySet().iterator(); - /** - * 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 parseLine (final String line) { - throw new UnsupportedOperationException(MessageFormat.format("Not supported yet: line={0}", line)); //NOI18N + // 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; } }