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