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