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