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