Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
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
 
637 rajveer 71
		String itemIds = "";
572 chandransh 72
		if (this.reqparams.get("productid") != null) {
73
			itemIds = this.reqparams.get("productid")[0];
637 rajveer 74
		}else{
75
			return "failure";
572 chandransh 76
		}
637 rajveer 77
 
572 chandransh 78
 
79
		StringTokenizer tokenizer = new StringTokenizer(itemIds, "_");
80
		while (tokenizer.hasMoreTokens()) {
81
			long itemId = Long.parseLong(tokenizer.nextToken());
82
 
83
			try {
84
				UserContextServiceClient userServiceClient = new UserContextServiceClient();
85
				UserContextService.Client userClient = userServiceClient.getClient();
86
				if (cartId == 0)
555 chandransh 87
					cartId = userClient.createCart(userId);
572 chandransh 88
				userClient.addItemToCart(cartId, itemId, 1);
536 rajveer 89
 
572 chandransh 90
			} catch (ShoppingCartException e) {
91
				e.printStackTrace();
92
			} catch (TException e) {
93
				e.printStackTrace();
94
			} catch (Exception e) {
95
				e.printStackTrace();
507 rajveer 96
			}
97
 
572 chandransh 98
		}
507 rajveer 99
 
572 chandransh 100
		userinfo.setCartId(cartId);
101
		userinfo.setTotalItems(Utils.getNumberOfItemsInCart(cartId));
102
 
103
		return "success";
104
	}		
105
 
507 rajveer 106
		// DELETE /entity
107
		public String destroy() {
108
	    	log.info("CartController.destroy");
109
	    	printParams();
110
	    	log.info("item id is " + this.request.getParameter("productid"));
111
			String itemIdString = this.request.getParameter("productid");
112
			long itemId = Long.parseLong(itemIdString);
517 rajveer 113
			if(userinfo.getCartId() == -1){
114
				log.info("Cart does not exist. Nothing to delete.");
115
			}else{
116
				if(Utils.deleteItemFromCart(userinfo.getCartId(), itemId, userinfo.getUserId(), userinfo.isSessionId())){
117
					userinfo.setTotalItems(userinfo.getTotalItems() - 1 );
118
					return "delsuccess";	
119
				}
507 rajveer 120
			}
121
			return "delfailure";
122
		}
123
 
124
 
125
		// DELETE /entity
126
		public String update() {
127
	    	log.info("CartController.update");
128
	    	printParams();
129
	    	log.info("item id is " + this.request.getParameter("productid"));
130
	    	log.info("item id is " + this.request.getParameter("quantity"));
131
			String itemIdString = this.request.getParameter("productid");
132
			String quantityString = this.request.getParameter("quantity");
133
			long itemId = Long.parseLong(itemIdString);
134
			long quantity = Long.parseLong(quantityString);
517 rajveer 135
			if(quantity <= 0){
136
				log.info("Not valid item quantity. Unable to change item quantity.");
137
			}else{
138
				if(Utils.updateItemQuantityInCart(userinfo.getCartId(), itemId, quantity)){
139
					return "delsuccess";	
140
				}
507 rajveer 141
			}
142
			return "delfailure";
143
		}
144
 
145
 
146
    public void printParams(){
147
    	for(String param : reqparams.keySet()) {
148
    		log.info("param name is " + param);
149
    		log.info("param first is " + reqparams.get(param)[0]);
150
    	}
151
    	log.info(this.reqparams);
152
    }
153
 
154
	@Override
155
	public void setParameters(Map<String, String[]> reqmap) {
156
		log.info("setParameters:" + reqmap);
157
 
158
		this.reqparams = reqmap;
410 rajveer 159
	}
507 rajveer 160
 
161
    public String getHeaderSnippet(){
162
		return htmlSnippets.get("HEADER");
163
	}
410 rajveer 164
 
507 rajveer 165
	public String getMainMenuSnippet(){
166
		return htmlSnippets.get("MAIN_MENU");
167
	}
410 rajveer 168
 
507 rajveer 169
	public String getSearchBarSnippet(){
170
		return htmlSnippets.get("SEARCH_BAR");
171
	}
172
 
173
 
174
	public String getCustomerServiceSnippet(){
175
		return htmlSnippets.get("CUSTOMER_SERVICE");
176
	}
177
 
178
	public String getCartHeaderSnippet(){
179
		return htmlSnippets.get("CART_HEADER");
180
	}
181
 
182
	public String getCartDetailsSnippet(){
183
		return htmlSnippets.get("CART_DETAILS");
184
	}
185
 
186
	public String getMyResearchSnippet(){
187
		return htmlSnippets.get("MY_RESEARCH");
188
	}
189
 
190
	public String getFooterSnippet(){
191
		return htmlSnippets.get("FOOTER");
192
	}
193
 
194
	public String getJsFileSnippet(){
195
		return htmlSnippets.get("JS_FILES");
196
	}
197
 
198
	public String getCssFileSnippet(){
199
		return htmlSnippets.get("CSS_FILES");
200
	}
201
 
202
	public long getNumberOfItems(){
203
		return userinfo.getTotalItems();
204
	}
205
}