]> git.mxchange.org Git - pizzaservice-war.git/blob - src/java/org/mxchange/pizzaapplication/filter/servlet/basket/BasketItemAddedFilter.java
13209def9bcf44f1a7803cd929c1dc03a53616b6
[pizzaservice-war.git] / src / java / org / mxchange / pizzaapplication / filter / servlet / basket / BasketItemAddedFilter.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.filter.servlet.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.Filter;
24 import javax.servlet.FilterChain;
25 import javax.servlet.ServletException;
26 import javax.servlet.ServletRequest;
27 import javax.servlet.ServletResponse;
28 import javax.servlet.http.HttpServletRequest;
29 import javax.servlet.http.HttpSession;
30 import org.mxchange.jcore.exceptions.BadTokenException;
31 import org.mxchange.jcore.exceptions.CorruptedDatabaseFileException;
32 import org.mxchange.jshop.beans.basket.BasketBean;
33 import org.mxchange.jshop.item.AddableBasketItem;
34 import org.mxchange.pizzaapplication.beans.controller.PizzaBean;
35 import org.mxchange.pizzaapplication.filter.servlet.BaseServletFilter;
36
37 /**
38  * A filter for handling added basket items
39  *
40  * @author Roland Haeder
41  */
42 public class BasketItemAddedFilter extends BaseServletFilter implements Filter {
43
44         @Override
45         @SuppressWarnings ("unchecked")
46         public void doFilter (final ServletRequest request, final ServletResponse response, final FilterChain chain) throws IOException, ServletException {
47                 // Trace message
48                 this.getLogger().trace(MessageFormat.format("request={0},response={1},chain={2} - CALLED!", request, response, chain)); //NOI18N
49
50                 // All must be set
51                 if (null == request) {
52                         // request is null
53                         throw new NullPointerException("request is null"); //NOI18N
54                 } else if (null == response) {
55                         // response is null
56                         throw new NullPointerException("response is null"); //NOI18N
57                 } else if (null == chain) {
58                         // chain is null
59                         throw new NullPointerException("chain is null"); //NOI18N
60                 }
61
62                 // Call doFilter to move on
63                 chain.doFilter(request, response);
64
65                 // Get item instance from request
66                 Object object = request.getAttribute("item"); //NOI18N
67
68                 // Debug message
69                 this.getLogger().debug(MessageFormat.format("object={0}", object)); //NOI18N
70
71                 // item should not be null
72                 if (null == object) {
73                         // item is null
74                         throw new NullPointerException("item is null"); //NOI18N
75                 } else if (!(object instanceof AddableBasketItem)) {
76                         // Not right instance
77                         throw new IllegalArgumentException("item does not implement AddableBasketItem"); //NOI18N
78                 }
79
80                 // Now it is secure to cast
81                 AddableBasketItem item = (AddableBasketItem) object;
82
83                 // Debug message
84                 this.getLogger().debug(MessageFormat.format("item.id={0},item.itemId={1},item.itemType={2},item.amount={3}", item.getId(), item.getItemId(), item.getItemType(), item.getAmount())); //NOI18N
85
86                 // Init instance
87                 BasketBean basket;
88                 try {
89                         // Get session instance
90                         HttpSession session = ((HttpServletRequest) request).getSession();
91
92                         // Debug message
93                         this.getLogger().debug(MessageFormat.format("session={0}", session)); //NOI18N
94
95                         // Should not be null
96                         if (null == session) {
97                                 // session is null
98                                 throw new NullPointerException("session is null"); //NOI18N
99                         }
100
101                         // Get man controller
102                         PizzaBean bean = (PizzaBean) session.getAttribute("controller"); //NOI18N
103
104                         // Debug message
105                         this.getLogger().debug("bean=" + bean);
106
107                         // Get basket instance
108                         basket = bean.getBasket();
109
110                         // Debug message
111                         this.getLogger().debug("basket=" + basket);
112
113                         // Is the item already added?
114                         if (item.getItemId() == null) {
115                                 // Item id is not set
116                                 throw new NullPointerException(MessageFormat.format("item id of item={0} is null", item)); //NOI18N
117                         } else if (item.getItemType() == null) {
118                                 // Item type is not set
119                                 throw new NullPointerException(MessageFormat.format("item type of item={0} is null", item)); //NOI18N
120                         } else if ((item.getAmount() == null) || (item.getAmount() == 0)) {
121                                 // Debug message
122                                 this.getLogger().debug(MessageFormat.format("Amount for item {0} is null", item)); //NOI18N
123
124                                 // Amount is not entered
125                                 return;
126                         } else if (basket.isItemAdded(item)) {
127                                 // Yes, then throw exception here
128                                 throw new ServletException(MessageFormat.format("item id={0} has already been added.", item.getItemId())); //NOI18N
129                         }
130
131                         // Register item with it
132                         basket.addItem(item);
133                 } catch (final SQLException | BadTokenException | CorruptedDatabaseFileException | NoSuchMethodException | IllegalAccessException | InvocationTargetException ex) {
134                         // Continue to throw
135                         throw new ServletException(ex);
136                 }
137
138                 // Trace message
139                 this.getLogger().trace("EXIT!"); //NOI18N
140         }
141
142 }