]> git.mxchange.org Git - pizzaservice-war.git/blob - src/java/org/mxchange/pizzaapplication/application/PizzaServiceApplication.java
Got rid of product iterator stuff and used a smaller for() loop
[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          * Adds given product to list or throws an exception if name is already found
125          *
126          * @param name Internal name of product
127          * @param title Product's title
128          * @param price Price
129          */
130         private void addProduct (final String name, final String title, final float price) {
131                 // Trace message
132                 this.getLogger().trace(MessageFormat.format("name={0},title={1},price={2} - CALLED!", name, title, price)); //NOI18N
133
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
138                 }
139
140                 // Instance product
141                 Product product = new PizzaProduct(name, title, price);
142
143                 // Debug message
144                 this.getLogger().debug(MessageFormat.format("Adding product={0} ...", product)); //NOI18N
145
146                 // Add it
147                 this.products.put(product.getName(), product);
148
149                 // Trace message
150                 this.getLogger().trace("EXIT!"); //NOI18N
151         }
152
153         /**
154          * Clears given parameter for product in session
155          * 
156          * @param product Product instance
157          * @param session Session instance
158          * @param parameter Parameter to clear
159          */
160         private void clearSessionAttribute (final Product product, final HttpSession session, final String parameter) {
161                 // Trace message
162                 this.getLogger().trace(MessageFormat.format("produce={0},parameter={1},session={2} - CALLED!", product, parameter, session)); //NOI18N
163
164                 // Clear in session
165                 this.getLogger().debug(MessageFormat.format("Clearing product={0},parameter={1} ...", product.getName(), parameter)); //NOI18N
166                 this.setValueInSession(product, session, parameter, null);
167
168                 // Trace message
169                 this.getLogger().trace("EXIT!"); //NOI18N
170         }
171
172         /**
173          * Fills products list
174          * @todo Very hard-coded stuff ...
175          */
176         private void fillProductsList () {
177                 // Trace message
178                 this.getLogger().trace("CALLED!"); //NOI18N
179
180                 // Add products
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
184
185                 // Trace message
186                 this.getLogger().trace("EXIT!"); //NOI18N
187         }
188
189         /**
190          * Some getter for value from session
191          *
192          * @param product Product instance
193          * @param session Session instance
194          * @param attribute Attribute to get value from
195          * @return Value from session
196          */
197         private Object getValueFromSession (final Product product, final HttpSession session, final String attribute) {
198                 // Trace message
199                 this.getLogger().trace(MessageFormat.format("product={0},session={1},attribute={2} - CALLED!", product, session, attribute)); //NOI18N
200
201                 // Init variable
202                 Object value = this.getValueFromSession(session, String.format(HTTP_PARAM_MASK, product.getName(), attribute));
203
204                 this.getLogger().debug(MessageFormat.format("product={0},attribute={1},value={2}", product.getName(), attribute, value)); //NOI18N
205
206                 // Trace message
207                 this.getLogger().trace(MessageFormat.format("value={0} - EXIT!", value)); //NOI18N
208
209                 // Return it
210                 return value;
211         }
212
213         /**
214          * Some getter for value from session
215          *
216          * @param session Session instance
217          * @param key Key to get value from
218          * @return Value from session
219          */
220         private Object getValueFromSession (final HttpSession session, final String key) {
221                 // Trace message
222                 this.getLogger().trace(MessageFormat.format("session={043},key={1} - CALLED!", session, key)); //NOI18N
223
224                 // Init value
225                 Object value = null;
226
227                 // Get it synchronized from session
228                 synchronized (session) {
229                         value = session.getAttribute(key);
230                 }
231
232                 // Trace message
233                 this.getLogger().trace(MessageFormat.format("value={0} - EXIT!", value)); //NOI18N
234
235                 // Return it
236                 return value;
237         }
238
239         /**
240          * Checks whether given product is already used
241          *
242          * @param name Name of product
243          * @return Whether the given product's name is already used
244          */
245         private boolean isProductNameUsed (final String name) {
246                 // Trace message
247                 this.getLogger().trace(MessageFormat.format("name={0} - CALLED!", name)); //NOI18N
248
249                 // Is it found?
250                 return this.products.containsKey(name);
251         }
252
253         /**
254          * For debugging purpose
255          *
256          * @param args Arguments
257          */
258         public static void main (String[] args) {
259                 // Get instance and start it
260                 new PizzaServiceApplication().start();
261         }
262
263         /**
264          * Checks if the product ordered?
265          *
266          * @param product 
267          * @param session
268          * @return 
269          */
270         private boolean isProductOrdered (final Product product, final HttpSession session) {
271                 // Trace message
272                 this.getLogger().trace(MessageFormat.format("product={0},session={1} - CALLED!", product, session)); //NOI18N
273
274                 // Get session
275                 Object isOrdered = this.getValueFromSession(product, session, SESSION_ORDERED);
276                 this.getLogger().debug(MessageFormat.format("product={0},isOrdered={1}", product.getName(), isOrdered)); //NOI18N
277
278                 // Return result
279                 return ("true".equals(isOrdered)); //NOI18N
280         }
281
282         /**
283          * Somewhat setter in session
284          *
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
289          */
290         private void setValueInSession (final Product product, final HttpSession session, final String keyPart, final Object value) {
291                 // Trace message
292                 this.getLogger().trace(MessageFormat.format("product={0},session={1},keyPart={2},value={3} - CALLED!", product, session, keyPart, value)); //NOI18N
293
294                 synchronized(session) {
295                         // Set it synced
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);
298                 }
299
300                 // Trace message
301                 this.getLogger().trace("EXIT!"); //NOI18N
302         }
303
304         /**
305          * Somewhat setter in session
306          *
307          * @param session Session instance
308          * @param key Session key to set
309          * @param value Value to set
310          */
311         @Override
312         public void setValueInSession (final HttpSession session, final String key, final Object value) {
313                 // Trace message
314                 this.getLogger().trace(MessageFormat.format("session={0},key={1},value={2} - CALLED!", session, key, value)); //NOI18N
315
316                 synchronized(session) {
317                         // Set it synced
318                         session.setAttribute(key, value);
319                 }
320
321                 // Trace message
322                 this.getLogger().trace("EXIT!"); //NOI18N
323         }
324
325         /**
326          * Application starter
327          */
328         private void start () {
329                 // "Walk" over all products
330                 for (final Product product : this.getProducts()) {
331                         // Output data
332                         this.getLogger().debug(MessageFormat.format("Product {0}, {1}: {2}", product.getName(), product.getTitle(), product.getPrice())); //NOI18N
333                 }
334
335                 // Generate fake Customer instance
336                 Customer customer = new PizzaServiceCustomer();
337
338                 /*
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.
344                  */
345
346                 // Gender is MALE now
347                 customer.setGender(Gender.MALE);
348
349                 // Get iterator on all its fields
350                 Iterator<Map.Entry<Field, Object>> it = customer.iterator();
351
352                 // Output it
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()));
356                 }
357         }
358
359         /**
360          * Some "getter" for amount from session
361          *
362          * @param product Product instance
363          * @param session Session instance
364          * @return Amount as string
365          */
366         @Override
367         public String getAmountFromSession (final Product product, final HttpSession session) {
368                 // Trace message
369                 this.getLogger().trace(MessageFormat.format("product={0},session={1} - CALLED!", product, session)); //NOI18N
370
371                 // Is product and session set?
372                 if (product == null) {
373                         // Not set
374                         throw new NullPointerException("product is null"); //NOI18N
375                 } else if (session == null) {
376                         // Not set
377                         throw new NullPointerException("session is null"); //NOI18N
378                 }
379
380                 // Get attribute
381                 Object object = this.getValueFromSession(product, session, HTTP_PARAM_AMOUNT);
382
383                 // Is the object null?
384                 if (object == null) {
385                         // Trace message
386                         this.getLogger().trace("Returning 0 - EXIT!"); //NOI18N
387
388                         // Not found
389                         return "0"; //NOI18N
390                 }
391
392                 // Trace message
393                 this.getLogger().trace(MessageFormat.format("object={0} - EXIT!", object)); //NOI18N
394
395                 // Cast to string and return it
396                 return (String) object;
397         }
398
399         /**
400          * Some "getter" for choose from session
401          * 
402          * @param product Product instance
403          * @param session Session instance
404          * @return Choose as string
405          */
406         @Override
407         public String getChooseFromSession (final Product product, final HttpSession session) {
408                 // Trace message
409                 this.getLogger().trace(MessageFormat.format("product={0},session={1} - CALLED!", product, session)); //NOI18N
410
411                         // Is product and session set?
412                 if (product == null) {
413                         // Not set
414                         throw new NullPointerException("product is null"); //NOI18N
415                 } else if (session == null) {
416                         // Not set
417                         throw new NullPointerException("session is null"); //NOI18N
418                 }
419
420                 // Get attribute
421                 Object object = this.getValueFromSession(product, session, HTTP_PARAM_CHOOSE);
422
423                 // Is the object null?
424                 if (object == null) {
425                         // Not found
426                         this.getLogger().debug(MessageFormat.format("Returning empty string for product={0} ...", product.getName())); //NOI18N
427                         return ""; //NOI18N
428                 }
429
430                 // Trace message
431                 this.getLogger().trace(MessageFormat.format("object={0} - CALLED!", object)); //NOI18N
432
433                 // Cast to string and return it
434                 return (String) object;
435         }
436
437         /**
438          * Handler for amount from request or session
439          * 
440          * @param product Product instance
441          * @param request Request instance
442          * @param session Session instance
443          * @return Amount as string
444          */
445         @Override
446         public String handleAmountFromRequestSession (final Product product, final HttpServletRequest request, final HttpSession session) {
447                 // Trace message
448                 this.getLogger().trace(MessageFormat.format("product={0},request={1},session={2} - CALLED!", product, request, session)); //NOI18N
449
450                 // Is product and session set?
451                 if (product == null) {
452                         // Not set
453                         throw new NullPointerException("product is null"); //NOI18N
454                 } else if (request == null) {
455                         // Not set
456                         throw new NullPointerException("request is null"); //NOI18N
457                 } else if (session == null) {
458                         // Not set
459                         throw new NullPointerException("session is null"); //NOI18N
460                 }
461
462                 // Init variabke
463                 Object object;
464
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()) {
470                         // Not choosen
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
473                         return "0"; //NOI18N
474                 }
475
476                 // Get attribute from request
477                 object = request.getParameter(String.format(HTTP_PARAM_MASK, HTTP_PARAM_AMOUNT, product.getName()));
478
479                 // Is it set?
480                 if (object instanceof String) {
481                         // Try to parse it to integer
482                         try {
483                                 Integer value = Integer.valueOf((String) object);
484                         } catch (final NumberFormatException ex) {
485                                 // Not valid input
486                                 this.getLogger().warn(ex);
487                                 return "0"; //NOI18N
488                         }
489
490                         // Then set it in session
491                         this.setValueInSession(product, session, HTTP_PARAM_AMOUNT, object);
492
493                         // And return it
494                         return (String) object;
495                 }
496
497                 // Trace message
498                 this.getLogger().trace("Calling getAmountFromSession() ..."); //NOI18N
499
500                 // Get attribute from session
501                 return this.getAmountFromSession(product, session);
502         }
503
504         /**
505          * Handler for choosen (checkbox) from request or session
506          *
507          * @param product Product instance
508          * @param request Request instance
509          * @param session Session instance
510          * @return Amount as string
511          */
512         @Override
513         public String handleChooseFromRequestSession (final Product product, final HttpServletRequest request, final HttpSession session) {
514                 // Trace message
515                 this.getLogger().trace(MessageFormat.format("product={0},request={1},session={2} - CALLED!", product, request, session)); //NOI18N
516
517                 // Is product and session set?
518                 if (product == null) {
519                         // Not set
520                         throw new NullPointerException("product is null"); //NOI18N
521                 } else if (request == null) {
522                         // Not set
523                         throw new NullPointerException("request is null"); //NOI18N
524                 } else if (session == null) {
525                         // Not set
526                         throw new NullPointerException("session is null"); //NOI18N
527                 }
528
529                 // Init variabke
530                 Object object;
531
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);
539                 }
540
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
544
545                 // Is it null?
546                 if (object == null) {
547                         // Unset session
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);
551
552                         // Return empty string
553                         return ""; //NOI18N
554                 }
555
556                 // Then set it in session
557                 this.setValueInSession(product, session, HTTP_PARAM_CHOOSE, object);
558
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;
562         }
563
564         /**
565          * Some "getter" for choosen (checkbox) from session
566          *
567          * @param product Product instance
568          * @param request Request instance
569          * @param session Session instance
570          * @return Amount as string
571          */
572         @Override
573         public String getPrintableChoosenFromRequestSession (final Product product, final HttpServletRequest request, final HttpSession session) {
574                 // Trace message
575                 this.getLogger().trace(MessageFormat.format("product={0},request={1},session={2} - CALLED!", product, request, session)); //NOI18N
576
577                 // Is product and session set?
578                 if (product == null) {
579                         // Not set
580                         throw new NullPointerException("product is null"); //NOI18N
581                 } else if (request == null) {
582                         // Not set
583                         throw new NullPointerException("request is null"); //NOI18N
584                 } else if (session == null) {
585                         // Not set
586                         throw new NullPointerException("session is null"); //NOI18N
587                 }
588
589                 // Get element
590                 String choosen = this.handleChooseFromRequestSession(product, request, session);
591                 this.getLogger().debug(MessageFormat.format("product={0},choosen={1}", product.getName(), choosen)); //NOI18N
592
593                 // Must not be null
594                 assert(choosen instanceof String): "choosen is null"; //NOI18N
595
596                 // Is it empty?
597                 if (choosen.isEmpty()) {
598                         // Not choosen
599                         return "Nein";
600                 }
601
602                 // Get amount
603                 String amount = this.handleAmountFromRequestSession(product, request, session);
604                 this.getLogger().debug(MessageFormat.format("product={0},amount={1}", product.getName(), amount)); //NOI18N
605
606                 // Must not be null
607                 assert(amount instanceof String): "amount is null"; //NOI18N
608
609                 // Is it empty?
610                 if (amount.isEmpty() || "0".equals(amount)) { //NOI18N
611                         // Choosen, but no amount
612                         return "Nein";
613                 } else {
614                         // Is choosen
615                         return "Ja";
616                 }
617         }
618
619         /**
620          * Some "getter" for total price of position from request or session.
621          * Single price and amount is multiplyed.
622          *
623          * @param product Product instance
624          * @param request Request instance
625          * @param session Session instance
626          * @return Amount as string
627          */
628         @Override
629         public float getTotalPositionPriceFromRequestSession (final Product product, final HttpServletRequest request, final HttpSession session) {
630                 // Trace message
631                 this.getLogger().trace(MessageFormat.format("product={0},request={1},session={2} - CALLED!", product, request, session)); //NOI18N
632
633                 // Is product and session set?
634                 if (product == null) {
635                         // Not set
636                         throw new NullPointerException("product is null"); //NOI18N
637                 } else if (request == null) {
638                         // Not set
639                         throw new NullPointerException("request is null"); //NOI18N
640                 } else if (session == null) {
641                         // Not set
642                         throw new NullPointerException("session is null"); //NOI18N
643                 }
644
645                 // Get choosen
646                 String choosen = this.handleChooseFromRequestSession(product, request, session);
647                 this.getLogger().debug(MessageFormat.format("product={0},choosen={1}", product.getName(), choosen)); //NOI18N
648
649                 // Must not be null
650                 assert(choosen instanceof String): "choosen is null"; //NOI18N
651
652                 // Is it set?
653                 if (choosen.isEmpty()) {
654                         // Is empty
655                         this.getLogger().debug(MessageFormat.format("product={0},choosen={1} - returning zero ...", product.getName(), choosen)); //NOI18N
656                         return 0.00f;
657                 }
658
659                 // Get amount
660                 String amount = this.handleAmountFromRequestSession(product, request, session);
661                 this.getLogger().debug(MessageFormat.format("product={0},amount={1}", product.getName(), amount)); //NOI18N
662
663                 // Must not be null
664                 assert(amount instanceof String): "amount is null"; //NOI18N
665
666                 // Is it empty?
667                 if (amount.isEmpty() || "0".equals(amount)) { //NOI18N
668                         // Is empty
669                         this.getLogger().debug(MessageFormat.format("product={0},amount={1} - returning zero ...", product.getName(), amount)); //NOI18N
670                         return 0.00f;
671                 }
672
673                 // Init variable
674                 Integer value = null;
675
676                 // Try it
677                 try {
678                         // Get amount as integer
679                         value = Integer.valueOf(amount);
680                 } catch (final NumberFormatException e) {
681                         // Bat input
682                         throw new IllegalArgumentException(e);
683                 }
684
685                 // Calculate price
686                 float price = (product.getPrice() * value);
687
688                 // Trace message
689                 this.getLogger().trace(MessageFormat.format("product={0},price={1} - EXIT!", product.getName(), price)); //NOI18N
690
691                 // Then multiply it with price
692                 return price;
693         }
694
695         /**
696          * Checks whether the given product is choosen, request overules session.
697          *
698          * @param product Product instance
699          * @param request Request instance
700          * @param session Session instance
701          * @return Whether the product is choosen
702          */
703         @Override
704         public boolean isProductChoosen (final Product product, final HttpServletRequest request, final HttpSession session) {
705                 // Trace message
706                 this.getLogger().trace(MessageFormat.format("product={0},request={1},session={2} - CALLED!", product, request, session)); //NOI18N
707
708                 // Is product and session set?
709                 if (product == null) {
710                         // Not set
711                         throw new NullPointerException("product is null"); //NOI18N
712                 } else if (request == null) {
713                         // Not set
714                         throw new NullPointerException("request is null"); //NOI18N
715                 } else if (session == null) {
716                         // Not set
717                         throw new NullPointerException("session is null"); //NOI18N
718                 }
719
720                 // Get choosen
721                 String choosen = this.handleChooseFromRequestSession(product, request, session);
722                 this.getLogger().debug(MessageFormat.format("product={0},choosen={1}", product.getName(), choosen)); //NOI18N
723
724                 // Must not be null
725                 assert(choosen instanceof String): "choosen is null"; //NOI18N
726
727                 // Is it not choosen?
728                 if (choosen.isEmpty()) {
729                         // Not choosen
730                         return false;
731                 }
732
733                 // Get amount
734                 String amount = this.handleAmountFromRequestSession(product, request, session);
735
736                 // Must not be null
737                 assert(amount instanceof String): "amount is not set"; //NOI18N
738
739                 // Trace message
740                 this.getLogger().trace(MessageFormat.format("amount={0} - EXIT!", amount)); //NOI18N
741
742                 // Must not be empty and not 0
743                 return (!amount.isEmpty() && !"0".equals(amount)); //NOI18N
744         }
745
746         /**
747          * Calculates total price of all choosen products
748          *
749          * @param request Request instance
750          * @param session Session instance
751          * @return Total price of all choosen products
752          */
753         @Override
754         public float calculateTotalPrice (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                 float totalPrice = 0.00f;
769
770                 // "Walk" over all products
771                 for (final Product product : this.getProducts()) {
772                         // Is this choosen?
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);
777                         }
778                         this.getLogger().debug(MessageFormat.format("product={0},totalPrice={1}", product.getName(), totalPrice)); //NOI18N
779                 }
780
781                 // Trace message
782                 this.getLogger().trace(MessageFormat.format(" totalPrice={0} - EXIT!", totalPrice)); //NOI18N
783
784                 // Return total price
785                 return totalPrice;
786         }
787
788         /**
789          * Calculates total amount of all choosen products
790          *
791          * @param request Request instance
792          * @param session Session instance
793          * @return Total amount of all choosen products
794          */
795         @Override
796         public int calculateTotalAmount (final HttpServletRequest request, final HttpSession session) {
797                 // Trace message
798                 this.getLogger().trace(MessageFormat.format("request={0},session={1} - CALLED!", request, session)); //NOI18N
799
800                 // Is product and session set?
801                 if (request == null) {
802                         // Not set
803                         throw new NullPointerException("request is null"); //NOI18N
804                 } else if (session == null) {
805                         // Not set
806                         throw new NullPointerException("session is null"); //NOI18N
807                 }
808
809                 // Init total price
810                 int totalAmount = 0;
811
812                 // "Walk" over all products
813                 for (final Product product : this.getProducts()) {
814                         // Is this choosen?
815                         if (this.isProductChoosen(product, request, session)) {
816                                 // Then add ordered amount
817                                 this.getLogger().debug(MessageFormat.format("Counting {0} ...", product.getName())); //NOI18N
818
819                                 // Getting amount
820                                 String amount = this.getAmountFromSession(product, session);
821
822                                 // Add it up
823                                 this.getLogger().debug(MessageFormat.format("amount={0}", amount)); //NOI18N
824                                 totalAmount += Integer.valueOf(amount);
825                         }
826                         this.getLogger().debug(MessageFormat.format("product={0},totalAmount={1}", product.getName(), totalAmount)); //NOI18N
827                 }
828
829                 // Trace message
830                 this.getLogger().trace(MessageFormat.format("totalAmount={0} - EXIT!", totalAmount)); //NOI18N
831
832                 // Return total price
833                 return totalAmount;
834         }
835
836         /**
837          * Some "getter" for HTML code 'checked="checked"' if the product is choosen
838          *
839          * @param product Product instance
840          * @param request Request instance
841          * @param session Session instance
842          * @return Whether the product is choosen
843          */
844         @Override
845         public String getCheckedHtmlFromProduct (final Product product, final HttpServletRequest request, final HttpSession session) {
846                 // Trace message
847                 this.getLogger().trace(MessageFormat.format("product={0},request={1},session={2} - CALLED!", product, request, session)); //NOI18N
848
849                 // Is product and session set?
850                 if (product == null) {
851                         // Not set
852                         throw new NullPointerException("product is null"); //NOI18N
853                 } else if (request == null) {
854                         // Not set
855                         throw new NullPointerException("request is null"); //NOI18N
856                 } else if (session == null) {
857                         // Not set
858                         throw new NullPointerException("session is null"); //NOI18N
859                 }
860
861                 // First let's check if the product is choosen
862                 if (this.isProductChoosen(product, request, session)) {
863                         // Trace message
864                         this.getLogger().trace("Returning checked=\"checked\" - EXIT!"); //NOI18N
865
866                         // Is choosen
867                         return "checked=\"checked\""; //NOI18N
868                 } else {
869                         // Trace message
870                         this.getLogger().trace("Returning empty string - EXIT!"); //NOI18N
871
872                         // Not choosen
873                         return ""; //NOI18N
874                 }
875         }
876
877         /**
878          * Some "getter" for HTML code 'disabled="disabled"' for e.g. submit buttons
879          *
880          * @param request Request instance
881          * @param session Session instance
882          * @return Whether the product is choosen
883          */
884         @Override
885         public String getDisabledHtmlFromSession (final HttpServletRequest request, final HttpSession session) {
886                 // Trace message
887                 this.getLogger().trace(MessageFormat.format("request={0},session={1} - CALLED!", request, session)); //NOI18N
888
889                 // Is product and session set?
890                 if (request == null) {
891                         // Not set
892                         throw new NullPointerException("request is null"); //NOI18N
893                 } else if (session == null) {
894                         // Not set
895                         throw new NullPointerException("session is null"); //NOI18N
896                 }
897
898                 // Is something selected?
899                 if (this.calculateTotalAmount(request, session) > 0) {
900                         // Trace message
901                         this.getLogger().trace("Returning empty string - EXIT!"); //NOI18N
902
903                         // Something has been choosen
904                         return ""; //NOI18N
905                 } else {
906                         // Trace message
907                         this.getLogger().trace("Returning disabled=\"disabled\" - EXIT!"); //NOI18N
908
909                         // Nothing choosen yet
910                         return "disabled=\"disabled\""; //NOI18N
911                 }
912         }
913
914         /**
915          * Marks given product as ordered in session
916          *
917          * @param product Product to mark as ordered
918          * @param session Session instance
919          */
920         @Override
921         public void markProductAsOrdered(final Product product, final HttpSession session) {
922                 // Trace message
923                 this.getLogger().trace(MessageFormat.format("product={0},session={1} - CALLED!", product, session)); //NOI18N
924
925                 // Is product and session set?
926                 if (product == null) {
927                         // Not set
928                         throw new NullPointerException("product is null"); //NOI18N
929                 } else if (session == null) {
930                         // Not set
931                         throw new NullPointerException("session is null"); //NOI18N
932                 }
933
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
937
938                 // Trace message
939                 this.getLogger().trace("EXIT!"); //NOI18N
940         }
941
942         /**
943          * Unmarks given product as ordered in session
944          *
945          * @param product Product to unmark as ordered
946          * @param session Session instance
947          */
948         @Override
949         public void unmarkProductAsOrdered(final Product product, final HttpSession session) {
950                 // Trace message
951                 this.getLogger().trace(MessageFormat.format("product={0},session={1} - CALLED!", product, session)); //NOI18N
952
953                 // Is product and session set?
954                 if (product == null) {
955                         // Not set
956                         throw new NullPointerException("product is null"); //NOI18N
957                 } else if (session == null) {
958                         // Not set
959                         throw new NullPointerException("session is null"); //NOI18N
960                 }
961
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);
965
966                 // Trace message
967                 this.getLogger().trace("EXIT!"); //NOI18N
968         }
969
970         /**
971          * Some getter for printable value from session or an empty string for null.
972          *
973          * @param session Session instance
974          * @param key Key to get
975          * @return Value from key, empty string for null
976          */
977         @Override
978         public Object getPrintableValeFromSession (final HttpSession session, final String key) {
979                 // Trace message
980                 this.getLogger().trace(MessageFormat.format("session={0},key={1} - CALLED", session, key));
981
982                 // Are both parameter not null?
983                 if (session == null) {
984                         // Abort here
985                         throw new NullPointerException("session is null");
986                 } else  if (key == null) {
987                         // Abort here
988                         throw new NullPointerException("key is null");
989                 }
990
991                 // Now get it
992                 Object value = this.getValueFromSession(session, key);
993
994                 // Debug message
995                 this.getLogger().debug(MessageFormat.format("value={0}", value));
996
997                 // Trace message
998                 this.getLogger().trace(MessageFormat.format("Calling this.convertNullToEmpty({0}) ... - EXIT!", value));
999
1000                 // Return actual value
1001                 return this.convertNullToEmpty(value);
1002         }
1003
1004         /**
1005          * Gets an array of products from product iterator and unmarks them as ordered
1006          * 
1007          * @param session HttpSession instance
1008          * @return Unmarked products
1009          */
1010         @Override
1011         public Product[] getUnmarkedProducts (final HttpSession session) {
1012                 // Init array
1013                 Product[] array = this.getProducts();
1014
1015                 // Unmark are all as ordered
1016                 for (final Product product : array) {
1017                         this.unmarkProductAsOrdered(product, session);
1018                 }
1019
1020                 // Return finished array
1021                 return array;
1022         }
1023
1024         /**
1025          * Some "getter" for a an array of all products
1026          * 
1027          * @return Unmarked products
1028          */
1029         @Override
1030         public Product[] getProducts () {
1031                 return (Product[]) this.products.values().toArray();
1032         }
1033 }