}
}
+ // Get all data from it
+ String title = request.getParameter(PizzaCategoryDatabaseConstants.COLUMN_TITLE);
+ String parent = request.getParameter(PizzaCategoryDatabaseConstants.COLUMN_PARENT);
+
+ try {
+ // The category is not found, so add it to database
+ this.categoryFrontend.addCategory(title, parent);
+ } catch (final SQLException ex) {
+ // Continue to throw it
+ throw new ServletException(ex);
+ }
+
// Trace message
this.getLogger().trace("EXIT!"); //NOI18N
}
--- /dev/null
+/*
+ * Copyright (C) 2015 Roland Haeder
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+package org.mxchange.pizzaapplication.category;
+
+import java.text.MessageFormat;
+import java.util.Objects;
+import org.mxchange.jcore.BaseFrameworkSystem;
+
+/**
+ * A general product category class
+ *
+ * @author Roland Haeder
+ */
+public class BaseCategory extends BaseFrameworkSystem implements Category {
+ /**
+ * Id number of category
+ */
+ private Integer id;
+
+ /**
+ * Parent category id
+ */
+ private Integer parent;
+
+ /**
+ * Title of category
+ */
+ private String title;
+
+ /**
+ * Constructor which accepts all database fields
+ * @param id Id number of database record
+ * @param title Category title
+ * @param parent Parent id
+ */
+ protected BaseCategory (final Integer id, final String title, final Integer parent) {
+ // Set all here
+ this.setId(id);
+ this.setTitle(title);
+ this.setParent(parent);
+ }
+
+ /**
+ * Compares two categories with each other
+ *
+ * @param category Category comparator
+ * @return Comparison value
+ */
+ @Override
+ public int compareTo (final Category category) {
+ // Trace message
+ this.getLogger().trace(MessageFormat.format("category={0} - CALLED!", category));
+
+ // category should not be null
+ if (category == null) {
+ throw new NullPointerException("category is null");
+ }
+
+ // Debug message
+ this.getLogger().debug(MessageFormat.format("this.id={0},category.id={1}", this.getId(), category.getId()));
+
+ // Is the id the same?
+ if (Objects.equals(this.getId(), category.getId())) {
+ // Same id, means same category
+ return 0;
+ } else if (this.getId() > category.getId()) {
+ // This id is larger than compared to
+ return -1;
+ }
+
+ // The other id is larger
+ return 1;
+ }
+
+ /**
+ * Id number of category
+ * @return the id
+ */
+ @Override
+ public final Integer getId () {
+ return this.id;
+ }
+
+ /**
+ * Id number of category
+ * @param id the id to set
+ */
+ @Override
+ public final void setId (final Integer id) {
+ this.id = id;
+ }
+
+ /**
+ * Parent category id
+ * @return the parent
+ */
+ @Override
+ public final Integer getParent () {
+ return this.parent;
+ }
+
+ /**
+ * Parent category id
+ * @param parent the parent to set
+ */
+ @Override
+ public final void setParent (final Integer parent) {
+ this.parent = parent;
+ }
+
+ /**
+ * Title of category
+ * @return the title
+ */
+ @Override
+ public final String getTitle () {
+ return this.title;
+ }
+
+ /**
+ * Title of category
+ * @param title the title to set
+ */
+ @Override
+ public final void setTitle (final String title) {
+ this.title = title;
+ }
+}
*
* @author Roland Haeder
*/
-public interface Category extends Storeable {
+public interface Category extends Storeable, Comparable<Category> {
+
+ /**
+ * Id number of category
+ * @return the id
+ */
+ public Integer getId ();
+
+ /**
+ * Id number of category
+ * @param id the id to set
+ */
+ public void setId (final Integer id);
+
+ /**
+ * Parent category id
+ * @return the parent
+ */
+ public Integer getParent ();
+
+ /**
+ * Parent category id
+ * @param parent the parent to set
+ */
+ public void setParent (final Integer parent);
+
+ /**
+ * Title of category
+ * @return the title
+ */
+ public String getTitle ();
+
+ /**
+ * Title of category
+ * @param title the title to set
+ */
+ public void setTitle (final String title);
+
+ /**
+ * Compare method
+ * @param category Category to compare to
+ */
+ @Override
+ public int compareTo (final Category category);
}
--- /dev/null
+/*
+ * Copyright (C) 2015 Roland Haeder
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+package org.mxchange.pizzaapplication.category.product;
+
+import org.mxchange.pizzaapplication.category.BaseCategory;
+
+/**
+ * A product category
+ * @author Roland Haeder
+ */
+public class ProductCategory extends BaseCategory {
+ /**
+ * Constructor which accepts all database fields
+ * @param id Id number of database record
+ * @param title Category title
+ * @param parent Parent id
+ */
+ public ProductCategory (final Integer id, final String title, final Integer parent) {
+ // Call parent constructor
+ super(id, title, parent);
+ }
+}
*/
public interface CategoryFrontend extends DatabaseFrontend {
+ /**
+ * 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
+ * @throws java.sql.SQLException If any SQL error occurs
+ */
+ public void addCategory (final String title, final String parent) throws SQLException;
+
/**
* An iterator on all categories
*
*/
package org.mxchange.pizzaapplication.database.frontend.category;
+import org.mxchange.pizzaapplication.category.product.ProductCategory;
import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
* @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
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 String parent) throws SQLException {
+ // Trace message
+ this.getLogger().trace(MessageFormat.format("title={0},parent={1} - CALLED!", title, parent));
+
+ // Title should not be null
+ if (title == null) {
+ // Abort here
+ throw new NullPointerException("title is null");
+ }
+
+ // 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
+ Result<? extends Storeable> result = this.doInsertDataSet();
+
+ // Debug message
+ this.getLogger().debug(MessageFormat.format("result={0}", result));
+
+ // Trace message
+ this.getLogger().trace("EXIT!");
+ }
+
/**
* Shuts down the database layer
* @throws java.sql.SQLException If an SQL error occurs
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));
+
+ // Copy value
+ Object v = value;
+
+ // Is the value empty?
+ if ((value instanceof String) && ("".equals(value))) {
+ // 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));
+
+ // Return it
+ return v;
+ }
+
@Override
@SuppressWarnings ("unchecked")
public Iterator<Category> getCategories () throws IOException, BadTokenException, SQLException {
// "Walk" through all entries
while (resultSet.next()) {
- // Unwrap whole object
- Category category = resultSet.unwrap(Category.class);
+ // Get id, title and parent id
+ Integer id = resultSet.getInt(PizzaCategoryDatabaseConstants.COLUMN_ID);
+ String title = resultSet.getString(PizzaCategoryDatabaseConstants.COLUMN_PARENT);
+ Integer parent = resultSet.getInt(PizzaCategoryDatabaseConstants.COLUMN_PARENT);
+
+ // Debug message
+ this.getLogger().debug(MessageFormat.format("id={0},title={1},parent={2}", id, title, parent));
+
+ // Instance new object
+ Category category = new ProductCategory(id, title, parent);
// Debug log
this.getLogger().debug(MessageFormat.format("category={0}", category));
return result;
}
+ /**
+ * Checks if given category title is already used
+ *
+ * @param title Title to check
+ * @return Whether the title has been used
+ */
+ @Override
+ public boolean isCategoryTitleUsed (final String title) throws IOException, SQLException, BadTokenException {
+ // Trace message
+ this.getLogger().trace(MessageFormat.format("title={0} - CALLED!", title));
+
+ // Get search criteria
+ SearchableCritera criteria = new SearchCriteria();
+
+ // Add criteria
+ criteria.addCriteria(PizzaCategoryDatabaseConstants.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()));
+
+ // Now check size of the result
+ boolean isFound = (result.size() == 1);
+
+ // Trace message
+ this.getLogger().trace(MessageFormat.format("isFound={0} - EXIT!", isFound));
+
+ // Return it
+ return isFound;
+ }
+
/**
* Parses given line from database backend into a Storeable instance. Please
* note that not all backends need this.
private Category parseLineToCategory (final String line) {
throw new UnsupportedOperationException(MessageFormat.format("Not supported yet: line={0}", line)); //NOI18N
}
-
- /**
- * Checks if given category title is already used
- *
- * @param title Title to check
- * @return Whether the title has been used
- */
- @Override
- public boolean isCategoryTitleUsed (final String title) throws IOException, SQLException, BadTokenException {
- // Trace message
- this.getLogger().trace(MessageFormat.format("title={0} - CALLED!", title));
-
- // Get search criteria
- SearchableCritera criteria = new SearchCriteria();
-
- // Add criteria
- criteria.addCriteria(PizzaCategoryDatabaseConstants.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()));
-
- // Now check size of the result
- boolean isFound = (result.size() == 1);
-
- // Trace message
- this.getLogger().trace(MessageFormat.format("isFound={0} - EXIT!", isFound));
-
- // Return it
- return isFound;
- }
}
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));
+
+ // Copy value
+ Object v = value;
+
+ // Is the value empty?
+ if ((value instanceof String) && ("".equals(value))) {
+ // This value may need to be changed
+ switch (key) {
+ }
+ }
+
+ // Trace message
+ this.getLogger().trace(MessageFormat.format("v={0} - EXIT!", v));
+
+ // Return it
+ return v;
+ }
+
@Override
@SuppressWarnings ("unchecked")
public Iterator<Product> getProducts () throws IOException, BadTokenException, SQLException {
${category.getTitle()}
</td>
<td>
- ${category.getPrintableParent()}
+ ${app.generateLinkForParent(category)}
</td>
</tr>
</c:forEach>