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