+
+ @Override
+ public String getPrintableProduktCategory (final Product product) throws ServletException {
+ // 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
+ }
+
+ // Declare category
+ Category category;
+
+ try {
+ // Get Category instance from product over the frontend
+ category = this.categoryFrontend.getCategory(product);
+ } catch (final IOException | SQLException | BadTokenException | CorruptedDatabaseFileException | NoSuchMethodException | IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) {
+ throw new ServletException(ex);
+ }
+
+ // Debug message
+ this.getLogger().debug(MessageFormat.format("category={0}", category)); //NOI18N
+
+ String title = null;
+ try {
+ // Now get title from it and return it
+ title = category.decodedTitle();
+ } catch (final UnsupportedEncodingException ex) {
+ // Continue to throw as cause
+ throw new ServletException(ex);
+ }
+
+ // Trace message
+ this.getLogger().trace(MessageFormat.format("title={0} - EXIT!", title)); //NOI18N
+
+ // Return it
+ return title;
+ }
+
+ /**
+ * 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(MessageFormat.format("request={0} - CALLED!", request)); //NOI18N
+
+ // Init title
+ String title = request.getParameter(PizzaProductDatabaseConstants.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"); //NOI18N
+ } else if (title == null) {
+ // title is not set
+ throw new IllegalArgumentException(MessageFormat.format("{0} is not set.", PizzaProductDatabaseConstants.COLUMN_TITLE)); //NOI18N
+ } else if (title.isEmpty()) {
+ // Is left empty
+ throw new IllegalArgumentException(MessageFormat.format("{0} is empty.", PizzaProductDatabaseConstants.COLUMN_TITLE)); //NOI18N
+ }
+
+ // Default is not used
+ boolean isUsed = this.isProductTitleUsed(title);
+
+ // Trace message
+ this.getLogger().trace(MessageFormat.format("isUsed={0} - EXIT!", isUsed)); //NOI18N
+
+ // 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(MessageFormat.format("request={0},response={1} - CALLED!", request, response)); //NOI18N
+
+ // request and response must both be set
+ if (request == null) {
+ // request is null
+ throw new NullPointerException("request is null"); //NOI18N
+ } else if (response == null) {
+ // response is null
+ throw new NullPointerException("response is null"); //NOI18N
+ }
+
+ // 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 it already added?
+ if (this.isProductTitleUsed(request)) {
+ // Debug message
+ this.getLogger().debug("Already used, redirecting ..."); //NOI18N
+
+ // Already added, so redirect here, else a ServletException will be thrown
+ response.sendRedirect(String.format("%s/admin/product.jsp?already=1", request.getContextPath())); //NOI18N
+ } 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!"); //NOI18N
+ }
+
+ /**
+ * 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(MessageFormat.format("request={0},response={1} - CALLED!", request, response)); //NOI18N
+
+ // request and response must both be set
+ if (request == null) {
+ // request is null
+ throw new NullPointerException("request is null"); //NOI18N
+ } else if (response == null) {
+ // response is null
+ throw new NullPointerException("response is null"); //NOI18N
+ }
+
+ // 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 ..."); //NOI18N
+
+ // Already added, so redirect here, else a ServletException will be thrown
+ response.sendRedirect(String.format("%s/admin/category.jsp?already=1", request.getContextPath())); //NOI18N
+ } 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!"); //NOI18N
+ }
+
+ /**
+ * 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(MessageFormat.format("request={0} - CALLED!", request)); //NOI18N
+
+ // 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"); //NOI18N
+ } 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(MessageFormat.format("isUsed={0} - EXIT!", isUsed)); //NOI18N
+
+ // Return it
+ return isUsed;
+ }