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