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