| 410 |
rajveer |
1 |
package in.shop2020.serving.controllers;
|
|
|
2 |
|
|
|
3 |
import in.shop2020.model.v1.shoppingcart.Cart;
|
|
|
4 |
import in.shop2020.model.v1.shoppingcart.ShoppingCartException;
|
|
|
5 |
import in.shop2020.serving.controllers.BaseController;
|
|
|
6 |
import in.shop2020.serving.pages.PageContentKeys;
|
|
|
7 |
import in.shop2020.serving.pages.PageEnum;
|
|
|
8 |
import in.shop2020.serving.pages.PageManager;
|
| 419 |
rajveer |
9 |
import in.shop2020.serving.utils.Utils;
|
| 410 |
rajveer |
10 |
import in.shop2020.thrift.clients.ShoppingCartClient;
|
|
|
11 |
import in.shop2020.thrift.clients.UserContextServiceClient;
|
|
|
12 |
|
|
|
13 |
import java.util.*;
|
|
|
14 |
|
|
|
15 |
import javax.servlet.http.Cookie;
|
|
|
16 |
import javax.servlet.http.HttpServletRequest;
|
|
|
17 |
|
|
|
18 |
import org.apache.commons.lang.StringUtils;
|
|
|
19 |
import org.apache.juli.logging.Log;
|
|
|
20 |
import org.apache.juli.logging.LogFactory;
|
|
|
21 |
import org.apache.struts2.convention.annotation.Result;
|
|
|
22 |
import org.apache.struts2.convention.annotation.Results;
|
|
|
23 |
import org.apache.struts2.interceptor.ParameterAware;
|
|
|
24 |
import org.apache.struts2.interceptor.ServletRequestAware;
|
|
|
25 |
import org.apache.struts2.rest.DefaultHttpHeaders;
|
|
|
26 |
import org.apache.struts2.rest.HttpHeaders;
|
|
|
27 |
import org.apache.thrift.TException;
|
|
|
28 |
|
|
|
29 |
import com.opensymphony.xwork2.ValidationAwareSupport;
|
|
|
30 |
|
|
|
31 |
@Results({
|
|
|
32 |
@Result(name="success", type="redirectAction",
|
| 419 |
rajveer |
33 |
params = {"actionName" , "cart"}),
|
|
|
34 |
@Result(name="redirect", location="${url}", type="redirect")
|
| 410 |
rajveer |
35 |
})
|
|
|
36 |
|
| 416 |
rajveer |
37 |
public class CartController extends BaseController implements ParameterAware {
|
| 410 |
rajveer |
38 |
|
|
|
39 |
private static final long serialVersionUID = 1L;
|
|
|
40 |
|
|
|
41 |
private Map<String, String[]> reqparams;
|
|
|
42 |
private Map<String, Object> sessions;
|
|
|
43 |
private String redirectURL;
|
|
|
44 |
private static Log log = LogFactory.getLog(CartController.class);
|
|
|
45 |
private String itemId;
|
| 416 |
rajveer |
46 |
//HttpServletRequest request;
|
| 410 |
rajveer |
47 |
private Map<String,String> htmlSnippets;
|
|
|
48 |
|
|
|
49 |
public CartController(){
|
|
|
50 |
|
|
|
51 |
}
|
|
|
52 |
|
|
|
53 |
|
| 419 |
rajveer |
54 |
// GET /cart
|
| 410 |
rajveer |
55 |
public HttpHeaders index() {
|
| 419 |
rajveer |
56 |
/* changed on 18 august
|
| 410 |
rajveer |
57 |
Cookie loginCookie = new Cookie("userId", "Rajveer");
|
|
|
58 |
Map cookiesMap = new HashMap();
|
|
|
59 |
cookiesMap.put("USER_ID", loginCookie);
|
|
|
60 |
setCookiesMap(cookiesMap);
|
|
|
61 |
|
|
|
62 |
log.info( "Cookies map is " + this.getCookiesMap());
|
|
|
63 |
log.info("CartController.index");
|
|
|
64 |
|
|
|
65 |
Map<PageContentKeys, String> params = new HashMap<PageContentKeys, String>();
|
|
|
66 |
|
| 416 |
rajveer |
67 |
long userId = 0;
|
|
|
68 |
boolean isSessionId = true;
|
| 419 |
rajveer |
69 |
//if(this.session.getAttribute("loggedin")!=null){
|
|
|
70 |
if(this.session.getAttribute("loggedin").toString().equals("true")){
|
|
|
71 |
userId = Long.parseLong(this.session.getAttribute("userid").toString());
|
|
|
72 |
isSessionId = false;
|
|
|
73 |
}else{
|
|
|
74 |
userId = Long.parseLong(this.session.getAttribute("userid").toString());
|
|
|
75 |
isSessionId = true;
|
|
|
76 |
}
|
|
|
77 |
// }else{
|
|
|
78 |
// System.out.println("No User is logged in. Redirect to login page.");
|
|
|
79 |
// return new DefaultHttpHeaders("fatal");
|
|
|
80 |
//
|
|
|
81 |
// }
|
|
|
82 |
|
|
|
83 |
|
|
|
84 |
|
|
|
85 |
params.put(PageContentKeys.CUSTOMER_ID, userId+"");
|
|
|
86 |
params.put(PageContentKeys.IS_SESSION_ID, isSessionId+"");
|
|
|
87 |
*/
|
|
|
88 |
|
|
|
89 |
long userId = 0;
|
|
|
90 |
boolean isSessionId = true;
|
|
|
91 |
Map<PageContentKeys, String> params = new HashMap<PageContentKeys, String>();
|
| 416 |
rajveer |
92 |
|
| 419 |
rajveer |
93 |
if(userinfo.isLoggedIn()){
|
|
|
94 |
userId = userinfo.getUserId();
|
|
|
95 |
isSessionId = false;
|
|
|
96 |
}
|
|
|
97 |
else{
|
|
|
98 |
userId = userinfo.getSessionId();
|
|
|
99 |
isSessionId = true;
|
|
|
100 |
}
|
|
|
101 |
|
| 416 |
rajveer |
102 |
params.put(PageContentKeys.CUSTOMER_ID, userId+"");
|
|
|
103 |
params.put(PageContentKeys.IS_SESSION_ID, isSessionId+"");
|
|
|
104 |
|
| 410 |
rajveer |
105 |
htmlSnippets = PageManager.getPageManager().getPageContents(PageEnum.SHOPPING_CART_PAGE, params);
|
|
|
106 |
|
|
|
107 |
|
|
|
108 |
|
|
|
109 |
return new DefaultHttpHeaders("index").disableCaching();
|
|
|
110 |
}
|
|
|
111 |
|
|
|
112 |
// POST /entity
|
|
|
113 |
public String create() {
|
| 419 |
rajveer |
114 |
/*
|
| 410 |
rajveer |
115 |
log.info("CartController.create");
|
|
|
116 |
printParams();
|
|
|
117 |
if(getCookiesMap() != null){
|
|
|
118 |
Cookie loginCookie = (Cookie)getCookiesMap().get("USER_ID");
|
|
|
119 |
log.info("login cookie name is " + loginCookie.getName() );
|
|
|
120 |
log.info("login cookie value is " + loginCookie.getValue());
|
|
|
121 |
}
|
|
|
122 |
log.info("item id is " + this.reqparams.get("item_id"));
|
|
|
123 |
itemId = this.reqparams.get("item_id")[0];
|
|
|
124 |
|
|
|
125 |
addItemToCart(itemId);
|
| 416 |
rajveer |
126 |
long userId = 0;
|
|
|
127 |
boolean isSessionId = true;
|
|
|
128 |
if(this.session.getAttribute("loggedin").toString().equals("true")){
|
|
|
129 |
userId = Long.parseLong(this.session.getAttribute("userid").toString());
|
|
|
130 |
isSessionId = false;
|
|
|
131 |
}else{
|
|
|
132 |
userId = Long.parseLong(this.session.getAttribute("userid").toString());
|
|
|
133 |
isSessionId = true;
|
|
|
134 |
}
|
|
|
135 |
addItemToCart(Long.parseLong(itemId), userId, isSessionId);
|
| 410 |
rajveer |
136 |
// Add data to the cart
|
| 419 |
rajveer |
137 |
*/
|
|
|
138 |
long userId = 0;
|
|
|
139 |
boolean isSessionId = true;
|
| 410 |
rajveer |
140 |
|
| 419 |
rajveer |
141 |
log.info("item id is " + this.reqparams.get("item_id"));
|
|
|
142 |
itemId = this.reqparams.get("item_id")[0];
|
|
|
143 |
|
|
|
144 |
if(userinfo.isLoggedIn()){
|
|
|
145 |
userId = userinfo.getUserId();
|
|
|
146 |
isSessionId = false;
|
|
|
147 |
}
|
|
|
148 |
else{
|
|
|
149 |
userId = userinfo.getSessionId();
|
|
|
150 |
isSessionId = true;
|
|
|
151 |
}
|
|
|
152 |
|
|
|
153 |
long cartId = Utils.addItemToCart(Long.parseLong(itemId), userId, isSessionId);
|
|
|
154 |
|
|
|
155 |
userinfo.setCartId(cartId);
|
|
|
156 |
userinfo.setTotalItems(userinfo.getTotalItems() + 1 );
|
|
|
157 |
|
|
|
158 |
this.session.setAttribute("userinfo", userinfo);
|
|
|
159 |
|
| 410 |
rajveer |
160 |
return "success";
|
|
|
161 |
}
|
|
|
162 |
|
|
|
163 |
public void printParams(){
|
|
|
164 |
for(String param : reqparams.keySet()) {
|
|
|
165 |
log.info("param name is " + param);
|
|
|
166 |
log.info("param first is " + reqparams.get(param)[0]);
|
|
|
167 |
}
|
|
|
168 |
log.info(this.reqparams);
|
|
|
169 |
}
|
|
|
170 |
|
|
|
171 |
private boolean addItemToCart(String itemId){
|
|
|
172 |
log.info("Session Id is " + request.getSession().getId());
|
|
|
173 |
log.info("Item Id is " + itemId);
|
|
|
174 |
return true;
|
|
|
175 |
}
|
|
|
176 |
|
|
|
177 |
|
| 416 |
rajveer |
178 |
private void addItemToCart(long catalogItemId, long userId, boolean isSessionId){
|
| 410 |
rajveer |
179 |
UserContextServiceClient userContextServiceClient = null;
|
|
|
180 |
in.shop2020.model.v1.user.UserContextService.Client userClient = null;
|
|
|
181 |
|
|
|
182 |
ShoppingCartClient shoppingCartClient = null;
|
|
|
183 |
in.shop2020.model.v1.shoppingcart.ShoppingCartService.Client cartClient = null;
|
|
|
184 |
long cartId = -1;
|
|
|
185 |
|
|
|
186 |
try {
|
|
|
187 |
userContextServiceClient = new UserContextServiceClient();
|
|
|
188 |
userClient = userContextServiceClient.getClient();
|
|
|
189 |
|
|
|
190 |
shoppingCartClient = new ShoppingCartClient();
|
|
|
191 |
cartClient = shoppingCartClient.getClient();
|
| 416 |
rajveer |
192 |
|
|
|
193 |
cartId = cartClient.createCart(userId, isSessionId);
|
| 410 |
rajveer |
194 |
cartClient.addItemToCart(cartId, catalogItemId, 1);
|
| 419 |
rajveer |
195 |
|
|
|
196 |
int NumberOfLines = cartClient.getShadowCart(cartId).getLinesSize();
|
|
|
197 |
this.session.setAttribute("totalitems", NumberOfLines+"");
|
|
|
198 |
|
|
|
199 |
|
| 410 |
rajveer |
200 |
} catch (ShoppingCartException e) {
|
|
|
201 |
// TODO Auto-generated catch block
|
|
|
202 |
e.printStackTrace();
|
|
|
203 |
} catch (TException e) {
|
|
|
204 |
// TODO Auto-generated catch block
|
|
|
205 |
e.printStackTrace();
|
|
|
206 |
} catch (Exception e) {
|
|
|
207 |
// TODO Auto-generated catch block
|
|
|
208 |
e.printStackTrace();
|
|
|
209 |
}
|
|
|
210 |
|
|
|
211 |
//if user is logged in create new cart
|
|
|
212 |
//if( userClient.getState(userId, false).isIsLoggedIn()){
|
|
|
213 |
}
|
|
|
214 |
|
|
|
215 |
public String getShoppingCartSnippets()
|
|
|
216 |
{
|
|
|
217 |
return htmlSnippets.get("SHOPPING_CART");
|
|
|
218 |
}
|
|
|
219 |
|
|
|
220 |
@Override
|
|
|
221 |
public void setParameters(Map<String, String[]> reqmap) {
|
|
|
222 |
log.info("setParameters:" + reqmap);
|
|
|
223 |
|
|
|
224 |
this.reqparams = reqmap;
|
|
|
225 |
}
|
| 416 |
rajveer |
226 |
//
|
|
|
227 |
// @Override
|
|
|
228 |
// public void setServletRequest(HttpServletRequest request) {
|
|
|
229 |
// this.request = request;
|
|
|
230 |
// }
|
|
|
231 |
//
|
| 410 |
rajveer |
232 |
}
|