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