]> git.mxchange.org Git - pizzaservice-war.git/blob - src/java/org/mxchange/pizzaapplication/application/PizzaServiceApplication.java
df6cf19228334e3215c3d6b2a9967d6373381824
[pizzaservice-war.git] / src / java / org / mxchange / pizzaapplication / application / PizzaServiceApplication.java
1 /*
2  * Copyright (C) 2015 Roland Haeder
3  *
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.
8  *
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.
13  *
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/>.
16  */
17 package org.mxchange.pizzaapplication.application;
18
19 import java.lang.reflect.Field;
20 import java.text.MessageFormat;
21 import java.util.Iterator;
22 import java.util.Map;
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;
35
36 /**
37  *
38  * @author Roland Haeder
39  */
40 public class PizzaServiceApplication extends BasePizzaServiceSystem implements PizzaApplication {
41         /**
42          * Main title
43          */
44         public static final String MAIN_TITLE = "Pizza-Service";
45
46         /**
47          * Product list
48          */
49         private final SortedMap<String, Product> products;
50
51         /**
52          * Some singleton getter for this instance. If the instance is not set in
53          * given application, it will be created.
54          *
55          * @param application Servlet context
56          * @return This instance
57          * @throws javax.servlet.ServletException If object is not set correctly
58          */
59         public static final PizzaApplication getInstance (final ServletContext application) throws ServletException {
60                 // Check application instance
61                 if (application == null) {
62                         // Not set
63                         throw new NullPointerException("application is null"); //NOI18N
64                 }
65
66                         // Init instance
67                 PizzaApplication instance = null;
68
69                 // Get instance from servlet application (aka. "application scope")
70                 Object object = application.getAttribute("app"); //NOI18N
71
72                 // Is it set?
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
79                 } else {
80                         // "service" is null, so initialize it
81                         instance = new PizzaServiceApplication();
82
83                         // And set it here
84                         application.setAttribute("app", instance); //NOI18N
85                 }
86
87                 // Trace message
88                 instance.getLogger().trace(MessageFormat.format("instance={0} - EXIT!", instance)); //NOI18N
89
90                 // Return it
91                 return instance;
92         }
93
94         /**
95          * Private constructor
96          */
97         private PizzaServiceApplication () {
98                 // Init products instance
99                 this.products = new TreeMap<>();
100
101                 // Init bundle
102                 this.initBundle();
103
104                 // Fill products list
105                 this.fillProductsList();
106         }
107
108         @Override
109         public void doBootstrap () {
110                 throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
111         }
112
113         @Override
114         public void doMainLoop () {
115                 throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
116         }
117
118         @Override
119         public void doShutdown () {
120                 throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
121         }
122
123         /**
124          * Getter for product (list) iterator
125          *
126          * @return An interator on all listed products
127          */
128         @Override
129         public Iterator<Map.Entry<String, Product>> getProductsIterator () {
130                 assert(this.products instanceof SortedMap) : "this.products is not initialized"; //NOI18N
131                 return this.products.entrySet().iterator();
132         }
133
134         /**
135          * Adds given product to list or throws an exception if name is already found
136          *
137          * @param name Internal name of product
138          * @param title Product's title
139          * @param price Price
140          */
141         private void addProduct (final String name, final String title, final float price) {
142                 // Trace message
143                 this.getLogger().trace(MessageFormat.format("name={0},title={1},price={2} - CALLED!", name, title, price)); //NOI18N
144
145                 // Is the name already used?
146                 if (this.isProductNameUsed(name)) {
147                         // Something went wrong
148                         throw new IllegalArgumentException(MessageFormat.format("product {0} is already used.", name)); //NOI18N
149                 }
150
151                 // Instance product
152                 Product product = new PizzaProduct(name, title, price);
153
154                 // Debug message
155                 this.getLogger().debug(MessageFormat.format("Adding product={0} ...", product)); //NOI18N
156
157                 // Add it
158                 this.products.put(product.getName(), product);
159
160                 // Trace message
161                 this.getLogger().trace("EXIT!"); //NOI18N
162         }
163
164         /**
165          * Clears given parameter for product in session
166          * 
167          * @param product Product instance
168          * @param session Session instance
169          * @param parameter Parameter to clear
170          */
171         private void clearSessionAttribute (final Product product, final HttpSession session, final String parameter) {
172                 // Trace message
173                 this.getLogger().trace(MessageFormat.format("produce={0},parameter={1},session={2} - CALLED!", product, parameter, session)); //NOI18N
174
175                 // Clear in session
176                 this.getLogger().debug(MessageFormat.format("Clearing product={0},parameter={1} ...", product.getName(), parameter)); //NOI18N
177                 this.setValueInSession(product, session, parameter, null);
178
179                 // Trace message
180                 this.getLogger().trace("EXIT!"); //NOI18N
181         }
182
183         /**
184          * Fills products list
185          * @todo Very hard-coded stuff ...
186          */
187         private void fillProductsList () {
188                 // Trace message
189                 this.getLogger().trace("CALLED!"); //NOI18N
190
191                 // Add products
192                 this.addProduct("italia", "Pizza Italia", 5.50f); //NOI18N
193                 this.addProduct("diablo", "Pizza Diablo", 7.80f); //NOI18N
194                 this.addProduct("bolognese", "Spagetti Bolognese", 11.95f); //NOI18N
195
196                 // Trace message
197                 this.getLogger().trace("EXIT!"); //NOI18N
198         }
199
200         /**
201          * Some getter for value from session
202          *
203          * @param product Product instance
204          * @param session Session instance
205          * @param attribute Attribute to get value from
206          * @return Value from session
207          */
208         private Object getValueFromSession (final Product product, final HttpSession session, final String attribute) {
209                 // Trace message
210                 this.getLogger().trace(MessageFormat.format("product={0},session={1},attribute={2} - CALLED!", product, session, attribute)); //NOI18N
211
212                 // Init variable
213                 Object value = this.getValueFromSession(session, String.format(HTTP_PARAM_MASK, product.getName(), attribute));
214
215                 this.getLogger().debug(MessageFormat.format("product={0},attribute={1},value={2}", product.getName(), attribute, value)); //NOI18N
216
217                 // Trace message
218                 this.getLogger().trace(MessageFormat.format("value={0} - EXIT!", value)); //NOI18N
219
220                 // Return it
221                 return value;
222         }
223
224         /**
225          * Some getter for value from session
226          *
227          * @param session Session instance
228          * @param key Key to get value from
229          * @return Value from session
230          */
231         private Object getValueFromSession (final HttpSession session, final String key) {
232                 // Trace message
233                 this.getLogger().trace(MessageFormat.format("session={043},key={1} - CALLED!", session, key)); //NOI18N
234
235                 // Init value
236                 Object value = null;
237
238                 // Get it synchronized from session
239                 synchronized (session) {
240                         value = session.getAttribute(key);
241                 }
242
243                 // Trace message
244                 this.getLogger().trace(MessageFormat.format("value={0} - EXIT!", value)); //NOI18N
245
246                 // Return it
247                 return value;
248         }
249
250         /**
251          * Checks whether given product is already used
252          *
253          * @param name Name of product
254          * @return Whether the given product's name is already used
255          */
256         private boolean isProductNameUsed (final String name) {
257                 // Trace message
258                 this.getLogger().trace(MessageFormat.format("name={0} - CALLED!", name)); //NOI18N
259
260                 // Is it found?
261                 return this.products.containsKey(name);
262         }
263
264         /**
265          * For debugging purpose
266          *
267          * @param args Arguments
268          */
269         public static void main (String[] args) {
270                 // Get instance and start it
271                 new PizzaServiceApplication().start();
272         }
273
274         /**
275          * Checks if the product ordered?
276          *
277          * @param product 
278          * @param session
279          * @return 
280          */
281         private boolean isProductOrdered (final Product product, final HttpSession session) {
282                 // Trace message
283                 this.getLogger().trace(MessageFormat.format("product={0},session={1} - CALLED!", product, session)); //NOI18N
284
285                 // Get session
286                 Object isOrdered = this.getValueFromSession(product, session, SESSION_ORDERED);
287                 this.getLogger().debug(MessageFormat.format("product={0},isOrdered={1}", product.getName(), isOrdered)); //NOI18N
288
289                 // Return result
290                 return ("true".equals(isOrdered)); //NOI18N
291         }
292
293         /**
294          * Somewhat setter in session
295          *
296          * @param product Product instance
297          * @param session Session instance
298          * @param keyPart Key part to include in final key
299          * @param value Value to set
300          */
301         private void setValueInSession (final Product product, final HttpSession session, final String keyPart, final Object value) {
302                 // Trace message
303                 this.getLogger().trace(MessageFormat.format("product={0},session={1},keyPart={2},value={3} - CALLED!", product, session, keyPart, value)); //NOI18N
304
305                 synchronized(session) {
306                         // Set it synced
307                         this.getLogger().debug(MessageFormat.format("Setting value={0} for product={1},keyPart={2}", value, product.getName(), keyPart)); //NOI18N
308                         this.setValueInSession(session, String.format(HTTP_PARAM_MASK, product.getName(), keyPart), value);
309                 }
310
311                 // Trace message
312                 this.getLogger().trace("EXIT!"); //NOI18N
313         }
314
315         /**
316          * Somewhat setter in session
317          *
318          * @param session Session instance
319          * @param key Session key to set
320          * @param value Value to set
321          */
322         @Override
323         public void setValueInSession (final HttpSession session, final String key, final Object value) {
324                 // Trace message
325                 this.getLogger().trace(MessageFormat.format("session={0},key={1},value={2} - CALLED!", session, key, value)); //NOI18N
326
327                 synchronized(session) {
328                         // Set it synced
329                         session.setAttribute(key, value);
330                 }
331
332                 // Trace message
333                 this.getLogger().trace("EXIT!"); //NOI18N
334         }
335
336         /**
337          * Application starter
338          */
339         private void start () {
340                 // Get iterator
341                 Iterator<Map.Entry<String, Product>> iterator = this.getProductsIterator();
342
343                 // Run over it
344                 while (iterator.hasNext()) {
345                         // Get product instance
346                         Map.Entry<String, Product> entry = iterator.next();
347
348                         // Get value
349                         Product product = entry.getValue();
350
351                         // Output data
352                         this.getLogger().debug(MessageFormat.format("Product {0}, {1}: {2}", product.getName(), product.getTitle(), product.getPrice())); //NOI18N
353                 }
354
355                 // Generate fake Customer instance
356                 Customer customer = new PizzaServiceCustomer();
357
358                 /*
359                  * Need a least a gender ... :( See, that is why I don't like default
360                  * constructors, you can easily miss something important and bam! You
361                  * get an NPE. The fix here is, to have construtors (or factories) which
362                  * requires all required instances that needs to be set to get a
363                  * consitent object back.
364                  */
365
366                 // Gender is MALE now
367                 customer.setGender(Gender.MALE);
368
369                 // Get iterator on all its fields
370                 Iterator<Map.Entry<Field, Object>> it = customer.iterator();
371
372                 // Output it
373                 while (it.hasNext()) {
374                         Map.Entry<Field, Object> entry = it.next();
375                         this.getLogger().debug(MessageFormat.format("entry {0}={1}", entry.getKey(), entry.getValue()));
376                 }
377         }
378
379         /**
380          * Some "getter" for amount from session
381          *
382          * @param product Product instance
383          * @param session Session instance
384          * @return Amount as string
385          */
386         @Override
387         public String getAmountFromSession (final Product product, final HttpSession session) {
388                 // Trace message
389                 this.getLogger().trace(MessageFormat.format("product={0},session={1} - CALLED!", product, session)); //NOI18N
390
391                 // Is product and session set?
392                 if (product == null) {
393                         // Not set
394                         throw new NullPointerException("product is null"); //NOI18N
395                 } else if (session == null) {
396                         // Not set
397                         throw new NullPointerException("session is null"); //NOI18N
398                 }
399
400                 // Get attribute
401                 Object object = this.getValueFromSession(product, session, HTTP_PARAM_AMOUNT);
402
403                 // Is the object null?
404                 if (object == null) {
405                         // Trace message
406                         this.getLogger().trace("Returning 0 - EXIT!"); //NOI18N
407
408                         // Not found
409                         return "0"; //NOI18N
410                 }
411
412                 // Trace message
413                 this.getLogger().trace(MessageFormat.format("object={0} - EXIT!", object)); //NOI18N
414
415                 // Cast to string and return it
416                 return (String) object;
417         }
418
419         /**
420          * Some "getter" for choose from session
421          * 
422          * @param product Product instance
423          * @param session Session instance
424          * @return Choose as string
425          */
426         @Override
427         public String getChooseFromSession (final Product product, final HttpSession session) {
428                 // Trace message
429                 this.getLogger().trace(MessageFormat.format("product={0},session={1} - CALLED!", product, session)); //NOI18N
430
431                         // Is product and session set?
432                 if (product == null) {
433                         // Not set
434                         throw new NullPointerException("product is null"); //NOI18N
435                 } else if (session == null) {
436                         // Not set
437                         throw new NullPointerException("session is null"); //NOI18N
438                 }
439
440                 // Get attribute
441                 Object object = this.getValueFromSession(product, session, HTTP_PARAM_CHOOSE);
442
443                 // Is the object null?
444                 if (object == null) {
445                         // Not found
446                         this.getLogger().debug(MessageFormat.format("Returning empty string for product={0} ...", product.getName())); //NOI18N
447                         return ""; //NOI18N
448                 }
449
450                 // Trace message
451                 this.getLogger().trace(MessageFormat.format("object={0} - CALLED!", object)); //NOI18N
452
453                 // Cast to string and return it
454                 return (String) object;
455         }
456
457         /**
458          * Handler for amount from request or session
459          * 
460          * @param product Product instance
461          * @param request Request instance
462          * @param session Session instance
463          * @return Amount as string
464          */
465         @Override
466         public String handleAmountFromRequestSession (final Product product, final HttpServletRequest request, final HttpSession session) {
467                 // Trace message
468                 this.getLogger().trace(MessageFormat.format("product={0},request={1},session={2} - CALLED!", product, request, session)); //NOI18N
469
470                 // Is product and session set?
471                 if (product == null) {
472                         // Not set
473                         throw new NullPointerException("product is null"); //NOI18N
474                 } else if (request == null) {
475                         // Not set
476                         throw new NullPointerException("request is null"); //NOI18N
477                 } else if (session == null) {
478                         // Not set
479                         throw new NullPointerException("session is null"); //NOI18N
480                 }
481
482                 // Init variabke
483                 Object object;
484
485                 // Check request method
486                 if (!"POST".equals(request.getMethod())) { //NOI18N
487                         // Not POST, so get from session
488                         return this.getAmountFromSession(product, session);
489                 } else if (this.handleChooseFromRequestSession(product, request, session).isEmpty()) {
490                         // Not choosen
491                         this.clearSessionAttribute(product, session, HTTP_PARAM_AMOUNT);
492                         this.getLogger().debug(MessageFormat.format("Unsetting for product={0} in session, returning zero ...", product.getName())); //NOI18N
493                         return "0"; //NOI18N
494                 }
495
496                 // Get attribute from request
497                 object = request.getParameter(String.format(HTTP_PARAM_MASK, HTTP_PARAM_AMOUNT, product.getName()));
498
499                 // Is it set?
500                 if (object instanceof String) {
501                         // Try to parse it to integer
502                         try {
503                                 Integer value = Integer.valueOf((String) object);
504                         } catch (final NumberFormatException ex) {
505                                 // Not valid input
506                                 this.getLogger().warn(ex);
507                                 return "0"; //NOI18N
508                         }
509
510                         // Then set it in session
511                         this.setValueInSession(product, session, HTTP_PARAM_AMOUNT, object);
512
513                         // And return it
514                         return (String) object;
515                 }
516
517                 // Trace message
518                 this.getLogger().trace("Calling getAmountFromSession() ..."); //NOI18N
519
520                 // Get attribute from session
521                 return this.getAmountFromSession(product, session);
522         }
523
524         /**
525          * Handler for choosen (checkbox) from request or session
526          *
527          * @param product Product instance
528          * @param request Request instance
529          * @param session Session instance
530          * @return Amount as string
531          */
532         @Override
533         public String handleChooseFromRequestSession (final Product product, final HttpServletRequest request, final HttpSession session) {
534                 // Trace message
535                 this.getLogger().trace(MessageFormat.format("product={0},request={1},session={2} - CALLED!", product, request, session)); //NOI18N
536
537                 // Is product and session set?
538                 if (product == null) {
539                         // Not set
540                         throw new NullPointerException("product is null"); //NOI18N
541                 } else if (request == null) {
542                         // Not set
543                         throw new NullPointerException("request is null"); //NOI18N
544                 } else if (session == null) {
545                         // Not set
546                         throw new NullPointerException("session is null"); //NOI18N
547                 }
548
549                 // Init variabke
550                 Object object;
551
552                 // Check request method
553                 if (!"POST".equals(request.getMethod())) { //NOI18N
554                         // Not POST, so get from session
555                         return this.getChooseFromSession(product, session);
556                 } else if (this.isProductOrdered(product, session)) {
557                         // Product is ordered
558                         return this.getChooseFromSession(product, session);
559                 }
560
561                 // Get reqzest element
562                 object = request.getParameter(String.format(HTTP_PARAM_MASK, HTTP_PARAM_CHOOSE, product.getName()));
563                 this.getLogger().debug(MessageFormat.format("product={0},object={1}", product.getName(), object)); //NOI18N
564
565                 // Is it null?
566                 if (object == null) {
567                         // Unset session
568                         this.getLogger().debug(MessageFormat.format("Unsetting session for product={0} ...", product.getName())); //NOI18N
569                         this.clearSessionAttribute(product, session, HTTP_PARAM_CHOOSE);
570                         this.clearSessionAttribute(product, session, HTTP_PARAM_AMOUNT);
571
572                         // Return empty string
573                         return ""; //NOI18N
574                 }
575
576                 // Then set it in session
577                 this.setValueInSession(product, session, HTTP_PARAM_CHOOSE, object);
578
579                 // Cast to string and return it
580                 this.getLogger().debug(MessageFormat.format("product={0} - Returning {1} ...", product.getName(), object)); //NOI18N
581                 return (String) object;
582         }
583
584         /**
585          * Some "getter" for choosen (checkbox) from session
586          *
587          * @param product Product instance
588          * @param request Request instance
589          * @param session Session instance
590          * @return Amount as string
591          */
592         @Override
593         public String getPrintableChoosenFromRequestSession (final Product product, final HttpServletRequest request, final HttpSession session) {
594                 // Trace message
595                 this.getLogger().trace(MessageFormat.format("product={0},request={1},session={2} - CALLED!", product, request, session)); //NOI18N
596
597                 // Is product and session set?
598                 if (product == null) {
599                         // Not set
600                         throw new NullPointerException("product is null"); //NOI18N
601                 } else if (request == null) {
602                         // Not set
603                         throw new NullPointerException("request is null"); //NOI18N
604                 } else if (session == null) {
605                         // Not set
606                         throw new NullPointerException("session is null"); //NOI18N
607                 }
608
609                 // Get element
610                 String choosen = this.handleChooseFromRequestSession(product, request, session);
611                 this.getLogger().debug(MessageFormat.format("product={0},choosen={1}", product.getName(), choosen)); //NOI18N
612
613                 // Must not be null
614                 assert(choosen instanceof String): "choosen is null"; //NOI18N
615
616                 // Is it empty?
617                 if (choosen.isEmpty()) {
618                         // Not choosen
619                         return "Nein";
620                 }
621
622                 // Get amount
623                 String amount = this.handleAmountFromRequestSession(product, request, session);
624                 this.getLogger().debug(MessageFormat.format("product={0},amount={1}", product.getName(), amount)); //NOI18N
625
626                 // Must not be null
627                 assert(amount instanceof String): "amount is null"; //NOI18N
628
629                 // Is it empty?
630                 if (amount.isEmpty() || "0".equals(amount)) { //NOI18N
631                         // Choosen, but no amount
632                         return "Nein";
633                 } else {
634                         // Is choosen
635                         return "Ja";
636                 }
637         }
638
639         /**
640          * Some "getter" for total price of position from request or session.
641          * Single price and amount is multiplyed.
642          *
643          * @param product Product instance
644          * @param request Request instance
645          * @param session Session instance
646          * @return Amount as string
647          */
648         @Override
649         public float getTotalPositionPriceFromRequestSession (final Product product, final HttpServletRequest request, final HttpSession session) {
650                 // Trace message
651                 this.getLogger().trace(MessageFormat.format("product={0},request={1},session={2} - CALLED!", product, request, session)); //NOI18N
652
653                 // Is product and session set?
654                 if (product == null) {
655                         // Not set
656                         throw new NullPointerException("product is null"); //NOI18N
657                 } else if (request == null) {
658                         // Not set
659                         throw new NullPointerException("request is null"); //NOI18N
660                 } else if (session == null) {
661                         // Not set
662                         throw new NullPointerException("session is null"); //NOI18N
663                 }
664
665                 // Get choosen
666                 String choosen = this.handleChooseFromRequestSession(product, request, session);
667                 this.getLogger().debug(MessageFormat.format("product={0},choosen={1}", product.getName(), choosen)); //NOI18N
668
669                 // Must not be null
670                 assert(choosen instanceof String): "choosen is null"; //NOI18N
671
672                 // Is it set?
673                 if (choosen.isEmpty()) {
674                         // Is empty
675                         this.getLogger().debug(MessageFormat.format("product={0},choosen={1} - returning zero ...", product.getName(), choosen)); //NOI18N
676                         return 0.00f;
677                 }
678
679                 // Get amount
680                 String amount = this.handleAmountFromRequestSession(product, request, session);
681                 this.getLogger().debug(MessageFormat.format("product={0},amount={1}", product.getName(), amount)); //NOI18N
682
683                 // Must not be null
684                 assert(amount instanceof String): "amount is null"; //NOI18N
685
686                 // Is it empty?
687                 if (amount.isEmpty() || "0".equals(amount)) { //NOI18N
688                         // Is empty
689                         this.getLogger().debug(MessageFormat.format("product={0},amount={1} - returning zero ...", product.getName(), amount)); //NOI18N
690                         return 0.00f;
691                 }
692
693                 // Init variable
694                 Integer value = null;
695
696                 // Try it
697                 try {
698                         // Get amount as integer
699                         value = Integer.valueOf(amount);
700                 } catch (final NumberFormatException e) {
701                         // Bat input
702                         throw new IllegalArgumentException(e);
703                 }
704
705                 // Calculate price
706                 float price = (product.getPrice() * value);
707
708                 // Trace message
709                 this.getLogger().trace(MessageFormat.format("product={0},price={1} - EXIT!", product.getName(), price)); //NOI18N
710
711                 // Then multiply it with price
712                 return price;
713         }
714
715         /**
716          * Checks whether the given product is choosen, request overules session.
717          *
718          * @param product Product instance
719          * @param request Request instance
720          * @param session Session instance
721          * @return Whether the product is choosen
722          */
723         @Override
724         public boolean isProductChoosen (final Product product, final HttpServletRequest request, final HttpSession session) {
725                 // Trace message
726                 this.getLogger().trace(MessageFormat.format("product={0},request={1},session={2} - CALLED!", product, request, session)); //NOI18N
727
728                 // Is product and session set?
729                 if (product == null) {
730                         // Not set
731                         throw new NullPointerException("product is null"); //NOI18N
732                 } else if (request == null) {
733                         // Not set
734                         throw new NullPointerException("request is null"); //NOI18N
735                 } else if (session == null) {
736                         // Not set
737                         throw new NullPointerException("session is null"); //NOI18N
738                 }
739
740                 // Get choosen
741                 String choosen = this.handleChooseFromRequestSession(product, request, session);
742                 this.getLogger().debug(MessageFormat.format("product={0},choosen={1}", product.getName(), choosen)); //NOI18N
743
744                 // Must not be null
745                 assert(choosen instanceof String): "choosen is null"; //NOI18N
746
747                 // Is it not choosen?
748                 if (choosen.isEmpty()) {
749                         // Not choosen
750                         return false;
751                 }
752
753                 // Get amount
754                 String amount = this.handleAmountFromRequestSession(product, request, session);
755
756                 // Must not be null
757                 assert(amount instanceof String): "amount is not set"; //NOI18N
758
759                 // Trace message
760                 this.getLogger().trace(MessageFormat.format("amount={0} - EXIT!", amount)); //NOI18N
761
762                 // Must not be empty and not 0
763                 return (!amount.isEmpty() && !"0".equals(amount)); //NOI18N
764         }
765
766         /**
767          * Calculates total price of all choosen products
768          *
769          * @param request Request instance
770          * @param session Session instance
771          * @return Total price of all choosen products
772          */
773         @Override
774         public float calculateTotalPrice (final HttpServletRequest request, final HttpSession session) {
775                 // Trace message
776                 this.getLogger().trace(MessageFormat.format("request={0},session={1} - CALLED!", request, session)); //NOI18N
777
778                 // Is product and session set?
779                 if (request == null) {
780                         // Not set
781                         throw new NullPointerException("request is null"); //NOI18N
782                 } else if (session == null) {
783                         // Not set
784                         throw new NullPointerException("session is null"); //NOI18N
785                 }
786
787                 // Init total price
788                 float totalPrice = 0.00f;
789
790                 // Get iterator
791                 Iterator<Map.Entry<String, Product>> iterator = this.getProductsIterator();
792
793                 // Walk through all products
794                 while (iterator.hasNext()) {
795                         // Get entry
796                         Map.Entry<String, Product> entry = iterator.next();
797
798                         // Get product instance
799                         Product product = entry.getValue();
800
801                         // Is this choosen?
802                         if (product.isChoosen()) {
803                                 // Then add product's total price
804                                 this.getLogger().debug(MessageFormat.format("Calling getTotalPositionPriceFromRequestSession({0},request,session) ...", product.getName())); //NOI18N
805                                 totalPrice += this.getTotalPositionPriceFromRequestSession(product, request, session);
806                         }
807                         this.getLogger().debug(MessageFormat.format("product={0},totalPrice={1}", product.getName(), totalPrice)); //NOI18N
808                 }
809
810                 // Trace message
811                 this.getLogger().trace(MessageFormat.format(" totalPrice={0} - EXIT!", totalPrice)); //NOI18N
812
813                 // Return total price
814                 return totalPrice;
815         }
816
817         /**
818          * Calculates total amount of all choosen products
819          *
820          * @param request Request instance
821          * @param session Session instance
822          * @return Total amount of all choosen products
823          */
824         @Override
825         public int calculateTotalAmount (final HttpServletRequest request, final HttpSession session) {
826                 // Trace message
827                 this.getLogger().trace(MessageFormat.format("request={0},session={1} - CALLED!", request, session)); //NOI18N
828
829                 // Is product and session set?
830                 if (request == null) {
831                         // Not set
832                         throw new NullPointerException("request is null"); //NOI18N
833                 } else if (session == null) {
834                         // Not set
835                         throw new NullPointerException("session is null"); //NOI18N
836                 }
837
838                 // Init total price
839                 int totalAmount = 0;
840
841                 // Get iterator
842                 Iterator<Map.Entry<String, Product>> iterator = this.getProductsIterator();
843
844                 // Walk through all products
845                 while (iterator.hasNext()) {
846                         // Get entry
847                         Map.Entry<String, Product> entry = iterator.next();
848
849                         // Get product instance
850                         Product product = entry.getValue();
851
852                         // Is this choosen?
853                         if (product.isChoosen()) {
854                                 // Then add ordered amount
855                                 this.getLogger().debug(MessageFormat.format("Counting {0} ...", product.getName())); //NOI18N
856
857                                 // Getting amount
858                                 String amount = this.getAmountFromSession(product, session);
859
860                                 // Add it up
861                                 this.getLogger().debug(MessageFormat.format("amount={0}", amount)); //NOI18N
862                                 totalAmount += Integer.valueOf(amount);
863                         }
864                         this.getLogger().debug(MessageFormat.format("product={0},totalAmount={1}", product.getName(), totalAmount)); //NOI18N
865                 }
866
867                 // Trace message
868                 this.getLogger().trace(MessageFormat.format("totalAmount={0} - EXIT!", totalAmount)); //NOI18N
869
870                 // Return total price
871                 return totalAmount;
872         }
873
874         /**
875          * Some "getter" for HTML code 'checked="checked"' if the product is choosen
876          *
877          * @param product Product instance
878          * @param request Request instance
879          * @param session Session instance
880          * @return Whether the product is choosen
881          */
882         @Override
883         public String getCheckedHtmlFromProduct (final Product product, final HttpServletRequest request, final HttpSession session) {
884                 // Trace message
885                 this.getLogger().trace(MessageFormat.format("product={0},request={1},session={2} - CALLED!", product, request, session)); //NOI18N
886
887                 // Is product and session set?
888                 if (product == null) {
889                         // Not set
890                         throw new NullPointerException("product is null"); //NOI18N
891                 } else if (request == null) {
892                         // Not set
893                         throw new NullPointerException("request is null"); //NOI18N
894                 } else if (session == null) {
895                         // Not set
896                         throw new NullPointerException("session is null"); //NOI18N
897                 }
898
899                 // First let's check if the product is choosen
900                 if (this.isProductChoosen(product, request, session)) {
901                         // Trace message
902                         this.getLogger().trace("Returning checked=\"checked\" - EXIT!"); //NOI18N
903
904                         // Is choosen
905                         return "checked=\"checked\""; //NOI18N
906                 } else {
907                         // Trace message
908                         this.getLogger().trace("Returning empty string - EXIT!"); //NOI18N
909
910                         // Not choosen
911                         return ""; //NOI18N
912                 }
913         }
914
915         /**
916          * Some "getter" for HTML code 'disabled="disabled"' for e.g. submit buttons
917          *
918          * @param request Request instance
919          * @param session Session instance
920          * @return Whether the product is choosen
921          */
922         @Override
923         public String getDisabledHtmlFromSession (final HttpServletRequest request, final HttpSession session) {
924                 // Trace message
925                 this.getLogger().trace(MessageFormat.format("request={0},session={1} - CALLED!", request, session)); //NOI18N
926
927                 // Is product and session set?
928                 if (request == null) {
929                         // Not set
930                         throw new NullPointerException("request is null"); //NOI18N
931                 } else if (session == null) {
932                         // Not set
933                         throw new NullPointerException("session is null"); //NOI18N
934                 }
935
936                 // Is something selected?
937                 if (this.calculateTotalAmount(request, session) > 0) {
938                         // Trace message
939                         this.getLogger().trace("Returning empty string - EXIT!"); //NOI18N
940
941                         // Something has been choosen
942                         return ""; //NOI18N
943                 } else {
944                         // Trace message
945                         this.getLogger().trace("Returning disabled=\"disabled\" - EXIT!"); //NOI18N
946
947                         // Nothing choosen yet
948                         return "disabled=\"disabled\""; //NOI18N
949                 }
950         }
951
952         /**
953          * Marks given product as ordered in session
954          *
955          * @param product Product to mark as ordered
956          * @param session Session instance
957          */
958         @Override
959         public void markProductAsOrdered(final Product product, final HttpSession session) {
960                 // Trace message
961                 this.getLogger().trace(MessageFormat.format("product={0},session={1} - CALLED!", product, session)); //NOI18N
962
963                 // Is product and session set?
964                 if (product == null) {
965                         // Not set
966                         throw new NullPointerException("product is null"); //NOI18N
967                 } else if (session == null) {
968                         // Not set
969                         throw new NullPointerException("session is null"); //NOI18N
970                 }
971
972                 // Mark it as ordered by setting flag
973                 this.getLogger().debug(MessageFormat.format("Marking product={0} as ordered.", product.getName())); //NOI18N
974                 this.setValueInSession(product, session, SESSION_ORDERED, "true"); //NOI18N
975
976                 // Trace message
977                 this.getLogger().trace("EXIT!"); //NOI18N
978         }
979
980         /**
981          * Unmarks given product as ordered in session
982          *
983          * @param product Product to unmark as ordered
984          * @param session Session instance
985          */
986         @Override
987         public void unmarkProductAsOrdered(final Product product, final HttpSession session) {
988                 // Trace message
989                 this.getLogger().trace(MessageFormat.format("product={0},session={1} - CALLED!", product, session)); //NOI18N
990
991                 // Is product and session set?
992                 if (product == null) {
993                         // Not set
994                         throw new NullPointerException("product is null"); //NOI18N
995                 } else if (session == null) {
996                         // Not set
997                         throw new NullPointerException("session is null"); //NOI18N
998                 }
999
1000                 // Mark it as ordered by setting flag
1001                 this.getLogger().debug(MessageFormat.format("Unmarking product={0} as ordered.", product.getName())); //NOI18N
1002                 this.clearSessionAttribute(product, session, SESSION_ORDERED);
1003
1004                 // Trace message
1005                 this.getLogger().trace("EXIT!"); //NOI18N
1006         }
1007
1008         /**
1009          * Some getter for printable value from session or an empty string for null.
1010          *
1011          * @param session Session instance
1012          * @param key Key to get
1013          * @return Value from key, empty string for null
1014          */
1015         @Override
1016         public Object getPrintableValeFromSession (final HttpSession session, final String key) {
1017                 // Trace message
1018                 this.getLogger().trace(MessageFormat.format("session={0},key={1} - CALLED", session, key));
1019
1020                 // Are both parameter not null?
1021                 if (session == null) {
1022                         // Abort here
1023                         throw new NullPointerException("session is null");
1024                 } else  if (key == null) {
1025                         // Abort here
1026                         throw new NullPointerException("key is null");
1027                 }
1028
1029                 // Now get it
1030                 Object value = this.getValueFromSession(session, key);
1031
1032                 // Debug message
1033                 this.getLogger().debug(MessageFormat.format("value={0}", value));
1034
1035                 // Trace message
1036                 this.getLogger().trace(MessageFormat.format("Calling this.convertNullToEmpty({0}) ... - EXIT!", value));
1037
1038                 // Return actual value
1039                 return this.convertNullToEmpty(value);
1040         }
1041 }