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