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