import java.text.MessageFormat;
import java.util.Iterator;
import java.util.Map;
-import java.util.logging.Level;
-import java.util.logging.Logger;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.mxchange.jcore.contact.Gender;
import org.mxchange.jcore.exceptions.BadTokenException;
* @param session Session instance
* @return Amount as string
*/
- private String handleChooseFromRequestSession(final Product product, final HttpServletRequest request, final HttpSession session) {
+ private String handleChooseFromRequestSession (final Product product, final HttpServletRequest request, final HttpSession session) {
// Trace message
this.getLogger().trace(MessageFormat.format("product={0},request={1},session={2} - CALLED!", product, request, session)); //NOI18N
* @param title Title of category to check
* @return Whether it has been found
*/
- private boolean isCategoryTitleUsed (final String title) throws IOException, SQLException, BadTokenException, CorruptedDatabaseFileException, NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
+ private boolean isCategoryTitleUsed(final String title) throws IOException, SQLException, BadTokenException, CorruptedDatabaseFileException, NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
// Delegate to frontend
return this.categoryFrontend.isCategoryTitleUsed(title);
}
* @param session HttpSession instance
* @return
*/
- private boolean isProductOrdered(final Product product, final HttpSession session) {
+ private boolean isProductOrdered (final Product product, final HttpSession session) {
// Trace message
this.getLogger().trace(MessageFormat.format("product={0},session={1} - CALLED!", product, session)); //NOI18N
/**
* Application starter
*/
- private void start () {
+ private void start() {
// Init bundle
this.initBundle();
Customer customer = new PizzaServiceCustomer();
/*
- * Need a least a gender ... :( See, that is why I don't like default
- * constructors, you can easily miss something important and bam! You
- * get an NPE. The fix here is, to have construtors (or factories) which
- * requires all required instances that needs to be set to get a
- * consitent object back.
- */
-
+ * Need a least a gender ... :( See, that is why I don't like default
+ * constructors, you can easily miss something important and bam! You
+ * get an NPE. The fix here is, to have construtors (or factories) which
+ * requires all required instances that needs to be set to get a
+ * consitent object back.
+ */
+
// Gender is MALE now
customer.setGender(Gender.MALE);
return title;
}
- @Override
- public boolean isProductTitleUsed (final HttpServletRequest request) throws ServletException {
+ /**
+ * Checks if product's title is already used.
+ *
+ * @param request Request instance
+ * @return Whether the product title is already 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
+ * @throws org.mxchange.jcore.exceptions.CorruptedDatabaseFileException If the database file is damaged
+ * @throws java.lang.NoSuchMethodException If a method was not found
+ * @throws java.lang.IllegalAccessException If the method cannot be accessed
+ * @throws java.lang.reflect.InvocationTargetException Any other problems?
+ */
+ private boolean isProductTitleUsed (final HttpServletRequest request) throws IOException, SQLException, BadTokenException, CorruptedDatabaseFileException, NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
// Trace message
this.getLogger().trace("request=" + request + " - CALLED!");
}
// Default is not used
- boolean isUsed = false;
+ boolean isUsed = this.isProductTitleUsed(title);
+
+ // Trace message
+ this.getLogger().trace("isUsed=" + isUsed + " - EXIT!");
+
+ // Return it
+ return isUsed;
+ }
+
+ /**
+ * Handles admin form requests
+ * @param request Request instance
+ * @param response Response instance
+ * @throws ServletException If something unexpected happened
+ */
+ @Override
+ public void doAdminHandleProductForms (final HttpServletRequest request, final HttpServletResponse response) throws ServletException {
+ // Trace message
+ this.getLogger().trace("request=" + request + ",response=" + response + " - CALLED!");
+ // request and response must both be set
+ if (request == null) {
+ // request is null
+ throw new NullPointerException("request is null");
+ } else if (response == null) {
+ // response is null
+ throw new NullPointerException("response is null");
+ }
+
+ // Try this operations
try {
- // So that all is tested, try to find the product
- isUsed = this.isProductTitleUsed(title);
- } catch (final IOException | SQLException | BadTokenException | CorruptedDatabaseFileException | NoSuchMethodException | IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) {
- // Continue to throw all as cause
+ // Is it post?
+ if ("POST".equals(request.getMethod())) { //NOI18N
+ // Is "add/edit/delete" set?
+ if (request.getParameter("add") != null) { //NOI18N
+ // Is it already added?
+ if (this.isProductTitleUsed(request)) {
+ // Debug message
+ this.getLogger().debug("Already used, redirecting ...");
+
+ // Already added, so redirect here, else a ServletException will be thrown
+ response.sendRedirect(request.getContextPath() + "/admin/product.jsp?already=1");
+ } else {
+ // Add new product
+ this.doAdminAddProduct(request);
+ }
+ } else if (request.getParameter("edit") != null) { //NOI18N
+ // @TODO
+ } else if (request.getParameter("delete") != null) { //NOI18N
+ // @TODO
+ }
+
+ // Redirect to proper URL
+ // @TODO Commented out for developing:
+ //response.sendRedirect(request.getContextPath() + "/finished.jsp");
+ }
+ } catch (final IOException | SQLException | BadTokenException | CorruptedDatabaseFileException | ProductTitleAlreadyUsedException | NoSuchMethodException | IllegalAccessException | InvocationTargetException | IllegalArgumentException ex) {
+ // Throw it as cause
throw new ServletException(ex);
}
+ // Trace message
+ this.getLogger().trace("EXIT!");
+ }
+
+ /**
+ * Handles admin form requests
+ * @param request Request instance
+ * @param response Response instance
+ * @throws ServletException If something unexpected happened
+ */
+ @Override
+ public void doAdminHandleCategoryForms (final HttpServletRequest request, final HttpServletResponse response) throws ServletException {
+ // Trace message
+ this.getLogger().trace("request=" + request + ",response=" + response + " - CALLED!");
+
+ // request and response must both be set
+ if (request == null) {
+ // request is null
+ throw new NullPointerException("request is null");
+ } else if (response == null) {
+ // response is null
+ throw new NullPointerException("response is null");
+ }
+
+ // Try this operations
+ try {
+ // Is it post?
+ if ("POST".equals(request.getMethod())) { //NOI18N
+ // Is "add/edit/delete" set?
+ if (request.getParameter("add") != null) { //NOI18N
+ // Is the category title already used?
+ if (this.isCategoryTitleUsed(request)) {
+ // Debug message
+ this.getLogger().debug("Already used, redirecting ...");
+
+ // Already added, so redirect here, else a ServletException will be thrown
+ response.sendRedirect(request.getContextPath() + "/admin/category.jsp?already=1");
+ } else {
+ // Add new category
+ this.doAdminAddCategory(request);
+ }
+ } else if (request.getParameter("edit") != null) { //NOI18N
+ // @TODO
+ } else if (request.getParameter("delete") != null) { //NOI18N
+ // @TODO
+ }
+
+ // Redirect to proper URL
+ // @TODO Commented out for developing:
+ //response.sendRedirect(request.getContextPath() + "/finished.jsp");
+ }
+ } catch (final IOException | SQLException | BadTokenException | CorruptedDatabaseFileException | CategoryTitleAlreadyUsedException | NoSuchMethodException | IllegalAccessException | InvocationTargetException | IllegalArgumentException ex) {
+ // Throw it as cause
+ throw new ServletException(ex);
+ }
+
+ // Trace message
+ this.getLogger().trace("EXIT!");
+ }
+
+ /**
+ * Checks if category's title is already used.
+ *
+ * @param request Request instance
+ * @return Whether the product title is already 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
+ * @throws org.mxchange.jcore.exceptions.CorruptedDatabaseFileException If the database file is damaged
+ * @throws java.lang.NoSuchMethodException If a method was not found
+ * @throws java.lang.IllegalAccessException If the method cannot be accessed
+ * @throws java.lang.reflect.InvocationTargetException Any other problems?
+ */
+ private boolean isCategoryTitleUsed (final HttpServletRequest request) throws IOException, SQLException, BadTokenException, CorruptedDatabaseFileException, NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
+ // Trace message
+ this.getLogger().trace("request=" + request + " - CALLED!");
+
+ // Init title
+ String title = request.getParameter(PizzaCategoryDatabaseConstants.COLUMN_TITLE);
+
+ // request must not be null and "title" must be found and non-empty
+ if (request == null) {
+ // Abort here
+ throw new NullPointerException("request is null");
+ } else if (title == null) {
+ // title is not set
+ throw new IllegalArgumentException(MessageFormat.format("{0} is not set.", PizzaCategoryDatabaseConstants.COLUMN_TITLE)); //NOI18N
+ } else if (title.isEmpty()) {
+ // Is left empty
+ throw new IllegalArgumentException(MessageFormat.format("{0} is empty.", PizzaCategoryDatabaseConstants.COLUMN_TITLE)); //NOI18N
+ }
+
+ // Default is not used
+ boolean isUsed = this.isCategoryTitleUsed(title);
+
// Trace message
this.getLogger().trace("isUsed=" + isUsed + " - EXIT!");