*/
public static final String MAIN_TITLE = "Pizza-Service";
- /**
- * Product list
- */
- private final SortedMap<String, Product> products;
-
/**
* Some singleton getter for this instance. If the instance is not set in
* given application, it will be created.
throw new NullPointerException("application is null"); //NOI18N
}
- // Init instance
+ // Init instance
PizzaApplication instance = null;
// Get instance from servlet application (aka. "application scope")
return instance;
}
+ /**
+ * For debugging purpose
+ *
+ * @param args Arguments
+ */
+ public static void main (String[] args) {
+ // Get instance and start it
+ new PizzaServiceApplication().start();
+ }
+
+ /**
+ * Product list
+ */
+ private final SortedMap<String, Product> products;
+
/**
* Private constructor
*/
private PizzaServiceApplication () {
// Init products instance
this.products = new TreeMap<>();
-
+
// Init bundle
this.initBundle();
-
+
// Fill products list
this.fillProductsList();
}
+ /**
+ * Calculates total amount of all choosen products
+ *
+ * @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) {
+ // Trace message
+ this.getLogger().trace(MessageFormat.format("request={0},session={1} - CALLED!", request, session)); //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
+ }
+
+ // Init total price
+ int totalAmount = 0;
+
+ // "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
+
+ // Getting amount
+ String amount = this.getAmountFromSession(product, session);
+
+ // 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.getName(), totalAmount)); //NOI18N
+ }
+
+ // Trace message
+ this.getLogger().trace(MessageFormat.format("totalAmount={0} - EXIT!", totalAmount)); //NOI18N
+
+ // Return total price
+ return totalAmount;
+ }
+
+ /**
+ * Calculates total price of all choosen products
+ *
+ * @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) {
+ // Trace message
+ this.getLogger().trace(MessageFormat.format("request={0},session={1} - CALLED!", request, session)); //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
+ }
+
+ // Init total price
+ float totalPrice = 0.00f;
+
+ // "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);
+ }
+ 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
+
+ // Return total price
+ return totalPrice;
+ }
+
@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.
}
-
+
@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
+ * Some "getter" for amount from session
*
- * @param name Internal name of product
- * @param title Product's title
- * @param price Price
+ * @param product Product instance
+ * @param session Session instance
+ * @return Amount as string
*/
- private void addProduct (final String name, final String title, final float price) {
+ @Override
+ public String getAmountFromSession (final Product product, final HttpSession session) {
// Trace message
- this.getLogger().trace(MessageFormat.format("name={0},title={1},price={2} - CALLED!", name, title, price)); //NOI18N
+ this.getLogger().trace(MessageFormat.format("product={0},session={1} - CALLED!", product, session)); //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
+ // 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
}
- // Instance product
- Product product = new PizzaProduct(name, title, price);
+ // Get attribute
+ Object object = this.getValueFromSession(product, session, HTTP_PARAM_AMOUNT);
- // Debug message
- this.getLogger().debug(MessageFormat.format("Adding product={0} ...", product)); //NOI18N
+ // Is the object null?
+ if (object == null) {
+ // Trace message
+ this.getLogger().trace("Returning 0 - EXIT!"); //NOI18N
- // Add it
- this.products.put(product.getName(), product);
+ // Not found
+ return "0"; //NOI18N
+ }
// Trace message
- this.getLogger().trace("EXIT!"); //NOI18N
+ this.getLogger().trace(MessageFormat.format("object={0} - EXIT!", object)); //NOI18N
+
+ // Cast to string and return it
+ return (String) object;
}
/**
- * Clears given parameter for product in 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
- * @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
- }
-
- /**
- * Fills products list
- * @todo Very hard-coded stuff ...
+ * @return Whether the product is choosen
*/
- private void fillProductsList () {
+ @Override
+ public String getCheckedHtmlFromProduct (final Product product, final HttpServletRequest request, final HttpSession session) {
// Trace message
- this.getLogger().trace("CALLED!"); //NOI18N
-
- // 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
+ 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
+ }
+
+ // 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
- // Trace message
- this.getLogger().trace("EXIT!"); //NOI18N
+ // Not choosen
+ return ""; //NOI18N
+ }
}
/**
- * Some getter for value from session
- *
+ * Some "getter" for choose from session
+ *
* @param product Product instance
* @param session Session instance
- * @param attribute Attribute to get value from
- * @return Value from session
+ * @return Choose as string
*/
- private Object getValueFromSession (final Product product, final HttpSession session, final String attribute) {
+ @Override
+ public String getChooseFromSession (final Product product, final HttpSession session) {
// 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().trace(MessageFormat.format("product={0},session={1} - CALLED!", product, session)); //NOI18N
- this.getLogger().debug(MessageFormat.format("product={0},attribute={1},value={2}", product.getName(), attribute, value)); //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
+ }
+
+ // Get attribute
+ Object object = this.getValueFromSession(product, session, HTTP_PARAM_CHOOSE);
+
+ // Is the object null?
+ if (object == null) {
+ // Not found
+ this.getLogger().debug(MessageFormat.format("Returning empty string for product={0} ...", product.getName())); //NOI18N
+ return ""; //NOI18N
+ }
// Trace message
- this.getLogger().trace(MessageFormat.format("value={0} - EXIT!", value)); //NOI18N
+ this.getLogger().trace(MessageFormat.format("object={0} - CALLED!", object)); //NOI18N
- // Return it
- return value;
+ // Cast to string and return it
+ return (String) object;
}
/**
- * Some getter for value from session
+ * Some "getter" for HTML code 'disabled="disabled"' for e.g. submit buttons
*
+ * @param request Request instance
* @param session Session instance
- * @param key Key to get value from
- * @return Value from session
+ * @return Whether the product is choosen
*/
- private Object getValueFromSession (final HttpSession session, final String key) {
+ @Override
+ public String getDisabledHtmlFromSession (final HttpServletRequest request, final HttpSession session) {
// Trace message
- this.getLogger().trace(MessageFormat.format("session={043},key={1} - CALLED!", session, key)); //NOI18N
-
- // Init value
- Object value = null;
-
- // Get it synchronized from session
- synchronized (session) {
- value = session.getAttribute(key);
- }
-
- // Trace message
- this.getLogger().trace(MessageFormat.format("value={0} - EXIT!", value)); //NOI18N
-
- // Return it
- return value;
- }
-
- /**
- * 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 it found?
- return this.products.containsKey(name);
- }
-
- /**
- * For debugging purpose
- *
- * @param args Arguments
- */
- public static void main (String[] args) {
- // Get instance and start it
- new PizzaServiceApplication().start();
- }
-
- /**
- * 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
-
- // Return result
- return ("true".equals(isOrdered)); //NOI18N
- }
-
- /**
- * 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
- }
-
- /**
- * 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
- }
-
- /**
- * 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())); //NOI18N
- }
- }
-
- /**
- * Some "getter" for amount from session
- *
- * @param product Product instance
- * @param session Session instance
- * @return Amount as string
- */
- @Override
- public String getAmountFromSession (final Product product, final HttpSession session) {
- // Trace message
- this.getLogger().trace(MessageFormat.format("product={0},session={1} - CALLED!", product, session)); //NOI18N
+ this.getLogger().trace(MessageFormat.format("request={0},session={1} - CALLED!", request, session)); //NOI18N
// Is product and session set?
- if (product == null) {
+ if (request == null) {
// Not set
- throw new NullPointerException("product is null"); //NOI18N
+ throw new NullPointerException("request is null"); //NOI18N
} else if (session == null) {
// Not set
throw new NullPointerException("session is null"); //NOI18N
}
-
- // Get attribute
- Object object = this.getValueFromSession(product, session, HTTP_PARAM_AMOUNT);
-
- // Is the object null?
- if (object == null) {
+
+ // Is something selected?
+ if (this.calculateTotalAmount(request, session) > 0) {
// Trace message
- this.getLogger().trace("Returning 0 - EXIT!"); //NOI18N
-
- // Not found
- return "0"; //NOI18N
- }
-
- // Trace message
- this.getLogger().trace(MessageFormat.format("object={0} - EXIT!", object)); //NOI18N
-
- // Cast to string and return it
- return (String) object;
- }
-
- /**
- * Some "getter" for choose from session
- *
- * @param product Product instance
- * @param session Session instance
- * @return Choose as string
- */
- @Override
- public String getChooseFromSession (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) {
- // Not set
- throw new NullPointerException("product is null"); //NOI18N
- } else if (session == null) {
- // Not set
- throw new NullPointerException("session is null"); //NOI18N
- }
-
- // Get attribute
- Object object = this.getValueFromSession(product, session, HTTP_PARAM_CHOOSE);
-
- // Is the object null?
- if (object == null) {
- // Not found
- this.getLogger().debug(MessageFormat.format("Returning empty string for product={0} ...", product.getName())); //NOI18N
+ this.getLogger().trace("Returning empty string - EXIT!"); //NOI18N
+
+ // Something has been choosen
return ""; //NOI18N
+ } else {
+ // Trace message
+ this.getLogger().trace("Returning disabled=\"disabled\" - EXIT!"); //NOI18N
+
+ // Nothing choosen yet
+ return "disabled=\"disabled\""; //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
- * @param request Request instance
- * @param session Session instance
- * @return Amount as string
- */
- @Override
- public String handleAmountFromRequestSession (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
-
- // 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 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 attribute from request
- object = request.getParameter(String.format(HTTP_PARAM_MASK, HTTP_PARAM_AMOUNT, product.getName()));
-
- // 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
- }
-
- // Then set it in session
- this.setValueInSession(product, session, HTTP_PARAM_AMOUNT, object);
-
- // And return it
- return (String) object;
- }
-
- // Trace message
- this.getLogger().trace("Calling getAmountFromSession() ..."); //NOI18N
-
- // Get attribute from session
- return this.getAmountFromSession(product, session);
}
/**
- * Handler for choosen (checkbox) from request or session
+ * Some "getter" for choosen (checkbox) from session
*
* @param product Product instance
* @param request Request instance
* @return Amount as string
*/
@Override
- public String handleChooseFromRequestSession (final Product product, final HttpServletRequest request, final HttpSession session) {
+ public String getPrintableChoosenFromRequestSession (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
throw new NullPointerException("session is null"); //NOI18N
}
- // Init variabke
- Object object;
+ // Get element
+ this.getLogger().debug("Calling handleChooseFromRequestSession() ..."); //NOI18N
+ String choosen = this.handleChooseFromRequestSession(product, request, session);
+ this.getLogger().debug(MessageFormat.format("product={0},choosen={1}", product.getName(), choosen)); //NOI18N
- // 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);
+ // Must not be null
+ assert(choosen instanceof String): "choosen is null"; //NOI18N
+
+ // Is it empty?
+ if (choosen.isEmpty()) {
+ // Not choosen
+ return "Nein";
}
- // 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
+ // Get amount
+ String amount = this.handleAmountFromRequestSession(product, request, session);
+ this.getLogger().debug(MessageFormat.format("product={0},amount={1}", product.getName(), amount)); //NOI18N
- // 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);
+ // Must not be null
+ assert(amount instanceof String): "amount is null"; //NOI18N
- // Return empty string
- return ""; //NOI18N
+ // Is it empty?
+ if (amount.isEmpty() || "0".equals(amount)) { //NOI18N
+ // Choosen, but no amount
+ return "Nein";
+ } else {
+ // Is choosen
+ return "Ja";
}
-
- // 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;
}
/**
- * Some "getter" for choosen (checkbox) from session
+ * Some getter for printable value from session or an empty string for null.
*
- * @param product Product instance
- * @param request Request instance
* @param session Session instance
- * @return Amount as string
+ * @param key Key to get
+ * @return Value from key, empty string for null
*/
@Override
- public String getPrintableChoosenFromRequestSession (final Product product, final HttpServletRequest request, final HttpSession session) {
+ public Object getPrintableValeFromSession (final HttpSession session, final String key) {
// 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
+ this.getLogger().trace(MessageFormat.format("session={0},key={1} - CALLED", session, key)); //NOI18N
+
+ // Are both parameter not null?
+ if (session == null) {
+ // Abort here
throw new NullPointerException("session is null"); //NOI18N
+ } else if (key == null) {
+ // Abort here
+ throw new NullPointerException("key is null"); //NOI18N
}
+
+ // Now get it
+ Object value = this.getValueFromSession(session, key);
+
+ // Debug message
+ this.getLogger().debug(MessageFormat.format("value={0}", value)); //NOI18N
+
+ // Trace message
+ this.getLogger().trace(MessageFormat.format("Calling this.convertNullToEmpty({0}) ... - EXIT!", value)); //NOI18N
- // Get element
- 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 empty?
- if (choosen.isEmpty()) {
- // Not choosen
- return "Nein";
- }
-
- // Get amount
- String amount = this.handleAmountFromRequestSession(product, request, session);
- this.getLogger().debug(MessageFormat.format("product={0},amount={1}", product.getName(), amount)); //NOI18N
-
- // Must not be null
- assert(amount instanceof String): "amount is null"; //NOI18N
+ // Return actual value
+ return this.convertNullToEmpty(value);
+ }
- // Is it empty?
- if (amount.isEmpty() || "0".equals(amount)) { //NOI18N
- // Choosen, but no amount
- return "Nein";
- } else {
- // Is choosen
- return "Ja";
- }
+ /**
+ * Some "getter" for a an array of all products
+ *
+ * @return Unmarked products
+ */
+ @Override
+ public Product[] getProducts () {
+ return this.products.values().toArray(new Product[this.products.size()]);
}
/**
}
// Get choosen
+ this.getLogger().debug("Calling handleChooseFromRequestSession() ..."); //NOI18N
String choosen = this.handleChooseFromRequestSession(product, request, session);
this.getLogger().debug(MessageFormat.format("product={0},choosen={1}", product.getName(), choosen)); //NOI18N
}
/**
- * Checks whether the given product is choosen, request overules session.
+ * Gets an array of products from product iterator and unmarks them as ordered
+ *
+ * @param session HttpSession instance
+ * @return Unmarked products
+ */
+ @Override
+ public Product[] getUnmarkedProducts (final HttpSession session) {
+ // Init array
+ Product[] array = this.getProducts();
+
+ // Unmark are all as ordered
+ for (final Product product : array) {
+ this.unmarkProductAsOrdered(product, session);
+ }
+
+ // Return finished array
+ return array;
+ }
+
+ /**
+ * Handler for amount from request or session
*
* @param product Product instance
* @param request Request instance
* @param session Session instance
- * @return Whether the product is choosen
+ * @return Amount as string
*/
@Override
- public boolean isProductChoosen (final Product product, final HttpServletRequest request, final HttpSession session) {
+ public String handleAmountFromRequestSession (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
// 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()) {
+
+ // 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
- return false;
+ 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 amount
- String amount = this.handleAmountFromRequestSession(product, request, session);
-
- // Must not be null
- assert(amount instanceof String): "amount is not set"; //NOI18N
-
+
+ // Get attribute from request
+ object = request.getParameter(String.format(HTTP_PARAM_MASK, HTTP_PARAM_AMOUNT, product.getName()));
+
+ // 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
+ }
+
+ // Then set it in session
+ this.setValueInSession(product, session, HTTP_PARAM_AMOUNT, object);
+
+ // And return it
+ return (String) object;
+ }
+
// Trace message
- this.getLogger().trace(MessageFormat.format("amount={0} - EXIT!", amount)); //NOI18N
+ this.getLogger().trace("Calling getAmountFromSession() ..."); //NOI18N
- // Must not be empty and not 0
- return (!amount.isEmpty() && !"0".equals(amount)); //NOI18N
+ // Get attribute from session
+ return this.getAmountFromSession(product, session);
}
/**
- * Calculates total price of all choosen products
+ * Checks whether the given product is choosen, request overules session.
*
+ * @param product Product instance
* @param request Request instance
* @param session Session instance
- * @return Total price of all choosen products
+ * @return Whether the product is choosen
*/
@Override
- public float calculateTotalPrice (final HttpServletRequest request, final HttpSession session) {
+ public boolean isProductChoosen (final Product product, final HttpServletRequest request, final HttpSession session) {
// Trace message
- this.getLogger().trace(MessageFormat.format("request={0},session={1} - CALLED!", request, session)); //NOI18N
+ this.getLogger().trace(MessageFormat.format("product={0},request={1},session={2} - CALLED!", product, request, session)); //NOI18N
// Is product and session set?
- if (request == null) {
+ 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 total price
- float totalPrice = 0.00f;
-
- // "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);
- }
- this.getLogger().debug(MessageFormat.format("product={0},totalPrice={1}", product.getName(), totalPrice)); //NOI18N
+
+ // Get choosen
+ this.getLogger().debug("Calling handleChooseFromRequestSession() ..."); //NOI18N
+ 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);
+
+ // Must not be null
+ assert(amount instanceof String): "amount is not set"; //NOI18N
+
// Trace message
- this.getLogger().trace(MessageFormat.format(" totalPrice={0} - EXIT!", totalPrice)); //NOI18N
+ this.getLogger().trace(MessageFormat.format("amount={0} - EXIT!", amount)); //NOI18N
- // Return total price
- return totalPrice;
+ // Must not be empty and not 0
+ return (!amount.isEmpty() && !"0".equals(amount)); //NOI18N
}
/**
- * Calculates total amount of all choosen products
+ * Marks given product as choosen in session
*
- * @param request Request instance
+ * @param product Product to mark as ordered
* @param session Session instance
- * @return Total amount of all choosen products
*/
@Override
- public int calculateTotalAmount (final HttpServletRequest request, final HttpSession session) {
+ public void markProductAsChoosen (final Product product, final HttpSession session) {
// Trace message
- this.getLogger().trace(MessageFormat.format("request={0},session={1} - CALLED!", request, session)); //NOI18N
+ this.getLogger().trace(MessageFormat.format("product={0},session={1} - CALLED!", product, session)); //NOI18N
// Is product and session set?
- if (request == null) {
+ if (product == null) {
// Not set
- throw new NullPointerException("request is null"); //NOI18N
+ throw new NullPointerException("product is null"); //NOI18N
} else if (session == null) {
// Not set
throw new NullPointerException("session is null"); //NOI18N
}
-
- // Init total price
- int totalAmount = 0;
-
- // "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
-
- // Getting amount
- String amount = this.getAmountFromSession(product, session);
-
- // 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.getName(), totalAmount)); //NOI18N
- }
+
+ // 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"); //NOI18N
// Trace message
- this.getLogger().trace(MessageFormat.format("totalAmount={0} - EXIT!", totalAmount)); //NOI18N
-
- // Return total price
- return totalAmount;
+ this.getLogger().trace("EXIT!"); //NOI18N
}
/**
- * Some "getter" for HTML code 'checked="checked"' if the product is choosen
+ * Marks given product as ordered in session
*
- * @param product Product instance
- * @param request Request instance
+ * @param product Product to mark as ordered
* @param session Session instance
- * @return Whether the product is choosen
*/
@Override
- public String getCheckedHtmlFromProduct (final Product product, final HttpServletRequest request, final HttpSession session) {
+ public void markProductAsOrdered (final Product product, final HttpSession session) {
// Trace message
- this.getLogger().trace(MessageFormat.format("product={0},request={1},session={2} - CALLED!", product, request, session)); //NOI18N
+ this.getLogger().trace(MessageFormat.format("product={0},session={1} - CALLED!", product, 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
}
+
+ // 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
- // 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
-
- // Not choosen
- return ""; //NOI18N
- }
+ // Trace message
+ this.getLogger().trace("EXIT!"); //NOI18N
}
/**
- * Some "getter" for HTML code 'disabled="disabled"' for e.g. submit buttons
+ * Somewhat setter in session
*
- * @param request Request instance
* @param session Session instance
- * @return Whether the product is choosen
+ * @param key Session key to set
+ * @param value Value to set
*/
@Override
- public String getDisabledHtmlFromSession (final HttpServletRequest request, final HttpSession session) {
- // Trace message
- this.getLogger().trace(MessageFormat.format("request={0},session={1} - CALLED!", request, session)); //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
- }
-
- // Is something selected?
- if (this.calculateTotalAmount(request, session) > 0) {
- // Trace message
- this.getLogger().trace("Returning empty string - EXIT!"); //NOI18N
-
- // Something has been choosen
- return ""; //NOI18N
- } else {
- // Trace message
- this.getLogger().trace("Returning disabled=\"disabled\" - EXIT!"); //NOI18N
+ 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
- // Nothing choosen yet
- return "disabled=\"disabled\""; //NOI18N
+ synchronized(session) {
+ // Set it synced
+ session.setAttribute(key, value);
}
+
+ // Trace message
+ this.getLogger().trace("EXIT!"); //NOI18N
}
/**
- * Marks given product as ordered in session
+ * Unmarks given product as choosen in session
*
- * @param product Product to mark as ordered
+ * @param product Product to unmark as choosen
* @param session Session instance
*/
@Override
- public void markProductAsOrdered(final Product product, final HttpSession session) {
+ public void unmarkProductAsChoosen (final Product product, final HttpSession session) {
// Trace message
this.getLogger().trace(MessageFormat.format("product={0},session={1} - CALLED!", product, session)); //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
+ this.getLogger().debug(MessageFormat.format("Unmarking product={0} as choosen.", product.getName())); //NOI18N
+ this.clearSessionAttribute(product, session, HTTP_PARAM_CHOOSE);
// Trace message
this.getLogger().trace("EXIT!"); //NOI18N
* @param session Session instance
*/
@Override
- public void unmarkProductAsOrdered(final Product product, final HttpSession session) {
+ public void unmarkProductAsOrdered (final Product product, final HttpSession session) {
// Trace message
this.getLogger().trace(MessageFormat.format("product={0},session={1} - CALLED!", product, session)); //NOI18N
}
/**
- * Some getter for printable value from session or an empty string for null.
+ * Adds given product to list or throws an exception if name is already found
*
- * @param session Session instance
- * @param key Key to get
- * @return Value from key, empty string for null
+ * @param name Internal name of product
+ * @param title Product's title
+ * @param price Price
*/
- @Override
- public Object getPrintableValeFromSession (final HttpSession session, final String key) {
+ private void addProduct (final String name, final String title, final float price) {
// Trace message
- this.getLogger().trace(MessageFormat.format("session={0},key={1} - CALLED", session, key)); //NOI18N
-
- // Are both parameter not null?
- if (session == null) {
- // Abort here
- throw new NullPointerException("session is null"); //NOI18N
- } else if (key == null) {
- // Abort here
- throw new NullPointerException("key is null"); //NOI18N
+ this.getLogger().trace(MessageFormat.format("name={0},title={1},price={2} - CALLED!", name, title, price)); //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
}
+
+ // Instance product
+ Product product = new PizzaProduct(name, title, price);
+
+ // Debug message
+ this.getLogger().debug(MessageFormat.format("Adding product={0} ...", product)); //NOI18N
- // Now get it
- Object value = this.getValueFromSession(session, key);
+ // Add it
+ this.products.put(product.getName(), product);
- // Debug message
- this.getLogger().debug(MessageFormat.format("value={0}", value)); //NOI18N
+ // 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("Calling this.convertNullToEmpty({0}) ... - EXIT!", value)); //NOI18N
+ 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
+ }
+
+ /**
+ * Fills products list
+ * @todo Very hard-coded stuff ...
+ */
+ private void fillProductsList () {
+ // Trace message
+ this.getLogger().trace("CALLED!"); //NOI18N
+
+ // 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
- // Return actual value
- return this.convertNullToEmpty(value);
+ // Trace message
+ this.getLogger().trace("EXIT!"); //NOI18N
}
/**
- * Gets an array of products from product iterator and unmarks them as ordered
- *
- * @param session HttpSession instance
- * @return Unmarked products
+ * 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
*/
- @Override
- public Product[] getUnmarkedProducts (final HttpSession session) {
- // Init array
- Product[] array = this.getProducts();
+ 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
- // Unmark are all as ordered
- for (final Product product : array) {
- this.unmarkProductAsOrdered(product, session);
- }
+ // 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
- // Return finished array
- return array;
+ // Trace message
+ this.getLogger().trace(MessageFormat.format("value={0} - EXIT!", value)); //NOI18N
+
+ // Return it
+ return value;
}
/**
- * Some "getter" for a an array of all products
- *
- * @return Unmarked products
+ * Some getter for value from session
+ *
+ * @param session Session instance
+ * @param key Key to get value from
+ * @return Value from session
*/
- @Override
- public Product[] getProducts () {
- return this.products.values().toArray(new Product[this.products.size()]);
+ private Object getValueFromSession (final HttpSession session, final String key) {
+ // Trace message
+ this.getLogger().trace(MessageFormat.format("session={043},key={1} - CALLED!", session, key)); //NOI18N
+
+ // Init value
+ Object value = null;
+
+ // Get it synchronized from session
+ synchronized (session) {
+ value = session.getAttribute(key);
+ }
+
+ // Trace message
+ this.getLogger().trace(MessageFormat.format("value={0} - EXIT!", value)); //NOI18N
+
+ // Return it
+ return value;
}
/**
- * Marks given product as choosen in session
+ * Handler for choosen (checkbox) from request or session
*
- * @param product Product to mark as ordered
+ * @param product Product instance
+ * @param request Request instance
* @param session Session instance
+ * @return Amount as string
*/
- @Override
- public void markProductAsChoosen(final Product product, final HttpSession session) {
+ private String handleChooseFromRequestSession(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) {
// 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 variabke
+ Object object;
+
+ // 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);
+ }
+
+ // 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
+
+ // 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);
+
+ // Return empty string
+ return ""; //NOI18N
+ }
+
+ // Then set it in session
+ this.setValueInSession(product, session, HTTP_PARAM_CHOOSE, object);
- // 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"); //NOI18N
+ // Cast to string and return it
+ this.getLogger().debug(MessageFormat.format("product={0} - Returning {1} ...", product.getName(), object)); //NOI18N
+ return (String) object;
+ }
+
+ /**
+ * 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 it found?
+ return this.products.containsKey(name);
+ }
+
+ /**
+ * 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
+
+ // Return result
+ return ("true".equals(isOrdered)); //NOI18N
+ }
+
+ /**
+ * 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
}
+
+ /**
+ * 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())); //NOI18N
+ }
+ }
+
}