Subversion Repositories SmartDukaan

Rev

Rev 462 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 462 Rev 507
Line 18... Line 18...
18
import org.apache.struts2.interceptor.ParameterAware;
18
import org.apache.struts2.interceptor.ParameterAware;
19
import org.apache.struts2.rest.DefaultHttpHeaders;
19
import org.apache.struts2.rest.DefaultHttpHeaders;
20
import org.apache.struts2.rest.HttpHeaders;
20
import org.apache.struts2.rest.HttpHeaders;
21
import org.apache.thrift.TException;
21
import org.apache.thrift.TException;
22
 
22
 
23
 
-
 
24
@Results({
-
 
25
    @Result(name="fail", type="redirectAction", 
-
 
26
    		params = {"actionName" , "cart"}),
-
 
27
    @Result(name="redirect", location="${url}", type="redirect")
-
 
28
})
-
 
29
 
-
 
30
public class CartController extends BaseController implements ParameterAware {
23
public class CartController extends BaseController implements ParameterAware{
31
	
24
	
32
	private static final long serialVersionUID = 1L;
25
	private static final long serialVersionUID = 1L;
33
	
-
 
34
	private Map<String, String[]> reqparams;
-
 
35
	private Map<String, Object> sessions;
-
 
36
	private String redirectURL;
-
 
37
	private static Log log = LogFactory.getLog(CartController.class);
26
	private static Log log = LogFactory.getLog(CartController.class);
38
	//private String itemId;
27
	Map<String, String[]> reqparams = null;
39
	//HttpServletRequest request;
-
 
-
 
28
	
40
	private Map<String,String> htmlSnippets;
29
	private Map<String,String> htmlSnippets;
-
 
30
	private PageManager pageManager = null;
41
	
31
	
42
	public CartController(){
32
	public CartController(){
-
 
33
		super();
-
 
34
		pageManager = PageManager.getPageManager();	
-
 
35
	}
-
 
36
	
-
 
37
	 // GET /cart
-
 
38
	 public HttpHeaders index() {
-
 
39
    	long userId = 0;
-
 
40
    	boolean isSessionId = true;
-
 
41
		Map<PageContentKeys, String> params = new HashMap<PageContentKeys, String>();
-
 
42
		
-
 
43
		if(userinfo.isLoggedIn()){
-
 
44
			userId = userinfo.getUserId();
-
 
45
			isSessionId = false;
-
 
46
		}
-
 
47
		else{
-
 
48
			userId = userinfo.getSessionId();
-
 
49
			isSessionId = true;
-
 
50
		}
-
 
51
		
-
 
52
		params.put(PageContentKeys.CUSTOMER_ID, userId+"");
-
 
53
		params.put(PageContentKeys.IS_SESSION_ID, isSessionId+"");
-
 
54
		params.put(PageContentKeys.CART_ID, userinfo.getCartId()+"");
-
 
55
		
-
 
56
		params.put(PageContentKeys.ITEM_COUNT, userinfo.getTotalItems()+"");
-
 
57
 
-
 
58
		htmlSnippets = PageManager.getPageManager().getPageContents(PageEnum.SHOPPING_CART_PAGE, params);
43
		
59
		
-
 
60
    	return new DefaultHttpHeaders("index").disableCaching();
-
 
61
	 }
-
 
62
 
-
 
63
		// POST /entity
-
 
64
		public String create() {
-
 
65
	    	log.info("CartController.create");
-
 
66
	    	
-
 
67
	    	printParams();
-
 
68
	    	
-
 
69
	    	long userId = 0;
-
 
70
	    	boolean isSessionId = true;
-
 
71
	    	
-
 
72
	    	log.info("item id is " + this.reqparams.get("productid"));
-
 
73
	    	
-
 
74
	    	String itemIds = "1000008_1000005"; 
-
 
75
	    	if(this.reqparams.get("productid") != null){
-
 
76
	    		itemIds = this.reqparams.get("productid")[0];
-
 
77
	    	}
-
 
78
	    	
-
 
79
	 	
-
 
80
//		    	log.info("list of item ids is " + this.request.getParameter("productid"));
-
 
81
//				String itemIds = this.request.getParameter("productid");
-
 
82
			
-
 
83
			if(userinfo.isLoggedIn()){
-
 
84
				userId = userinfo.getUserId();
-
 
85
				isSessionId = false;
-
 
86
			}
-
 
87
			else{
-
 
88
				userId = userinfo.getSessionId();
-
 
89
				isSessionId = true;
-
 
90
			}
-
 
91
			long cartId = 0;
-
 
92
		
-
 
93
			StringTokenizer tokenizer = new StringTokenizer(itemIds,"_");
-
 
94
			int numberOfItems = tokenizer.countTokens();
-
 
95
			while(tokenizer.hasMoreTokens()){
-
 
96
				long itemId = Long.parseLong(tokenizer.nextToken());
-
 
97
				cartId = Utils.addItemToCart(itemId, userId, isSessionId);
-
 
98
			}
-
 
99
 
-
 
100
	    	//long cartId = Utils.addItemToCart(Long.parseLong(itemId), userId, isSessionId);
-
 
101
	    	
-
 
102
	    	userinfo.setCartId(cartId);
-
 
103
	    	userinfo.setTotalItems(Utils.getNumberOfItemsInCart(cartId) );
-
 
104
	    	
-
 
105
	    	this.session.setAttribute("userinfo", userinfo);
-
 
106
	    	
-
 
107
			return "success";
-
 
108
	    }
-
 
109
		
-
 
110
 
-
 
111
		// DELETE /entity
-
 
112
		public String destroy() {
-
 
113
	    	log.info("CartController.destroy");
-
 
114
	    	printParams();
-
 
115
	    	log.info("item id is " + this.request.getParameter("productid"));
-
 
116
			String itemIdString = this.request.getParameter("productid");
-
 
117
			long itemId = Long.parseLong(itemIdString);
-
 
118
			
-
 
119
			if(Utils.deleteItemFromCart(itemId, userinfo.getUserId(), userinfo.isSessionId())){
-
 
120
				return "delsuccess";	
-
 
121
			}
-
 
122
			return "delfailure";
-
 
123
		}
-
 
124
		
-
 
125
		
-
 
126
		// DELETE /entity
-
 
127
		public String update() {
-
 
128
	    	log.info("CartController.update");
-
 
129
	    	printParams();
-
 
130
	    	log.info("item id is " + this.request.getParameter("productid"));
-
 
131
	    	log.info("item id is " + this.request.getParameter("quantity"));
-
 
132
			String itemIdString = this.request.getParameter("productid");
-
 
133
			String quantityString = this.request.getParameter("quantity");
-
 
134
			long itemId = Long.parseLong(itemIdString);
-
 
135
			long quantity = Long.parseLong(quantityString);
-
 
136
			
-
 
137
			if(Utils.updateItemQuantityInCart(userinfo.getCartId(), itemId, quantity)){
-
 
138
				return "delsuccess";	
-
 
139
			}
-
 
140
			return "delfailure";
-
 
141
		}
-
 
142
		
-
 
143
		
-
 
144
    public void printParams(){
-
 
145
    	for(String param : reqparams.keySet()) {
-
 
146
    		log.info("param name is " + param);
-
 
147
    		log.info("param first is " + reqparams.get(param)[0]);
-
 
148
    	}
-
 
149
    	log.info(this.reqparams);
-
 
150
    }
-
 
151
 
-
 
152
	@Override
-
 
153
	public void setParameters(Map<String, String[]> reqmap) {
-
 
154
		log.info("setParameters:" + reqmap);
-
 
155
		
-
 
156
		this.reqparams = reqmap;
44
	}
157
	}
-
 
158
 
-
 
159
    public String getHeaderSnippet(){
-
 
160
		return htmlSnippets.get("HEADER");
-
 
161
	}
-
 
162
	
-
 
163
	public String getMainMenuSnippet(){
-
 
164
		return htmlSnippets.get("MAIN_MENU");
-
 
165
	}
-
 
166
	
-
 
167
	public String getSearchBarSnippet(){
-
 
168
		return htmlSnippets.get("SEARCH_BAR");
-
 
169
	}
-
 
170
			
45
	
171
	
-
 
172
	public String getCustomerServiceSnippet(){
-
 
173
		return htmlSnippets.get("CUSTOMER_SERVICE");
-
 
174
	}
-
 
175
	
-
 
176
	public String getCartHeaderSnippet(){
-
 
177
		return htmlSnippets.get("CART_HEADER");
-
 
178
	}
46
	
179
	
-
 
180
	public String getCartDetailsSnippet(){
-
 
181
		return htmlSnippets.get("CART_DETAILS");
-
 
182
	}
-
 
183
	
-
 
184
	public String getMyResearchSnippet(){
-
 
185
		return htmlSnippets.get("MY_RESEARCH");
-
 
186
	}
-
 
187
				
-
 
188
	public String getFooterSnippet(){
-
 
189
		return htmlSnippets.get("FOOTER");
-
 
190
	}
-
 
191
	
-
 
192
	public String getJsFileSnippet(){
-
 
193
		return htmlSnippets.get("JS_FILES");
-
 
194
	}
-
 
195
	
-
 
196
	public String getCssFileSnippet(){
-
 
197
		return htmlSnippets.get("CSS_FILES");
-
 
198
	}
-
 
199
	
-
 
200
	public long getNumberOfItems(){
-
 
201
		return userinfo.getTotalItems();
-
 
202
	}
-
 
203
}
-
 
204
/*
47
	 // GET /cart
205
	 // GET /cart
48
    public HttpHeaders index() {
206
	 public HttpHeaders index() {
49
    	/* changed on 18 august
207
    	 changed on 18 august
50
    	Cookie loginCookie = new Cookie("userId", "Rajveer");
208
    	Cookie loginCookie = new Cookie("userId", "Rajveer");
51
    	Map cookiesMap = new HashMap();
209
    	Map cookiesMap = new HashMap();
52
    	cookiesMap.put("USER_ID", loginCookie);
210
    	cookiesMap.put("USER_ID", loginCookie);
53
    	setCookiesMap(cookiesMap);
211
    	setCookiesMap(cookiesMap);
54
    	
212
    	
Line 75... Line 233...
75
    	
233
    	
76
    	
234
    	
77
    	
235
    	
78
		params.put(PageContentKeys.CUSTOMER_ID, userId+"");
236
		params.put(PageContentKeys.CUSTOMER_ID, userId+"");
79
		params.put(PageContentKeys.IS_SESSION_ID, isSessionId+"");
237
		params.put(PageContentKeys.IS_SESSION_ID, isSessionId+"");
80
*/
238
 
81
 
239
 
82
    	long userId = 0;
240
    	long userId = 0;
83
    	boolean isSessionId = true;
241
    	boolean isSessionId = true;
84
		Map<PageContentKeys, String> params = new HashMap<PageContentKeys, String>();
242
		Map<PageContentKeys, String> params = new HashMap<PageContentKeys, String>();
85
		
243
		
Line 99... Line 257...
99
		
257
		
100
 
258
 
101
    	
259
    	
102
    	return new DefaultHttpHeaders("index").disableCaching();
260
    	return new DefaultHttpHeaders("index").disableCaching();
103
    }
