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