2 * Copyright (C) 2015 Roland Haeder
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.
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.
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/>.
17 package org.mxchange.pizzaapplication.tags.basket;
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;
36 * A basket tag that outputs a small basket and a link to the full basket website.
38 * @author Roland Haeder
40 public class MiniBasketTag extends BodyTagSupport implements BasketTag {
44 private static final long serialVersionUID = 457415727452384L;
49 private BasketBean basket;
54 private final Logger LOG;
60 this.LOG = LogManager.getLogger(this);
64 * Outputs a div container with last added item + a link to the basket
67 * @return No need to process the body
68 * @throws JspException If anything happens
71 public int doStartTag () throws JspException {
73 this.LOG.trace("CALLED!");
76 StringBuilder out = new StringBuilder("<div class=\"mini_basket\">\n");
78 // basket should not be null
79 if (this.getBasket() == null) {
81 throw new NullPointerException("basket instance is null");
85 // Some entries found?
86 if (this.getBasket().isEmpty()) {
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");
93 AddableBasketItem item = this.getBasket().getLast();
95 // item cannot be null here
98 throw new NullPointerException("item is null");
101 // Get application bean from session
102 PizzaBean bean = (PizzaBean) this.getBasket().getSession().getAttribute("controller");
105 this.LOG.debug("bean=" + bean);
107 // Should not be null
110 throw new NullPointerException("bean is null");
113 // Get product instance
114 Product product = bean.getProduct(item);
117 this.LOG.debug("product=" + product);
120 int lastNumRows = this.getBasket().getLastNumRows();
123 this.LOG.debug("lastNumRows=" + lastNumRows);
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");
138 } catch (final ServletException | IOException | SQLException | BadTokenException | CorruptedDatabaseFileException | NoSuchMethodException | IllegalAccessException | InvocationTargetException ex) {
140 throw new JspException(ex);
144 out.append("</div>\n");
147 // Get output instance and write it
148 this.pageContext.getOut().print(out.toString());
149 } catch (final IOException ex) {
151 throw new JspException(ex);
155 this.LOG.trace("Returning " + SKIP_BODY + " ... - EXIT!");
157 // Don't process any body
165 public BasketBean getBasket () {
167 //* NOISY-DEBUG: */ this.LOG.trace("basket=" + this.basket + " - EXIT!");
174 * @param basket the basket to set
177 public void setBasket (final BasketBean basket) {
179 //* NOISY-DEBUG: */ this.LOG.trace("basked=" + basket + " - CALLED!");
182 this.basket = basket;