261
    }
104
    
262
*/    
105
    // POST /entity
263
    // POST /entity
-
 
264
    /*
106
    public String create() {
265
    public String create() {
107
    	/*
266
    	
108
    	log.info("CartController.create");
267
    	log.info("CartController.create");
109
    	printParams();
268
    	printParams();
110
    	if(getCookiesMap() != null){
269
    	if(getCookiesMap() != null){
111
    		Cookie loginCookie = (Cookie)getCookiesMap().get("USER_ID");
270
    		Cookie loginCookie = (Cookie)getCookiesMap().get("USER_ID");
112
    		log.info("login cookie name is " + loginCookie.getName() );
271
    		log.info("login cookie name is " + loginCookie.getName() );
Line 125... Line 284...
125
    		userId = Long.parseLong(this.session.getAttribute("userid").toString());
284
    		userId = Long.parseLong(this.session.getAttribute("userid").toString());
126
    		isSessionId = true;
285
    		isSessionId = true;
127
    	}
286
    	}
128
    	addItemToCart(Long.parseLong(itemId), userId, isSessionId);
287
    	addItemToCart(Long.parseLong(itemId), userId, isSessionId);
129
    	// Add data to the cart
288
    	// Add data to the cart
130
    	*/
289
    	
131
    	long userId = 0;
