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