2 * Copyright (C) 2015 Roland Haeder
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 package org.mxchange.pizzaapplication.application;
19 import java.lang.reflect.Field;
20 import java.text.MessageFormat;
21 import java.util.Iterator;
23 import java.util.SortedMap;
24 import java.util.TreeMap;
25 import javax.servlet.ServletContext;
26 import javax.servlet.ServletException;
27 import javax.servlet.http.HttpServletRequest;
28 import javax.servlet.http.HttpSession;
29 import org.mxchange.jcore.contact.Gender;
30 import org.mxchange.pizzaapplication.BasePizzaServiceSystem;
31 import org.mxchange.pizzaapplication.customer.Customer;
32 import org.mxchange.pizzaapplication.customer.PizzaServiceCustomer;
33 import org.mxchange.pizzaapplication.product.PizzaProduct;
34 import org.mxchange.pizzaapplication.product.Product;
38 * @author Roland Haeder
40 public class PizzaServiceApplication extends BasePizzaServiceSystem implements PizzaApplication {
44 public static final String MAIN_TITLE = "Pizza-Service";
49 private final SortedMap<String, Product> products;
52 * Some singleton getter for this instance. If the instance is not set in
53 * given application, it will be created.
55 * @param application Servlet context
56 * @return This instance
57 * @throws javax.servlet.ServletException If object is not set correctly
59 public static final PizzaApplication getInstance (final ServletContext application) throws ServletException {
60 // Check application instance
61 if (application == null) {
63 throw new NullPointerException("application is null"); //NOI18N
67 PizzaApplication instance = null;
69 // Get instance from servlet application (aka. "application scope")
70 Object object = application.getAttribute("app"); //NOI18N
73 if (object instanceof PizzaApplication) {
74 // Instance is set, so casting should work
75 instance = (PizzaApplication) object;
76 } else if (object instanceof Object) {
77 // Not correct instance
78 throw new ServletException("app is not set correctly"); //NOI18N
80 // "service" is null, so initialize it
81 instance = new PizzaServiceApplication();
84 application.setAttribute("app", instance); //NOI18N
88 instance.getLogger().trace(MessageFormat.format("instance={0} - EXIT!", instance)); //NOI18N
97 private PizzaServiceApplication () {
98 // Init products instance
99 this.products = new TreeMap<>();
104 // Fill products list
105 this.fillProductsList();
109 public void doBootstrap () {
110 throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
114 public void doMainLoop () {
115 throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
119 public void doShutdown () {
120 throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
124 * Adds given product to list or throws an exception if name is already found
126 * @param name Internal name of product
127 * @param title Product's title
130 private void addProduct (final String name, final String title, final float price) {
132 this.getLogger().trace(MessageFormat.format("name={0},title={1},price={2} - CALLED!", name, title, price)); //NOI18N
134 // Is the name already used?
135 if (this.isProductNameUsed(name)) {
136 // Something went wrong
137 throw new IllegalArgumentException(MessageFormat.format("product {0} is already used.", name)); //NOI18N
141 Product product = new PizzaProduct(name, title, price);
144 this.getLogger().debug(MessageFormat.format("Adding product={0} ...", product)); //NOI18N
147 this.products.put(product.getName(), product);
150 this.getLogger().trace("EXIT!"); //NOI18N
154 * Clears given parameter for product in session
156 * @param product Product instance
157 * @param session Session instance
158 * @param parameter Parameter to clear
160 private void clearSessionAttribute (final Product product, final HttpSession session, final String parameter) {
162 this.getLogger().trace(MessageFormat.format("produce={0},parameter={1},session={2} - CALLED!", product, parameter, session)); //NOI18N
165 this.getLogger().debug(MessageFormat.format("Clearing product={0},parameter={1} ...", product.getName(), parameter)); //NOI18N
166 this.setValueInSession(product, session, parameter, null);
169 this.getLogger().trace("EXIT!"); //NOI18N
173 * Fills products list
174 * @todo Very hard-coded stuff ...
176 private void fillProductsList () {
178 this.getLogger().trace("CALLED!"); //NOI18N
181 this.addProduct("italia", "Pizza Italia", 5.50f); //NOI18N
182 this.addProduct("diablo", "Pizza Diablo", 7.80f); //NOI18N
183 this.addProduct("bolognese", "Spagetti Bolognese", 11.95f); //NOI18N
186 this.getLogger().trace("EXIT!"); //NOI18N
190 * Some getter for value from session
192 * @param product Product instance
193 * @param session Session instance
194 * @param attribute Attribute to get value from
195 * @return Value from session
197 private Object getValueFromSession (final Product product, final HttpSession session, final String attribute) {
199 this.getLogger().trace(MessageFormat.format("product={0},session={1},attribute={2} - CALLED!", product, session, attribute)); //NOI18N
202 Object value = this.getValueFromSession(session, String.format(HTTP_PARAM_MASK, product.getName(), attribute));
204 this.getLogger().debug(MessageFormat.format("product={0},attribute={1},value={2}", product.getName(), attribute, value)); //NOI18N
207 this.getLogger().trace(MessageFormat.format("value={0} - EXIT!", value)); //NOI18N
214 * Some getter for value from session
216 * @param session Session instance
217 * @param key Key to get value from
218 * @return Value from session
220 private Object getValueFromSession (final HttpSession session, final String key) {
222 this.getLogger().trace(MessageFormat.format("session={043},key={1} - CALLED!", session, key)); //NOI18N
227 // Get it synchronized from session
228 synchronized (session) {
229 value = session.getAttribute(key);
233 this.getLogger().trace(MessageFormat.format("value={0} - EXIT!", value)); //NOI18N
240 * Checks whether given product is already used
242 * @param name Name of product
243 * @return Whether the given product's name is already used
245 private boolean isProductNameUsed (final String name) {
247 this.getLogger().trace(MessageFormat.format("name={0} - CALLED!", name)); //NOI18N
250 return this.products.containsKey(name);
254 * For debugging purpose
256 * @param args Arguments
258 public static void main (String[] args) {
259 // Get instance and start it
260 new PizzaServiceApplication().start();
264 * Checks if the product ordered?
270 private boolean isProductOrdered (final Product product, final HttpSession session) {
272 this.getLogger().trace(MessageFormat.format("product={0},session={1} - CALLED!", product, session)); //NOI18N
275 Object isOrdered = this.getValueFromSession(product, session, SESSION_ORDERED);
276 this.getLogger().debug(MessageFormat.format("product={0},isOrdered={1}", product.getName(), isOrdered)); //NOI18N
279 return ("true".equals(isOrdered)); //NOI18N
283 * Somewhat setter in session
285 * @param product Product instance
286 * @param session Session instance
287 * @param keyPart Key part to include in final key
288 * @param value Value to set
290 private void setValueInSession (final Product product, final HttpSession session, final String keyPart, final Object value) {
292 this.getLogger().trace(MessageFormat.format("product={0},session={1},keyPart={2},value={3} - CALLED!", product, session, keyPart, value)); //NOI18N
294 synchronized(session) {
296 this.getLogger().debug(MessageFormat.format("Setting value={0} for product={1},keyPart={2}", value, product.getName(), keyPart)); //NOI18N
297 this.setValueInSession(session, String.format(HTTP_PARAM_MASK, product.getName(), keyPart), value);
301 this.getLogger().trace("EXIT!"); //NOI18N
305 * Somewhat setter in session
307 * @param session Session instance
308 * @param key Session key to set
309 * @param value Value to set
312 public void setValueInSession (final HttpSession session, final String key, final Object value) {
314 this.getLogger().trace(MessageFormat.format("session={0},key={1},value={2} - CALLED!", session, key, value)); //NOI18N
316 synchronized(session) {
318 session.setAttribute(key, value);
322 this.getLogger().trace("EXIT!"); //NOI18N
326 * Application starter
328 private void start () {
329 // "Walk" over all products
330 for (final Product product : this.getProducts()) {
332 this.getLogger().debug(MessageFormat.format("Product {0}, {1}: {2}", product.getName(), product.getTitle(), product.getPrice())); //NOI18N
335 // Generate fake Customer instance
336 Customer customer = new PizzaServiceCustomer();
339 * Need a least a gender ... :( See, that is why I don't like default
340 * constructors, you can easily miss something important and bam! You
341 * get an NPE. The fix here is, to have construtors (or factories) which
342 * requires all required instances that needs to be set to get a
343 * consitent object back.
346 // Gender is MALE now
347 customer.setGender(Gender.MALE);
349 // Get iterator on all its fields
350 Iterator<Map.Entry<Field, Object>> it = customer.iterator();
353 while (it.hasNext()) {
354 Map.Entry<Field, Object> entry = it.next();
355 this.getLogger().debug(MessageFormat.format("entry {0}={1}", entry.getKey(), entry.getValue()));
360 * Some "getter" for amount from session
362 * @param product Product instance
363 * @param session Session instance
364 * @return Amount as string
367 public String getAmountFromSession (final Product product, final HttpSession session) {
369 this.getLogger().trace(MessageFormat.format("product={0},session={1} - CALLED!", product, session)); //NOI18N
371 // Is product and session set?
372 if (product == null) {
374 throw new NullPointerException("product is null"); //NOI18N
375 } else if (session == null) {
377 throw new NullPointerException("session is null"); //NOI18N
381 Object object = this.getValueFromSession(product, session, HTTP_PARAM_AMOUNT);
383 // Is the object null?
384 if (object == null) {
386 this.getLogger().trace("Returning 0 - EXIT!"); //NOI18N
393 this.getLogger().trace(MessageFormat.format("object={0} - EXIT!", object)); //NOI18N
395 // Cast to string and return it
396 return (String) object;
400 * Some "getter" for choose from session
402 * @param product Product instance
403 * @param session Session instance
404 * @return Choose as string
407 public String getChooseFromSession (final Product product, final HttpSession session) {
409 this.getLogger().trace(MessageFormat.format("product={0},session={1} - CALLED!", product, session)); //NOI18N
411 // Is product and session set?
412 if (product == null) {
414 throw new NullPointerException("product is null"); //NOI18N
415 } else if (session == null) {
417 throw new NullPointerException("session is null"); //NOI18N
421 Object object = this.getValueFromSession(product, session, HTTP_PARAM_CHOOSE);
423 // Is the object null?
424 if (object == null) {
426 this.getLogger().debug(MessageFormat.format("Returning empty string for product={0} ...", product.getName())); //NOI18N
431 this.getLogger().trace(MessageFormat.format("object={0} - CALLED!", object)); //NOI18N
433 // Cast to string and return it
434 return (String) object;
438 * Handler for amount from request or session
440 * @param product Product instance
441 * @param request Request instance
442 * @param session Session instance
443 * @return Amount as string
446 public String handleAmountFromRequestSession (final Product product, final HttpServletRequest request, final HttpSession session) {
448 this.getLogger().trace(MessageFormat.format("product={0},request={1},session={2} - CALLED!", product, request, session)); //NOI18N
450 // Is product and session set?
451 if (product == null) {
453 throw new NullPointerException("product is null"); //NOI18N
454 } else if (request == null) {
456 throw new NullPointerException("request is null"); //NOI18N
457 } else if (session == null) {
459 throw new NullPointerException("session is null"); //NOI18N
465 // Check request method
466 if (!"POST".equals(request.getMethod())) { //NOI18N
467 // Not POST, so get from session
468 return this.getAmountFromSession(product, session);
469 } else if (this.handleChooseFromRequestSession(product, request, session).isEmpty()) {
471 this.clearSessionAttribute(product, session, HTTP_PARAM_AMOUNT);
472 this.getLogger().debug(MessageFormat.format("Unsetting for product={0} in session, returning zero ...", product.getName())); //NOI18N
476 // Get attribute from request
477 object = request.getParameter(String.format(HTTP_PARAM_MASK, HTTP_PARAM_AMOUNT, product.getName()));
480 if (object instanceof String) {
481 // Try to parse it to integer
483 Integer value = Integer.valueOf((String) object);
484 } catch (final NumberFormatException ex) {
486 this.getLogger().warn(ex);
490 // Then set it in session
491 this.setValueInSession(product, session, HTTP_PARAM_AMOUNT, object);
494 return (String) object;
498 this.getLogger().trace("Calling getAmountFromSession() ..."); //NOI18N
500 // Get attribute from session
501 return this.getAmountFromSession(product, session);
505 * Handler for choosen (checkbox) from request or session
507 * @param product Product instance
508 * @param request Request instance
509 * @param session Session instance
510 * @return Amount as string
513 public String handleChooseFromRequestSession (final Product product, final HttpServletRequest request, final HttpSession session) {
515 this.getLogger().trace(MessageFormat.format("product={0},request={1},session={2} - CALLED!", product, request, session)); //NOI18N
517 // Is product and session set?
518 if (product == null) {
520 throw new NullPointerException("product is null"); //NOI18N
521 } else if (request == null) {
523 throw new NullPointerException("request is null"); //NOI18N
524 } else if (session == null) {
526 throw new NullPointerException("session is null"); //NOI18N
532 // Check request method
533 if (!"POST".equals(request.getMethod())) { //NOI18N
534 // Not POST, so get from session
535 return this.getChooseFromSession(product, session);
536 } else if (this.isProductOrdered(product, session)) {
537 // Product is ordered
538 return this.getChooseFromSession(product, session);
541 // Get reqzest element
542 object = request.getParameter(String.format(HTTP_PARAM_MASK, HTTP_PARAM_CHOOSE, product.getName()));
543 this.getLogger().debug(MessageFormat.format("product={0},object={1}", product.getName(), object)); //NOI18N
546 if (object == null) {
548 this.getLogger().debug(MessageFormat.format("Unsetting session for product={0} ...", product.getName())); //NOI18N
549 this.clearSessionAttribute(product, session, HTTP_PARAM_CHOOSE);
550 this.clearSessionAttribute(product, session, HTTP_PARAM_AMOUNT);
552 // Return empty string
556 // Then set it in session
557 this.setValueInSession(product, session, HTTP_PARAM_CHOOSE, object);
559 // Cast to string and return it
560 this.getLogger().debug(MessageFormat.format("product={0} - Returning {1} ...", product.getName(), object)); //NOI18N
561 return (String) object;
565 * Some "getter" for choosen (checkbox) from session
567 * @param product Product instance
568 * @param request Request instance
569 * @param session Session instance
570 * @return Amount as string
573 public String getPrintableChoosenFromRequestSession (final Product product, final HttpServletRequest request, final HttpSession session) {
575 this.getLogger().trace(MessageFormat.format("product={0},request={1},session={2} - CALLED!", product, request, session)); //NOI18N
577 // Is product and session set?
578 if (product == null) {
580 throw new NullPointerException("product is null"); //NOI18N
581 } else if (request == null) {
583 throw new NullPointerException("request is null"); //NOI18N
584 } else if (session == null) {
586 throw new NullPointerException("session is null"); //NOI18N
590 String choosen = this.handleChooseFromRequestSession(product, request, session);
591 this.getLogger().debug(MessageFormat.format("product={0},choosen={1}", product.getName(), choosen)); //NOI18N
594 assert(choosen instanceof String): "choosen is null"; //NOI18N
597 if (choosen.isEmpty()) {
603 String amount = this.handleAmountFromRequestSession(product, request, session);
604 this.getLogger().debug(MessageFormat.format("product={0},amount={1}", product.getName(), amount)); //NOI18N
607 assert(amount instanceof String): "amount is null"; //NOI18N
610 if (amount.isEmpty() || "0".equals(amount)) { //NOI18N
611 // Choosen, but no amount
620 * Some "getter" for total price of position from request or session.
621 * Single price and amount is multiplyed.
623 * @param product Product instance
624 * @param request Request instance
625 * @param session Session instance
626 * @return Amount as string
629 public float getTotalPositionPriceFromRequestSession (final Product product, final HttpServletRequest request, final HttpSession session) {
631 this.getLogger().trace(MessageFormat.format("product={0},request={1},session={2} - CALLED!", product, request, session)); //NOI18N
633 // Is product and session set?
634 if (product == null) {
636 throw new NullPointerException("product is null"); //NOI18N
637 } else if (request == null) {
639 throw new NullPointerException("request is null"); //NOI18N
640 } else if (session == null) {
642 throw new NullPointerException("session is null"); //NOI18N
646 String choosen = this.handleChooseFromRequestSession(product, request, session);
647 this.getLogger().debug(MessageFormat.format("product={0},choosen={1}", product.getName(), choosen)); //NOI18N
650 assert(choosen instanceof String): "choosen is null"; //NOI18N
653 if (choosen.isEmpty()) {
655 this.getLogger().debug(MessageFormat.format("product={0},choosen={1} - returning zero ...", product.getName(), choosen)); //NOI18N
660 String amount = this.handleAmountFromRequestSession(product, request, session);
661 this.getLogger().debug(MessageFormat.format("product={0},amount={1}", product.getName(), amount)); //NOI18N
664 assert(amount instanceof String): "amount is null"; //NOI18N
667 if (amount.isEmpty() || "0".equals(amount)) { //NOI18N
669 this.getLogger().debug(MessageFormat.format("product={0},amount={1} - returning zero ...", product.getName(), amount)); //NOI18N
674 Integer value = null;
678 // Get amount as integer
679 value = Integer.valueOf(amount);
680 } catch (final NumberFormatException e) {
682 throw new IllegalArgumentException(e);
686 float price = (product.getPrice() * value);
689 this.getLogger().trace(MessageFormat.format("product={0},price={1} - EXIT!", product.getName(), price)); //NOI18N
691 // Then multiply it with price
696 * Checks whether the given product is choosen, request overules session.
698 * @param product Product instance
699 * @param request Request instance
700 * @param session Session instance
701 * @return Whether the product is choosen
704 public boolean isProductChoosen (final Product product, final HttpServletRequest request, final HttpSession session) {
706 this.getLogger().trace(MessageFormat.format("product={0},request={1},session={2} - CALLED!", product, request, session)); //NOI18N
708 // Is product and session set?
709 if (product == null) {
711 throw new NullPointerException("product is null"); //NOI18N
712 } else if (request == null) {
714 throw new NullPointerException("request is null"); //NOI18N
715 } else if (session == null) {
717 throw new NullPointerException("session is null"); //NOI18N
721 String choosen = this.handleChooseFromRequestSession(product, request, session);
722 this.getLogger().debug(MessageFormat.format("product={0},choosen={1}", product.getName(), choosen)); //NOI18N
725 assert(choosen instanceof String): "choosen is null"; //NOI18N
727 // Is it not choosen?
728 if (choosen.isEmpty()) {
734 String amount = this.handleAmountFromRequestSession(product, request, session);
737 assert(amount instanceof String): "amount is not set"; //NOI18N
740 this.getLogger().trace(MessageFormat.format("amount={0} - EXIT!", amount)); //NOI18N
742 // Must not be empty and not 0
743 return (!amount.isEmpty() && !"0".equals(amount)); //NOI18N
747 * Calculates total price of all choosen products
749 * @param request Request instance
750 * @param session Session instance
751 * @return Total price of all choosen products
754 public float calculateTotalPrice (final HttpServletRequest request, final HttpSession session) {
756 this.getLogger().trace(MessageFormat.format("request={0},session={1} - CALLED!", request, session)); //NOI18N
758 // Is product and session set?
759 if (request == null) {
761 throw new NullPointerException("request is null"); //NOI18N
762 } else if (session == null) {
764 throw new NullPointerException("session is null"); //NOI18N
768 float totalPrice = 0.00f;
770 // "Walk" over all products
771 for (final Product product : this.getProducts()) {
773 if (this.isProductChoosen(product, request, session)) {
774 // Then add product's total price
775 this.getLogger().debug(MessageFormat.format("Calling getTotalPositionPriceFromRequestSession({0},request,session) ...", product.getName())); //NOI18N
776 totalPrice += this.getTotalPositionPriceFromRequestSession(product, request, session);
778 this.getLogger().debug(MessageFormat.format("product={0},totalPrice={1}", product.getName(), totalPrice)); //NOI18N
782 this.getLogger().trace(MessageFormat.format(" totalPrice={0} - EXIT!", totalPrice)); //NOI18N
784 // Return total price
789 * Calculates total amount of all choosen products
791 * @param request Request instance
792 * @param session Session instance
793 * @return Total amount of all choosen products
796 public int calculateTotalAmount (final HttpServletRequest request, final HttpSession session) {
798 this.getLogger().trace(MessageFormat.format("request={0},session={1} - CALLED!", request, session)); //NOI18N
800 // Is product and session set?
801 if (request == null) {
803 throw new NullPointerException("request is null"); //NOI18N
804 } else if (session == null) {
806 throw new NullPointerException("session is null"); //NOI18N
812 // "Walk" over all products
813 for (final Product product : this.getProducts()) {
815 if (this.isProductChoosen(product, request, session)) {
816 // Then add ordered amount
817 this.getLogger().debug(MessageFormat.format("Counting {0} ...", product.getName())); //NOI18N
820 String amount = this.getAmountFromSession(product, session);
823 this.getLogger().debug(MessageFormat.format("amount={0}", amount)); //NOI18N
824 totalAmount += Integer.valueOf(amount);
826 this.getLogger().debug(MessageFormat.format("product={0},totalAmount={1}", product.getName(), totalAmount)); //NOI18N
830 this.getLogger().trace(MessageFormat.format("totalAmount={0} - EXIT!", totalAmount)); //NOI18N
832 // Return total price
837 * Some "getter" for HTML code 'checked="checked"' if the product is choosen
839 * @param product Product instance
840 * @param request Request instance
841 * @param session Session instance
842 * @return Whether the product is choosen
845 public String getCheckedHtmlFromProduct (final Product product, final HttpServletRequest request, final HttpSession session) {
847 this.getLogger().trace(MessageFormat.format("product={0},request={1},session={2} - CALLED!", product, request, session)); //NOI18N
849 // Is product and session set?
850 if (product == null) {
852 throw new NullPointerException("product is null"); //NOI18N
853 } else if (request == null) {
855 throw new NullPointerException("request is null"); //NOI18N
856 } else if (session == null) {
858 throw new NullPointerException("session is null"); //NOI18N
861 // First let's check if the product is choosen
862 if (this.isProductChoosen(product, request, session)) {
864 this.getLogger().trace("Returning checked=\"checked\" - EXIT!"); //NOI18N
867 return "checked=\"checked\""; //NOI18N
870 this.getLogger().trace("Returning empty string - EXIT!"); //NOI18N
878 * Some "getter" for HTML code 'disabled="disabled"' for e.g. submit buttons
880 * @param request Request instance
881 * @param session Session instance
882 * @return Whether the product is choosen
885 public String getDisabledHtmlFromSession (final HttpServletRequest request, final HttpSession session) {
887 this.getLogger().trace(MessageFormat.format("request={0},session={1} - CALLED!", request, session)); //NOI18N
889 // Is product and session set?
890 if (request == null) {
892 throw new NullPointerException("request is null"); //NOI18N
893 } else if (session == null) {
895 throw new NullPointerException("session is null"); //NOI18N
898 // Is something selected?
899 if (this.calculateTotalAmount(request, session) > 0) {
901 this.getLogger().trace("Returning empty string - EXIT!"); //NOI18N
903 // Something has been choosen
907 this.getLogger().trace("Returning disabled=\"disabled\" - EXIT!"); //NOI18N
909 // Nothing choosen yet
910 return "disabled=\"disabled\""; //NOI18N
915 * Marks given product as ordered in session
917 * @param product Product to mark as ordered
918 * @param session Session instance
921 public void markProductAsOrdered(final Product product, final HttpSession session) {
923 this.getLogger().trace(MessageFormat.format("product={0},session={1} - CALLED!", product, session)); //NOI18N
925 // Is product and session set?
926 if (product == null) {
928 throw new NullPointerException("product is null"); //NOI18N
929 } else if (session == null) {
931 throw new NullPointerException("session is null"); //NOI18N
934 // Mark it as ordered by setting flag
935 this.getLogger().debug(MessageFormat.format("Marking product={0} as ordered.", product.getName())); //NOI18N
936 this.setValueInSession(product, session, SESSION_ORDERED, "true"); //NOI18N
939 this.getLogger().trace("EXIT!"); //NOI18N
943 * Unmarks given product as ordered in session
945 * @param product Product to unmark as ordered
946 * @param session Session instance
949 public void unmarkProductAsOrdered(final Product product, final HttpSession session) {
951 this.getLogger().trace(MessageFormat.format("product={0},session={1} - CALLED!", product, session)); //NOI18N
953 // Is product and session set?
954 if (product == null) {
956 throw new NullPointerException("product is null"); //NOI18N
957 } else if (session == null) {
959 throw new NullPointerException("session is null"); //NOI18N
962 // Mark it as ordered by setting flag
963 this.getLogger().debug(MessageFormat.format("Unmarking product={0} as ordered.", product.getName())); //NOI18N
964 this.clearSessionAttribute(product, session, SESSION_ORDERED);
967 this.getLogger().trace("EXIT!"); //NOI18N
971 * Some getter for printable value from session or an empty string for null.
973 * @param session Session instance
974 * @param key Key to get
975 * @return Value from key, empty string for null
978 public Object getPrintableValeFromSession (final HttpSession session, final String key) {
980 this.getLogger().trace(MessageFormat.format("session={0},key={1} - CALLED", session, key));
982 // Are both parameter not null?
983 if (session == null) {
985 throw new NullPointerException("session is null");
986 } else if (key == null) {
988 throw new NullPointerException("key is null");
992 Object value = this.getValueFromSession(session, key);
995 this.getLogger().debug(MessageFormat.format("value={0}", value));
998 this.getLogger().trace(MessageFormat.format("Calling this.convertNullToEmpty({0}) ... - EXIT!", value));
1000 // Return actual value
1001 return this.convertNullToEmpty(value);
1005 * Gets an array of products from product iterator and unmarks them as ordered
1007 * @param session HttpSession instance
1008 * @return Unmarked products
1011 public Product[] getUnmarkedProducts (final HttpSession session) {
1013 Product[] array = this.getProducts();
1015 // Unmark are all as ordered
1016 for (final Product product : array) {
1017 this.unmarkProductAsOrdered(product, session);
1020 // Return finished array
1025 * Some "getter" for a an array of all products
1027 * @return Unmarked products
1030 public Product[] getProducts () {
1031 return (Product[]) this.products.values().toArray();
1035 * Marks given product as choosen in session
1037 * @param product Product to mark as ordered
1038 * @param session Session instance
1041 public void markProductAsChoosen(final Product product, final HttpSession session) {
1043 this.getLogger().trace(MessageFormat.format("product={0},session={1} - CALLED!", product, session)); //NOI18N
1045 // Is product and session set?
1046 if (product == null) {
1048 throw new NullPointerException("product is null"); //NOI18N
1049 } else if (session == null) {
1051 throw new NullPointerException("session is null"); //NOI18N
1054 // Mark it as ordered by setting flag
1055 this.getLogger().debug(MessageFormat.format("Marking product={0} as choosen.", product.getName())); //NOI18N
1056 this.setValueInSession(product, session, HTTP_PARAM_CHOOSE, "1");
1059 this.getLogger().trace("EXIT!"); //NOI18N