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