290
    	long userId = 0;
132
    	boolean isSessionId = true;
291
    	boolean isSessionId = true;
133
    	
292
    	
134
    	log.info("item id is " + this.reqparams.get("productid"));
293
    	log.info("item id is " + this.reqparams.get("productid"));
135
    	
294
    	
Line 167... Line 326...
167
    	this.session.setAttribute("userinfo", userinfo);
326
    	this.session.setAttribute("userinfo", userinfo);
168
    	
327
    	
169
		return "success";
328
		return "success";
170
    }
329
    }
171
    
330
    
-
 
331
    
172
    public void printParams(){
332
    public void printParams(){
173
    	for(String param : reqparams.keySet()) {
333
    	for(String param : reqparams.keySet()) {
174
    		log.info("param name is " + param);
334
    		log.info("param name is " + param);
175
    		log.info("param first is " + reqparams.get(param)[0]);
335
    		log.info("param first is " + reqparams.get(param)[0]);
176
    	}
336
    	}
Line 234... Line 394...
234
	public void setParameters(Map<String, String[]> reqmap) {
394
	public void setParameters(Map<String, String[]> reqmap) {
235
		log.info("setParameters:" + reqmap);
395
		log.info("setParameters:" + reqmap);
236
		
396
		
237
		this.reqparams = reqmap;
397
		this.reqparams = reqmap;
238
	}
398
	}
-
 
399
	*/
239
//	
400
//	
240
//	@Override
401
//	@Override
241
//	public void setServletRequest(HttpServletRequest request) {
402
//	public void setServletRequest(HttpServletRequest request) {
242
//		this.request = request;
403
//		this.request = request;
243
//	}
404
//	}
244
//    
405
//    
245
}
406