| 410 |
rajveer |
1 |
package in.shop2020.serving.controllers;
|
|
|
2 |
|
| 555 |
chandransh |
3 |
import in.shop2020.model.v1.user.ShoppingCartException;
|
| 572 |
chandransh |
4 |
import in.shop2020.model.v1.user.UserContextService;
|
| 410 |
rajveer |
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.UserContextServiceClient;
|
|
|
11 |
|
|
|
12 |
import java.util.*;
|
|
|
13 |
|
|
|
14 |
import org.apache.juli.logging.Log;
|
|
|
15 |
import org.apache.juli.logging.LogFactory;
|
|
|
16 |
import org.apache.struts2.interceptor.ParameterAware;
|
|
|
17 |
import org.apache.struts2.rest.DefaultHttpHeaders;
|
|
|
18 |
import org.apache.struts2.rest.HttpHeaders;
|
|
|
19 |
import org.apache.thrift.TException;
|
|
|
20 |
|
| 507 |
rajveer |
21 |
public class CartController extends BaseController implements ParameterAware{
|
| 410 |
rajveer |
22 |
|
|
|
23 |
private static final long serialVersionUID = 1L;
|
| 507 |
rajveer |
24 |
private static Log log = LogFactory.getLog(CartController.class);
|
|
|
25 |
Map<String, String[]> reqparams = null;
|
| 410 |
rajveer |
26 |
|
|
|
27 |
private Map<String,String> htmlSnippets;
|
| 572 |
chandransh |
28 |
private String errorMsg = "";
|
| 410 |
rajveer |
29 |
|
|
|
30 |
public CartController(){
|
| 507 |
rajveer |
31 |
super();
|
|
|
32 |
}
|
|
|
33 |
|
|
|
34 |
// GET /cart
|
|
|
35 |
public HttpHeaders index() {
|
| 555 |
chandransh |
36 |
long userId = userinfo.getUserId();
|
| 572 |
chandransh |
37 |
try {
|
|
|
38 |
UserContextService.Client userClient = (new UserContextServiceClient()).getClient();
|
|
|
39 |
if(!userClient.validateCart(userinfo.getCartId()))
|
|
|
40 |
errorMsg = "Your cart has been updated.";
|
|
|
41 |
} catch (Exception e) {
|
|
|
42 |
// This exception can be ignored for showing the cart. Not so
|
|
|
43 |
// innocent when this occurs at the time of checkout or when the
|
|
|
44 |
// user is proceeding to pay.
|
|
|
45 |
e.printStackTrace();
|
|
|
46 |
}
|
| 507 |
rajveer |
47 |
Map<PageContentKeys, String> params = new HashMap<PageContentKeys, String>();
|
| 410 |
rajveer |
48 |
|
| 507 |
rajveer |
49 |
params.put(PageContentKeys.CUSTOMER_ID, userId+"");
|
| 555 |
chandransh |
50 |
params.put(PageContentKeys.IS_LOGGED_IN, userinfo.isLoggedIn()+"");
|
| 572 |
chandransh |
51 |
params.put(PageContentKeys.CART_ID, userinfo.getCartId()+"");
|
| 507 |
rajveer |
52 |
params.put(PageContentKeys.ITEM_COUNT, userinfo.getTotalItems()+"");
|
| 517 |
rajveer |
53 |
params.put(PageContentKeys.USER_NAME, userinfo.getNameOfUser());
|
| 572 |
chandransh |
54 |
params.put(PageContentKeys.ERROR_MSG, errorMsg);
|
| 507 |
rajveer |
55 |
htmlSnippets = PageManager.getPageManager().getPageContents(PageEnum.SHOPPING_CART_PAGE, params);
|
|
|
56 |
|
|
|
57 |
return new DefaultHttpHeaders("index").disableCaching();
|
|
|
58 |
}
|
|
|
59 |
|
| 572 |
chandransh |
60 |
// POST /entity
|
|
|
61 |
public String create() {
|
|
|
62 |
log.info("CartController.create");
|
| 555 |
chandransh |
63 |
|
| 572 |
chandransh |
64 |
printParams();
|
|
|
65 |
|
|
|
66 |
long userId = userinfo.getUserId();
|
|
|
67 |
long cartId = userinfo.getCartId();
|
|
|
68 |
|
|
|
69 |
log.info("item id is " + this.reqparams.get("productid"));
|
|
|
70 |
|
|
|
71 |
String itemIds = "1000008_1000005";
|
|
|
72 |
if (this.reqparams.get("productid") != null) {
|
|
|
73 |
itemIds = this.reqparams.get("productid")[0];
|
|
|
74 |
}
|
|
|
75 |
|
|
|
76 |
StringTokenizer tokenizer = new StringTokenizer(itemIds, "_");
|
|
|
77 |
while (tokenizer.hasMoreTokens()) {
|
|
|
78 |
long itemId = Long.parseLong(tokenizer.nextToken());
|
|
|
79 |
|
|
|
80 |
try {
|
|
|
81 |
UserContextServiceClient userServiceClient = new UserContextServiceClient();
|
|
|
82 |
UserContextService.Client userClient = userServiceClient.getClient();
|
|
|
83 |
if (cartId == 0)
|
| 555 |
chandransh |
84 |
cartId = userClient.createCart(userId);
|
| 572 |
chandransh |
85 |
userClient.addItemToCart(cartId, itemId, 1);
|
| 536 |
rajveer |
86 |
|
| 572 |
chandransh |
87 |
} catch (ShoppingCartException e) {
|
|
|
88 |
e.printStackTrace();
|
|
|
89 |
} catch (TException e) {
|
|
|
90 |
e.printStackTrace();
|
|
|
91 |
} catch (Exception e) {
|
|
|
92 |
e.printStackTrace();
|
| 507 |
rajveer |
93 |
}
|
|
|
94 |
|
| 572 |
chandransh |
95 |
}
|
| 507 |
rajveer |
96 |
|
| 572 |
chandransh |
97 |
userinfo.setCartId(cartId);
|
|
|
98 |
userinfo.setTotalItems(Utils.getNumberOfItemsInCart(cartId));
|
|
|
99 |
|
|
|
100 |
return "success";
|
|
|
101 |
}
|
|
|
102 |
|
| 507 |
rajveer |
103 |
// DELETE /entity
|
|
|
104 |
public String destroy() {
|
|
|
105 |
log.info("CartController.destroy");
|
|
|
106 |
printParams();
|
|
|
107 |
log.info("item id is " + this.request.getParameter("productid"));
|
|
|
108 |
String itemIdString = this.request.getParameter("productid");
|
|
|
109 |
long itemId = Long.parseLong(itemIdString);
|
| 517 |
rajveer |
110 |
if(userinfo.getCartId() == -1){
|
|
|
111 |
log.info("Cart does not exist. Nothing to delete.");
|
|
|
112 |
}else{
|
|
|
113 |
if(Utils.deleteItemFromCart(userinfo.getCartId(), itemId, userinfo.getUserId(), userinfo.isSessionId())){
|
|
|
114 |
userinfo.setTotalItems(userinfo.getTotalItems() - 1 );
|
|
|
115 |
return "delsuccess";
|
|
|
116 |
}
|
| 507 |
rajveer |
117 |
}
|
|
|
118 |
return "delfailure";
|
|
|
119 |
}
|
|
|
120 |
|
|
|
121 |
|
|
|
122 |
// DELETE /entity
|
|
|
123 |
public String update() {
|
|
|
124 |
log.info("CartController.update");
|
|
|
125 |
printParams();
|
|
|
126 |
log.info("item id is " + this.request.getParameter("productid"));
|
|
|
127 |
log.info("item id is " + this.request.getParameter("quantity"));
|
|
|
128 |
String itemIdString = this.request.getParameter("productid");
|
|
|
129 |
String quantityString = this.request.getParameter("quantity");
|
|
|
130 |
long itemId = Long.parseLong(itemIdString);
|
|
|
131 |
long quantity = Long.parseLong(quantityString);
|
| 517 |
rajveer |
132 |
if(quantity <= 0){
|
|
|
133 |
log.info("Not valid item quantity. Unable to change item quantity.");
|
|
|
134 |
}else{
|
|
|
135 |
if(Utils.updateItemQuantityInCart(userinfo.getCartId(), itemId, quantity)){
|
|
|
136 |
return "delsuccess";
|
|
|
137 |
}
|
| 507 |
rajveer |
138 |
}
|
|
|
139 |
return "delfailure";
|
|
|
140 |
}
|
|
|
141 |
|
|
|
142 |
|
|
|
143 |
public void printParams(){
|
|
|
144 |
for(String param : reqparams.keySet()) {
|
|
|
145 |
log.info("param name is " + param);
|
|
|
146 |
log.info("param first is " + reqparams.get(param)[0]);
|
|
|
147 |
}
|
|
|
148 |
log.info(this.reqparams);
|
|
|
149 |
}
|
|
|
150 |
|
|
|
151 |
@Override
|
|
|
152 |
public void setParameters(Map<String, String[]> reqmap) {
|
|
|
153 |
log.info("setParameters:" + reqmap);
|
|
|
154 |
|
|
|
155 |
this.reqparams = reqmap;
|
| 410 |
rajveer |
156 |
}
|
| 507 |
rajveer |
157 |
|
|
|
158 |
public String getHeaderSnippet(){
|
|
|
159 |
return htmlSnippets.get("HEADER");
|
|
|
160 |
}
|
| 410 |
rajveer |
161 |
|
| 507 |
rajveer |
162 |
public String getMainMenuSnippet(){
|
|
|
163 |
return htmlSnippets.get("MAIN_MENU");
|
|
|
164 |
}
|
| 410 |
rajveer |
165 |
|
| 507 |
rajveer |
166 |
public String getSearchBarSnippet(){
|
|
|
167 |
return htmlSnippets.get("SEARCH_BAR");
|
|
|
168 |
}
|
|
|
169 |
|
|
|
170 |
|
|
|
171 |
public String getCustomerServiceSnippet(){
|
|
|
172 |
return htmlSnippets.get("CUSTOMER_SERVICE");
|
|
|
173 |
}
|
|
|
174 |
|
|
|
175 |
public String getCartHeaderSnippet(){
|
|
|
176 |
return htmlSnippets.get("CART_HEADER");
|
|
|
177 |
}
|
|
|
178 |
|
|
|
179 |
public String getCartDetailsSnippet(){
|
|
|
180 |
return htmlSnippets.get("CART_DETAILS");
|
|
|
181 |
}
|
|
|
182 |
|
|
|
183 |
public String getMyResearchSnippet(){
|
|
|
184 |
return htmlSnippets.get("MY_RESEARCH");
|
|
|
185 |
}
|
|
|
186 |
|
|
|
187 |
public String getFooterSnippet(){
|
|
|
188 |
return htmlSnippets.get("FOOTER");
|
|
|
189 |
}
|
|
|
190 |
|
|
|
191 |
public String getJsFileSnippet(){
|
|
|
192 |
return htmlSnippets.get("JS_FILES");
|
|
|
193 |
}
|
|
|
194 |
|
|
|
195 |
public String getCssFileSnippet(){
|
|
|
196 |
return htmlSnippets.get("CSS_FILES");
|
|
|
197 |
}
|
|
|
198 |
|
|
|
199 |
public long getNumberOfItems(){
|
|
|
200 |
return userinfo.getTotalItems();
|
|
|
201 |
}
|
|
|
202 |
}
|
|
|
203 |
/*
|
| 419 |
rajveer |
204 |
// GET /cart
|
| 507 |
rajveer |
205 |
public HttpHeaders index() {
|
|
|
206 |
changed on 18 august
|
| 410 |
rajveer |
207 |
Cookie loginCookie = new Cookie("userId", "Rajveer");
|
|
|
208 |
Map cookiesMap = new HashMap();
|
|
|
209 |
cookiesMap.put("USER_ID", loginCookie);
|
|
|
210 |
setCookiesMap(cookiesMap);
|
|
|
211 |
|
|
|
212 |
log.info( "Cookies map is " + this.getCookiesMap());
|
|
|
213 |
log.info("CartController.index");
|
|
|
214 |
|
|
|
215 |
Map<PageContentKeys, String> params = new HashMap<PageContentKeys, String>();
|
|
|
216 |
|
| 416 |
rajveer |
217 |
long userId = 0;
|
|
|
218 |
boolean isSessionId = true;
|
| 419 |
rajveer |
219 |
//if(this.session.getAttribute("loggedin")!=null){
|
|
|
220 |
if(this.session.getAttribute("loggedin").toString().equals("true")){
|
|
|
221 |
userId = Long.parseLong(this.session.getAttribute("userid").toString());
|
|
|
222 |
isSessionId = false;
|
|
|
223 |
}else{
|
|
|
224 |
userId = Long.parseLong(this.session.getAttribute("userid").toString());
|
|
|
225 |
isSessionId = true;
|
|
|
226 |
}
|
|
|
227 |
// }else{
|
|
|
228 |
// System.out.println("No User is logged in. Redirect to login page.");
|
|
|
229 |
// return new DefaultHttpHeaders("fatal");
|
|
|
230 |
//
|
|
|
231 |
// }
|
|
|
232 |
|
|
|
233 |
|
|
|
234 |
|
|
|
235 |
params.put(PageContentKeys.CUSTOMER_ID, userId+"");
|
|
|
236 |
params.put(PageContentKeys.IS_SESSION_ID, isSessionId+"");
|
|
|
237 |
|
| 507 |
rajveer |
238 |
|
| 419 |
rajveer |
239 |
long userId = 0;
|
|
|
240 |
boolean isSessionId = true;
|
|
|
241 |
Map<PageContentKeys, String> params = new HashMap<PageContentKeys, String>();
|
| 416 |
rajveer |
242 |
|
| 419 |
rajveer |
243 |
if(userinfo.isLoggedIn()){
|
|
|
244 |
userId = userinfo.getUserId();
|
|
|
245 |
isSessionId = false;
|
|
|
246 |
}
|
|
|
247 |
else{
|
|
|
248 |
userId = userinfo.getSessionId();
|
|
|
249 |
isSessionId = true;
|
|
|
250 |
}
|
|
|
251 |
|
| 416 |
rajveer |
252 |
params.put(PageContentKeys.CUSTOMER_ID, userId+"");
|
|
|
253 |
params.put(PageContentKeys.IS_SESSION_ID, isSessionId+"");
|
|
|
254 |
|
| 410 |
rajveer |
255 |
htmlSnippets = PageManager.getPageManager().getPageContents(PageEnum.SHOPPING_CART_PAGE, params);
|
|
|
256 |
|
|
|
257 |
|
|
|
258 |
|
|
|
259 |
return new DefaultHttpHeaders("index").disableCaching();
|
|
|
260 |
}
|
| 507 |
rajveer |
261 |
*/
|
| 410 |
rajveer |
262 |
// POST /entity
|
| 507 |
rajveer |
263 |
/*
|
| 410 |
rajveer |
264 |
public String create() {
|
| 507 |
rajveer |
265 |
|
| 410 |
rajveer |
266 |
log.info("CartController.create");
|
|
|
267 |
printParams();
|
|
|
268 |
if(getCookiesMap() != null){
|
|
|
269 |
Cookie loginCookie = (Cookie)getCookiesMap().get("USER_ID");
|
|
|
270 |
log.info("login cookie name is " + loginCookie.getName() );
|
|
|
271 |
log.info("login cookie value is " + loginCookie.getValue());
|
|
|
272 |
}
|
|
|
273 |
log.info("item id is " + this.reqparams.get("item_id"));
|
|
|
274 |
itemId = this.reqparams.get("item_id")[0];
|
|
|
275 |
|
|
|
276 |
addItemToCart(itemId);
|
| 416 |
rajveer |
277 |
long userId = 0;
|
|
|
278 |
boolean isSessionId = true;
|
|
|
279 |
if(this.session.getAttribute("loggedin").toString().equals("true")){
|
|
|
280 |
userId = Long.parseLong(this.session.getAttribute("userid").toString());
|
|
|
281 |
isSessionId = false;
|
|
|
282 |
}else{
|
|
|
283 |
userId = Long.parseLong(this.session.getAttribute("userid").toString());
|
|
|
284 |
isSessionId = true;
|
|
|
285 |
}
|
|
|
286 |
addItemToCart(Long.parseLong(itemId), userId, isSessionId);
|
| 410 |
rajveer |
287 |
// Add data to the cart
|
| 507 |
rajveer |
288 |
|
| 419 |
rajveer |
289 |
long userId = 0;
|
|
|
290 |
boolean isSessionId = true;
|
| 410 |
rajveer |
291 |
|
| 449 |
rajveer |
292 |
log.info("item id is " + this.reqparams.get("productid"));
|
| 419 |
rajveer |
293 |
|
| 449 |
rajveer |
294 |
String itemIds = "1000008_1000005";
|
|
|
295 |
if(this.reqparams.get("productid") != null){
|
|
|
296 |
itemIds = this.reqparams.get("productid")[0];
|
|
|
297 |
}
|
|
|
298 |
|
|
|
299 |
|
|
|
300 |
// log.info("list of item ids is " + this.request.getParameter("productid"));
|
|
|
301 |
// String itemIds = this.request.getParameter("productid");
|
|
|
302 |
|
| 419 |
rajveer |
303 |
if(userinfo.isLoggedIn()){
|
|
|
304 |
userId = userinfo.getUserId();
|
|
|
305 |
isSessionId = false;
|
|
|
306 |
}
|
|
|
307 |
else{
|
|
|
308 |
userId = userinfo.getSessionId();
|
|
|
309 |
isSessionId = true;
|
|
|
310 |
}
|
| 449 |
rajveer |
311 |
long cartId = 0;
|
|
|
312 |
|
|
|
313 |
StringTokenizer tokenizer = new StringTokenizer(itemIds,"_");
|
|
|
314 |
int numberOfItems = tokenizer.countTokens();
|
|
|
315 |
while(tokenizer.hasMoreTokens()){
|
|
|
316 |
long itemId = Long.parseLong(tokenizer.nextToken());
|
|
|
317 |
cartId = Utils.addItemToCart(itemId, userId, isSessionId);
|
|
|
318 |
}
|
|
|
319 |
|
|
|
320 |
//long cartId = Utils.addItemToCart(Long.parseLong(itemId), userId, isSessionId);
|
| 419 |
rajveer |
321 |
|
|
|
322 |
userinfo.setCartId(cartId);
|
| 449 |
rajveer |
323 |
userinfo.setTotalItems(Utils.getNumberOfItemsInCart(cartId) );
|
| 419 |
rajveer |
324 |
|
|
|
325 |
this.session.setAttribute("userinfo", userinfo);
|
|
|
326 |
|
| 410 |
rajveer |
327 |
return "success";
|
|
|
328 |
}
|
|
|
329 |
|
| 507 |
rajveer |
330 |
|
| 410 |
rajveer |
331 |
public void printParams(){
|
|
|
332 |
for(String param : reqparams.keySet()) {
|
|
|
333 |
log.info("param name is " + param);
|
|
|
334 |
log.info("param first is " + reqparams.get(param)[0]);
|
|
|
335 |
}
|
|
|
336 |
log.info(this.reqparams);
|
|
|
337 |
}
|
|
|
338 |
|
|
|
339 |
private boolean addItemToCart(String itemId){
|
|
|
340 |
log.info("Session Id is " + request.getSession().getId());
|
|
|
341 |
log.info("Item Id is " + itemId);
|
|
|
342 |
return true;
|
|
|
343 |
}
|
|
|
344 |
|
|
|
345 |
|
| 416 |
rajveer |
346 |
private void addItemToCart(long catalogItemId, long userId, boolean isSessionId){
|
| 410 |
rajveer |
347 |
UserContextServiceClient userContextServiceClient = null;
|
|
|
348 |
in.shop2020.model.v1.user.UserContextService.Client userClient = null;
|
|
|
349 |
|
|
|
350 |
ShoppingCartClient shoppingCartClient = null;
|
|
|
351 |
in.shop2020.model.v1.shoppingcart.ShoppingCartService.Client cartClient = null;
|
|
|
352 |
long cartId = -1;
|
|
|
353 |
|
|
|
354 |
try {
|
|
|
355 |
userContextServiceClient = new UserContextServiceClient();
|
|
|
356 |
userClient = userContextServiceClient.getClient();
|
|
|
357 |
|
|
|
358 |
shoppingCartClient = new ShoppingCartClient();
|
|
|
359 |
cartClient = shoppingCartClient.getClient();
|
| 416 |
rajveer |
360 |
|
|
|
361 |
cartId = cartClient.createCart(userId, isSessionId);
|
| 410 |
rajveer |
362 |
cartClient.addItemToCart(cartId, catalogItemId, 1);
|
| 419 |
rajveer |
363 |
|
|
|
364 |
int NumberOfLines = cartClient.getShadowCart(cartId).getLinesSize();
|
|
|
365 |
this.session.setAttribute("totalitems", NumberOfLines+"");
|
|
|
366 |
|
|
|
367 |
|
| 410 |
rajveer |
368 |
} catch (ShoppingCartException e) {
|
|
|
369 |
e.printStackTrace();
|
|
|
370 |
} catch (TException e) {
|
|
|
371 |
e.printStackTrace();
|
|
|
372 |
} catch (Exception e) {
|
|
|
373 |
e.printStackTrace();
|
|
|
374 |
}
|
|
|
375 |
|
|
|
376 |
//if user is logged in create new cart
|
|
|
377 |
//if( userClient.getState(userId, false).isIsLoggedIn()){
|
|
|
378 |
}
|
|
|
379 |
|
| 458 |
rajveer |
380 |
public long getNumberOfItems(){
|
| 449 |
rajveer |
381 |
return userinfo.getTotalItems();
|
|
|
382 |
}
|
|
|
383 |
|
| 410 |
rajveer |
384 |
public String getShoppingCartSnippets()
|
|
|
385 |
{
|
|
|
386 |
return htmlSnippets.get("SHOPPING_CART");
|
|
|
387 |
}
|
|
|
388 |
|
|
|
389 |
@Override
|
|
|
390 |
public void setParameters(Map<String, String[]> reqmap) {
|
|
|
391 |
log.info("setParameters:" + reqmap);
|
|
|
392 |
|
|
|
393 |
this.reqparams = reqmap;
|
|
|
394 |
}
|
| 507 |
rajveer |
395 |
*/
|
| 416 |
rajveer |
396 |
//
|
|
|
397 |
// @Override
|
|
|
398 |
// public void setServletRequest(HttpServletRequest request) {
|
|
|
399 |
// this.request = request;
|
|
|
400 |
// }
|
|
|
401 |
//
|
| 507 |
rajveer |
402 |
|