+
+ /**
+ * Adds given product data from request to database
+ *
+ * @param request Request instance
+ */
+ @Override
+ public void doAdminAddProduct (final HttpServletRequest request) throws ServletException, ProductTitleAlreadyUsedException {
+ // 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"); //NOI18N
+ }
+
+ // Get title, price and category id
+ String title = request.getParameter(PizzaProductDatabaseConstants.COLUMN_TITLE);
+ String price = request.getParameter(PizzaProductDatabaseConstants.COLUMN_PRICE);
+ String category = request.getParameter(PizzaProductDatabaseConstants.COLUMN_CATEGORY);
+ String available = request.getParameter(PizzaProductDatabaseConstants.COLUMN_AVAILABLE);
+
+ // Debug message
+ this.getLogger().debug(MessageFormat.format("title={0},price={1},category={2},available={3}", title, price, category, available)); //NOI18N
+
+ // Variables for converting
+ Long id = null;
+ Float p = null;
+
+ // Check all fields
+ if (title == null) {
+ // "title" 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
+ } else if (price == null) {
+ // "price" not set
+ throw new IllegalArgumentException(MessageFormat.format("{0} is not set.", PizzaProductDatabaseConstants.COLUMN_PRICE)); //NOI18N
+ } else if (price.isEmpty()) {
+ // Is left empty
+ throw new IllegalArgumentException(MessageFormat.format("{0} is empty.", PizzaProductDatabaseConstants.COLUMN_PRICE)); //NOI18N
+ } else if (category == null) {
+ // "title" not set
+ throw new IllegalArgumentException(MessageFormat.format("{0} is not set.", PizzaProductDatabaseConstants.COLUMN_CATEGORY)); //NOI18N
+ } else if (category.isEmpty()) {
+ // Is left empty
+ throw new IllegalArgumentException(MessageFormat.format("{0} is empty.", PizzaProductDatabaseConstants.COLUMN_CATEGORY)); //NOI18N
+ } else if (available == null) {
+ // "title" not set
+ throw new IllegalArgumentException(MessageFormat.format("{0} is not set.", PizzaProductDatabaseConstants.COLUMN_AVAILABLE)); //NOI18N
+ } else if (available.isEmpty()) {
+ // Is left empty
+ throw new IllegalArgumentException(MessageFormat.format("{0} is empty.", PizzaProductDatabaseConstants.COLUMN_AVAILABLE)); //NOI18N
+ } else if ((!"true".equals(available)) && (!"false".equals(available))) { //NOI18N
+ // Invalid value
+ throw new IllegalArgumentException(MessageFormat.format("{0} is invalid: {1}", PizzaProductDatabaseConstants.COLUMN_AVAILABLE, available)); //NOI18N
+ }
+
+ // Parse numbers
+ try {
+ id = Long.parseLong(category);
+ p = Float.parseFloat(price);
+ } catch (final NumberFormatException e) {
+ // Not valid number
+ throw new IllegalArgumentException(e);
+ }
+
+ // Parse boolean
+ Boolean a = Boolean.parseBoolean(available);
+
+ // Test on product title
+ try {
+ // Try to check if title is used already
+ if (this.isProductTitleUsed(title)) {
+ // Title already used
+ throw new ProductTitleAlreadyUsedException(request);
+ }
+ } catch (final IOException | SQLException | BadTokenException | CorruptedDatabaseFileException | NoSuchMethodException | IllegalAccessException | InvocationTargetException ex) {
+ throw new ServletException(ex);
+ }
+
+ try {
+ // The product is not found, so add it to database
+ this.productFrontend.addProduct(title, p, id, a);
+ } catch (final SQLException | IOException ex) {
+ // Continue to throw it
+ throw new ServletException(ex);
+ }
+
+ // Trace message
+ this.getLogger().trace("EXIT!"); //NOI18N
+ }
+
+ /**
+ * Generates link HTML code for given category's parent id, if set. This
+ * link then points to products.jsp?category_id=x
+ *
+ * @param category Category instance
+ * @return HTML code
+ */
+ @Override
+ public String generateLinkForParent (final Category category) {
+ // Trace message
+ this.getLogger().trace(MessageFormat.format("category={0} - CALLED!", category)); //NOI18N
+
+ // category must not be null
+ if (category == null) {
+ // Is null
+ throw new NullPointerException("category is null"); //NOI18N
+ }
+
+ // Get parent id
+ Long parent = category.getParent();
+
+ // Is the id set?
+ if (parent > 0) {
+ // Product HTML code for link
+ throw new UnsupportedOperationException(MessageFormat.format("parent={0} - Unfinished!", parent)); //NOI18N
+ }
+
+ // No parent set
+ return "Keine";
+ }
+
+ @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;
+ }