]> git.mxchange.org Git - pizzaservice-war.git/blob - src/java/org/mxchange/pizzaapplication/application/PizzaServiceApplication.java
5b8c210ff656d7e00fb45e77bca73a6e0a5a21f7
[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 javax.servlet.ServletContext;
27 import javax.servlet.ServletException;
28 import javax.servlet.http.HttpServletRequest;
29 import javax.servlet.http.HttpSession;
30 import org.mxchange.jcore.contact.Gender;
31 import org.mxchange.jcore.exceptions.BadTokenException;
32 import org.mxchange.jcore.exceptions.UnsupportedDatabaseBackendException;
33 import org.mxchange.pizzaapplication.BasePizzaServiceSystem;
34 import org.mxchange.pizzaapplication.category.Category;
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          * Frontend for products
53          */
54         private ProductFrontend productFrontend;
55
56         /**
57          * Some singleton getter for this instance. If the instance is not set in
58          * given application, it will be created.
59          *
60          * @param context Servlet context
61          * @return This instance
62          * @throws javax.servlet.ServletException If object is not set correctly
63          */
64         public static final PizzaApplication getInstance (final ServletContext context) throws ServletException {
65                 // Check application instance
66                 if (context == null) {
67                         // Not set
68                         throw new NullPointerException("application is null"); //NOI18N
69                 }
70
71                 // Init instance
72                 PizzaApplication instance = null;
73
74                 // Get instance from servlet application (aka. "application scope")
75                 Object object = context.getAttribute("app"); //NOI18N
76
77                 // Is it set?
78                 if (object instanceof PizzaApplication) {
79                         // Instance is set, so casting should work
80                         instance = (PizzaApplication) object;
81                 } else if (object instanceof Object) {
82                         // Not correct instance
83                         throw new ServletException("app is not set correctly"); //NOI18N
84                 } else {
85                         try {
86                                 // "service" is null, so initialize it
87                                 instance = new PizzaServiceApplication(context);
88                         } catch (final UnsupportedDatabaseBackendException | SQLException | IOException | BadTokenException ex) {
89                                 throw new ServletException(ex);
90                         }
91
92                         // And set it here
93                         context.setAttribute("app", instance); //NOI18N
94                 }
95
96                 // Trace message
97                 instance.getLogger().trace(MessageFormat.format("instance={0} - EXIT!", instance)); //NOI18N
98
99                 // Return it
100                 return instance;
101         }
102
103         /**
104          * For debugging purpose
105          *
106          * @param args Arguments
107          */
108         public static void main (String[] args) {
109                 // Get instance and start it
110                 new PizzaServiceApplication().start();
111         }
112
113         /**
114          * Constructor with servet configuration
115          *
116          * @param context Servlet context
117          */
118         private PizzaServiceApplication (final ServletContext context) throws UnsupportedDatabaseBackendException, SQLException, IOException, BadTokenException {
119                 // Temporary initialize default bundle
120                 // @TODO The JSF may have better internatialization support
121                 this.initBundle();
122
123                 // Initialize properties from config
124                 this.initProperties(context);
125
126                 // Init database frontends
127                 this.initDatabaseFrontends();
128         }
129
130         /**
131          * Default constructor
132          */
133         private PizzaServiceApplication () {
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.getId())); //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.getId(), 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.getId())); //NOI18N
214                                 totalPrice += this.getTotalPositionPriceFromRequestSession(product, request, session);
215                         }
216                         this.getLogger().debug(MessageFormat.format("product={0},totalPrice={1}", product.getId(), 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.getId())); //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.getId(), request, session)); //NOI18N
424                 String choosen = this.handleChooseFromRequestSession(product, request, session);
425                 this.getLogger().debug(MessageFormat.format("product={0},choosen={1}", product.getId(), 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.getId(), 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          * Checks if given Product instance is available and returns a printable
455          * (human-readable) string.
456          * 
457          * @param product Product instance to check
458          * @return Human-readable version of product availability
459          */
460         @Override
461         public String getPrintableProduktAvailability (final Product product) {
462                 throw new UnsupportedOperationException(MessageFormat.format("Not supported yet: product={0}", product));
463         }
464
465         /**
466          * Some getter for printable value from session or an empty string for null.
467          *
468          * @param session Session instance
469          * @param key Key to get
470          * @return Value from key, empty string for null
471          */
472         @Override
473         public Object getPrintableValeFromSession (final HttpSession session, final String key) {
474                 // Trace message
475                 this.getLogger().trace(MessageFormat.format("session={0},key={1} - CALLED", session, key)); //NOI18N
476
477                 // Are both parameter not null?
478                 if (session == null) {
479                         // Abort here
480                         throw new NullPointerException("session is null"); //NOI18N
481                 } else  if (key == null) {
482                         // Abort here
483                         throw new NullPointerException("key is null"); //NOI18N
484                 }
485
486                 // Now get it
487                 Object value = this.getValueFromSession(session, key);
488
489                 // Debug message
490                 this.getLogger().debug(MessageFormat.format("value={0}", value)); //NOI18N
491
492                 // Trace message
493                 this.getLogger().trace(MessageFormat.format("Calling this.convertNullToEmpty({0}) ... - EXIT!", value)); //NOI18N
494
495                 // Return actual value
496                 return this.convertNullToEmpty(value);
497         }
498
499         /**
500          * Some "getter" for a an array of all products
501          *
502          * @return All products
503          */
504         @Override
505         public Iterable<Product> getProducts () {
506                 throw new UnsupportedOperationException("Needs refacturing ...");
507         }
508
509         /**
510          * Some "getter" for a an array of all categories
511          *
512          * @return All categories
513          */
514         @Override
515         public Iterable<Category> getCategories () {
516                 throw new UnsupportedOperationException("Needs refacturing ...");
517         }
518
519         /**
520          * Some "getter" for total price of position from request or session.
521          * Single price and amount is multiplyed.
522          *
523          * @param product Product instance
524          * @param request Request instance
525          * @param session Session instance
526          * @return Amount as string
527          */
528         @Override
529         public float getTotalPositionPriceFromRequestSession (final Product product, final HttpServletRequest request, final HttpSession session) {
530                 // Trace message
531                 this.getLogger().trace(MessageFormat.format("product={0},request={1},session={2} - CALLED!", product, request, session)); //NOI18N
532
533                 // Is product and session set?
534                 if (product == null) {
535                         // Not set
536                         throw new NullPointerException("product is null"); //NOI18N
537                 } else if (request == null) {
538                         // Not set
539                         throw new NullPointerException("request is null"); //NOI18N
540                 } else if (session == null) {
541                         // Not set
542                         throw new NullPointerException("session is null"); //NOI18N
543                 }
544
545                 // Get choosen
546                 this.getLogger().debug(MessageFormat.format("Calling handleChooseFromRequestSession({0},{1},{2}) ...", product.getId(), request, session)); //NOI18N
547                 String choosen = this.handleChooseFromRequestSession(product, request, session);
548                 this.getLogger().debug(MessageFormat.format("product={0},choosen={1}", product.getId(), choosen)); //NOI18N
549
550                 // Must not be null
551                 assert(choosen instanceof String): "choosen is null"; //NOI18N
552
553                 // Is it set?
554                 if (choosen.isEmpty()) {
555                         // Is empty
556                         this.getLogger().debug(MessageFormat.format("product={0},choosen={1} - returning zero ...", product.getId(), choosen)); //NOI18N
557                         return 0.00f;
558                 }
559
560                 // Get amount
561                 String amount = this.handleAmountFromRequestSession(product, request, session);
562                 this.getLogger().debug(MessageFormat.format("product={0},amount={1}", product.getId(), amount)); //NOI18N
563
564                 // Must not be null
565                 assert(amount instanceof String): "amount is null"; //NOI18N
566
567                 // Is it empty?
568                 if (amount.isEmpty() || "0".equals(amount)) { //NOI18N
569                         // Is empty
570                         this.getLogger().debug(MessageFormat.format("product={0},amount={1} - returning zero ...", product.getId(), amount)); //NOI18N
571                         return 0.00f;
572                 }
573
574                 // Init variable
575                 Integer value = null;
576
577                 // Try it
578                 try {
579                         // Get amount as integer
580                         value = Integer.valueOf(amount);
581                 } catch (final NumberFormatException e) {
582                         // Bat input
583                         throw new IllegalArgumentException(e);
584                 }
585
586                 // Calculate price
587                 float price = (product.getPrice() * value);
588
589                 // Trace message
590                 this.getLogger().trace(MessageFormat.format("product={0},price={1} - EXIT!", product.getId(), price)); //NOI18N
591
592                 // Then multiply it with price
593                 return price;
594         }
595
596         /**
597          * Handler for amount from request or session
598          *
599          * @param product Product instance
600          * @param request Request instance
601          * @param session Session instance
602          * @return Amount as string
603          */
604         @Override
605         public String handleAmountFromRequestSession (final Product product, final HttpServletRequest request, final HttpSession session) {
606                 // Trace message
607                 this.getLogger().trace(MessageFormat.format("product={0},request={1},session={2} - CALLED!", product, request, session)); //NOI18N
608
609                 // Is product and session set?
610                 if (product == null) {
611                         // Not set
612                         throw new NullPointerException("product is null"); //NOI18N
613                 } else if (request == null) {
614                         // Not set
615                         throw new NullPointerException("request is null"); //NOI18N
616                 } else if (session == null) {
617                         // Not set
618                         throw new NullPointerException("session is null"); //NOI18N
619                 }
620
621                 // Init variabke
622                 Object object;
623
624                 // Check request method
625                 if (!"POST".equals(request.getMethod())) { //NOI18N
626                         // Not POST, so get from session
627                         return this.getAmountFromSession(product, session);
628                 } else if (this.handleChooseFromRequestSession(product, request, session).isEmpty()) {
629                         // Not choosen
630                         this.clearSessionAttribute(product, session, HTTP_PARAM_AMOUNT);
631                         this.getLogger().debug(MessageFormat.format("Unsetting for product={0} in session, returning zero ...", product.getId())); //NOI18N
632                         return "0"; //NOI18N
633                 }
634
635                 // Get attribute from request
636                 object = request.getParameter(String.format(HTTP_PARAM_MASK, HTTP_PARAM_AMOUNT, product.getId()));
637
638                 // Is it set?
639                 if (object instanceof String) {
640                         // Try to parse it to integer
641                         try {
642                                 Integer value = Integer.valueOf((String) object);
643                         } catch (final NumberFormatException ex) {
644                                 // Not valid input
645                                 this.getLogger().warn(ex);
646                                 return "0"; //NOI18N
647                         }
648
649                         // Then set it in session
650                         this.setValueInSession(product, session, HTTP_PARAM_AMOUNT, object);
651
652                         // And return it
653                         return (String) object;
654                 }
655
656                 // Trace message
657                 this.getLogger().trace("Calling getAmountFromSession() ..."); //NOI18N
658
659                 // Get attribute from session
660                 return this.getAmountFromSession(product, session);
661         }
662
663         /**
664          * Checks whether the given product is choosen, request overules session.
665          *
666          * @param product Product instance
667          * @param request Request instance
668          * @param session Session instance
669          * @return Whether the product is choosen
670          */
671         @Override
672         public boolean isProductChoosen (final Product product, final HttpServletRequest request, final HttpSession session) {
673                 // Trace message
674                 this.getLogger().trace(MessageFormat.format("product={0},request={1},session={2} - CALLED!", product, request, session)); //NOI18N
675
676                 // Is product and session set?
677                 if (product == null) {
678                         // Not set
679                         throw new NullPointerException("product is null"); //NOI18N
680                 } else if (request == null) {
681                         // Not set
682                         throw new NullPointerException("request is null"); //NOI18N
683                 } else if (session == null) {
684                         // Not set
685                         throw new NullPointerException("session is null"); //NOI18N
686                 }
687
688                 // Get choosen
689                 this.getLogger().debug(MessageFormat.format("Calling handleChooseFromRequestSession({0},{1},{2}) ...", product.getId(), request, session)); //NOI18N
690                 String choosen = this.handleChooseFromRequestSession(product, request, session);
691                 this.getLogger().debug(MessageFormat.format("product={0},choosen={1}", product.getId(), choosen)); //NOI18N
692
693                 // Must not be null
694                 assert(choosen instanceof String): "choosen is null"; //NOI18N
695
696                 // Is it not choosen?
697                 if (choosen.isEmpty()) {
698                         // Not choosen
699                         return false;
700                 }
701
702                 // Get amount
703                 String amount = this.handleAmountFromRequestSession(product, request, session);
704
705                 // Must not be null
706                 assert(amount instanceof String): "amount is not set"; //NOI18N
707
708                 // Trace message
709                 this.getLogger().trace(MessageFormat.format("amount={0} - EXIT!", amount)); //NOI18N
710
711                 // Must not be empty and not 0
712                 return (!amount.isEmpty() && !"0".equals(amount)); //NOI18N
713         }
714
715         /**
716          * Marks given product as choosen in session
717          *
718          * @param product Product to mark as ordered
719          * @param session Session instance
720          */
721         @Override
722         public void markProductAsChoosen (final Product product, final HttpSession session) {
723                 // Trace message
724                 this.getLogger().trace(MessageFormat.format("product={0},session={1} - CALLED!", product, session)); //NOI18N
725
726                 // Is product and session set?
727                 if (product == null) {
728                         // Not set
729                         throw new NullPointerException("product is null"); //NOI18N
730                 } else if (session == null) {
731                         // Not set
732                         throw new NullPointerException("session is null"); //NOI18N
733                 }
734
735                 // Mark it as ordered by setting flag
736                 this.getLogger().debug(MessageFormat.format("Marking product={0} as choosen.", product.getId())); //NOI18N
737                 this.setValueInSession(product, session, HTTP_PARAM_CHOOSE, "1"); //NOI18N
738
739                 // Trace message
740                 this.getLogger().trace("EXIT!"); //NOI18N
741         }
742
743         /**
744          * Marks given product as ordered in session
745          *
746          * @param product Product to mark as ordered
747          * @param session Session instance
748          */
749         @Override
750         public void markProductAsOrdered (final Product product, final HttpSession session) {
751                 // Trace message
752                 this.getLogger().trace(MessageFormat.format("product={0},session={1} - CALLED!", product, session)); //NOI18N
753
754                 // Is product and session set?
755                 if (product == null) {
756                         // Not set
757                         throw new NullPointerException("product is null"); //NOI18N
758                 } else if (session == null) {
759                         // Not set
760                         throw new NullPointerException("session is null"); //NOI18N
761                 }
762
763                 // Mark it as ordered by setting flag
764                 this.getLogger().debug(MessageFormat.format("Marking product={0} as ordered.", product.getId())); //NOI18N
765                 this.setValueInSession(product, session, SESSION_ORDERED, "true"); //NOI18N
766
767                 // Trace message
768                 this.getLogger().trace("EXIT!"); //NOI18N
769         }
770
771         /**
772          * Somewhat setter in session
773          *
774          * @param session Session instance
775          * @param key Session key to set
776          * @param value Value to set
777          */
778         @Override
779         public void setValueInSession (final HttpSession session, final String key, final Object value) {
780                 // Trace message
781                 this.getLogger().trace(MessageFormat.format("session={0},key={1},value={2} - CALLED!", session, key, value)); //NOI18N
782
783                 synchronized(session) {
784                         // Set it synced
785                         session.setAttribute(key, value);
786                 }
787
788                 // Trace message
789                 this.getLogger().trace("EXIT!"); //NOI18N
790         }
791
792         /**
793          * Unmarks given product as choosen in session
794          *
795          * @param product Product to unmark as choosen
796          * @param session Session instance
797          */
798         @Override
799         public void unmarkProductAsChoosen (final Product product, final HttpSession session) {
800                 // Trace message
801                 this.getLogger().trace(MessageFormat.format("product={0},session={1} - CALLED!", product, session)); //NOI18N
802
803                 // Is product and session set?
804                 if (product == null) {
805                         // Not set
806                         throw new NullPointerException("product is null"); //NOI18N
807                 } else if (session == null) {
808                         // Not set
809                         throw new NullPointerException("session is null"); //NOI18N
810                 }
811
812                 // Mark it as ordered by setting flag
813                 this.getLogger().debug(MessageFormat.format("Unmarking product={0} as choosen.", product.getId())); //NOI18N
814                 this.clearSessionAttribute(product, session, HTTP_PARAM_CHOOSE);
815
816                 // Trace message
817                 this.getLogger().trace("EXIT!"); //NOI18N
818         }
819
820         /**
821          * Unmarks given product as ordered in session
822          *
823          * @param product Product to unmark as ordered
824          * @param session Session instance
825          */
826         @Override
827         public void unmarkProductAsOrdered (final Product product, final HttpSession session) {
828                 // Trace message
829                 this.getLogger().trace(MessageFormat.format("product={0},session={1} - CALLED!", product, session)); //NOI18N
830
831                 // Is product and session set?
832                 if (product == null) {
833                         // Not set
834                         throw new NullPointerException("product is null"); //NOI18N
835                 } else if (session == null) {
836                         // Not set
837                         throw new NullPointerException("session is null"); //NOI18N
838                 }
839
840                 // Mark it as ordered by setting flag
841                 this.getLogger().debug(MessageFormat.format("Unmarking product={0} as ordered.", product.getId())); //NOI18N
842                 this.clearSessionAttribute(product, session, SESSION_ORDERED);
843
844                 // Trace message
845                 this.getLogger().trace("EXIT!"); //NOI18N
846         }
847
848         /**
849          * Clears given parameter for product in session
850          *
851          * @param product Product instance
852          * @param session Session instance
853          * @param parameter Parameter to clear
854          */
855         private void clearSessionAttribute (final Product product, final HttpSession session, final String parameter) {
856                 // Trace message
857                 this.getLogger().trace(MessageFormat.format("produce={0},parameter={1},session={2} - CALLED!", product, parameter, session)); //NOI18N
858
859                 // Clear in session
860                 this.getLogger().debug(MessageFormat.format("Clearing product={0},parameter={1} ...", product.getId(), parameter)); //NOI18N
861                 this.setValueInSession(product, session, parameter, null);
862
863                 // Trace message
864                 this.getLogger().trace("EXIT!"); //NOI18N
865         }
866
867         /**
868          * Some getter for value from session
869          *
870          * @param product Product instance
871          * @param session Session instance
872          * @param attribute Attribute to get value from
873          * @return Value from session
874          */
875         private Object getValueFromSession (final Product product, final HttpSession session, final String attribute) {
876                 // Trace message
877                 this.getLogger().trace(MessageFormat.format("product={0},session={1},attribute={2} - CALLED!", product, session, attribute)); //NOI18N
878
879                 // Init variable
880                 Object value = this.getValueFromSession(session, String.format(HTTP_PARAM_MASK, attribute, product.getId()));
881                 
882                 this.getLogger().debug(MessageFormat.format("product={0},attribute={1},value={2}", product.getId(), attribute, value)); //NOI18N
883
884                 // Trace message
885                 this.getLogger().trace(MessageFormat.format("value={0} - EXIT!", value)); //NOI18N
886
887                 // Return it
888                 return value;
889         }
890
891         /**
892          * Some getter for value from session
893          *
894          * @param session Session instance
895          * @param key Key to get value from
896          * @return Value from session
897          */
898         private Object getValueFromSession (final HttpSession session, final String key) {
899                 // Trace message
900                 this.getLogger().trace(MessageFormat.format("session={0},key={1} - CALLED!", session, key)); //NOI18N
901
902                 // Init value
903                 Object value = null;
904
905                 // Get it synchronized from session
906                 synchronized (session) {
907                         value = session.getAttribute(key);
908                 }
909
910                 // Trace message
911                 this.getLogger().trace(MessageFormat.format("value={0} - EXIT!", value)); //NOI18N
912
913                 // Return it
914                 return value;
915         }
916
917         /**
918          * Handler for choosen (checkbox) from request or session
919          *
920          * @param product Product instance
921          * @param request Request instance
922          * @param session Session instance
923          * @return Amount as string
924          */
925         private String handleChooseFromRequestSession(final Product product, final HttpServletRequest request, final HttpSession session) {
926                 // Trace message
927                 this.getLogger().trace(MessageFormat.format("product={0},request={1},session={2} - CALLED!", product, request, session)); //NOI18N
928
929                 // Is product and session set?
930                 if (product == null) {
931                         // Not set
932                         throw new NullPointerException("product is null"); //NOI18N
933                 } else if (request == null) {
934                         // Not set
935                         throw new NullPointerException("request is null"); //NOI18N
936                 } else if (session == null) {
937                         // Not set
938                         throw new NullPointerException("session is null"); //NOI18N
939                 }
940
941                 // Init variabke
942                 Object object;
943
944                 // Check request method
945                 if (!"POST".equals(request.getMethod())) { //NOI18N
946                         // Not POST, so get from session
947                         this.getLogger().trace(MessageFormat.format("Calling this.getChooseFromSession({0},{1}) ... - EXIT!", product.getId(), session));
948                         return this.getChooseFromSession(product, session);
949                 } else if (this.isProductOrdered(product, session)) {
950                         // Product is ordered
951                         this.getLogger().trace(MessageFormat.format("Calling this.getChooseFromSession({0},{1}) ... - EXIT!", product.getId(), session));
952                         return this.getChooseFromSession(product, session);
953                 } else if (!this.getChooseFromSession(product, session).isEmpty()) {
954                         // Found in session
955                         this.getLogger().trace(MessageFormat.format("Calling this.getChooseFromSession({0},{1}) ... - EXIT!", product.getId(), session));
956                         return this.getChooseFromSession(product, session);
957                 }
958
959                 // Get reqzest element
960                 object = request.getParameter(String.format(HTTP_PARAM_MASK, HTTP_PARAM_CHOOSE, product.getId()));
961                 this.getLogger().debug(MessageFormat.format("product={0},object={1}", product.getId(), object)); //NOI18N
962
963                 // Is it null?
964                 if (object == null) {
965                         // Unset session
966                         this.getLogger().debug(MessageFormat.format("Unsetting session for product={0} ...", product.getId())); //NOI18N
967                         this.clearSessionAttribute(product, session, HTTP_PARAM_CHOOSE);
968                         this.clearSessionAttribute(product, session, HTTP_PARAM_AMOUNT);
969
970                         // Return empty string
971                         return ""; //NOI18N
972                 }
973
974                 // Then set it in session
975                 this.setValueInSession(product, session, HTTP_PARAM_CHOOSE, object);
976
977                 // Cast to string and return it
978                 this.getLogger().debug(MessageFormat.format("product={0} - Returning {1} ...", product.getId(), object)); //NOI18N
979                 return (String) object;
980         }
981
982         /**
983          * Initializes database frontends.
984          */
985         private void initDatabaseFrontends () throws UnsupportedDatabaseBackendException, SQLException {
986                 this.productFrontend = new PizzaProductDatabaseFrontend();
987         }
988
989         /**
990          * Checks if the product ordered?
991          *
992          * @param product Product instance
993          * @param session HttpSession instance
994          * @return
995          */
996         private boolean isProductOrdered(final Product product, final HttpSession session) {
997                 // Trace message
998                 this.getLogger().trace(MessageFormat.format("product={0},session={1} - CALLED!", product, session)); //NOI18N
999
1000                 // Get session
1001                 Object isOrdered = this.getValueFromSession(product, session, SESSION_ORDERED);
1002                 this.getLogger().debug(MessageFormat.format("product={0},isOrdered={1}", product.getId(), isOrdered)); //NOI18N
1003
1004                 // Return result
1005                 return ("true".equals(isOrdered)); //NOI18N
1006         }
1007
1008         /**
1009          * Somewhat setter in session
1010          *
1011          * @param product Product instance
1012          * @param session Session instance
1013          * @param keyPart Key part to include in final key
1014          * @param value Value to set
1015          */
1016         private void setValueInSession (final Product product, final HttpSession session, final String keyPart, final Object value) {
1017                 // Trace message
1018                 this.getLogger().trace(MessageFormat.format("product={0},session={1},keyPart={2},value={3} - CALLED!", product, session, keyPart, value)); //NOI18N
1019
1020                 // Set it synced
1021                 this.getLogger().debug(MessageFormat.format("Setting value={0} for product={1},keyPart={2}", value, product.getId(), keyPart)); //NOI18N
1022                 this.setValueInSession(session, String.format(HTTP_PARAM_MASK, keyPart, product.getId()), value);
1023
1024                 // Trace message
1025                 this.getLogger().trace("EXIT!"); //NOI18N
1026         }
1027         
1028         /**
1029          * Application starter
1030          */
1031         private void start () {
1032                 // Init bundle
1033                 this.initBundle();
1034
1035                 try {
1036                         // Init properties
1037                         this.initProperties();
1038                 } catch (final IOException ex) {
1039                         // Abort here
1040                         this.abortProgramWithException(ex);
1041                 }
1042
1043                 // "Walk" over all products
1044                 for (final Product product : this.getProducts()) {
1045                         // Output data
1046                         this.getLogger().debug(MessageFormat.format("Product {0}, {1}: {2}", product.getId(), product.getTitle(), product.getPrice())); //NOI18N
1047                 }
1048
1049                 // Generate fake Customer instance
1050                 Customer customer = new PizzaServiceCustomer();
1051
1052                 /*
1053                  * Need a least a gender ... :( See, that is why I don't like default
1054                  * constructors, you can easily miss something important and bam! You
1055                  * get an NPE. The fix here is, to have construtors (or factories) which
1056                  * requires all required instances that needs to be set to get a
1057                  * consitent object back.
1058                  */
1059
1060                 // Gender is MALE now
1061                 customer.setGender(Gender.MALE);
1062
1063                 // Init iterator
1064                 Iterator<Map.Entry<Field, Object>> it = null;
1065
1066                 try {
1067                         // Get iterator on all its fields
1068                         it = customer.iterator();
1069                 } catch (final NoSuchMethodException | IllegalAccessException | InvocationTargetException ex) {
1070                         this.abortProgramWithException(ex);
1071                 }
1072
1073                 // Output it
1074                 while ((it instanceof Iterator) && (it.hasNext())) {
1075                         Map.Entry<Field, Object> entry = it.next();
1076                         this.getLogger().debug(MessageFormat.format("entry {0}={1}", entry.getKey(), entry.getValue())); //NOI18N
1077                 }
1078         }
1079 }