]> git.mxchange.org Git - pizzaservice-war.git/blob - src/java/org/mxchange/pizzaapplication/application/PizzaServiceApplication.java
Refacturing towards JSF:
[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.Deque;
25 import java.util.Iterator;
26 import javax.servlet.ServletContext;
27 import javax.servlet.ServletException;
28 import javax.servlet.ServletRequest;
29 import javax.servlet.ServletResponse;
30 import javax.servlet.http.HttpServletRequest;
31 import javax.servlet.http.HttpServletResponse;
32 import javax.servlet.http.HttpSession;
33 import org.mxchange.jcore.exceptions.BadTokenException;
34 import org.mxchange.jcore.exceptions.CorruptedDatabaseFileException;
35 import org.mxchange.jcore.exceptions.UnsupportedDatabaseBackendException;
36 import org.mxchange.jshop.category.Category;
37 import org.mxchange.jshop.database.frontend.category.CategoryDatabaseFrontend;
38 import org.mxchange.jshop.database.frontend.category.CategoryFrontend;
39 import org.mxchange.jshop.database.frontend.product.ProductDatabaseFrontend;
40 import org.mxchange.jshop.database.frontend.product.ProductFrontend;
41 import org.mxchange.jshop.exceptions.CategoryTitleAlreadyUsedException;
42 import org.mxchange.jshop.exceptions.ProductTitleAlreadyUsedException;
43 import org.mxchange.jshop.item.AddableBasketItem;
44 import org.mxchange.jshop.product.Product;
45 import org.mxchange.pizzaapplication.BasePizzaServiceSystem;
46
47 /**
48  * Main application class
49  *
50  * @author Roland Haeder
51  */
52 public class PizzaServiceApplication extends BasePizzaServiceSystem implements PizzaApplication {
53         /**
54          * Database frontend for products
55          */
56         private ProductFrontend productFrontend;
57
58         /**
59          * Database frontend for categories
60          */
61         private CategoryFrontend categoryFrontend;
62
63         /**
64          * Default constructor
65          */
66         public PizzaServiceApplication () {
67                 // Trace message
68                 this.getLogger().trace("CALLED!"); //NOI18N
69         }
70
71         @Override
72         public Deque<Category> getAllCategories () throws ServletException {
73                 try {
74                         // Deligate to frontend
75                         return this.categoryFrontend.getAllCategories();
76                 } catch (final IOException | BadTokenException | SQLException | CorruptedDatabaseFileException | NoSuchMethodException | IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) {
77                         // Continue to throw
78                         throw new ServletException(ex);
79                 }
80         }
81
82         @Override
83         public Deque<Product> getAllProducts () throws ServletException {
84                 try {
85                         // Deligate to frontend
86                         return this.productFrontend.getAllProducts();
87                 } catch (final IOException | BadTokenException | SQLException | CorruptedDatabaseFileException | NoSuchMethodException | IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) {
88                         // Continue to throw
89                         throw new ServletException(ex);
90                 }
91         }
92
93         @Override
94         public Deque<Product> getAvailableProducts () throws ServletException {
95                 try {
96                         // Deligate to frontend
97                         return this.productFrontend.getAllAvailableProducts();
98                 } catch (final IOException | BadTokenException | SQLException | CorruptedDatabaseFileException | NoSuchMethodException | IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) {
99                         // Continue to throw
100                         throw new ServletException(ex);
101                 }
102         }
103
104         @Override
105         public void init (final ServletContext context) throws UnsupportedDatabaseBackendException, SQLException {
106                 // Trace message
107                 this.getLogger().trace(MessageFormat.format("context={0} - CALLED!", context)); //NOI18N
108
109                 // context should not be null
110                 if (null == context) {
111                         // Abort here
112                         throw new NullPointerException("context is null"); //NOI18N
113                 }
114
115                 // Is the bundle initialized?
116                 if (!this.isBundledInitialized()) {
117                         // Temporary initialize default bundle
118                         // TODO The enum Gender uses this
119                         this.initBundle();
120                 }
121
122                 // Initialize properties from context
123                 this.initProperties(context);
124
125                 // Init database frontends
126                 this.initDatabaseFrontends();
127
128                 // Trace message
129                 this.getLogger().trace("EXIT!"); //NOI18N
130         }
131
132         /**
133          * Calculates total amount of all choosen products
134          *
135          * @param request Request instance
136          * @param session Session instance
137          * @return Total amount of all choosen products
138          * @deprecated Old lost 
139          */
140         @Deprecated
141         private int calculateTotalAmount (final ServletRequest request, final HttpSession session) throws ServletException {
142                 // Trace message
143                 this.getLogger().trace(MessageFormat.format("request={0},session={1} - CALLED!", request, session)); //NOI18N
144
145                 // Is product and session set?
146                 if (null == request) {
147                         // Not set
148                         throw new NullPointerException("request is null"); //NOI18N
149                 } else if (null == session) {
150                         // Not set
151                         throw new NullPointerException("session is null"); //NOI18N
152                 }
153
154                 // Init/declare total price and iterator
155                 int totalAmount = 0;
156                 Iterator<Product> iterator = this.getAvailableProductsIterator();
157
158                 // "Walk" over all products
159                 while (iterator.hasNext()) {
160                         // Get next product
161                         Product product = iterator.next();
162
163                         // Is this choosen?
164                         if (this.isProductChoosen(product, request, session)) {
165                                 // Then add ordered amount
166                                 this.getLogger().debug(MessageFormat.format("Counting {0} ...", product.getItemId())); //NOI18N
167
168                                 // Getting amount
169                                 String amount = this.getAmountFromSession(product, session);
170
171                                 // Add it up
172                                 this.getLogger().debug(MessageFormat.format("amount={0}", amount)); //NOI18N
173                                 totalAmount += Integer.valueOf(amount);
174                         }
175                         this.getLogger().debug(MessageFormat.format("product={0},totalAmount={1}", product.getItemId(), totalAmount)); //NOI18N
176                 }
177
178                 // Trace message
179                 this.getLogger().trace(MessageFormat.format("totalAmount={0} - EXIT!", totalAmount)); //NOI18N
180
181                 // Return total price
182                 return totalAmount;
183         }
184
185         @Override
186         public void doBootstrap () {
187                 throw new UnsupportedOperationException("Not supported yet."); //NOI18N
188         }
189
190         @Override
191         public void doMainLoop () {
192                 throw new UnsupportedOperationException("Not supported yet."); //NOI18N
193         }
194
195         @Override
196         public void doShutdown () {
197                 throw new UnsupportedOperationException("Not supported yet."); //NOI18N
198         }
199
200         /**
201          * Some "getter" for amount from session
202          *
203          * @param product Product instance
204          * @param session Session instance
205          * @return Amount as string
206          * @deprecated Old lost code
207          */
208         @Deprecated
209         private String getAmountFromSession (final Product product, final HttpSession session) {
210                 // Trace message
211                 this.getLogger().trace(MessageFormat.format("product={0},session={1} - CALLED!", product, session)); //NOI18N
212
213                 // Is product and session set?
214                 if (null == product) {
215                         // Not set
216                         throw new NullPointerException("product is null"); //NOI18N
217                 } else if (null == session) {
218                         // Not set
219                         throw new NullPointerException("session is null"); //NOI18N
220                 }
221
222                 // Get attribute
223                 Object object = this.getValueFromSession(product, session, HTTP_PARAM_AMOUNT);
224
225                 // Is the object null?
226                 if (null == object) {
227                         // Trace message
228                         this.getLogger().trace("Returning 0 - EXIT!"); //NOI18N
229
230                         // Not found
231                         return "0"; //NOI18N
232                 }
233
234                 // Trace message
235                 this.getLogger().trace(MessageFormat.format("object={0} - EXIT!", object)); //NOI18N
236
237                 // Cast to string and return it
238                 return (String) object;
239         }
240
241         @Override
242         public String getCheckedHtmlFromProduct (final Product product, final ServletRequest request, final HttpSession session) {
243                 // Trace message
244                 this.getLogger().trace(MessageFormat.format("product={0},request={1},session={2} - CALLED!", product, request, session)); //NOI18N
245
246                 // Is product and session set?
247                 if (null == product) {
248                         // Not set
249                         throw new NullPointerException("product is null"); //NOI18N
250                 } else if (null == request) {
251                         // Not set
252                         throw new NullPointerException("request is null"); //NOI18N
253                 } else if (null == session) {
254                         // Not set
255                         throw new NullPointerException("session is null"); //NOI18N
256                 }
257
258                 // First let's check if the product is choosen
259                 if (this.isProductChoosen(product, request, session)) {
260                         // Trace message
261                         this.getLogger().trace("Returning checked=\"checked\" - EXIT!"); //NOI18N
262
263                         // Is choosen
264                         return "checked=\"checked\""; //NOI18N
265                 } else {
266                         // Trace message
267                         this.getLogger().trace("Returning empty string - EXIT!"); //NOI18N
268
269                         // Not choosen
270                         return ""; //NOI18N
271                 }
272         }
273
274         @Override
275         public String getDisabledHtmlFromSession (final ServletRequest request, final HttpSession session) throws ServletException {
276                 // Trace message
277                 this.getLogger().trace(MessageFormat.format("request={0},session={1} - CALLED!", request, session)); //NOI18N
278
279                 // Is product and session set?
280                 if (null == request) {
281                         // Not set
282                         throw new NullPointerException("request is null"); //NOI18N
283                 } else if (null == session) {
284                         // Not set
285                         throw new NullPointerException("session is null"); //NOI18N
286                 }
287
288                 // Get "enabled" from request scope
289                 Boolean enabled = Boolean.parseBoolean((String) request.getAttribute("enabled")); //NOI18N
290
291                 // Debug message
292                 this.getLogger().debug(MessageFormat.format("enabled={0}", enabled)); //NOI18N
293
294                 // Is something selected?
295                 if ((enabled) || (this.calculateTotalAmount(request, session) > 0)) {
296                         // Trace message
297                         this.getLogger().trace("Returning empty string - EXIT!"); //NOI18N
298
299                         // Something has been choosen
300                         return ""; //NOI18N
301                 } else {
302                         // Trace message
303                         this.getLogger().trace("Returning disabled=\"disabled\" - EXIT!"); //NOI18N
304
305                         // Nothing choosen yet
306                         return "disabled=\"disabled\""; //NOI18N
307                 }
308         }
309
310         @Override
311         public String getPrintableProduktAvailability (final Product product) {
312                 // Trace message
313                 this.getLogger().trace(MessageFormat.format("product={0} - CALLED!", product)); //NOI18N
314
315                 // Is it null?
316                 if (null == product) {
317                         // Should not be null
318                         throw new NullPointerException("product is null"); //NOI18N
319                 }
320
321                 // Get availability
322                 if (product.getAvailable() == true) {
323                         // Is available
324                         return "Ja";
325                 } else {
326                         // Not, not for public
327                         return "Nein";
328                 }
329         }
330
331         @Override
332         @SuppressWarnings ("unchecked")
333         public Iterator<Product> getAvailableProductsIterator () throws ServletException {
334                 // categoryFrontend must be set
335                 if (null == this.productFrontend) {
336                         // Abort here
337                         throw new NullPointerException("productFrontend is null"); //NOI18N
338                 }
339
340                 try {
341                         // Ask frontend for a list of products
342                         return (Iterator<Product>) this.productFrontend.getAvailableProductsIterator();
343                 } catch (final IOException | BadTokenException | SQLException | CorruptedDatabaseFileException | NoSuchMethodException | IllegalAccessException | InvocationTargetException ex) {
344                         throw new ServletException(ex);
345                 }
346         }
347
348         @Override
349         @SuppressWarnings ("unchecked")
350         public Iterator<Product> getAllProductsIterator () throws ServletException {
351                 // Trace message
352                 this.getLogger().trace("CALLED!"); //NOI18N
353
354                 // categoryFrontend must be set
355                 if (null == this.productFrontend) {
356                         // Abort here
357                         throw new NullPointerException("productFrontend is null"); //NOI18N
358                 }
359
360                 try {
361                         // Ask frontend for a list of products
362                         return (Iterator<Product>) this.productFrontend.getAllProductsIterator();
363                 } catch (final IOException | BadTokenException | SQLException | CorruptedDatabaseFileException | NoSuchMethodException | IllegalAccessException | InvocationTargetException ex) {
364                         throw new ServletException(ex);
365                 }
366         }
367
368         @Override
369         @SuppressWarnings ("unchecked")
370         public Iterator<Category> getAllCategoriesIterator () throws ServletException {
371                 // Trace message
372                 this.getLogger().trace("CALLED!"); //NOI18N
373
374                 // categoryFrontend must be set
375                 if (null == this.categoryFrontend) {
376                         // Abort here
377                         throw new NullPointerException("categoryFrontend is null"); //NOI18N
378                 }
379
380                 try {
381                         // Ask frontend for a list of categories
382                         return (Iterator<Category>) this.categoryFrontend.getAllCategoriesIterator();
383                 } catch (final IOException | BadTokenException | SQLException | CorruptedDatabaseFileException | NoSuchMethodException | IllegalAccessException | InvocationTargetException ex) {
384                         throw new ServletException(ex);
385                 }
386         }
387
388         @Deprecated
389         private boolean isProductChoosen (final Product product, final ServletRequest request, final HttpSession session) {
390                 throw new UnsupportedOperationException("This method is deprecated and shall not be called"); //NOI18N
391         }
392
393         @Override
394         public void setValueInSession (final HttpSession session, final String key, final Object value) {
395                 // Trace message
396                 this.getLogger().trace(MessageFormat.format("session={0},key={1},value={2} - CALLED!", session, key, value)); //NOI18N
397
398                 synchronized(session) {
399                         // Set it synced
400                         session.setAttribute(key, value);
401                 }
402
403                 // Trace message
404                 this.getLogger().trace("EXIT!"); //NOI18N
405         }
406
407         /**
408          * Some getter for value from session
409          *
410          * @param product Product instance
411          * @param session Session instance
412          * @param attribute Attribute to get value from
413          * @return Value from session
414          * @deprecated Old lost code
415          */
416         @Deprecated
417         private Object getValueFromSession (final Product product, final HttpSession session, final String attribute) {
418                 // Trace message
419                 this.getLogger().trace(MessageFormat.format("product={0},session={1},attribute={2} - CALLED!", product, session, attribute)); //NOI18N
420
421                 // Init variable
422                 Object value = this.getValueFromSession(session, String.format(HTTP_PARAM_MASK, attribute, product.getItemId()));
423                 
424                 this.getLogger().debug(MessageFormat.format("product={0},attribute={1},value={2}", product.getItemId(), attribute, value)); //NOI18N
425
426                 // Trace message
427                 this.getLogger().trace(MessageFormat.format("value={0} - EXIT!", value)); //NOI18N
428
429                 // Return it
430                 return value;
431         }
432
433         /**
434          * Some getter for value from session
435          *
436          * @param session Session instance
437          * @param key Key to get value from
438          * @return Value from session
439          */
440         private Object getValueFromSession (final HttpSession session, final String key) {
441                 // Trace message
442                 this.getLogger().trace(MessageFormat.format("session={0},key={1} - CALLED!", session, key)); //NOI18N
443
444                 // Init value
445                 Object value;
446
447                 // Get it synchronized from session
448                 synchronized (session) {
449                         value = session.getAttribute(key);
450                 }
451
452                 // Trace message
453                 this.getLogger().trace(MessageFormat.format("value={0} - EXIT!", value)); //NOI18N
454
455                 // Return it
456                 return value;
457         }
458
459         /**
460          * Initializes database frontends.
461          */
462         private void initDatabaseFrontends () throws UnsupportedDatabaseBackendException, SQLException {
463                 // Trace message
464                 this.getLogger().trace("CALLED!"); //NOI18N
465
466                 // Product frontend
467                 this.productFrontend = new ProductDatabaseFrontend();
468
469                 // Category frontend
470                 this.categoryFrontend = new CategoryDatabaseFrontend();
471
472                 // Trace message
473                 this.getLogger().trace("EXIT!"); //NOI18N
474         }
475
476         /**
477          * Checks whether given category title is already used
478          *
479          * @param title Title of category to check
480          * @return Whether it has been found
481          */
482         private boolean isCategoryTitleUsed(final String title) throws IOException, SQLException, BadTokenException, CorruptedDatabaseFileException, NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
483                 // categoryFrontend must be set
484                 if (null == this.categoryFrontend) {
485                         // Abort here
486                         throw new NullPointerException("categoryFrontend is null"); //NOI18N
487                 }
488
489                 // Delegate to frontend
490                 return this.categoryFrontend.isCategoryTitleUsed(title);
491         }
492
493         /**
494          * Checks if given product title is already used
495          * @param title Product title to check
496          * @return Whether the product title has already been used
497          */
498         private boolean isProductTitleUsed (final String title) throws IOException, SQLException, BadTokenException, CorruptedDatabaseFileException, NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
499                 // categoryFrontend must be set
500                 if (null == this.productFrontend) {
501                         // Abort here
502                         throw new NullPointerException("productFrontend is null"); //NOI18N
503                 }
504
505                 // Delegate to frontend
506                 return this.productFrontend.isProductTitleUsed(title);
507         }
508
509         @Override
510         public void doAdminAddCategory (final ServletRequest request) throws ServletException, CategoryTitleAlreadyUsedException {
511                 // Trace message
512                 this.getLogger().trace(MessageFormat.format("request={0} - CALLED!", request)); //NOI18N
513
514                 // request must not be null
515                 if (null == request) {
516                         // Is null
517                         throw new NullPointerException("request is null"); //NOI18N
518                 }
519
520                 // Get all fields
521                 String title = request.getParameter(CategoryFrontend.COLUMN_TITLE);
522                 String parentId = request.getParameter(CategoryFrontend.COLUMN_PARENT);
523
524                 // Debug message
525                 this.getLogger().debug(MessageFormat.format("title={0},parentId={1}", title, parentId)); //NOI18N
526
527                 // Init variables for casting
528                 Integer id = 0;
529
530                 // Check all fields
531                 if (null == title) {
532                         // "title" not set
533                         throw new IllegalArgumentException(MessageFormat.format("{0} is not set.", CategoryFrontend.COLUMN_TITLE)); //NOI18N
534                 } else if (title.isEmpty()) {
535                         // Is left empty
536                         throw new IllegalArgumentException(MessageFormat.format("{0} is empty.", CategoryFrontend.COLUMN_TITLE)); //NOI18N
537                 } else if ((parentId != null) && (!parentId.isEmpty())) {
538                         // "parent" is set, so check it
539                         try {
540                                 id = Integer.parseInt(parentId);
541                         } catch (final NumberFormatException e) {
542                                 // Not valid number
543                                 throw new IllegalArgumentException(e);
544                         }
545                 }
546
547                 try {
548                         // Try to check if title is used already
549                         if (this.isCategoryTitleUsed(title)) {
550                                 // Title already used
551                                 throw new CategoryTitleAlreadyUsedException(request);
552                         }
553                 } catch (final IOException | SQLException | BadTokenException | CorruptedDatabaseFileException | NoSuchMethodException | IllegalAccessException | InvocationTargetException ex) {
554                         throw new ServletException(ex);
555                 }
556
557                 try {
558                         // The category is not found, so add it to database
559                         this.categoryFrontend.addCategory(title, id);
560                 } catch (final SQLException | IOException ex) {
561                         // Continue to throw it
562                         throw new ServletException(ex);
563                 }
564
565                 // Trace message
566                 this.getLogger().trace("EXIT!"); //NOI18N
567         }
568
569         @Override
570         public void doAdminAddProduct (final ServletRequest request) throws ServletException, ProductTitleAlreadyUsedException {
571                 // Trace message
572                 this.getLogger().trace(MessageFormat.format("request={0} - CALLED!", request)); //NOI18N
573
574                 // request must not be null
575                 if (null == request) {
576                         // Is null
577                         throw new NullPointerException("request is null"); //NOI18N
578                 }
579
580                 // Get title, price and category id
581                 String title = request.getParameter(ProductFrontend.COLUMN_TITLE);
582                 String price = request.getParameter(ProductFrontend.COLUMN_PRICE);
583                 String categoryId = request.getParameter(ProductFrontend.COLUMN_CATEGORY);
584                 String available = request.getParameter(ProductFrontend.COLUMN_AVAILABLE);
585
586                 // Debug message
587                 this.getLogger().debug(MessageFormat.format("title={0},price={1},categoryId={2},available={3}", title, price, categoryId, available)); //NOI18N
588
589                 // Variables for converting
590                 Long id = null;
591                 Float p = null;
592
593                 // Check all fields
594                 if (null == title) {
595                         // "title" not set
596                         throw new IllegalArgumentException(MessageFormat.format("{0} is not set.", ProductFrontend.COLUMN_TITLE)); //NOI18N
597                 } else if (title.isEmpty()) {
598                         // Is left empty
599                         throw new IllegalArgumentException(MessageFormat.format("{0} is empty.", ProductFrontend.COLUMN_TITLE)); //NOI18N
600                 } else if (null == price) {
601                         // "price" not set
602                         throw new IllegalArgumentException(MessageFormat.format("{0} is not set.", ProductFrontend.COLUMN_PRICE)); //NOI18N
603                 } else if (price.isEmpty()) {
604                         // Is left empty
605                         throw new IllegalArgumentException(MessageFormat.format("{0} is empty.", ProductFrontend.COLUMN_PRICE)); //NOI18N
606                 } else if (null == categoryId) {
607                         // "title" not set
608                         throw new IllegalArgumentException(MessageFormat.format("{0} is not set.", ProductFrontend.COLUMN_CATEGORY)); //NOI18N
609                 } else if (categoryId.isEmpty()) {
610                         // Is left empty
611                         throw new IllegalArgumentException(MessageFormat.format("{0} is empty.", ProductFrontend.COLUMN_CATEGORY)); //NOI18N
612                 } else if (null == available) {
613                         // "title" not set
614                         throw new IllegalArgumentException(MessageFormat.format("{0} is not set.", ProductFrontend.COLUMN_AVAILABLE)); //NOI18N
615                 } else if (available.isEmpty()) {
616                         // Is left empty
617                         throw new IllegalArgumentException(MessageFormat.format("{0} is empty.", ProductFrontend.COLUMN_AVAILABLE)); //NOI18N
618                 } else if ((!"true".equals(available)) && (!"false".equals(available))) { //NOI18N
619                         // Invalid value
620                         throw new IllegalArgumentException(MessageFormat.format("{0} is invalid: {1}", ProductFrontend.COLUMN_AVAILABLE, available)); //NOI18N
621                 }
622
623                 // Parse numbers
624                 try {
625                         id = Long.parseLong(categoryId);
626                         p = Float.parseFloat(price);
627                 } catch (final NumberFormatException e) {
628                         // Not valid number
629                         throw new IllegalArgumentException(e);
630                 }
631
632                 // Parse boolean
633                 Boolean a = Boolean.parseBoolean(available);
634
635                 // Test on product title
636                 try {
637                         // Try to check if title is used already
638                         if (this.isProductTitleUsed(title)) {
639                                 // Title already used
640                                 throw new ProductTitleAlreadyUsedException(request);
641                         }
642                 } catch (final IOException | SQLException | BadTokenException | CorruptedDatabaseFileException | NoSuchMethodException | IllegalAccessException | InvocationTargetException ex) {
643                         throw new ServletException(ex);
644                 }
645
646                 try {
647                         // The product is not found, so add it to database
648                         this.productFrontend.addProduct(title, p, id, a);
649                 } catch (final SQLException | IOException ex) {
650                         // Continue to throw it
651                         throw new ServletException(ex);
652                 }
653
654                 // Trace message
655                 this.getLogger().trace("EXIT!"); //NOI18N
656         }
657
658         @Override
659         public String generateLinkForParent (final Category category) {
660                 // Trace message
661                 this.getLogger().trace(MessageFormat.format("category={0} - CALLED!", category)); //NOI18N
662
663                 // category must not be null
664                 if (null == category) {
665                         // Is null
666                         throw new NullPointerException("category is null"); //NOI18N
667                 }
668
669                 // Get parent id
670                 Long parentId = category.getParentId();
671
672                 // Is the id set?
673                 if (parentId > 0) {
674                         // Product HTML code for link
675                         throw new UnsupportedOperationException(MessageFormat.format("parentId={0} - Unfinished!", parentId)); //NOI18N
676                 }
677
678                 // No parent set
679                 return "Keine";
680         }
681
682         @Override
683         public String getPrintableProduktCategory (final Product product) throws ServletException {
684                 // Trace message
685                 this.getLogger().trace(MessageFormat.format("product={0} - CALLED!", product)); //NOI18N
686
687                 // product must not be null
688                 if (null == product) {
689                         // Abort here
690                         throw new NullPointerException("product is null"); //NOI18N
691                 }
692
693                 // Declare category
694                 Category category;
695
696                 try {
697                         // Get Category instance from product over the frontend
698                         category = this.categoryFrontend.getCategory(product);
699                 } catch (final IOException | SQLException | BadTokenException | CorruptedDatabaseFileException | NoSuchMethodException | IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) {
700                         throw new ServletException(ex);
701                 }
702
703                 // Debug message
704                 this.getLogger().debug(MessageFormat.format("categoryId={0}", category)); //NOI18N
705
706                 String title = null;
707                 try {
708                         // Now get title from it and return it
709                         title = category.getDecodedTitle();
710                 } catch (final UnsupportedEncodingException ex) {
711                         // Continue to throw as cause
712                         throw new ServletException(ex);
713                 }
714
715                 // Trace message
716                 this.getLogger().trace(MessageFormat.format("title={0} - EXIT!", title)); //NOI18N
717
718                 // Return it
719                 return title;
720         }
721
722         /**
723          * Checks if product's title is already used.
724          * 
725          * @param request Request instance
726          * @return Whether the product title is already used
727          * @throws java.io.IOException If any IO error occurs
728          * @throws org.mxchange.jcore.exceptions.BadTokenException If a bad token was found in a file-based database backend's file ... ;-)
729          * @throws java.sql.SQLException If any SQL error occurs
730          * @throws org.mxchange.jcore.exceptions.CorruptedDatabaseFileException If the database file is damaged
731          * @throws java.lang.NoSuchMethodException If a method was not found
732          * @throws java.lang.IllegalAccessException If the method cannot be accessed
733          * @throws java.lang.reflect.InvocationTargetException Any other problems?
734          */
735         private boolean isProductTitleUsed (final ServletRequest request) throws IOException, SQLException, BadTokenException, CorruptedDatabaseFileException, NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
736                 // Trace message
737                 this.getLogger().trace(MessageFormat.format("request={0} - CALLED!", request)); //NOI18N
738
739                 // Init title
740                 String title = request.getParameter(ProductFrontend.COLUMN_TITLE);
741
742                 // request must not be null and "title" must be found and non-empty
743                 if (null == request) {
744                         // Abort here
745                         throw new NullPointerException("request is null"); //NOI18N
746                 } else if (null == title) {
747                         // title is not set
748                         throw new IllegalArgumentException(MessageFormat.format("{0} is not set.", ProductFrontend.COLUMN_TITLE)); //NOI18N
749                 } else if (title.isEmpty()) {
750                         // Is left empty
751                         throw new IllegalArgumentException(MessageFormat.format("{0} is empty.", ProductFrontend.COLUMN_TITLE)); //NOI18N
752                 }
753
754                 // Default is not used
755                 boolean isUsed = this.isProductTitleUsed(title);
756
757                 // Trace message
758                 this.getLogger().trace(MessageFormat.format("isUsed={0} - EXIT!", isUsed)); //NOI18N
759
760                 // Return it
761                 return isUsed;
762         }
763
764         @Override
765         public void doAdminHandleProductForms (final ServletRequest request, final ServletResponse response) throws ServletException {
766                 // Trace message
767                 this.getLogger().trace(MessageFormat.format("request={0},response={1} - CALLED!", request, response)); //NOI18N
768
769                 // request and response must both be set
770                 if (null == request) {
771                         // request is null
772                         throw new NullPointerException("request is null"); //NOI18N
773                 } else if (null == response) {
774                         // response is null
775                         throw new NullPointerException("response is null"); //NOI18N
776                 }
777
778                 // Try this operations
779                 try {
780                         // Is it post?
781                         if ("POST".equals(((HttpServletRequest) request).getMethod())) { //NOI18N
782                                 // Is "add/edit/delete" set?
783                                 if (request.getParameter("add") != null) { //NOI18N
784                                         // Is it already added?
785                                         if (this.isProductTitleUsed(request)) {
786                                                 // Debug message
787                                                 this.getLogger().debug("Already used, redirecting ..."); //NOI18N
788
789                                                 // Already added, so redirect here, else a ServletException will be thrown
790                                                 ((HttpServletResponse) response).sendRedirect(String.format("%s/admin/product.jsp?already=1", ((HttpServletRequest) request).getContextPath())); //NOI18N
791                                         } else {
792                                                 // Add new product
793                                                 this.doAdminAddProduct(request);
794
795                                                 // Redirect to proper URL
796                                                 ((HttpServletResponse) response).sendRedirect(String.format("%s/admin/product.jsp?added=1", ((HttpServletRequest) request).getContextPath())); //NOI18N
797                                         }
798                                 } else if (request.getParameter("edit") != null) { //NOI18N
799                                         // TODO
800                                 } else if (request.getParameter("delete") != null) { //NOI18N
801                                         // TODO
802                                 }
803
804                                 // Request not handled
805                                 throw new ServletException("request not handled."); //NOI18N
806                         }
807                 } catch (final IOException | SQLException | BadTokenException | CorruptedDatabaseFileException | ProductTitleAlreadyUsedException | NoSuchMethodException | IllegalAccessException | InvocationTargetException | IllegalArgumentException ex) {
808                         // Throw it as cause
809                         throw new ServletException(ex);
810                 }
811
812                 // Trace message
813                 this.getLogger().trace("EXIT!"); //NOI18N
814         }
815
816         @Override
817         public void doAdminHandleCategoryForms (final ServletRequest request, final ServletResponse response) throws ServletException {
818                 // Trace message
819                 this.getLogger().trace(MessageFormat.format("request={0},response={1} - CALLED!", request, response)); //NOI18N
820
821                 // request and response must both be set
822                 if (null == request) {
823                         // request is null
824                         throw new NullPointerException("request is null"); //NOI18N
825                 } else if (null == response) {
826                         // response is null
827                         throw new NullPointerException("response is null"); //NOI18N
828                 }
829
830                 // Try this operations
831                 try {
832                         // Is it post?
833                         if ("POST".equals(((HttpServletRequest) request).getMethod())) { //NOI18N
834                                 // Is "add/edit/delete" set?
835                                 if (request.getParameter("add") != null) { //NOI18N
836                                         // Is the category title already used?
837                                         if (this.isCategoryTitleUsed(request)) {
838                                                 // Debug message
839                                                 this.getLogger().debug("Already used, redirecting ..."); //NOI18N
840
841                                                 // Already added, so redirect here, else a ServletException will be thrown
842                                                 ((HttpServletResponse) response).sendRedirect(String.format("%s/admin/category.jsp?already=1", ((HttpServletRequest) request).getContextPath())); //NOI18N
843                                         } else {
844                                                 // Add new category
845                                                 this.doAdminAddCategory(request);
846
847                                                 // Redirect to proper URL
848                                                 ((HttpServletResponse) response).sendRedirect(String.format("%s/admin/category.jsp?added=1", ((HttpServletRequest) request).getContextPath())); //NOI18N
849                                         }
850                                 } else if (request.getParameter("edit") != null) { //NOI18N
851                                         // TODO
852                                 } else if (request.getParameter("delete") != null) { //NOI18N
853                                         // TODO
854                                 }
855
856                                 // Request not handled
857                                 throw new ServletException("request not handled."); //NOI18N
858                         }
859                 } catch (final IOException | SQLException | BadTokenException | CorruptedDatabaseFileException | CategoryTitleAlreadyUsedException | NoSuchMethodException | IllegalAccessException | InvocationTargetException | IllegalArgumentException ex) {
860                         // Throw it as cause
861                         throw new ServletException(ex);
862                 }
863
864                 // Trace message
865                 this.getLogger().trace("EXIT!"); //NOI18N
866         }
867
868         /**
869          * Checks if category's title is already used.
870          * 
871          * @param request Request instance
872          * @return Whether the product title is already used
873          * @throws java.io.IOException If any IO error occurs
874          * @throws org.mxchange.jcore.exceptions.BadTokenException If a bad token was found in a file-based database backend's file ... ;-)
875          * @throws java.sql.SQLException If any SQL error occurs
876          * @throws org.mxchange.jcore.exceptions.CorruptedDatabaseFileException If the database file is damaged
877          * @throws java.lang.NoSuchMethodException If a method was not found
878          * @throws java.lang.IllegalAccessException If the method cannot be accessed
879          * @throws java.lang.reflect.InvocationTargetException Any other problems?
880          */
881         private boolean isCategoryTitleUsed (final ServletRequest request) throws IOException, SQLException, BadTokenException, CorruptedDatabaseFileException, NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
882                 // Trace message
883                 this.getLogger().trace(MessageFormat.format("request={0} - CALLED!", request)); //NOI18N
884
885                 // Init title
886                 String title = request.getParameter(CategoryFrontend.COLUMN_TITLE);
887
888                 // request must not be null and "title" must be found and non-empty
889                 if (null == request) {
890                         // Abort here
891                         throw new NullPointerException("request is null"); //NOI18N
892                 } else if (null == title) {
893                         // title is not set
894                         throw new IllegalArgumentException(MessageFormat.format("{0} is not set.", CategoryFrontend.COLUMN_TITLE)); //NOI18N
895                 } else if (title.isEmpty()) {
896                         // Is left empty
897                         throw new IllegalArgumentException(MessageFormat.format("{0} is empty.", CategoryFrontend.COLUMN_TITLE)); //NOI18N
898                 }
899
900                 // Default is not used
901                 boolean isUsed = this.isCategoryTitleUsed(title);
902
903                 // Trace message
904                 this.getLogger().trace(MessageFormat.format("isUsed={0} - EXIT!", isUsed)); //NOI18N
905
906                 // Return it
907                 return isUsed;
908         }
909
910         @Override
911         public Product getProduct (final AddableBasketItem item) throws ServletException {
912                 // Trace message
913                 this.getLogger().trace(MessageFormat.format("item={0} - CALLED!", item)); //NOI18N
914
915                 // item should not be null
916                 if (null == item) {
917                         // Abort here
918                         throw new NullPointerException("item is null"); //NOI18N
919                 } else if (null == this.productFrontend) {
920                         // Abort here
921                         throw new NullPointerException("productFrontend is null"); //NOI18N
922                 }
923
924                 // Init product instance
925                 Product product = null;
926
927                 try {
928                         // Call frontend
929                         product = this.productFrontend.getProduct(item);
930                 } catch (final SQLException | IOException | BadTokenException | CorruptedDatabaseFileException | NoSuchMethodException | IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) {
931                         // Continue to throw
932                         throw new ServletException(ex);
933                 }
934
935                 // Trace message
936                 this.getLogger().trace(MessageFormat.format("product={0} - EXIT!", product)); //NOI18N //NOI18N
937
938                 // Return it
939                 return product;
940         }
941 }