| 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;
|
|
|
9 |
import in.shop2020.thrift.clients.ShoppingCartClient;
|
|
|
10 |
import in.shop2020.thrift.clients.UserContextServiceClient;
|
|
|
11 |
|
|
|
12 |
import java.util.*;
|
|
|
13 |
|
|
|
14 |
import javax.servlet.http.Cookie;
|
|
|
15 |
import javax.servlet.http.HttpServletRequest;
|
|
|
16 |
|
|
|
17 |
import org.apache.commons.lang.StringUtils;
|
|
|
18 |
import org.apache.juli.logging.Log;
|
|
|
19 |
import org.apache.juli.logging.LogFactory;
|
|
|
20 |
import org.apache.struts2.convention.annotation.Result;
|
|
|
21 |
import org.apache.struts2.convention.annotation.Results;
|
|
|
22 |
import org.apache.struts2.interceptor.ParameterAware;
|
|
|
23 |
import org.apache.struts2.interceptor.ServletRequestAware;
|
|
|
24 |
import org.apache.struts2.rest.DefaultHttpHeaders;
|
|
|
25 |
import org.apache.struts2.rest.HttpHeaders;
|
|
|
26 |
import org.apache.thrift.TException;
|
|
|
27 |
|
|
|
28 |
import com.opensymphony.xwork2.ValidationAwareSupport;
|
|
|
29 |
|
|
|
30 |
@Results({
|
|
|
31 |
@Result(name="success", type="redirectAction",
|
|
|
32 |
params = {"actionName" , "cart"})
|
|
|
33 |
})
|
|
|
34 |
|
|
|
35 |
public class CartController extends BaseController implements ParameterAware, ServletRequestAware {
|
|
|
36 |
|
|
|
37 |
private static final long serialVersionUID = 1L;
|
|
|
38 |
|
|
|
39 |
private Map<String, String[]> reqparams;
|
|
|
40 |
private Map<String, Object> sessions;
|
|
|
41 |
private String redirectURL;
|
|
|
42 |
private static Log log = LogFactory.getLog(CartController.class);
|
|
|
43 |
private String itemId;
|
|
|
44 |
HttpServletRequest request;
|
|
|
45 |
private Map<String,String> htmlSnippets;
|
|
|
46 |
|
|
|
47 |
public CartController(){
|
|
|
48 |
|
|
|
49 |
}
|
|
|
50 |
|
|
|
51 |
|
|
|
52 |
// GET /entity
|
|
|
53 |
public HttpHeaders index() {
|
|
|
54 |
Cookie loginCookie = new Cookie("userId", "Rajveer");
|
|
|
55 |
Map cookiesMap = new HashMap();
|
|
|
56 |
cookiesMap.put("USER_ID", loginCookie);
|
|
|
57 |
setCookiesMap(cookiesMap);
|
|
|
58 |
|
|
|
59 |
log.info( "Cookies map is " + this.getCookiesMap());
|
|
|
60 |
log.info("CartController.index");
|
|
|
61 |
|
|
|
62 |
Map<PageContentKeys, String> params = new HashMap<PageContentKeys, String>();
|
|
|
63 |
|
|
|
64 |
params.put(PageContentKeys.CUSTOMER_ID, "4");
|
|
|
65 |
params.put(PageContentKeys.IS_SESSION_ID, "1");
|
|
|
66 |
|
|
|
67 |
|
|
|
68 |
htmlSnippets = PageManager.getPageManager().getPageContents(PageEnum.SHOPPING_CART_PAGE, params);
|
|
|
69 |
|
|
|
70 |
|
|
|
71 |
|
|
|
72 |
return new DefaultHttpHeaders("index").disableCaching();
|
|
|
73 |
}
|
|
|
74 |
|
|
|
75 |
// POST /entity
|
|
|
76 |
public String create() {
|
|
|
77 |
log.info("CartController.create");
|
|
|
78 |
printParams();
|
|
|
79 |
if(getCookiesMap() != null){
|
|
|
80 |
Cookie loginCookie = (Cookie)getCookiesMap().get("USER_ID");
|
|
|
81 |
log.info("login cookie name is " + loginCookie.getName() );
|
|
|
82 |
log.info("login cookie value is " + loginCookie.getValue());
|
|
|
83 |
}
|
|
|
84 |
log.info("item id is " + this.reqparams.get("item_id"));
|
|
|
85 |
itemId = this.reqparams.get("item_id")[0];
|
|
|
86 |
|
|
|
87 |
addItemToCart(itemId);
|
| 413 |
rajveer |
88 |
addItemToCart(Long.parseLong(itemId), 4);
|
| 410 |
rajveer |
89 |
// Add data to the cart
|
|
|
90 |
|
|
|
91 |
return "success";
|
|
|
92 |
}
|
|
|
93 |
|
|
|
94 |
public void printParams(){
|
|
|
95 |
for(String param : reqparams.keySet()) {
|
|
|
96 |
log.info("param name is " + param);
|
|
|
97 |
log.info("param first is " + reqparams.get(param)[0]);
|
|
|
98 |
}
|
|
|
99 |
log.info(this.reqparams);
|
|
|
100 |
}
|
|
|
101 |
|
|
|
102 |
private boolean addItemToCart(String itemId){
|
|
|
103 |
log.info("Session Id is " + request.getSession().getId());
|
|
|
104 |
log.info("Item Id is " + itemId);
|
|
|
105 |
return true;
|
|
|
106 |
}
|
|
|
107 |
|
|
|
108 |
|
|
|
109 |
private void addItemToCart(long catalogItemId, long userId){
|
|
|
110 |
UserContextServiceClient userContextServiceClient = null;
|
|
|
111 |
in.shop2020.model.v1.user.UserContextService.Client userClient = null;
|
|
|
112 |
|
|
|
113 |
ShoppingCartClient shoppingCartClient = null;
|
|
|
114 |
in.shop2020.model.v1.shoppingcart.ShoppingCartService.Client cartClient = null;
|
|
|
115 |
long cartId = -1;
|
|
|
116 |
|
|
|
117 |
try {
|
|
|
118 |
userContextServiceClient = new UserContextServiceClient();
|
|
|
119 |
userClient = userContextServiceClient.getClient();
|
|
|
120 |
|
|
|
121 |
shoppingCartClient = new ShoppingCartClient();
|
|
|
122 |
cartClient = shoppingCartClient.getClient();
|
|
|
123 |
cartId = cartClient.createCart(userId, false);
|
|
|
124 |
cartClient.addItemToCart(cartId, catalogItemId, 1);
|
|
|
125 |
} catch (ShoppingCartException e) {
|
|
|
126 |
// TODO Auto-generated catch block
|
|
|
127 |
e.printStackTrace();
|
|
|
128 |
} catch (TException e) {
|
|
|
129 |
// TODO Auto-generated catch block
|
|
|
130 |
e.printStackTrace();
|
|
|
131 |
} catch (Exception e) {
|
|
|
132 |
// TODO Auto-generated catch block
|
|
|
133 |
e.printStackTrace();
|
|
|
134 |
}
|
|
|
135 |
|
|
|
136 |
//if user is logged in create new cart
|
|
|
137 |
//if( userClient.getState(userId, false).isIsLoggedIn()){
|
|
|
138 |
}
|
|
|
139 |
|
|
|
140 |
public String getShoppingCartSnippets()
|
|
|
141 |
{
|
|
|
142 |
return htmlSnippets.get("SHOPPING_CART");
|
|
|
143 |
}
|
|
|
144 |
|
|
|
145 |
@Override
|
|
|
146 |
public void setParameters(Map<String, String[]> reqmap) {
|
|
|
147 |
log.info("setParameters:" + reqmap);
|
|
|
148 |
|
|
|
149 |
this.reqparams = reqmap;
|
|
|
150 |
}
|
|
|
151 |
|
|
|
152 |
@Override
|
|
|
153 |
public void setServletRequest(HttpServletRequest request) {
|
|
|
154 |
this.request = request;
|
|
|
155 |
}
|
|
|
156 |
|
|
|
157 |
}
|