* Adds given category data from request to database
*
* @param request Request instance
+ * @throws javax.servlet.ServletException If something unexpected happened
*/
- public void doAdminAddCategory (final HttpServletRequest request);
+ public void doAdminAddCategory (final HttpServletRequest request) throws ServletException;
/**
* Adds given product data from request to database
*
* @param request Request instance
+ * @throws javax.servlet.ServletException If something unexpected happened
*/
- public void doAdminAddProduct (final HttpServletRequest request);
+ public void doAdminAddProduct (final HttpServletRequest request) throws ServletException;
}
import org.mxchange.pizzaapplication.category.Category;
import org.mxchange.pizzaapplication.customer.Customer;
import org.mxchange.pizzaapplication.customer.PizzaServiceCustomer;
+import org.mxchange.pizzaapplication.database.category.PizzaCategoryDatabaseConstants;
import org.mxchange.pizzaapplication.database.frontend.category.CategoryFrontend;
import org.mxchange.pizzaapplication.database.frontend.category.PizzaCategoryDatabaseFrontend;
import org.mxchange.pizzaapplication.database.frontend.product.PizzaProductDatabaseFrontend;
this.categoryFrontend = new PizzaCategoryDatabaseFrontend();
}
+ /**
+ * Checks whether given category title is already used
+ * @param title Title of category to check
+ * @return Whether it has been found
+ */
+ private boolean isCategoryUsed (final String title) throws IOException, SQLException, BadTokenException {
+ // Delegate to frontend
+ return this.categoryFrontend.isCategoryTitleUsed(title);
+ }
+
/**
* Checks if the product ordered?
*
* @param request Request instance
*/
@Override
- public void doAdminAddCategory (final HttpServletRequest request) {
+ public void doAdminAddCategory (final HttpServletRequest request) throws ServletException {
// Trace message
this.getLogger().trace(MessageFormat.format("request={0} - CALLED!", request)); //NOI18N
+ // request must not be null
+ if (request == null) {
+ // Is null
+ throw new NullPointerException("request is null");
+ }
+
+ // Check if all fields are given
+ if (request.getParameter(PizzaCategoryDatabaseConstants.COLUMN_TITLE) == null) {
+ // "title" not set
+ throw new IllegalArgumentException(MessageFormat.format("{0} is not set.", PizzaCategoryDatabaseConstants.COLUMN_TITLE));
+ } else if (request.getParameter(PizzaCategoryDatabaseConstants.COLUMN_TITLE).isEmpty()) {
+ // Is left empty
+ throw new IllegalArgumentException(MessageFormat.format("{0} is empty.", PizzaCategoryDatabaseConstants.COLUMN_TITLE));
+ } else if ((request.getParameter(PizzaCategoryDatabaseConstants.COLUMN_PARENT) != null) && (!request.getParameter(PizzaCategoryDatabaseConstants.COLUMN_PARENT).isEmpty())) {
+ // "parent" is set, so check it
+ try {
+ Integer dummy = Integer.parseInt(request.getParameter(PizzaCategoryDatabaseConstants.COLUMN_PARENT));
+ } catch (final NumberFormatException e) {
+ // Not valid number
+ throw new IllegalArgumentException(e);
+ }
+ } else {
+ try {
+ // Try to check if title is used already
+ if (this.isCategoryUsed(request.getParameter(PizzaCategoryDatabaseConstants.COLUMN_TITLE))) {
+ // Title already used
+ throw new IllegalArgumentException(MessageFormat.format("Title {0} is already used.", request.getParameter(PizzaCategoryDatabaseConstants.COLUMN_TITLE)));
+ }
+ } catch (final IOException | SQLException | BadTokenException ex) {
+ throw new ServletException(ex);
+ }
+ }
+
// Trace message
this.getLogger().trace("EXIT!"); //NOI18N
}
// Trace message
this.getLogger().trace(MessageFormat.format("request={0} - CALLED!", request)); //NOI18N
+ // request must not be null
+ if (request == null) {
+ // Is null
+ throw new NullPointerException("request is null");
+ }
+
+ // Check if all fields are given
// Trace message
this.getLogger().trace("EXIT!"); //NOI18N
}
* @throws java.sql.SQLException If any SQL error occurs
*/
public Iterator<Category> getCategories () throws IOException, BadTokenException, SQLException;
+
+ /**
+ * Checks if given category title is already used
+ *
+ * @param title Title to check
+ * @return Whether the title has been 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 error occurs
+ */
+ public boolean isCategoryTitleUsed (final String title) throws IOException, SQLException, BadTokenException;
}
import org.mxchange.jcore.exceptions.BadTokenException;
import org.mxchange.jcore.exceptions.UnsupportedDatabaseBackendException;
import org.mxchange.pizzaapplication.category.Category;
+import org.mxchange.pizzaapplication.database.category.PizzaCategoryDatabaseConstants;
/**
* Stores and retrieves Contact instances
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;
+ }
}