| 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 |
|
| 416 |
rajveer |
35 |
public class CartController extends BaseController implements ParameterAware {
|
| 410 |
rajveer |
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;
|
| 416 |
rajveer |
44 |
//HttpServletRequest request;
|
| 410 |
rajveer |
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 |
|
| 416 |
rajveer |
64 |
long userId = 0;
|
|
|
65 |
boolean isSessionId = true;
|
|
|
66 |
|
|
|
67 |
if(this.session.getAttribute("loggedin").toString().equals("true")){
|
|
|
68 |
userId = Long.parseLong(this.session.getAttribute("userid").toString());
|
|
|
69 |
isSessionId = false;
|
|
|
70 |
}else{
|
|
|
71 |
userId = Long.parseLong(this.session.getAttribute("userid").toString());
|
|
|
72 |
isSessionId = true;
|
|
|
73 |
}
|
| 410 |
rajveer |
74 |
|
| 416 |
rajveer |
75 |
params.put(PageContentKeys.CUSTOMER_ID, userId+"");
|
|
|
76 |
params.put(PageContentKeys.IS_SESSION_ID, isSessionId+"");
|
|
|
77 |
|
| 410 |
rajveer |
78 |
|
|
|
79 |
htmlSnippets = PageManager.getPageManager().getPageContents(PageEnum.SHOPPING_CART_PAGE, params);
|
|
|
80 |
|
|
|
81 |
|
|
|
82 |
|
|
|
83 |
return new DefaultHttpHeaders("index").disableCaching();
|
|
|
84 |
}
|
|
|
85 |
|
|
|
86 |
// POST /entity
|
|
|
87 |
public String create() {
|
|
|
88 |
log.info("CartController.create");
|
|
|
89 |
printParams();
|
|
|
90 |
if(getCookiesMap() != null){
|
|
|
91 |
Cookie loginCookie = (Cookie)getCookiesMap().get("USER_ID");
|
|
|
92 |
log.info("login cookie name is " + loginCookie.getName() );
|
|
|
93 |
log.info("login cookie value is " + loginCookie.getValue());
|
|
|
94 |
}
|
|
|
95 |
log.info("item id is " + this.reqparams.get("item_id"));
|
|
|
96 |
itemId = this.reqparams.get("item_id")[0];
|
|
|
97 |
|
|
|
98 |
addItemToCart(itemId);
|
| 416 |
rajveer |
99 |
long userId = 0;
|
|
|
100 |
boolean isSessionId = true;
|
|
|
101 |
if(this.session.getAttribute("loggedin").toString().equals("true")){
|
|
|
102 |
userId = Long.parseLong(this.session.getAttribute("userid").toString());
|
|
|
103 |
isSessionId = false;
|
|
|
104 |
}else{
|
|
|
105 |
userId = Long.parseLong(this.session.getAttribute("userid").toString());
|
|
|
106 |
isSessionId = true;
|
|
|
107 |
}
|
|
|
108 |
addItemToCart(Long.parseLong(itemId), userId, isSessionId);
|
| 410 |
rajveer |
109 |
// Add data to the cart
|
|
|
110 |
|
|
|
111 |
return "success";
|
|
|
112 |
}
|
|
|
113 |
|
|
|
114 |
public void printParams(){
|
|
|
115 |
for(String param : reqparams.keySet()) {
|
|
|
116 |
log.info("param name is " + param);
|
|
|
117 |
log.info("param first is " + reqparams.get(param)[0]);
|
|
|
118 |
}
|
|
|
119 |
log.info(this.reqparams);
|
|
|
120 |
}
|
|
|
121 |
|
|
|
122 |
private boolean addItemToCart(String itemId){
|
|
|
123 |
log.info("Session Id is " + request.getSession().getId());
|
|
|
124 |
log.info("Item Id is " + itemId);
|
|
|
125 |
return true;
|
|
|
126 |
}
|
|
|
127 |
|
|
|
128 |
|
| 416 |
rajveer |
129 |
private void addItemToCart(long catalogItemId, long userId, boolean isSessionId){
|
| 410 |
rajveer |
130 |
UserContextServiceClient userContextServiceClient = null;
|
|
|
131 |
in.shop2020.model.v1.user.UserContextService.Client userClient = null;
|
|
|
132 |
|
|
|
133 |
ShoppingCartClient shoppingCartClient = null;
|
|
|
134 |
in.shop2020.model.v1.shoppingcart.ShoppingCartService.Client cartClient = null;
|
|
|
135 |
long cartId = -1;
|
|
|
136 |
|
|
|
137 |
try {
|
|
|
138 |
userContextServiceClient = new UserContextServiceClient();
|
|
|
139 |
userClient = userContextServiceClient.getClient();
|
|
|
140 |
|
|
|
141 |
shoppingCartClient = new ShoppingCartClient();
|
|
|
142 |
cartClient = shoppingCartClient.getClient();
|
| 416 |
rajveer |
143 |
|
|
|
144 |
cartId = cartClient.createCart(userId, isSessionId);
|
| 410 |
rajveer |
145 |
cartClient.addItemToCart(cartId, catalogItemId, 1);
|
|
|
146 |
} catch (ShoppingCartException e) {
|
|
|
147 |
// TODO Auto-generated catch block
|
|
|
148 |
e.printStackTrace();
|
|
|
149 |
} catch (TException e) {
|
|
|
150 |
// TODO Auto-generated catch block
|
|
|
151 |
e.printStackTrace();
|
|
|
152 |
} catch (Exception e) {
|
|
|
153 |
// TODO Auto-generated catch block
|
|
|
154 |
e.printStackTrace();
|
|
|
155 |
}
|
|
|
156 |
|
|
|
157 |
//if user is logged in create new cart
|
|
|
158 |
//if( userClient.getState(userId, false).isIsLoggedIn()){
|
|
|
159 |
}
|
|
|
160 |
|
|
|
161 |
public String getShoppingCartSnippets()
|
|
|
162 |
{
|
|
|
163 |
return htmlSnippets.get("SHOPPING_CART");
|
|
|
164 |
}
|
|
|
165 |
|
|
|
166 |
@Override
|
|
|
167 |
public void setParameters(Map<String, String[]> reqmap) {
|
|
|
168 |
log.info("setParameters:" + reqmap);
|
|
|
169 |
|
|
|
170 |
this.reqparams = reqmap;
|
|
|
171 |
}
|
| 416 |
rajveer |
172 |
//
|
|
|
173 |
// @Override
|
|
|
174 |
// public void setServletRequest(HttpServletRequest request) {
|
|
|
175 |
// this.request = request;
|
|
|
176 |
// }
|
|
|
177 |
//
|
| 410 |
rajveer |
178 |
}
|