]> git.mxchange.org Git - pizzaservice-war.git/blob - src/java/org/mxchange/pizzaapplication/filter/servlet/basket/BasketItemAddedFilter.java
Get the basket bean directly from session scope
[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.HttpServletResponse;
30 import javax.servlet.http.HttpSession;
31 import org.mxchange.jcore.exceptions.BadTokenException;
32 import org.mxchange.jcore.exceptions.CorruptedDatabaseFileException;
33 import org.mxchange.jshop.beans.basket.BasketBean;
34 import org.mxchange.jshop.item.AddableBasketItem;
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("object is null"); //NOI18N
75                 } else if (!(object instanceof AddableBasketItem)) {
76                         // Not right instance
77                         throw new IllegalArgumentException("object 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                 try {
87                         // Cast to servlet request
88                         HttpServletRequest servletRequest = (HttpServletRequest) request;
89
90                         // Get session instance
91                         HttpSession session = servletRequest.getSession();
92
93                         // Debug message
94                         this.getLogger().debug(MessageFormat.format("session={0}", session)); //NOI18N
95
96                         // Should not be null
97                         if (null == session) {
98                                 // session is null
99                                 throw new NullPointerException("session is null"); //NOI18N
100                         }
101
102                         // Get basket instance
103                         BasketBean basket = (BasketBean) session.getAttribute("basket"); //NOI18N
104
105                         // Debug message
106                         this.getLogger().debug(MessageFormat.format("basket={0}", basket)); //NOI18N
107
108                         // Is the item already added?
109                         if (item.getItemId() == null) {
110                                 // Item id is not set
111                                 throw new NullPointerException(MessageFormat.format("item id of item={0} is null", item)); //NOI18N
112                         } else if (item.getItemType() == null) {
113                                 // Item type is not set
114                                 throw new NullPointerException(MessageFormat.format("item type of item={0} is null", item)); //NOI18N
115                         } else if ((item.getAmount() == null) || (item.getAmount() == 0)) {
116                                 // Debug message
117                                 this.getLogger().debug(MessageFormat.format("Amount for item {0} is null - EXIT!", item)); //NOI18N
118
119                                 // Amount is not entered
120                                 return;
121                         } else if (basket.isItemAdded(item)) {
122                                 // Yes, then throw exception here
123                                 throw new ServletException(MessageFormat.format("item id={0} has already been added.", item.getItemId())); //NOI18N
124                         }
125
126                         // Register item with it
127                         basket.addItem(item);
128
129                         // Is amount null or zero?
130                         if ((item.getAmount() == null) || (item.getAmount() == 0)) {
131                                 // Then redirect to added=0
132                                 ((HttpServletResponse) response).sendRedirect(servletRequest.getContextPath() + "/?add=0"); //NOI18N
133                         } else {
134                                 // Redirect to proper URL
135                                 ((HttpServletResponse) response).sendRedirect(servletRequest.getContextPath() + "/?add=1"); //NOI18N
136                         }
137                 } catch (final SQLException | BadTokenException | CorruptedDatabaseFileException | NoSuchMethodException | IllegalAccessException | InvocationTargetException ex) {
138                         // Continue to throw
139                         throw new ServletException(ex);
140                 }
141
142                 // Trace message
143                 this.getLogger().trace("EXIT!"); //NOI18N
144         }
145
146 }