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