]> git.mxchange.org Git - pizzaservice-war.git/blob - src/java/org/mxchange/pizzaapplication/tags/basket/MiniBasketTag.java
Continued with project:
[pizzaservice-war.git] / src / java / org / mxchange / pizzaapplication / tags / basket / MiniBasketTag.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.tags.basket;
18
19 import java.io.IOException;
20 import java.lang.reflect.InvocationTargetException;
21 import java.sql.SQLException;
22 import java.text.MessageFormat;
23 import javax.servlet.ServletException;
24 import javax.servlet.jsp.JspException;
25 import javax.servlet.jsp.tagext.BodyTagSupport;
26 import org.apache.logging.log4j.LogManager;
27 import org.apache.logging.log4j.Logger;
28 import org.mxchange.jcore.exceptions.BadTokenException;
29 import org.mxchange.jcore.exceptions.CorruptedDatabaseFileException;
30 import org.mxchange.pizzaapplication.beans.PizzaBean;
31 import org.mxchange.pizzaapplication.beans.basket.BasketBean;
32 import org.mxchange.pizzaapplication.item.AddableBasketItem;
33 import org.mxchange.pizzaapplication.product.Product;
34
35 /**
36  * A basket tag that outputs a small basket and a link to the full basket website.
37  *
38  * @author Roland Haeder
39  */
40 public class MiniBasketTag extends BodyTagSupport implements BasketTag {
41         /**
42          * Serial number
43          */
44         private static final long serialVersionUID = 457415727452384L;
45
46         /**
47          * Basket instance
48          */
49         private BasketBean basket;
50
51         /**
52          * Logger instance
53          */
54         private final Logger LOG;
55
56         /**
57          * Initializer
58          */
59         {
60                 this.LOG = LogManager.getLogger(this);
61         }
62
63         /**
64          * Outputs a div container with last added item + a link to the basket
65          * web page.
66          *
67          * @return No need to process the body
68          * @throws JspException If anything happens
69          */
70         @Override
71         public int doStartTag () throws JspException {
72                 // Trace message
73                 this.LOG.trace("CALLED!");
74
75                 // Init output
76                 StringBuilder out = new StringBuilder("<div class=\"mini_basket\">\n");
77
78                 // basket should not be null
79                 if (this.getBasket() == null) {
80                         // Not set
81                         throw new NullPointerException("basket instance is null");
82                 }
83
84                 try {
85                         // Some entries found?
86                         if (this.getBasket().isEmpty()) {
87                                 // Empty basket
88                                 out.append("<div class=\"mini_basket_box\">\n");
89                                 out.append(this.getBasket().getMessageStringFromKey("MiniBasketTag.basket_is_empty")).append("\n");
90                                 out.append("</div>\n");
91                         } else {
92                                 // Get all items
93                                 AddableBasketItem item = this.getBasket().getLast();
94
95                                 // item cannot be null here
96                                 if (null == item) {
97                                         // Abort here
98                                         throw new NullPointerException("item is null");
99                                 }
100
101                                 // Get application bean from session
102                                 PizzaBean bean = (PizzaBean) this.getBasket().getSession().getAttribute("controller");
103
104                                 // Debug log
105                                 this.LOG.debug("bean=" + bean);
106
107                                 // Should not be null
108                                 if (null == bean) {
109                                         // Abort here
110                                         throw new NullPointerException("bean is null");
111                                 }
112
113                                 // Get product instance
114                                 Product product = bean.getProduct(item);
115
116                                 // Debug message
117                                 this.LOG.debug("product=" + product);
118
119                                 // Get last num rows
120                                 int lastNumRows = this.getBasket().getLastNumRows();
121
122                                 // Debug message
123                                 this.LOG.debug("lastNumRows=" + lastNumRows);
124
125                                 // Output all
126                                 out.append("<div class=\"mini_basket_box\">\n");
127                                 out.append("  <div class=\"mini_basket_last\">\n");
128                                 out.append("    ").append(MessageFormat.format(this.getBasket().getMessageStringFromKey("MiniBasketTag.last_item"), product.getTitle()));
129                                 out.append("  </div>\n");
130                                 out.append("  <div class=\"mini_basket_more\">\n");
131                                 out.append("    ").append(MessageFormat.format(this.getBasket().getMessageStringFromKey("MiniBasketTag.additional_items"), (lastNumRows - 1)));
132                                 out.append("  </div>\n");
133                                 out.append("  <div class=\"mini_basket_link\">\n");
134                                 out.append("    <a href=\"").append(this.getBasket().getApplication().getContextPath()).append("/basket.jsp\">").append(this.getBasket().getMessageStringFromKey("MiniBasketTag.to_basket")).append("</a>\n");
135                                 out.append("  </div>\n");
136                                 out.append("</div>\n");
137                         }
138                 } catch (final ServletException | IOException | SQLException | BadTokenException | CorruptedDatabaseFileException | NoSuchMethodException | IllegalAccessException | InvocationTargetException ex) {
139                         // Continue to throw
140                         throw new JspException(ex);
141                 }
142
143                 // Finish output
144                 out.append("</div>\n");
145
146                 try {
147                         // Get output instance and write it
148                         this.pageContext.getOut().print(out.toString());
149                 } catch (final IOException ex) {
150                         // Continue to throw
151                         throw new JspException(ex);
152                 }
153
154                 // Trace message
155                 this.LOG.trace("Returning " + SKIP_BODY + " ... - EXIT!");
156
157                 // Don't process any body
158                 return SKIP_BODY;
159         }
160
161         /**
162          * @return the basket
163          */
164         @Override
165         public BasketBean getBasket () {
166                 // Trace message
167                 //* NOISY-DEBUG: */ this.LOG.trace("basket=" + this.basket + " - EXIT!");
168
169                 // Return it
170                 return this.basket;
171         }
172
173         /**
174          * @param basket the basket to set
175          */
176         @Override
177         public void setBasket (final BasketBean basket) {
178                 // Trace message
179                 //* NOISY-DEBUG: */ this.LOG.trace("basked=" + basket + " - CALLED!");
180
181                 // Set it here
182                 this.basket = basket;
183         }
184 }