]> git.mxchange.org Git - pizzaservice-war.git/blob - src/java/org/mxchange/pizzaapplication/application/PizzaServiceApplication.java
Added more thrown exceptions + implemented getProducts()
[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.lang.reflect.Field;
21 import java.lang.reflect.InvocationTargetException;
22 import java.sql.SQLException;
23 import java.text.MessageFormat;
24 import java.util.Iterator;
25 import java.util.Map;
26 import javax.servlet.ServletContext;
27 import javax.servlet.ServletException;
28 import javax.servlet.http.HttpServletRequest;
29 import javax.servlet.http.HttpSession;
30 import org.mxchange.jcore.contact.Gender;
31 import org.mxchange.jcore.exceptions.BadTokenException;
32 import org.mxchange.jcore.exceptions.UnsupportedDatabaseBackendException;
33 import org.mxchange.pizzaapplication.BasePizzaServiceSystem;
34 import org.mxchange.pizzaapplication.category.Category;
35 import org.mxchange.pizzaapplication.customer.Customer;
36 import org.mxchange.pizzaapplication.customer.PizzaServiceCustomer;
37 import org.mxchange.pizzaapplication.database.frontend.product.PizzaProductDatabaseFrontend;
38 import org.mxchange.pizzaapplication.database.frontend.product.ProductFrontend;
39 import org.mxchange.pizzaapplication.product.Product;
40
41 /**
42  *
43  * @author Roland Haeder
44  */
45 public class PizzaServiceApplication extends BasePizzaServiceSystem implements PizzaApplication {
46         /**
47          * Main title
48          */
49         public static final String MAIN_TITLE = "Pizza-Service";
50
51         /**
52          * Frontend for products
53          */
54         private ProductFrontend productFrontend;
55
56         /**
57          * Some singleton getter for this instance. If the instance is not set in
58          * given application, it will be created.
59          *
60          * @param context Servlet context
61          * @return This instance
62          * @throws javax.servlet.ServletException If object is not set correctly
63          */
64         public static final PizzaApplication getInstance (final ServletContext context) throws ServletException {
65                 // Check application instance
66                 if (context == null) {
67                         // Not set
68                         throw new NullPointerException("application is null"); //NOI18N
69                 }
70
71                 // Init instance
72                 PizzaApplication instance = null;
73
74                 // Get instance from servlet application (aka. "application scope")
75                 Object object = context.getAttribute("app"); //NOI18N
76
77                 // Is it set?
78                 if (object instanceof PizzaApplication) {
79                         // Instance is set, so casting should work
80                         instance = (PizzaApplication) object;
81                 } else if (object instanceof Object) {
82                         // Not correct instance
83                         throw new ServletException("app is not set correctly"); //NOI18N
84                 } else {
85                         try {
86                                 // "service" is null, so initialize it
87                                 instance = new PizzaServiceApplication(context);
88                         } catch (final UnsupportedDatabaseBackendException | SQLException | IOException | BadTokenException ex) {
89                                 throw new ServletException(ex);
90                         }
91
92                         // And set it here
93                         context.setAttribute("app", instance); //NOI18N
94                 }
95
96                 // Trace message
97                 instance.getLogger().trace(MessageFormat.format("instance={0} - EXIT!", instance)); //NOI18N
98
99                 // Return it
100                 return instance;
101         }
102
103         /**
104          * For debugging purpose
105          *
106          * @param args Arguments
107          */
108         public static void main (String[] args) {
109                 // Get instance and start it
110                 new PizzaServiceApplication().start();
111         }
112
113         /**
114          * Constructor with servet configuration
115          *
116          * @param context Servlet context
117          */
118         private PizzaServiceApplication (final ServletContext context) throws UnsupportedDatabaseBackendException, SQLException, IOException, BadTokenException {
119                 // Temporary initialize default bundle
120                 // @TODO The JSF may have better internatialization support
121                 this.initBundle();
122
123                 // Initialize properties from config
124                 this.initProperties(context);
125
126                 // Init database frontends
127                 this.initDatabaseFrontends();
128         }
129
130         /**
131          * Default constructor
132          */
133         private PizzaServiceApplication () {
134         }
135
136         /**
137          * Calculates total amount of all choosen products
138          *
139          * @param request Request instance
140          * @param session Session instance
141          * @return Total amount of all choosen products
142          */
143         @Override
144         public int calculateTotalAmount (final HttpServletRequest request, final HttpSession session) throws ServletException {
145                 // Trace message
146                 this.getLogger().trace(MessageFormat.format("request={0},session={1} - CALLED!", request, session)); //NOI18N
147
148                 // Is product and session set?
149                 if (request == null) {
150                         // Not set
151                         throw new NullPointerException("request is null"); //NOI18N
152                 } else if (session == null) {
153                         // Not set
154                         throw new NullPointerException("session is null"); //NOI18N
155                 }
156
157                 // Init/declare total price and iterator
158                 int totalAmount = 0;
159                 Iterator<Product> iterator;
160
161                 try {
162                         // Get iterator
163                         iterator = this.getProducts();
164                 } catch (final IOException | BadTokenException ex) {
165                         throw new ServletException(ex);
166                 }
167
168                 // "Walk" over all products
169                 while (iterator.hasNext()) {
170                         // Get next product
171                         Product product = iterator.next();
172
173                         // Is this choosen?
174                         if (this.isProductChoosen(product, request, session)) {
175                                 // Then add ordered amount
176                                 this.getLogger().debug(MessageFormat.format("Counting {0} ...", product.getId())); //NOI18N
177
178                                 // Getting amount
179                                 String amount = this.getAmountFromSession(product, session);
180
181                                 // Add it up
182                                 this.getLogger().debug(MessageFormat.format("amount={0}", amount)); //NOI18N
183                                 totalAmount += Integer.valueOf(amount);
184                         }
185                         this.getLogger().debug(MessageFormat.format("product={0},totalAmount={1}", product.getId(), totalAmount)); //NOI18N
186                 }
187
188                 // Trace message
189                 this.getLogger().trace(MessageFormat.format("totalAmount={0} - EXIT!", totalAmount)); //NOI18N
190
191                 // Return total price
192                 return totalAmount;
193         }
194
195         /**
196          * Calculates total price of all choosen products
197          *
198          * @param request Request instance
199          * @param session Session instance
200          * @return Total price of all choosen products
201          */
202         @Override
203         public float calculateTotalPrice (final HttpServletRequest request, final HttpSession session) throws ServletException {
204                 // Trace message
205                 this.getLogger().trace(MessageFormat.format("request={0},session={1} - CALLED!", request, session)); //NOI18N
206
207                 // Is product and session set?
208                 if (request == null) {
209                         // Not set
210                         throw new NullPointerException("request is null"); //NOI18N
211                 } else if (session == null) {
212                         // Not set
213                         throw new NullPointerException("session is null"); //NOI18N
214                 }
215
216                 // Init total price
217                 float totalPrice = 0.00f;
218
219                 // Get iterator
220                 Iterator<Product> iterator;
221                 try {
222                         iterator = this.getProducts();
223                 } catch (final IOException | BadTokenException ex) {
224                         throw new ServletException(ex);
225                 }
226
227                 // "Walk" over all products
228                 while (iterator.hasNext()) {
229                         // Get next product
230                         Product product = iterator.next();
231
232                         // Is this choosen?
233                         if (this.isProductChoosen(product, request, session)) {
234                                 // Then add product's total price
235                                 this.getLogger().debug(MessageFormat.format("Calling getTotalPositionPriceFromRequestSession({0},request,session) ...", product.getId())); //NOI18N
236                                 totalPrice += this.getTotalPositionPriceFromRequestSession(product, request, session);
237                         }
238                         this.getLogger().debug(MessageFormat.format("product={0},totalPrice={1}", product.getId(), totalPrice)); //NOI18N
239                 }
240
241                 // Trace message
242                 this.getLogger().trace(MessageFormat.format(" totalPrice={0} - EXIT!", totalPrice)); //NOI18N
243
244                 // Return total price
245                 return totalPrice;
246         }
247
248         @Override
249         public void doBootstrap () {
250                 throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
251         }
252
253         @Override
254         public void doMainLoop () {
255                 throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
256         }
257
258         @Override
259         public void doShutdown () {
260                 throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
261         }
262
263         /**
264          * Some "getter" for amount from session
265          *
266          * @param product Product instance
267          * @param session Session instance
268          * @return Amount as string
269          */
270         @Override
271         public String getAmountFromSession (final Product product, final HttpSession session) {
272                 // Trace message
273                 this.getLogger().trace(MessageFormat.format("product={0},session={1} - CALLED!", product, session)); //NOI18N
274
275                 // Is product and session set?
276                 if (product == null) {
277                         // Not set
278                         throw new NullPointerException("product is null"); //NOI18N
279                 } else if (session == null) {
280                         // Not set
281                         throw new NullPointerException("session is null"); //NOI18N
282                 }
283
284                 // Get attribute
285                 Object object = this.getValueFromSession(product, session, HTTP_PARAM_AMOUNT);
286
287                 // Is the object null?
288                 if (object == null) {
289                         // Trace message
290                         this.getLogger().trace("Returning 0 - EXIT!"); //NOI18N
291
292                         // Not found
293                         return "0"; //NOI18N
294                 }
295
296                 // Trace message
297                 this.getLogger().trace(MessageFormat.format("object={0} - EXIT!", object)); //NOI18N
298
299                 // Cast to string and return it
300                 return (String) object;
301         }
302
303         /**
304          * Some "getter" for HTML code 'checked="checked"' if the product is choosen
305          * 
306          * @param product Product instance
307          * @param request Request instance
308          * @param session Session instance
309          * @return Whether the product is choosen
310          */
311         @Override
312         public String getCheckedHtmlFromProduct (final Product product, final HttpServletRequest request, final HttpSession session) {
313                 // Trace message
314                 this.getLogger().trace(MessageFormat.format("product={0},request={1},session={2} - CALLED!", product, request, session)); //NOI18N
315
316                 // Is product and session set?
317                 if (product == null) {
318                         // Not set
319                         throw new NullPointerException("product is null"); //NOI18N
320                 } else if (request == null) {
321                         // Not set
322                         throw new NullPointerException("request is null"); //NOI18N
323                 } else if (session == null) {
324                         // Not set
325                         throw new NullPointerException("session is null"); //NOI18N
326                 }
327
328                 // First let's check if the product is choosen
329                 if (this.isProductChoosen(product, request, session)) {
330                         // Trace message
331                         this.getLogger().trace("Returning checked=\"checked\" - EXIT!"); //NOI18N
332
333                         // Is choosen
334                         return "checked=\"checked\""; //NOI18N
335                 } else {
336                         // Trace message
337                         this.getLogger().trace("Returning empty string - EXIT!"); //NOI18N
338
339                         // Not choosen
340                         return ""; //NOI18N
341                 }
342         }
343
344         /**
345          * Some "getter" for choose from session
346          * 
347          * @param product Product instance
348          * @param session Session instance
349          * @return Choose as string
350          */
351         @Override
352         public String getChooseFromSession (final Product product, final HttpSession session) {
353                 // Trace message
354                 this.getLogger().trace(MessageFormat.format("product={0},session={1} - CALLED!", product, session)); //NOI18N
355
356                 // Is product and session set?
357                 if (product == null) {
358                         // Not set
359                         throw new NullPointerException("product is null"); //NOI18N
360                 } else if (session == null) {
361                         // Not set
362                         throw new NullPointerException("session is null"); //NOI18N
363                 }
364
365                 // Get attribute
366                 Object object = this.getValueFromSession(product, session, HTTP_PARAM_CHOOSE);
367
368                 // Is the object null?
369                 if (object == null) {
370                         // Not found
371                         this.getLogger().debug(MessageFormat.format("Returning empty string for product={0} ...", product.getId())); //NOI18N
372                         return ""; //NOI18N
373                 }
374
375                 // Trace message
376                 this.getLogger().trace(MessageFormat.format("object={0} - CALLED!", object)); //NOI18N
377
378                 // Cast to string and return it
379                 return (String) object;
380         }
381
382         /**
383          * Some "getter" for HTML code 'disabled="disabled"' for e.g. submit buttons
384          *
385          * @param request Request instance
386          * @param session Session instance
387          * @return Whether the product is choosen
388          */
389         @Override
390         public String getDisabledHtmlFromSession (final HttpServletRequest request, final HttpSession session) throws ServletException {
391                 // Trace message
392                 this.getLogger().trace(MessageFormat.format("request={0},session={1} - CALLED!", request, session)); //NOI18N
393
394                 // Is product and session set?
395                 if (request == null) {
396                         // Not set
397                         throw new NullPointerException("request is null"); //NOI18N
398                 } else if (session == null) {
399                         // Not set
400                         throw new NullPointerException("session is null"); //NOI18N
401                 }
402
403                 // Is something selected?
404                 if (this.calculateTotalAmount(request, session) > 0) {
405                         // Trace message
406                         this.getLogger().trace("Returning empty string - EXIT!"); //NOI18N
407
408                         // Something has been choosen
409                         return ""; //NOI18N
410                 } else {
411                         // Trace message
412                         this.getLogger().trace("Returning disabled=\"disabled\" - EXIT!"); //NOI18N
413
414                         // Nothing choosen yet
415                         return "disabled=\"disabled\""; //NOI18N
416                 }
417         }
418
419         /**
420          * Some "getter" for choosen (checkbox) from session
421          *
422          * @param product Product instance
423          * @param request Request instance
424          * @param session Session instance
425          * @return Amount as string
426          */
427         @Override
428         public String getPrintableChoosenFromRequestSession (final Product product, final HttpServletRequest request, final HttpSession session) {
429                 // Trace message
430                 this.getLogger().trace(MessageFormat.format("product={0},request={1},session={2} - CALLED!", product, request, session)); //NOI18N
431
432                 // Is product and session set?
433                 if (product == null) {
434                         // Not set
435                         throw new NullPointerException("product is null"); //NOI18N
436                 } else if (request == null) {
437                         // Not set
438                         throw new NullPointerException("request is null"); //NOI18N
439                 } else if (session == null) {
440                         // Not set
441                         throw new NullPointerException("session is null"); //NOI18N
442                 }
443
444                 // Get element
445                 this.getLogger().debug(MessageFormat.format("Calling handleChooseFromRequestSession({0},{1},{2}) ...", product.getId(), request, session)); //NOI18N
446                 String choosen = this.handleChooseFromRequestSession(product, request, session);
447                 this.getLogger().debug(MessageFormat.format("product={0},choosen={1}", product.getId(), choosen)); //NOI18N
448
449                 // Must not be null
450                 assert(choosen instanceof String): "choosen is null"; //NOI18N
451
452                 // Is it empty?
453                 if (choosen.isEmpty()) {
454                         // Not choosen
455                         return "Nein";
456                 }
457
458                 // Get amount
459                 String amount = this.handleAmountFromRequestSession(product, request, session);
460                 this.getLogger().debug(MessageFormat.format("product={0},amount={1}", product.getId(), amount)); //NOI18N
461
462                 // Must not be null
463                 assert(amount instanceof String): "amount is null"; //NOI18N
464
465                 // Is it empty?
466                 if (amount.isEmpty() || "0".equals(amount)) { //NOI18N
467                         // Choosen, but no amount
468                         return "Nein";
469                 } else {
470                         // Is choosen
471                         return "Ja";
472                 }
473         }
474
475         /**
476          * Checks if given Product instance is available and returns a printable
477          * (human-readable) string.
478          * 
479          * @param product Product instance to check
480          * @return Human-readable version of product availability
481          */
482         @Override
483         public String getPrintableProduktAvailability (final Product product) {
484                 throw new UnsupportedOperationException(MessageFormat.format("Not supported yet: product={0}", product));
485         }
486
487         /**
488          * Some getter for printable value from session or an empty string for null.
489          *
490          * @param session Session instance
491          * @param key Key to get
492          * @return Value from key, empty string for null
493          */
494         @Override
495         public Object getPrintableValeFromSession (final HttpSession session, final String key) {
496                 // Trace message
497                 this.getLogger().trace(MessageFormat.format("session={0},key={1} - CALLED", session, key)); //NOI18N
498
499                 // Are both parameter not null?
500                 if (session == null) {
501                         // Abort here
502                         throw new NullPointerException("session is null"); //NOI18N
503                 } else  if (key == null) {
504                         // Abort here
505                         throw new NullPointerException("key is null"); //NOI18N
506                 }
507
508                 // Now get it
509                 Object value = this.getValueFromSession(session, key);
510
511                 // Debug message
512                 this.getLogger().debug(MessageFormat.format("value={0}", value)); //NOI18N
513
514                 // Trace message
515                 this.getLogger().trace(MessageFormat.format("Calling this.convertNullToEmpty({0}) ... - EXIT!", value)); //NOI18N
516
517                 // Return actual value
518                 return this.convertNullToEmpty(value);
519         }
520
521         /**
522          * Some "getter" for a an array of all products
523          *
524          * @return All products
525          */
526         @Override
527         public Iterator<Product> getProducts () throws IOException, BadTokenException {
528                 // Ask frontend for a list of products
529                 return this.productFrontend.getProducts();
530         }
531
532         /**
533          * Some "getter" for a an array of all categories
534          *
535          * @return All categories
536          */
537         @Override
538         public Iterator<Category> getCategories () {
539                 throw new UnsupportedOperationException("Needs refacturing ...");
540         }
541
542         /**
543          * Some "getter" for total price of position from request or session.
544          * Single price and amount is multiplyed.
545          *
546          * @param product Product instance
547          * @param request Request instance
548          * @param session Session instance
549          * @return Amount as string
550          */
551         @Override
552         public float getTotalPositionPriceFromRequestSession (final Product product, final HttpServletRequest request, final HttpSession session) {
553                 // Trace message
554                 this.getLogger().trace(MessageFormat.format("product={0},request={1},session={2} - CALLED!", product, request, session)); //NOI18N
555
556                 // Is product and session set?
557                 if (product == null) {
558                         // Not set
559                         throw new NullPointerException("product is null"); //NOI18N
560                 } else if (request == null) {
561                         // Not set
562                         throw new NullPointerException("request is null"); //NOI18N
563                 } else if (session == null) {
564                         // Not set
565                         throw new NullPointerException("session is null"); //NOI18N
566                 }
567
568                 // Get choosen
569                 this.getLogger().debug(MessageFormat.format("Calling handleChooseFromRequestSession({0},{1},{2}) ...", product.getId(), request, session)); //NOI18N
570                 String choosen = this.handleChooseFromRequestSession(product, request, session);
571                 this.getLogger().debug(MessageFormat.format("product={0},choosen={1}", product.getId(), choosen)); //NOI18N
572
573                 // Must not be null
574                 assert(choosen instanceof String): "choosen is null"; //NOI18N
575
576                 // Is it set?
577                 if (choosen.isEmpty()) {
578                         // Is empty
579                         this.getLogger().debug(MessageFormat.format("product={0},choosen={1} - returning zero ...", product.getId(), choosen)); //NOI18N
580                         return 0.00f;
581                 }
582
583                 // Get amount
584                 String amount = this.handleAmountFromRequestSession(product, request, session);
585                 this.getLogger().debug(MessageFormat.format("product={0},amount={1}", product.getId(), amount)); //NOI18N
586
587                 // Must not be null
588                 assert(amount instanceof String): "amount is null"; //NOI18N
589
590                 // Is it empty?
591                 if (amount.isEmpty() || "0".equals(amount)) { //NOI18N
592                         // Is empty
593                         this.getLogger().debug(MessageFormat.format("product={0},amount={1} - returning zero ...", product.getId(), amount)); //NOI18N
594                         return 0.00f;
595                 }
596
597                 // Init variable
598                 Integer value = null;
599
600                 // Try it
601                 try {
602                         // Get amount as integer
603                         value = Integer.valueOf(amount);
604                 } catch (final NumberFormatException e) {
605                         // Bat input
606                         throw new IllegalArgumentException(e);
607                 }
608
609                 // Calculate price
610                 float price = (product.getPrice() * value);
611
612                 // Trace message
613                 this.getLogger().trace(MessageFormat.format("product={0},price={1} - EXIT!", product.getId(), price)); //NOI18N
614
615                 // Then multiply it with price
616                 return price;
617         }
618
619         /**
620          * Handler for amount from request or session
621          *
622          * @param product Product instance
623          * @param request Request instance
624          * @param session Session instance
625          * @return Amount as string
626          */
627         @Override
628         public String handleAmountFromRequestSession (final Product product, final HttpServletRequest request, final HttpSession session) {
629                 // Trace message
630                 this.getLogger().trace(MessageFormat.format("product={0},request={1},session={2} - CALLED!", product, request, session)); //NOI18N
631
632                 // Is product and session set?
633                 if (product == null) {
634                         // Not set
635                         throw new NullPointerException("product is null"); //NOI18N
636                 } else if (request == null) {
637                         // Not set
638                         throw new NullPointerException("request is null"); //NOI18N
639                 } else if (session == null) {
640                         // Not set
641                         throw new NullPointerException("session is null"); //NOI18N
642                 }
643
644                 // Init variabke
645                 Object object;
646
647                 // Check request method
648                 if (!"POST".equals(request.getMethod())) { //NOI18N
649                         // Not POST, so get from session
650                         return this.getAmountFromSession(product, session);
651                 } else if (this.handleChooseFromRequestSession(product, request, session).isEmpty()) {
652                         // Not choosen
653                         this.clearSessionAttribute(product, session, HTTP_PARAM_AMOUNT);
654                         this.getLogger().debug(MessageFormat.format("Unsetting for product={0} in session, returning zero ...", product.getId())); //NOI18N
655                         return "0"; //NOI18N
656                 }
657
658                 // Get attribute from request
659                 object = request.getParameter(String.format(HTTP_PARAM_MASK, HTTP_PARAM_AMOUNT, product.getId()));
660
661                 // Is it set?
662                 if (object instanceof String) {
663                         // Try to parse it to integer
664                         try {
665                                 Integer value = Integer.valueOf((String) object);
666                         } catch (final NumberFormatException ex) {
667                                 // Not valid input
668                                 this.getLogger().warn(ex);
669                                 return "0"; //NOI18N
670                         }
671
672                         // Then set it in session
673                         this.setValueInSession(product, session, HTTP_PARAM_AMOUNT, object);
674
675                         // And return it
676                         return (String) object;
677                 }
678
679                 // Trace message
680                 this.getLogger().trace("Calling getAmountFromSession() ..."); //NOI18N
681
682                 // Get attribute from session
683                 return this.getAmountFromSession(product, session);
684         }
685
686         /**
687          * Checks whether the given product is choosen, request overules session.
688          *
689          * @param product Product instance
690          * @param request Request instance
691          * @param session Session instance
692          * @return Whether the product is choosen
693          */
694         @Override
695         public boolean isProductChoosen (final Product product, final HttpServletRequest request, final HttpSession session) {
696                 // Trace message
697                 this.getLogger().trace(MessageFormat.format("product={0},request={1},session={2} - CALLED!", product, request, session)); //NOI18N
698
699                 // Is product and session set?
700                 if (product == null) {
701                         // Not set
702                         throw new NullPointerException("product is null"); //NOI18N
703                 } else if (request == null) {
704                         // Not set
705                         throw new NullPointerException("request is null"); //NOI18N
706                 } else if (session == null) {
707                         // Not set
708                         throw new NullPointerException("session is null"); //NOI18N
709                 }
710
711                 // Get choosen
712                 this.getLogger().debug(MessageFormat.format("Calling handleChooseFromRequestSession({0},{1},{2}) ...", product.getId(), request, session)); //NOI18N
713                 String choosen = this.handleChooseFromRequestSession(product, request, session);
714                 this.getLogger().debug(MessageFormat.format("product={0},choosen={1}", product.getId(), choosen)); //NOI18N
715
716                 // Must not be null
717                 assert(choosen instanceof String): "choosen is null"; //NOI18N
718
719                 // Is it not choosen?
720                 if (choosen.isEmpty()) {
721                         // Not choosen
722                         return false;
723                 }
724
725                 // Get amount
726                 String amount = this.handleAmountFromRequestSession(product, request, session);
727
728                 // Must not be null
729                 assert(amount instanceof String): "amount is not set"; //NOI18N
730
731                 // Trace message
732                 this.getLogger().trace(MessageFormat.format("amount={0} - EXIT!", amount)); //NOI18N
733
734                 // Must not be empty and not 0
735                 return (!amount.isEmpty() && !"0".equals(amount)); //NOI18N
736         }
737
738         /**
739          * Marks given product as choosen in session
740          *
741          * @param product Product to mark as ordered
742          * @param session Session instance
743          */
744         @Override
745         public void markProductAsChoosen (final Product product, final HttpSession session) {
746                 // Trace message
747                 this.getLogger().trace(MessageFormat.format("product={0},session={1} - CALLED!", product, session)); //NOI18N
748
749                 // Is product and session set?
750                 if (product == null) {
751                         // Not set
752                         throw new NullPointerException("product is null"); //NOI18N
753                 } else if (session == null) {
754                         // Not set
755                         throw new NullPointerException("session is null"); //NOI18N
756                 }
757
758                 // Mark it as ordered by setting flag
759                 this.getLogger().debug(MessageFormat.format("Marking product={0} as choosen.", product.getId())); //NOI18N
760                 this.setValueInSession(product, session, HTTP_PARAM_CHOOSE, "1"); //NOI18N
761
762                 // Trace message
763                 this.getLogger().trace("EXIT!"); //NOI18N
764         }
765
766         /**
767          * Marks given product as ordered in session
768          *
769          * @param product Product to mark as ordered
770          * @param session Session instance
771          */
772         @Override
773         public void markProductAsOrdered (final Product product, final HttpSession session) {
774                 // Trace message
775                 this.getLogger().trace(MessageFormat.format("product={0},session={1} - CALLED!", product, session)); //NOI18N
776
777                 // Is product and session set?
778                 if (product == null) {
779                         // Not set
780                         throw new NullPointerException("product is null"); //NOI18N
781                 } else if (session == null) {
782                         // Not set
783                         throw new NullPointerException("session is null"); //NOI18N
784                 }
785
786                 // Mark it as ordered by setting flag
787                 this.getLogger().debug(MessageFormat.format("Marking product={0} as ordered.", product.getId())); //NOI18N
788                 this.setValueInSession(product, session, SESSION_ORDERED, "true"); //NOI18N
789
790                 // Trace message
791                 this.getLogger().trace("EXIT!"); //NOI18N
792         }
793
794         /**
795          * Somewhat setter in session
796          *
797          * @param session Session instance
798          * @param key Session key to set
799          * @param value Value to set
800          */
801         @Override
802         public void setValueInSession (final HttpSession session, final String key, final Object value) {
803                 // Trace message
804                 this.getLogger().trace(MessageFormat.format("session={0},key={1},value={2} - CALLED!", session, key, value)); //NOI18N
805
806                 synchronized(session) {
807                         // Set it synced
808                         session.setAttribute(key, value);
809                 }
810
811                 // Trace message
812                 this.getLogger().trace("EXIT!"); //NOI18N
813         }
814
815         /**
816          * Unmarks given product as choosen in session
817          *
818          * @param product Product to unmark as choosen
819          * @param session Session instance
820          */
821         @Override
822         public void unmarkProductAsChoosen (final Product product, final HttpSession session) {
823                 // Trace message
824                 this.getLogger().trace(MessageFormat.format("product={0},session={1} - CALLED!", product, session)); //NOI18N
825
826                 // Is product and session set?
827                 if (product == null) {
828                         // Not set
829                         throw new NullPointerException("product is null"); //NOI18N
830                 } else if (session == null) {
831                         // Not set
832                         throw new NullPointerException("session is null"); //NOI18N
833                 }
834
835                 // Mark it as ordered by setting flag
836                 this.getLogger().debug(MessageFormat.format("Unmarking product={0} as choosen.", product.getId())); //NOI18N
837                 this.clearSessionAttribute(product, session, HTTP_PARAM_CHOOSE);
838
839                 // Trace message
840                 this.getLogger().trace("EXIT!"); //NOI18N
841         }
842
843         /**
844          * Unmarks given product as ordered in session
845          *
846          * @param product Product to unmark as ordered
847          * @param session Session instance
848          */
849         @Override
850         public void unmarkProductAsOrdered (final Product product, final HttpSession session) {
851                 // Trace message
852                 this.getLogger().trace(MessageFormat.format("product={0},session={1} - CALLED!", product, session)); //NOI18N
853
854                 // Is product and session set?
855                 if (product == null) {
856                         // Not set
857                         throw new NullPointerException("product is null"); //NOI18N
858                 } else if (session == null) {
859                         // Not set
860                         throw new NullPointerException("session is null"); //NOI18N
861                 }
862
863                 // Mark it as ordered by setting flag
864                 this.getLogger().debug(MessageFormat.format("Unmarking product={0} as ordered.", product.getId())); //NOI18N
865                 this.clearSessionAttribute(product, session, SESSION_ORDERED);
866
867                 // Trace message
868                 this.getLogger().trace("EXIT!"); //NOI18N
869         }
870
871         /**
872          * Clears given parameter for product in session
873          *
874          * @param product Product instance
875          * @param session Session instance
876          * @param parameter Parameter to clear
877          */
878         private void clearSessionAttribute (final Product product, final HttpSession session, final String parameter) {
879                 // Trace message
880                 this.getLogger().trace(MessageFormat.format("produce={0},parameter={1},session={2} - CALLED!", product, parameter, session)); //NOI18N
881
882                 // Clear in session
883                 this.getLogger().debug(MessageFormat.format("Clearing product={0},parameter={1} ...", product.getId(), parameter)); //NOI18N
884                 this.setValueInSession(product, session, parameter, null);
885
886                 // Trace message
887                 this.getLogger().trace("EXIT!"); //NOI18N
888         }
889
890         /**
891          * Some getter for value from session
892          *
893          * @param product Product instance
894          * @param session Session instance
895          * @param attribute Attribute to get value from
896          * @return Value from session
897          */
898         private Object getValueFromSession (final Product product, final HttpSession session, final String attribute) {
899                 // Trace message
900                 this.getLogger().trace(MessageFormat.format("product={0},session={1},attribute={2} - CALLED!", product, session, attribute)); //NOI18N
901
902                 // Init variable
903                 Object value = this.getValueFromSession(session, String.format(HTTP_PARAM_MASK, attribute, product.getId()));
904                 
905                 this.getLogger().debug(MessageFormat.format("product={0},attribute={1},value={2}", product.getId(), attribute, value)); //NOI18N
906
907                 // Trace message
908                 this.getLogger().trace(MessageFormat.format("value={0} - EXIT!", value)); //NOI18N
909
910                 // Return it
911                 return value;
912         }
913
914         /**
915          * Some getter for value from session
916          *
917          * @param session Session instance
918          * @param key Key to get value from
919          * @return Value from session
920          */
921         private Object getValueFromSession (final HttpSession session, final String key) {
922                 // Trace message
923                 this.getLogger().trace(MessageFormat.format("session={0},key={1} - CALLED!", session, key)); //NOI18N
924
925                 // Init value
926                 Object value = null;
927
928                 // Get it synchronized from session
929                 synchronized (session) {
930                         value = session.getAttribute(key);
931                 }
932
933                 // Trace message
934                 this.getLogger().trace(MessageFormat.format("value={0} - EXIT!", value)); //NOI18N
935
936                 // Return it
937                 return value;
938         }
939
940         /**
941          * Handler for choosen (checkbox) from request or session
942          *
943          * @param product Product instance
944          * @param request Request instance
945          * @param session Session instance
946          * @return Amount as string
947          */
948         private String handleChooseFromRequestSession(final Product product, final HttpServletRequest request, final HttpSession session) {
949                 // Trace message
950                 this.getLogger().trace(MessageFormat.format("product={0},request={1},session={2} - CALLED!", product, request, session)); //NOI18N
951
952                 // Is product and session set?
953                 if (product == null) {
954                         // Not set
955                         throw new NullPointerException("product is null"); //NOI18N
956                 } else if (request == null) {
957                         // Not set
958                         throw new NullPointerException("request is null"); //NOI18N
959                 } else if (session == null) {
960                         // Not set
961                         throw new NullPointerException("session is null"); //NOI18N
962                 }
963
964                 // Init variabke
965                 Object object;
966
967                 // Check request method
968                 if (!"POST".equals(request.getMethod())) { //NOI18N
969                         // Not POST, so get from session
970                         this.getLogger().trace(MessageFormat.format("Calling this.getChooseFromSession({0},{1}) ... - EXIT!", product.getId(), session));
971                         return this.getChooseFromSession(product, session);
972                 } else if (this.isProductOrdered(product, session)) {
973                         // Product is ordered
974                         this.getLogger().trace(MessageFormat.format("Calling this.getChooseFromSession({0},{1}) ... - EXIT!", product.getId(), session));
975                         return this.getChooseFromSession(product, session);
976                 } else if (!this.getChooseFromSession(product, session).isEmpty()) {
977                         // Found in session
978                         this.getLogger().trace(MessageFormat.format("Calling this.getChooseFromSession({0},{1}) ... - EXIT!", product.getId(), session));
979                         return this.getChooseFromSession(product, session);
980                 }
981
982                 // Get reqzest element
983                 object = request.getParameter(String.format(HTTP_PARAM_MASK, HTTP_PARAM_CHOOSE, product.getId()));
984                 this.getLogger().debug(MessageFormat.format("product={0},object={1}", product.getId(), object)); //NOI18N
985
986                 // Is it null?
987                 if (object == null) {
988                         // Unset session
989                         this.getLogger().debug(MessageFormat.format("Unsetting session for product={0} ...", product.getId())); //NOI18N
990                         this.clearSessionAttribute(product, session, HTTP_PARAM_CHOOSE);
991                         this.clearSessionAttribute(product, session, HTTP_PARAM_AMOUNT);
992
993                         // Return empty string
994                         return ""; //NOI18N
995                 }
996
997                 // Then set it in session
998                 this.setValueInSession(product, session, HTTP_PARAM_CHOOSE, object);
999
1000                 // Cast to string and return it
1001                 this.getLogger().debug(MessageFormat.format("product={0} - Returning {1} ...", product.getId(), object)); //NOI18N
1002                 return (String) object;
1003         }
1004
1005         /**
1006          * Initializes database frontends.
1007          */
1008         private void initDatabaseFrontends () throws UnsupportedDatabaseBackendException, SQLException {
1009                 this.productFrontend = new PizzaProductDatabaseFrontend();
1010         }
1011
1012         /**
1013          * Checks if the product ordered?
1014          *
1015          * @param product Product instance
1016          * @param session HttpSession instance
1017          * @return
1018          */
1019         private boolean isProductOrdered(final Product product, final HttpSession session) {
1020                 // Trace message
1021                 this.getLogger().trace(MessageFormat.format("product={0},session={1} - CALLED!", product, session)); //NOI18N
1022
1023                 // Get session
1024                 Object isOrdered = this.getValueFromSession(product, session, SESSION_ORDERED);
1025                 this.getLogger().debug(MessageFormat.format("product={0},isOrdered={1}", product.getId(), isOrdered)); //NOI18N
1026
1027                 // Return result
1028                 return ("true".equals(isOrdered)); //NOI18N
1029         }
1030
1031         /**
1032          * Somewhat setter in session
1033          *
1034          * @param product Product instance
1035          * @param session Session instance
1036          * @param keyPart Key part to include in final key
1037          * @param value Value to set
1038          */
1039         private void setValueInSession (final Product product, final HttpSession session, final String keyPart, final Object value) {
1040                 // Trace message
1041                 this.getLogger().trace(MessageFormat.format("product={0},session={1},keyPart={2},value={3} - CALLED!", product, session, keyPart, value)); //NOI18N
1042
1043                 // Set it synced
1044                 this.getLogger().debug(MessageFormat.format("Setting value={0} for product={1},keyPart={2}", value, product.getId(), keyPart)); //NOI18N
1045                 this.setValueInSession(session, String.format(HTTP_PARAM_MASK, keyPart, product.getId()), value);
1046
1047                 // Trace message
1048                 this.getLogger().trace("EXIT!"); //NOI18N
1049         }
1050         
1051         /**
1052          * Application starter
1053          */
1054         private void start () {
1055                 // Init bundle
1056                 this.initBundle();
1057
1058                 try {
1059                         // Init properties
1060                         this.initProperties();
1061                 } catch (final IOException ex) {
1062                         // Abort here
1063                         this.abortProgramWithException(ex);
1064                 }
1065
1066                 // Init instance
1067                 Iterator<Product> iterator = null;
1068
1069                 try {
1070                         // Get iterator
1071                         iterator = this.getProducts();
1072                 } catch (final IOException | BadTokenException ex) {
1073                         this.abortProgramWithException(ex);
1074                 }
1075
1076                 // "Walk" over all products
1077                 while ((iterator instanceof Iterator) && (iterator.hasNext())) {
1078                         // Get next product
1079                         Product product = iterator.next();
1080
1081                         // Output data
1082                         this.getLogger().debug(MessageFormat.format("Product {0}, {1}: {2}", product.getId(), product.getTitle(), product.getPrice())); //NOI18N
1083                 }
1084
1085                 // Generate fake Customer instance
1086                 Customer customer = new PizzaServiceCustomer();
1087
1088                 /*
1089                  * Need a least a gender ... :( See, that is why I don't like default
1090                  * constructors, you can easily miss something important and bam! You
1091                  * get an NPE. The fix here is, to have construtors (or factories) which
1092                  * requires all required instances that needs to be set to get a
1093                  * consitent object back.
1094                  */
1095
1096                 // Gender is MALE now
1097                 customer.setGender(Gender.MALE);
1098
1099                 // Init iterator
1100                 Iterator<Map.Entry<Field, Object>> it = null;
1101
1102                 try {
1103                         // Get iterator on all its fields
1104                         it = customer.iterator();
1105                 } catch (final NoSuchMethodException | IllegalAccessException | InvocationTargetException ex) {
1106                         this.abortProgramWithException(ex);
1107                 }
1108
1109                 // Output it
1110                 while ((it instanceof Iterator) && (it.hasNext())) {
1111                         Map.Entry<Field, Object> entry = it.next();
1112                         this.getLogger().debug(MessageFormat.format("entry {0}={1}", entry.getKey(), entry.getValue())); //NOI18N
1113                 }
1114         }
1115 }