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