*/
package org.mxchange.pizzaapplication.application;
-import java.lang.reflect.Field;
+import java.io.IOException;
+import java.io.UnsupportedEncodingException;
+import java.lang.reflect.InvocationTargetException;
+import java.sql.SQLException;
import java.text.MessageFormat;
import java.util.Iterator;
-import java.util.Map;
-import java.util.SortedMap;
-import java.util.TreeMap;
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;
+import org.mxchange.jcore.exceptions.CorruptedDatabaseFileException;
+import org.mxchange.jcore.exceptions.UnsupportedDatabaseBackendException;
+import org.mxchange.jshop.category.Category;
+import org.mxchange.jshop.database.frontend.category.CategoryDatabaseFrontend;
+import org.mxchange.jshop.database.frontend.category.CategoryFrontend;
+import org.mxchange.jshop.database.frontend.product.ProductDatabaseFrontend;
+import org.mxchange.jshop.database.frontend.product.ProductFrontend;
+import org.mxchange.jshop.exceptions.CategoryTitleAlreadyUsedException;
+import org.mxchange.jshop.exceptions.ProductTitleAlreadyUsedException;
+import org.mxchange.jshop.item.AddableBasketItem;
+import org.mxchange.jshop.product.Product;
import org.mxchange.pizzaapplication.BasePizzaServiceSystem;
-import org.mxchange.pizzaapplication.customer.Customer;
-import org.mxchange.pizzaapplication.customer.PizzaServiceCustomer;
-import org.mxchange.pizzaapplication.product.PizzaProduct;
-import org.mxchange.pizzaapplication.product.Product;
/**
+ * Main application class
*
* @author Roland Haeder
*/
public class PizzaServiceApplication extends BasePizzaServiceSystem implements PizzaApplication {
/**
- * Main title
+ * Frontend for products
*/
- public static final String MAIN_TITLE = "Pizza-Service";
+ private ProductFrontend productFrontend;
/**
- * Product list
+ * Frontend for categories
*/
- private final SortedMap<String, Product> products;
+ private CategoryFrontend categoryFrontend;
/**
- * Some singleton getter for this instance. If the instance is not set in
- * given application, it will be created.
- *
- * @param application Servlet context
- * @return This instance
- * @throws javax.servlet.ServletException If object is not set correctly
+ * Default constructor
*/
- public static final PizzaApplication getInstance (final ServletContext application) throws ServletException {
- // Check application instance
- if (application == null) {
- // Not set
- throw new NullPointerException("application is null"); //NOI18N
- }
-
- // Init instance
- PizzaApplication instance = null;
-
- // Get instance from servlet application (aka. "application scope")
- Object object = application.getAttribute("app"); //NOI18N
-
- // Is it set?
- if (object instanceof PizzaApplication) {
- // Instance is set, so casting should work
- instance = (PizzaApplication) object;
- } else if (object instanceof Object) {
- // Not correct instance
- throw new ServletException("app is not set correctly"); //NOI18N
- } else {
- // "service" is null, so initialize it
- instance = new PizzaServiceApplication();
-
- // And set it here
- application.setAttribute("app", instance); //NOI18N
- }
-
+ public PizzaServiceApplication () {
// Trace message
- instance.getLogger().trace(MessageFormat.format("instance={0} - EXIT!", instance)); //NOI18N
-
- // Return it
- return instance;
- }
-
- /**
- * Private constructor
- */
- private PizzaServiceApplication () {
- // Init products instance
- this.products = new TreeMap<>();
-
- // Init bundle
- this.initBundle();
-
- // Fill products list
- this.fillProductsList();
- }
-
- @Override
- public void doBootstrap () {
- throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
- }
-
- @Override
- public void doMainLoop () {
- throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
+ this.getLogger().trace("CALLED!");
}
@Override
- public void doShutdown () {
- throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
- }
-
- /**
- * Adds given product to list or throws an exception if name is already found
- *
- * @param name Internal name of product
- * @param title Product's title
- * @param price Price
- */
- private void addProduct (final String name, final String title, final float price) {
+ public void init (final ServletContext context) throws UnsupportedDatabaseBackendException, SQLException {
// Trace message
- this.getLogger().trace(MessageFormat.format("name={0},title={1},price={2} - CALLED!", name, title, price)); //NOI18N
+ this.getLogger().trace(MessageFormat.format("context={0} - CALLED!", context)); //NOI18N
- // Is the name already used?
- if (this.isProductNameUsed(name)) {
- // Something went wrong
- throw new IllegalArgumentException(MessageFormat.format("product {0} is already used.", name)); //NOI18N
+ // context should not be null
+ if (null == context) {
+ // Abort here
+ throw new NullPointerException("context is null");
}
- // Instance product
- Product product = new PizzaProduct(name, title, price);
-
- // Debug message
- this.getLogger().debug(MessageFormat.format("Adding product={0} ...", product)); //NOI18N
-
- // Add it
- this.products.put(product.getName(), product);
-
- // Trace message
- this.getLogger().trace("EXIT!"); //NOI18N
- }
-
- /**
- * Clears given parameter for product in session
- *
- * @param product Product instance
- * @param session Session instance
- * @param parameter Parameter to clear
- */
- private void clearSessionAttribute (final Product product, final HttpSession session, final String parameter) {
- // Trace message
- this.getLogger().trace(MessageFormat.format("produce={0},parameter={1},session={2} - CALLED!", product, parameter, session)); //NOI18N
-
- // Clear in session
- this.getLogger().debug(MessageFormat.format("Clearing product={0},parameter={1} ...", product.getName(), parameter)); //NOI18N
- this.setValueInSession(product, session, parameter, null);
-
- // Trace message
- this.getLogger().trace("EXIT!"); //NOI18N
- }
+ // Is the bundle initialized?
+ if (!this.isBundledInitialized()) {
+ // Temporary initialize default bundle
+ // @TODO The enum Gender uses this
+ this.initBundle();
+ }
- /**
- * Fills products list
- * @todo Very hard-coded stuff ...
- */
- private void fillProductsList () {
- // Trace message
- this.getLogger().trace("CALLED!"); //NOI18N
+ // Initialize properties from context
+ this.initProperties(context);
- // Add products
- this.addProduct("italia", "Pizza Italia", 5.50f); //NOI18N
- this.addProduct("diablo", "Pizza Diablo", 7.80f); //NOI18N
- this.addProduct("bolognese", "Spagetti Bolognese", 11.95f); //NOI18N
+ // Init database frontends
+ this.initDatabaseFrontends();
// Trace message
this.getLogger().trace("EXIT!"); //NOI18N
}
/**
- * Some getter for value from session
- *
- * @param product Product instance
- * @param session Session instance
- * @param attribute Attribute to get value from
- * @return Value from session
- */
- private Object getValueFromSession (final Product product, final HttpSession session, final String attribute) {
- // Trace message
- this.getLogger().trace(MessageFormat.format("product={0},session={1},attribute={2} - CALLED!", product, session, attribute)); //NOI18N
-
- // Init variable
- Object value = this.getValueFromSession(session, String.format(HTTP_PARAM_MASK, product.getName(), attribute));
-
- this.getLogger().debug(MessageFormat.format("product={0},attribute={1},value={2}", product.getName(), attribute, value)); //NOI18N
-
- // Trace message
- this.getLogger().trace(MessageFormat.format("value={0} - EXIT!", value)); //NOI18N
-
- // Return it
- return value;
- }
-
- /**
- * Some getter for value from session
+ * Calculates total amount of all choosen products
*
+ * @param request Request instance
* @param session Session instance
- * @param key Key to get value from
- * @return Value from session
+ * @return Total amount of all choosen products
+ * @deprecated Old lost
*/
- private Object getValueFromSession (final HttpSession session, final String key) {
+ @Deprecated
+ private int calculateTotalAmount (final HttpServletRequest request, final HttpSession session) throws ServletException {
// Trace message
- this.getLogger().trace(MessageFormat.format("session={043},key={1} - CALLED!", session, key)); //NOI18N
-
- // Init value
- Object value = null;
+ this.getLogger().trace(MessageFormat.format("request={0},session={1} - CALLED!", request, session)); //NOI18N
- // Get it synchronized from session
- synchronized (session) {
- value = session.getAttribute(key);
+ // Is product and session set?
+ if (null == request) {
+ // Not set
+ throw new NullPointerException("request is null"); //NOI18N
+ } else if (null == session) {
+ // Not set
+ throw new NullPointerException("session is null"); //NOI18N
}
- // Trace message
- this.getLogger().trace(MessageFormat.format("value={0} - EXIT!", value)); //NOI18N
+ // Init/declare total price and iterator
+ int totalAmount = 0;
+ Iterator<Product> iterator = this.getAvailableProducts();
- // Return it
- return value;
- }
+ // "Walk" over all products
+ while (iterator.hasNext()) {
+ // Get next product
+ Product product = iterator.next();
- /**
- * Checks whether given product is already used
- *
- * @param name Name of product
- * @return Whether the given product's name is already used
- */
- private boolean isProductNameUsed (final String name) {
- // Trace message
- this.getLogger().trace(MessageFormat.format("name={0} - CALLED!", name)); //NOI18N
+ // Is this choosen?
+ if (this.isProductChoosen(product, request, session)) {
+ // Then add ordered amount
+ this.getLogger().debug(MessageFormat.format("Counting {0} ...", product.getItemId())); //NOI18N
- // Is it found?
- return this.products.containsKey(name);
- }
+ // Getting amount
+ String amount = this.getAmountFromSession(product, session);
- /**
- * For debugging purpose
- *
- * @param args Arguments
- */
- public static void main (String[] args) {
- // Get instance and start it
- new PizzaServiceApplication().start();
- }
+ // Add it up
+ this.getLogger().debug(MessageFormat.format("amount={0}", amount)); //NOI18N
+ totalAmount += Integer.valueOf(amount);
+ }
+ this.getLogger().debug(MessageFormat.format("product={0},totalAmount={1}", product.getItemId(), totalAmount)); //NOI18N
+ }
- /**
- * Checks if the product ordered?
- *
- * @param product
- * @param session
- * @return
- */
- private boolean isProductOrdered (final Product product, final HttpSession session) {
// Trace message
- this.getLogger().trace(MessageFormat.format("product={0},session={1} - CALLED!", product, session)); //NOI18N
-
- // Get session
- Object isOrdered = this.getValueFromSession(product, session, SESSION_ORDERED);
- this.getLogger().debug(MessageFormat.format("product={0},isOrdered={1}", product.getName(), isOrdered)); //NOI18N
+ this.getLogger().trace(MessageFormat.format("totalAmount={0} - EXIT!", totalAmount)); //NOI18N
- // Return result
- return ("true".equals(isOrdered)); //NOI18N
+ // Return total price
+ return totalAmount;
}
- /**
- * Somewhat setter in session
- *
- * @param product Product instance
- * @param session Session instance
- * @param keyPart Key part to include in final key
- * @param value Value to set
- */
- private void setValueInSession (final Product product, final HttpSession session, final String keyPart, final Object value) {
- // Trace message
- this.getLogger().trace(MessageFormat.format("product={0},session={1},keyPart={2},value={3} - CALLED!", product, session, keyPart, value)); //NOI18N
-
- synchronized(session) {
- // Set it synced
- this.getLogger().debug(MessageFormat.format("Setting value={0} for product={1},keyPart={2}", value, product.getName(), keyPart)); //NOI18N
- this.setValueInSession(session, String.format(HTTP_PARAM_MASK, product.getName(), keyPart), value);
- }
-
- // Trace message
- this.getLogger().trace("EXIT!"); //NOI18N
+ @Override
+ public void doBootstrap () {
+ throw new UnsupportedOperationException("Not supported yet."); //NOI18N
}
- /**
- * Somewhat setter in session
- *
- * @param session Session instance
- * @param key Session key to set
- * @param value Value to set
- */
@Override
- public void setValueInSession (final HttpSession session, final String key, final Object value) {
- // Trace message
- this.getLogger().trace(MessageFormat.format("session={0},key={1},value={2} - CALLED!", session, key, value)); //NOI18N
-
- synchronized(session) {
- // Set it synced
- session.setAttribute(key, value);
- }
-
- // Trace message
- this.getLogger().trace("EXIT!"); //NOI18N
+ public void doMainLoop () {
+ throw new UnsupportedOperationException("Not supported yet."); //NOI18N
}
- /**
- * Application starter
- */
- private void start () {
- // "Walk" over all products
- for (final Product product : this.getProducts()) {
- // Output data
- this.getLogger().debug(MessageFormat.format("Product {0}, {1}: {2}", product.getName(), product.getTitle(), product.getPrice())); //NOI18N
- }
-
- // Generate fake Customer instance
- 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.
- */
-
- // Gender is MALE now
- customer.setGender(Gender.MALE);
-
- // Get iterator on all its fields
- Iterator<Map.Entry<Field, Object>> it = customer.iterator();
-
- // Output it
- while (it.hasNext()) {
- Map.Entry<Field, Object> entry = it.next();
- this.getLogger().debug(MessageFormat.format("entry {0}={1}", entry.getKey(), entry.getValue()));
- }
+ @Override
+ public void doShutdown () {
+ throw new UnsupportedOperationException("Not supported yet."); //NOI18N
}
/**
* @param product Product instance
* @param session Session instance
* @return Amount as string
+ * @deprecated Old lost code
*/
- @Override
- public String getAmountFromSession (final Product product, final HttpSession session) {
+ @Deprecated
+ private String getAmountFromSession (final Product product, final HttpSession session) {
// Trace message
this.getLogger().trace(MessageFormat.format("product={0},session={1} - CALLED!", product, session)); //NOI18N
// Is product and session set?
- if (product == null) {
+ if (null == product) {
// Not set
throw new NullPointerException("product is null"); //NOI18N
- } else if (session == null) {
+ } else if (null == session) {
// Not set
throw new NullPointerException("session is null"); //NOI18N
}
Object object = this.getValueFromSession(product, session, HTTP_PARAM_AMOUNT);
// Is the object null?
- if (object == null) {
+ if (null == object) {
// Trace message
this.getLogger().trace("Returning 0 - EXIT!"); //NOI18N
}
/**
- * Some "getter" for choose from session
+ * Some "getter" for HTML code 'checked="checked"' if the product is choosen
*
* @param product Product instance
+ * @param request Request instance
* @param session Session instance
- * @return Choose as string
+ * @return Whether the product is choosen
*/
@Override
- public String getChooseFromSession (final Product product, final HttpSession session) {
+ public String getCheckedHtmlFromProduct (final Product product, final HttpServletRequest request, final HttpSession session) {
// Trace message
- this.getLogger().trace(MessageFormat.format("product={0},session={1} - CALLED!", product, session)); //NOI18N
+ this.getLogger().trace(MessageFormat.format("product={0},request={1},session={2} - CALLED!", product, request, session)); //NOI18N
- // Is product and session set?
- if (product == null) {
+ // Is product and session set?
+ if (null == product) {
// Not set
throw new NullPointerException("product is null"); //NOI18N
- } else if (session == null) {
+ } else if (null == request) {
+ // Not set
+ throw new NullPointerException("request is null"); //NOI18N
+ } else if (null == session) {
// Not set
throw new NullPointerException("session is null"); //NOI18N
}
- // Get attribute
- Object object = this.getValueFromSession(product, session, HTTP_PARAM_CHOOSE);
+ // First let's check if the product is choosen
+ if (this.isProductChoosen(product, request, session)) {
+ // Trace message
+ this.getLogger().trace("Returning checked=\"checked\" - EXIT!"); //NOI18N
- // Is the object null?
- if (object == null) {
- // Not found
- this.getLogger().debug(MessageFormat.format("Returning empty string for product={0} ...", product.getName())); //NOI18N
+ // Is choosen
+ return "checked=\"checked\""; //NOI18N
+ } else {
+ // Trace message
+ this.getLogger().trace("Returning empty string - EXIT!"); //NOI18N
+
+ // Not choosen
return ""; //NOI18N
}
-
- // Trace message
- this.getLogger().trace(MessageFormat.format("object={0} - CALLED!", object)); //NOI18N
-
- // Cast to string and return it
- return (String) object;
}
/**
- * Handler for amount from request or session
- *
- * @param product Product instance
+ * Some "getter" for HTML code 'disabled="disabled"' for e.g. submit buttons
+ *
* @param request Request instance
* @param session Session instance
- * @return Amount as string
+ * @return Whether the product is choosen
*/
@Override
- public String handleAmountFromRequestSession (final Product product, final HttpServletRequest request, final HttpSession session) {
+ public String getDisabledHtmlFromSession (final HttpServletRequest request, final HttpSession session) throws ServletException {
// Trace message
- this.getLogger().trace(MessageFormat.format("product={0},request={1},session={2} - CALLED!", product, request, session)); //NOI18N
+ this.getLogger().trace(MessageFormat.format("request={0},session={1} - CALLED!", request, session)); //NOI18N
// Is product and session set?
- if (product == null) {
- // Not set
- throw new NullPointerException("product is null"); //NOI18N
- } else if (request == null) {
+ if (null == request) {
// Not set
throw new NullPointerException("request is null"); //NOI18N
- } else if (session == null) {
+ } else if (null == session) {
// Not set
throw new NullPointerException("session is null"); //NOI18N
}
- // Init variabke
- Object object;
-
- // Check request method
- if (!"POST".equals(request.getMethod())) { //NOI18N
- // Not POST, so get from session
- return this.getAmountFromSession(product, session);
- } else if (this.handleChooseFromRequestSession(product, request, session).isEmpty()) {
- // Not choosen
- this.clearSessionAttribute(product, session, HTTP_PARAM_AMOUNT);
- this.getLogger().debug(MessageFormat.format("Unsetting for product={0} in session, returning zero ...", product.getName())); //NOI18N
- return "0"; //NOI18N
- }
+ // Get "enabled" from request scope
+ Boolean enabled = Boolean.parseBoolean((String) request.getAttribute("enabled")); //NOI18N
- // Get attribute from request
- object = request.getParameter(String.format(HTTP_PARAM_MASK, HTTP_PARAM_AMOUNT, product.getName()));
+ // Debug message
+ this.getLogger().debug(MessageFormat.format("enabled={0}", enabled)); //NOI18N
- // Is it set?
- if (object instanceof String) {
- // Try to parse it to integer
- try {
- Integer value = Integer.valueOf((String) object);
- } catch (final NumberFormatException ex) {
- // Not valid input
- this.getLogger().warn(ex);
- return "0"; //NOI18N
- }
+ // Is something selected?
+ if ((enabled) || (this.calculateTotalAmount(request, session) > 0)) {
+ // Trace message
+ this.getLogger().trace("Returning empty string - EXIT!"); //NOI18N
- // Then set it in session
- this.setValueInSession(product, session, HTTP_PARAM_AMOUNT, object);
+ // Something has been choosen
+ return ""; //NOI18N
+ } else {
+ // Trace message
+ this.getLogger().trace("Returning disabled=\"disabled\" - EXIT!"); //NOI18N
- // And return it
- return (String) object;
+ // Nothing choosen yet
+ return "disabled=\"disabled\""; //NOI18N
}
+ }
+ /**
+ * Checks if given Product instance is available and returns a printable
+ * (human-readable) string.
+ *
+ * @param product Product instance to check
+ * @return Human-readable version of product availability
+ */
+ @Override
+ public String getPrintableProduktAvailability (final Product product) {
// Trace message
- this.getLogger().trace("Calling getAmountFromSession() ..."); //NOI18N
+ this.getLogger().trace(MessageFormat.format("product={0} - CALLED!", product)); //NOI18N
- // Get attribute from session
- return this.getAmountFromSession(product, session);
+ // Is it null?
+ if (null == product) {
+ // Should not be null
+ throw new NullPointerException("product is null"); //NOI18N
+ }
+
+ // Get availability
+ if (product.getAvailable() == true) {
+ // Is available
+ return "Ja";
+ } else {
+ // Not, not for public
+ return "Nein";
+ }
}
/**
- * Handler for choosen (checkbox) from request or session
+ * Some "getter" for a an array of only available products
*
- * @param product Product instance
- * @param request Request instance
- * @param session Session instance
- * @return Amount as string
+ * @return All products
*/
@Override
- public 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
+ public Iterator<Product> getAvailableProducts () throws ServletException {
+ // categoryFrontend must be set
+ if (null == this.productFrontend) {
+ // Abort here
+ throw new NullPointerException("productFrontend is null");
+ }
- // Is product and session set?
- if (product == null) {
- // Not set
- throw new NullPointerException("product is null"); //NOI18N
- } else if (request == null) {
- // Not set
- throw new NullPointerException("request is null"); //NOI18N
- } else if (session == null) {
- // Not set
- throw new NullPointerException("session is null"); //NOI18N
+ try {
+ // Ask frontend for a list of products
+ return this.productFrontend.getAvailableProducts();
+ } catch (final IOException | BadTokenException | SQLException | CorruptedDatabaseFileException | NoSuchMethodException | IllegalAccessException | InvocationTargetException ex) {
+ throw new ServletException(ex);
}
+ }
- // Init variabke
- Object object;
+ /**
+ * Some "getter" for a an array of all products
+ *
+ * @return All products
+ */
+ @Override
+ public Iterator<Product> getAllProducts () throws ServletException {
+ // Trace message
+ this.getLogger().trace("CALLED!");
- // Check request method
- if (!"POST".equals(request.getMethod())) { //NOI18N
- // Not POST, so get from session
- return this.getChooseFromSession(product, session);
- } else if (this.isProductOrdered(product, session)) {
- // Product is ordered
- return this.getChooseFromSession(product, session);
+ // categoryFrontend must be set
+ if (null == this.productFrontend) {
+ // Abort here
+ throw new NullPointerException("productFrontend is null");
}
- // Get reqzest element
- object = request.getParameter(String.format(HTTP_PARAM_MASK, HTTP_PARAM_CHOOSE, product.getName()));
- this.getLogger().debug(MessageFormat.format("product={0},object={1}", product.getName(), object)); //NOI18N
+ try {
+ // Ask frontend for a list of products
+ return this.productFrontend.getAllProducts();
+ } catch (final IOException | BadTokenException | SQLException | CorruptedDatabaseFileException | NoSuchMethodException | IllegalAccessException | InvocationTargetException ex) {
+ throw new ServletException(ex);
+ }
+ }
- // Is it null?
- if (object == null) {
- // Unset session
- this.getLogger().debug(MessageFormat.format("Unsetting session for product={0} ...", product.getName())); //NOI18N
- this.clearSessionAttribute(product, session, HTTP_PARAM_CHOOSE);
- this.clearSessionAttribute(product, session, HTTP_PARAM_AMOUNT);
+ /**
+ * Some "getter" for a an array of all categories
+ *
+ * @return All categories
+ */
+ @Override
+ public Iterator<Category> getAllCategories () throws ServletException {
+ // Trace message
+ this.getLogger().trace("CALLED!");
- // Return empty string
- return ""; //NOI18N
+ // categoryFrontend must be set
+ if (null == this.categoryFrontend) {
+ // Abort here
+ throw new NullPointerException("categoryFrontend is null");
}
- // Then set it in session
- this.setValueInSession(product, session, HTTP_PARAM_CHOOSE, object);
-
- // Cast to string and return it
- this.getLogger().debug(MessageFormat.format("product={0} - Returning {1} ...", product.getName(), object)); //NOI18N
- return (String) object;
+ try {
+ // Ask frontend for a list of categories
+ return this.categoryFrontend.getAllCategories();
+ } catch (final IOException | BadTokenException | SQLException | CorruptedDatabaseFileException | NoSuchMethodException | IllegalAccessException | InvocationTargetException ex) {
+ throw new ServletException(ex);
+ }
}
/**
- * Some "getter" for choosen (checkbox) from session
+ * Checks whether the given product is choosen, request overules session.
*
* @param product Product instance
* @param request Request instance
* @param session Session instance
- * @return Amount as string
+ * @return Whether the product is choosen
*/
@Override
- public String getPrintableChoosenFromRequestSession (final Product product, final HttpServletRequest request, final HttpSession session) {
+ @Deprecated
+ public boolean isProductChoosen (final Product product, final HttpServletRequest request, final HttpSession session) {
+ throw new UnsupportedOperationException("This method is deprecated and shall not be called");
+ }
+
+ /**
+ * Somewhat setter in session
+ *
+ * @param session Session instance
+ * @param key Session key to set
+ * @param value Value to set
+ */
+ @Override
+ public void setValueInSession (final HttpSession session, final String key, final Object value) {
// Trace message
- this.getLogger().trace(MessageFormat.format("product={0},request={1},session={2} - CALLED!", product, request, session)); //NOI18N
+ this.getLogger().trace(MessageFormat.format("session={0},key={1},value={2} - CALLED!", session, key, value)); //NOI18N
- // Is product and session set?
- if (product == null) {
- // Not set
- throw new NullPointerException("product is null"); //NOI18N
- } else if (request == null) {
- // Not set
- throw new NullPointerException("request is null"); //NOI18N
- } else if (session == null) {
- // Not set
- throw new NullPointerException("session is null"); //NOI18N
+ synchronized(session) {
+ // Set it synced
+ session.setAttribute(key, value);
}
- // Get element
- String choosen = this.handleChooseFromRequestSession(product, request, session);
- this.getLogger().debug(MessageFormat.format("product={0},choosen={1}", product.getName(), choosen)); //NOI18N
+ // Trace message
+ this.getLogger().trace("EXIT!"); //NOI18N
+ }
- // Must not be null
- assert(choosen instanceof String): "choosen is null"; //NOI18N
+ /**
+ * Clears given parameter for product in session
+ *
+ * @param product Product instance
+ * @param session Session instance
+ * @param parameter Parameter to clear
+ */
+ private void clearSessionAttribute (final Product product, final HttpSession session, final String parameter) {
+ // Trace message
+ this.getLogger().trace(MessageFormat.format("produce={0},parameter={1},session={2} - CALLED!", product, parameter, session)); //NOI18N
- // Is it empty?
- if (choosen.isEmpty()) {
- // Not choosen
- return "Nein";
- }
+ // Clear in session
+ this.getLogger().debug(MessageFormat.format("Clearing product={0},parameter={1} ...", product.getItemId(), parameter)); //NOI18N
+ this.setValueInSession(product, session, parameter, null);
+
+ // Trace message
+ this.getLogger().trace("EXIT!"); //NOI18N
+ }
- // Get amount
- String amount = this.handleAmountFromRequestSession(product, request, session);
- this.getLogger().debug(MessageFormat.format("product={0},amount={1}", product.getName(), amount)); //NOI18N
+ /**
+ * Some getter for value from session
+ *
+ * @param product Product instance
+ * @param session Session instance
+ * @param attribute Attribute to get value from
+ * @return Value from session
+ */
+ private Object getValueFromSession (final Product product, final HttpSession session, final String attribute) {
+ // Trace message
+ this.getLogger().trace(MessageFormat.format("product={0},session={1},attribute={2} - CALLED!", product, session, attribute)); //NOI18N
- // Must not be null
- assert(amount instanceof String): "amount is null"; //NOI18N
+ // Init variable
+ Object value = this.getValueFromSession(session, String.format(HTTP_PARAM_MASK, attribute, product.getItemId()));
+
+ this.getLogger().debug(MessageFormat.format("product={0},attribute={1},value={2}", product.getItemId(), attribute, value)); //NOI18N
- // Is it empty?
- if (amount.isEmpty() || "0".equals(amount)) { //NOI18N
- // Choosen, but no amount
- return "Nein";
- } else {
- // Is choosen
- return "Ja";
- }
+ // Trace message
+ this.getLogger().trace(MessageFormat.format("value={0} - EXIT!", value)); //NOI18N
+
+ // Return it
+ return value;
}
/**
- * Some "getter" for total price of position from request or session.
- * Single price and amount is multiplyed.
+ * Some getter for value from session
*
- * @param product Product instance
- * @param request Request instance
* @param session Session instance
- * @return Amount as string
+ * @param key Key to get value from
+ * @return Value from session
*/
- @Override
- public float getTotalPositionPriceFromRequestSession (final Product product, final HttpServletRequest request, final HttpSession session) {
+ private Object getValueFromSession (final HttpSession session, final String key) {
// Trace message
- this.getLogger().trace(MessageFormat.format("product={0},request={1},session={2} - CALLED!", product, request, session)); //NOI18N
+ this.getLogger().trace(MessageFormat.format("session={0},key={1} - CALLED!", session, key)); //NOI18N
- // Is product and session set?
- if (product == null) {
- // Not set
- throw new NullPointerException("product is null"); //NOI18N
- } else if (request == null) {
- // Not set
- throw new NullPointerException("request is null"); //NOI18N
- } else if (session == null) {
- // Not set
- throw new NullPointerException("session is null"); //NOI18N
+ // Init value
+ Object value;
+
+ // Get it synchronized from session
+ synchronized (session) {
+ value = session.getAttribute(key);
}
- // Get choosen
- String choosen = this.handleChooseFromRequestSession(product, request, session);
- this.getLogger().debug(MessageFormat.format("product={0},choosen={1}", product.getName(), choosen)); //NOI18N
+ // Trace message
+ this.getLogger().trace(MessageFormat.format("value={0} - EXIT!", value)); //NOI18N
+
+ // Return it
+ return value;
+ }
- // Must not be null
- assert(choosen instanceof String): "choosen is null"; //NOI18N
+ /**
+ * Initializes database frontends.
+ */
+ private void initDatabaseFrontends () throws UnsupportedDatabaseBackendException, SQLException {
+ // Trace message
+ this.getLogger().trace("CALLED!"); //NOI18N
- // Is it set?
- if (choosen.isEmpty()) {
- // Is empty
- this.getLogger().debug(MessageFormat.format("product={0},choosen={1} - returning zero ...", product.getName(), choosen)); //NOI18N
- return 0.00f;
- }
+ // Product frontend
+ this.productFrontend = new ProductDatabaseFrontend();
- // Get amount
- String amount = this.handleAmountFromRequestSession(product, request, session);
- this.getLogger().debug(MessageFormat.format("product={0},amount={1}", product.getName(), amount)); //NOI18N
+ // Category frontend
+ this.categoryFrontend = new CategoryDatabaseFrontend();
- // Must not be null
- assert(amount instanceof String): "amount is null"; //NOI18N
+ // Trace message
+ this.getLogger().trace("EXIT!"); //NOI18N
+ }
- // Is it empty?
- if (amount.isEmpty() || "0".equals(amount)) { //NOI18N
- // Is empty
- this.getLogger().debug(MessageFormat.format("product={0},amount={1} - returning zero ...", product.getName(), amount)); //NOI18N
- return 0.00f;
+ /**
+ * Checks whether given category title is already used
+ *
+ * @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 {
+ // categoryFrontend must be set
+ if (null == this.categoryFrontend) {
+ // Abort here
+ throw new NullPointerException("categoryFrontend is null");
}
- // Init variable
- Integer value = null;
+ // Delegate to frontend
+ return this.categoryFrontend.isCategoryTitleUsed(title);
+ }
- // Try it
- try {
- // Get amount as integer
- value = Integer.valueOf(amount);
- } catch (final NumberFormatException e) {
- // Bat input
- throw new IllegalArgumentException(e);
+ /**
+ * Checks if given product title is already used
+ * @param title Product title to check
+ * @return Whether the product title has already been used
+ */
+ private boolean isProductTitleUsed (final String title) throws IOException, SQLException, BadTokenException, CorruptedDatabaseFileException, NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
+ // categoryFrontend must be set
+ if (null == this.productFrontend) {
+ // Abort here
+ throw new NullPointerException("productFrontend is null");
}
- // Calculate price
- float price = (product.getPrice() * value);
+ // Delegate to frontend
+ return this.productFrontend.isProductTitleUsed(title);
+ }
+ /**
+ * Checks if the product ordered?
+ *
+ * @param product Product instance
+ * @param session HttpSession instance
+ * @return Whether the product has been ordered
+ */
+ private boolean isProductOrdered (final Product product, final HttpSession session) {
// Trace message
- this.getLogger().trace(MessageFormat.format("product={0},price={1} - EXIT!", product.getName(), price)); //NOI18N
+ this.getLogger().trace(MessageFormat.format("product={0},session={1} - CALLED!", product, session)); //NOI18N
+
+ // Get session
+ Object isOrdered = this.getValueFromSession(product, session, SESSION_ORDERED);
+ this.getLogger().debug(MessageFormat.format("product={0},isOrdered={1}", product.getItemId(), isOrdered)); //NOI18N
- // Then multiply it with price
- return price;
+ // Return result
+ return ("true".equals(isOrdered)); //NOI18N
}
/**
- * Checks whether the given product is choosen, request overules session.
+ * Somewhat setter in session
*
* @param product Product instance
- * @param request Request instance
* @param session Session instance
- * @return Whether the product is choosen
+ * @param keyPart Key part to include in final key
+ * @param value Value to set
*/
- @Override
- public boolean isProductChoosen (final Product product, final HttpServletRequest request, final HttpSession session) {
+ private void setValueInSession (final Product product, final HttpSession session, final String keyPart, final Object value) {
// Trace message
- this.getLogger().trace(MessageFormat.format("product={0},request={1},session={2} - CALLED!", product, request, session)); //NOI18N
-
- // Is product and session set?
- if (product == null) {
- // Not set
- throw new NullPointerException("product is null"); //NOI18N
- } else if (request == null) {
- // Not set
- throw new NullPointerException("request is null"); //NOI18N
- } else if (session == null) {
- // Not set
- throw new NullPointerException("session is null"); //NOI18N
- }
-
- // Get choosen
- String choosen = this.handleChooseFromRequestSession(product, request, session);
- this.getLogger().debug(MessageFormat.format("product={0},choosen={1}", product.getName(), choosen)); //NOI18N
-
- // Must not be null
- assert(choosen instanceof String): "choosen is null"; //NOI18N
-
- // Is it not choosen?
- if (choosen.isEmpty()) {
- // Not choosen
- return false;
- }
-
- // Get amount
- String amount = this.handleAmountFromRequestSession(product, request, session);
+ this.getLogger().trace(MessageFormat.format("product={0},session={1},keyPart={2},value={3} - CALLED!", product, session, keyPart, value)); //NOI18N
- // Must not be null
- assert(amount instanceof String): "amount is not set"; //NOI18N
+ // Set it synced
+ this.getLogger().debug(MessageFormat.format("Setting value={0} for product={1},keyPart={2}", value, product.getItemId(), keyPart)); //NOI18N
+ this.setValueInSession(session, String.format(HTTP_PARAM_MASK, keyPart, product.getItemId()), value);
// Trace message
- this.getLogger().trace(MessageFormat.format("amount={0} - EXIT!", amount)); //NOI18N
-
- // Must not be empty and not 0
- return (!amount.isEmpty() && !"0".equals(amount)); //NOI18N
+ this.getLogger().trace("EXIT!"); //NOI18N
}
/**
- * Calculates total price of all choosen products
+ * Adds given category data from request to database
*
* @param request Request instance
- * @param session Session instance
- * @return Total price of all choosen products
*/
@Override
- public float calculateTotalPrice (final HttpServletRequest request, final HttpSession session) {
+ public void doAdminAddCategory (final HttpServletRequest request) throws ServletException, CategoryTitleAlreadyUsedException {
// Trace message
- this.getLogger().trace(MessageFormat.format("request={0},session={1} - CALLED!", request, session)); //NOI18N
+ this.getLogger().trace(MessageFormat.format("request={0} - CALLED!", request)); //NOI18N
- // Is product and session set?
- if (request == null) {
- // Not set
+ // request must not be null
+ if (null == request) {
+ // Is null
throw new NullPointerException("request is null"); //NOI18N
- } else if (session == null) {
- // Not set
- throw new NullPointerException("session is null"); //NOI18N
}
- // Init total price
- float totalPrice = 0.00f;
+ // Get all fields
+ String title = request.getParameter(CategoryFrontend.COLUMN_TITLE);
+ String parent = request.getParameter(CategoryFrontend.COLUMN_PARENT);
- // "Walk" over all products
- for (final Product product : this.getProducts()) {
- // Is this choosen?
- if (this.isProductChoosen(product, request, session)) {
- // Then add product's total price
- this.getLogger().debug(MessageFormat.format("Calling getTotalPositionPriceFromRequestSession({0},request,session) ...", product.getName())); //NOI18N
- totalPrice += this.getTotalPositionPriceFromRequestSession(product, request, session);
+ // Debug message
+ this.getLogger().debug(MessageFormat.format("title={0},parent={1}", title, parent)); //NOI18N
+
+ // Init variables for casting
+ Integer id = 0;
+
+ // Check all fields
+ if (null == title) {
+ // "title" not set
+ throw new IllegalArgumentException(MessageFormat.format("{0} is not set.", CategoryFrontend.COLUMN_TITLE)); //NOI18N
+ } else if (title.isEmpty()) {
+ // Is left empty
+ throw new IllegalArgumentException(MessageFormat.format("{0} is empty.", CategoryFrontend.COLUMN_TITLE)); //NOI18N
+ } else if ((parent != null) && (!parent.isEmpty())) {
+ // "parent" is set, so check it
+ try {
+ id = Integer.parseInt(parent);
+ } catch (final NumberFormatException e) {
+ // Not valid number
+ throw new IllegalArgumentException(e);
}
- this.getLogger().debug(MessageFormat.format("product={0},totalPrice={1}", product.getName(), totalPrice)); //NOI18N
}
- // Trace message
- this.getLogger().trace(MessageFormat.format(" totalPrice={0} - EXIT!", totalPrice)); //NOI18N
+ try {
+ // Try to check if title is used already
+ if (this.isCategoryTitleUsed(title)) {
+ // Title already used
+ throw new CategoryTitleAlreadyUsedException(request);
+ }
+ } catch (final IOException | SQLException | BadTokenException | CorruptedDatabaseFileException | NoSuchMethodException | IllegalAccessException | InvocationTargetException ex) {
+ throw new ServletException(ex);
+ }
- // Return total price
- return totalPrice;
+ try {
+ // The category is not found, so add it to database
+ this.categoryFrontend.addCategory(title, id);
+ } catch (final SQLException | IOException ex) {
+ // Continue to throw it
+ throw new ServletException(ex);
+ }
+
+ // Trace message
+ this.getLogger().trace("EXIT!"); //NOI18N
}
/**
- * Calculates total amount of all choosen products
+ * Adds given product data from request to database
*
* @param request Request instance
- * @param session Session instance
- * @return Total amount of all choosen products
*/
@Override
- public int calculateTotalAmount (final HttpServletRequest request, final HttpSession session) {
+ public void doAdminAddProduct (final HttpServletRequest request) throws ServletException, ProductTitleAlreadyUsedException {
// Trace message
- this.getLogger().trace(MessageFormat.format("request={0},session={1} - CALLED!", request, session)); //NOI18N
+ this.getLogger().trace(MessageFormat.format("request={0} - CALLED!", request)); //NOI18N
- // Is product and session set?
- if (request == null) {
- // Not set
+ // request must not be null
+ if (null == request) {
+ // Is null
throw new NullPointerException("request is null"); //NOI18N
- } else if (session == null) {
- // Not set
- throw new NullPointerException("session is null"); //NOI18N
}
- // Init total price
- int totalAmount = 0;
+ // Get title, price and category id
+ String title = request.getParameter(ProductFrontend.COLUMN_TITLE);
+ String price = request.getParameter(ProductFrontend.COLUMN_PRICE);
+ String category = request.getParameter(ProductFrontend.COLUMN_CATEGORY);
+ String available = request.getParameter(ProductFrontend.COLUMN_AVAILABLE);
- // "Walk" over all products
- for (final Product product : this.getProducts()) {
- // Is this choosen?
- if (this.isProductChoosen(product, request, session)) {
- // Then add ordered amount
- this.getLogger().debug(MessageFormat.format("Counting {0} ...", product.getName())); //NOI18N
+ // 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 (null == title) {
+ // "title" not set
+ throw new IllegalArgumentException(MessageFormat.format("{0} is not set.", ProductFrontend.COLUMN_TITLE)); //NOI18N
+ } else if (title.isEmpty()) {
+ // Is left empty
+ throw new IllegalArgumentException(MessageFormat.format("{0} is empty.", ProductFrontend.COLUMN_TITLE)); //NOI18N
+ } else if (null == price) {
+ // "price" not set
+ throw new IllegalArgumentException(MessageFormat.format("{0} is not set.", ProductFrontend.COLUMN_PRICE)); //NOI18N
+ } else if (price.isEmpty()) {
+ // Is left empty
+ throw new IllegalArgumentException(MessageFormat.format("{0} is empty.", ProductFrontend.COLUMN_PRICE)); //NOI18N
+ } else if (null == category) {
+ // "title" not set
+ throw new IllegalArgumentException(MessageFormat.format("{0} is not set.", ProductFrontend.COLUMN_CATEGORY)); //NOI18N
+ } else if (category.isEmpty()) {
+ // Is left empty
+ throw new IllegalArgumentException(MessageFormat.format("{0} is empty.", ProductFrontend.COLUMN_CATEGORY)); //NOI18N
+ } else if (null == available) {
+ // "title" not set
+ throw new IllegalArgumentException(MessageFormat.format("{0} is not set.", ProductFrontend.COLUMN_AVAILABLE)); //NOI18N
+ } else if (available.isEmpty()) {
+ // Is left empty
+ throw new IllegalArgumentException(MessageFormat.format("{0} is empty.", ProductFrontend.COLUMN_AVAILABLE)); //NOI18N
+ } else if ((!"true".equals(available)) && (!"false".equals(available))) { //NOI18N
+ // Invalid value
+ throw new IllegalArgumentException(MessageFormat.format("{0} is invalid: {1}", ProductFrontend.COLUMN_AVAILABLE, available)); //NOI18N
+ }
- // Getting amount
- String amount = this.getAmountFromSession(product, session);
+ // Parse numbers
+ try {
+ id = Long.parseLong(category);
+ p = Float.parseFloat(price);
+ } catch (final NumberFormatException e) {
+ // Not valid number
+ throw new IllegalArgumentException(e);
+ }
- // Add it up
- this.getLogger().debug(MessageFormat.format("amount={0}", amount)); //NOI18N
- totalAmount += Integer.valueOf(amount);
+ // 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);
}
- this.getLogger().debug(MessageFormat.format("product={0},totalAmount={1}", product.getName(), totalAmount)); //NOI18N
+ } catch (final IOException | SQLException | BadTokenException | CorruptedDatabaseFileException | NoSuchMethodException | IllegalAccessException | InvocationTargetException ex) {
+ throw new ServletException(ex);
}
- // Trace message
- this.getLogger().trace(MessageFormat.format("totalAmount={0} - EXIT!", totalAmount)); //NOI18N
+ 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);
+ }
- // Return total price
- return totalAmount;
+ // Trace message
+ this.getLogger().trace("EXIT!"); //NOI18N
}
/**
- * Some "getter" for HTML code 'checked="checked"' if the product is choosen
+ * Generates link HTML code for given category's parent id, if set. This
+ * link then points to products.jsp?category_id=x
*
- * @param product Product instance
- * @param request Request instance
- * @param session Session instance
- * @return Whether the product is choosen
+ * @param category Category instance
+ * @return HTML code
*/
@Override
- public String getCheckedHtmlFromProduct (final Product product, final HttpServletRequest request, final HttpSession session) {
+ public String generateLinkForParent (final Category category) {
// Trace message
- this.getLogger().trace(MessageFormat.format("product={0},request={1},session={2} - CALLED!", product, request, session)); //NOI18N
+ this.getLogger().trace(MessageFormat.format("category={0} - CALLED!", category)); //NOI18N
- // Is product and session set?
- if (product == null) {
- // Not set
- throw new NullPointerException("product is null"); //NOI18N
- } else if (request == null) {
- // Not set
- throw new NullPointerException("request is null"); //NOI18N
- } else if (session == null) {
- // Not set
- throw new NullPointerException("session is null"); //NOI18N
+ // category must not be null
+ if (null == category) {
+ // Is null
+ throw new NullPointerException("category is null"); //NOI18N
}
- // First let's check if the product is choosen
- if (this.isProductChoosen(product, request, session)) {
- // Trace message
- this.getLogger().trace("Returning checked=\"checked\" - EXIT!"); //NOI18N
-
- // Is choosen
- return "checked=\"checked\""; //NOI18N
- } else {
- // Trace message
- this.getLogger().trace("Returning empty string - EXIT!"); //NOI18N
+ // Get parent id
+ Long parent = category.getParent();
- // Not choosen
- return ""; //NOI18N
+ // 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";
}
- /**
- * Some "getter" for HTML code 'disabled="disabled"' for e.g. submit buttons
- *
- * @param request Request instance
- * @param session Session instance
- * @return Whether the product is choosen
- */
@Override
- public String getDisabledHtmlFromSession (final HttpServletRequest request, final HttpSession session) {
+ public String getPrintableProduktCategory (final Product product) throws ServletException {
// Trace message
- this.getLogger().trace(MessageFormat.format("request={0},session={1} - CALLED!", request, session)); //NOI18N
+ this.getLogger().trace(MessageFormat.format("product={0} - CALLED!", product)); //NOI18N
- // Is product and session set?
- if (request == null) {
- // Not set
- throw new NullPointerException("request is null"); //NOI18N
- } else if (session == null) {
- // Not set
- throw new NullPointerException("session is null"); //NOI18N
+ // product must not be null
+ if (null == product) {
+ // Abort here
+ throw new NullPointerException("product is null"); //NOI18N
}
- // Is something selected?
- if (this.calculateTotalAmount(request, session) > 0) {
- // Trace message
- this.getLogger().trace("Returning empty string - EXIT!"); //NOI18N
+ // Declare category
+ Category category;
- // Something has been choosen
- return ""; //NOI18N
- } else {
- // Trace message
- this.getLogger().trace("Returning disabled=\"disabled\" - EXIT!"); //NOI18N
+ 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);
+ }
- // Nothing choosen yet
- return "disabled=\"disabled\""; //NOI18N
+ // 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.getDecodedTitle();
+ } 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;
}
/**
- * Marks given product as ordered in session
- *
- * @param product Product to mark as ordered
- * @param session Session instance
+ * 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?
*/
- @Override
- public void markProductAsOrdered(final Product product, final HttpSession session) {
+ private boolean isProductTitleUsed (final HttpServletRequest request) throws IOException, SQLException, BadTokenException, CorruptedDatabaseFileException, NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
// Trace message
- this.getLogger().trace(MessageFormat.format("product={0},session={1} - CALLED!", product, session)); //NOI18N
+ this.getLogger().trace(MessageFormat.format("request={0} - CALLED!", request)); //NOI18N
- // Is product and session set?
- if (product == null) {
- // Not set
- throw new NullPointerException("product is null"); //NOI18N
- } else if (session == null) {
- // Not set
- throw new NullPointerException("session is null"); //NOI18N
+ // Init title
+ String title = request.getParameter(ProductFrontend.COLUMN_TITLE);
+
+ // request must not be null and "title" must be found and non-empty
+ if (null == request) {
+ // Abort here
+ throw new NullPointerException("request is null"); //NOI18N
+ } else if (null == title) {
+ // title is not set
+ throw new IllegalArgumentException(MessageFormat.format("{0} is not set.", ProductFrontend.COLUMN_TITLE)); //NOI18N
+ } else if (title.isEmpty()) {
+ // Is left empty
+ throw new IllegalArgumentException(MessageFormat.format("{0} is empty.", ProductFrontend.COLUMN_TITLE)); //NOI18N
}
- // Mark it as ordered by setting flag
- this.getLogger().debug(MessageFormat.format("Marking product={0} as ordered.", product.getName())); //NOI18N
- this.setValueInSession(product, session, SESSION_ORDERED, "true"); //NOI18N
+ // Default is not used
+ boolean isUsed = this.isProductTitleUsed(title);
// Trace message
- this.getLogger().trace("EXIT!"); //NOI18N
+ this.getLogger().trace(MessageFormat.format("isUsed={0} - EXIT!", isUsed)); //NOI18N
+
+ // Return it
+ return isUsed;
}
/**
- * Unmarks given product as ordered in session
- *
- * @param product Product to unmark as ordered
- * @param session Session instance
+ * Handles admin form requests
+ * @param request Request instance
+ * @param response Response instance
+ * @throws ServletException If something unexpected happened
*/
@Override
- public void unmarkProductAsOrdered(final Product product, final HttpSession session) {
+ public void doAdminHandleProductForms (final HttpServletRequest request, final HttpServletResponse response) throws ServletException {
// Trace message
- this.getLogger().trace(MessageFormat.format("product={0},session={1} - CALLED!", product, session)); //NOI18N
+ this.getLogger().trace(MessageFormat.format("request={0},response={1} - CALLED!", request, response)); //NOI18N
- // Is product and session set?
- if (product == null) {
- // Not set
- throw new NullPointerException("product is null"); //NOI18N
- } else if (session == null) {
- // Not set
- throw new NullPointerException("session is null"); //NOI18N
+ // request and response must both be set
+ if (null == request) {
+ // request is null
+ throw new NullPointerException("request is null"); //NOI18N
+ } else if (null == response) {
+ // response is null
+ throw new NullPointerException("response is null"); //NOI18N
}
- // Mark it as ordered by setting flag
- this.getLogger().debug(MessageFormat.format("Unmarking product={0} as ordered.", product.getName())); //NOI18N
- this.clearSessionAttribute(product, session, SESSION_ORDERED);
+ // 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
}
/**
- * Some getter for printable value from session or an empty string for null.
- *
- * @param session Session instance
- * @param key Key to get
- * @return Value from key, empty string for null
+ * Handles admin form requests
+ * @param request Request instance
+ * @param response Response instance
+ * @throws ServletException If something unexpected happened
*/
@Override
- public Object getPrintableValeFromSession (final HttpSession session, final String key) {
+ public void doAdminHandleCategoryForms (final HttpServletRequest request, final HttpServletResponse response) throws ServletException {
// Trace message
- this.getLogger().trace(MessageFormat.format("session={0},key={1} - CALLED", session, key));
+ this.getLogger().trace(MessageFormat.format("request={0},response={1} - CALLED!", request, response)); //NOI18N
- // Are both parameter not null?
- if (session == null) {
- // Abort here
- throw new NullPointerException("session is null");
- } else if (key == null) {
- // Abort here
- throw new NullPointerException("key is null");
+ // request and response must both be set
+ if (null == request) {
+ // request is null
+ throw new NullPointerException("request is null"); //NOI18N
+ } else if (null == response) {
+ // response is null
+ throw new NullPointerException("response is null"); //NOI18N
}
- // Now get it
- Object value = this.getValueFromSession(session, key);
-
- // Debug message
- this.getLogger().debug(MessageFormat.format("value={0}", value));
+ // 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(MessageFormat.format("Calling this.convertNullToEmpty({0}) ... - EXIT!", value));
-
- // Return actual value
- return this.convertNullToEmpty(value);
+ this.getLogger().trace("EXIT!"); //NOI18N
}
/**
- * Gets an array of products from product iterator and unmarks them as ordered
+ * Checks if category's title is already used.
*
- * @param session HttpSession instance
- * @return Unmarked products
+ * @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?
*/
- @Override
- public Product[] getUnmarkedProducts (final HttpSession session) {
- // Init array
- Product[] array = this.getProducts();
+ 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
- // Unmark are all as ordered
- for (final Product product : array) {
- this.unmarkProductAsOrdered(product, session);
+ // Init title
+ String title = request.getParameter(CategoryFrontend.COLUMN_TITLE);
+
+ // request must not be null and "title" must be found and non-empty
+ if (null == request) {
+ // Abort here
+ throw new NullPointerException("request is null"); //NOI18N
+ } else if (null == title) {
+ // title is not set
+ throw new IllegalArgumentException(MessageFormat.format("{0} is not set.", CategoryFrontend.COLUMN_TITLE)); //NOI18N
+ } else if (title.isEmpty()) {
+ // Is left empty
+ throw new IllegalArgumentException(MessageFormat.format("{0} is empty.", CategoryFrontend.COLUMN_TITLE)); //NOI18N
}
- // Return finished array
- return array;
- }
+ // Default is not used
+ boolean isUsed = this.isCategoryTitleUsed(title);
- /**
- * Some "getter" for a an array of all products
- *
- * @return Unmarked products
- */
- @Override
- public Product[] getProducts () {
- return (Product[]) this.products.values().toArray();
+ // Trace message
+ this.getLogger().trace(MessageFormat.format("isUsed={0} - EXIT!", isUsed)); //NOI18N
+
+ // Return it
+ return isUsed;
}
- /**
- * Marks given product as choosen in session
- *
- * @param product Product to mark as ordered
- * @param session Session instance
- */
@Override
- public void markProductAsChoosen(final Product product, final HttpSession session) {
+ public Product getProduct (final AddableBasketItem item) throws ServletException {
// Trace message
- this.getLogger().trace(MessageFormat.format("product={0},session={1} - CALLED!", product, session)); //NOI18N
+ this.getLogger().trace("item=" + item + " - CALLED!");
- // Is product and session set?
- if (product == null) {
- // Not set
- throw new NullPointerException("product is null"); //NOI18N
- } else if (session == null) {
- // Not set
- throw new NullPointerException("session is null"); //NOI18N
+ // item should not be null
+ if (null == item) {
+ // Abort here
+ throw new NullPointerException("item is null");
+ } else if (null == this.productFrontend) {
+ // Abort here
+ throw new NullPointerException("productFrontend is null");
}
- // Mark it as ordered by setting flag
- this.getLogger().debug(MessageFormat.format("Marking product={0} as choosen.", product.getName())); //NOI18N
- this.setValueInSession(product, session, HTTP_PARAM_CHOOSE, "1");
+ // Init product instance
+ Product product = null;
+
+ try {
+ // Call frontend
+ product = this.productFrontend.getProduct(item);
+ } catch (final SQLException | IOException | BadTokenException | CorruptedDatabaseFileException | NoSuchMethodException | IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) {
+ // Continue to throw
+ throw new ServletException(ex);
+ }
// Trace message
- this.getLogger().trace("EXIT!"); //NOI18N
+ this.getLogger().trace("product=" + product + " - EXIT!");
+
+ // Return it
+ return product;
}
}