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