Subversion Repositories SmartDukaan

Rev

Rev 1466 | Rev 1703 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
410 rajveer 1
package in.shop2020.serving.controllers;
2
 
650 rajveer 3
import java.util.Map;
4
import java.util.StringTokenizer;
5
 
555 chandransh 6
import in.shop2020.model.v1.user.ShoppingCartException;
572 chandransh 7
import in.shop2020.model.v1.user.UserContextService;
410 rajveer 8
import in.shop2020.serving.controllers.BaseController;
9
import in.shop2020.thrift.clients.UserContextServiceClient;
10
 
832 rajveer 11
import org.apache.log4j.Logger;
1614 rajveer 12
import org.apache.struts2.convention.annotation.Action;
13
import org.apache.struts2.convention.annotation.InterceptorRef;
14
import org.apache.struts2.convention.annotation.InterceptorRefs;
801 rajveer 15
import org.apache.struts2.convention.annotation.Result;
1614 rajveer 16
import org.apache.struts2.convention.annotation.Results;
410 rajveer 17
import org.apache.struts2.interceptor.ParameterAware;
18
import org.apache.thrift.TException;
19
 
1614 rajveer 20
 
21
@Results({
22
	@Result(name="redirect", type="redirectAction", 
23
	   		params = {"actionName" , "cart"}),
24
	@Result(name="failure", location="cart-failure.vm"),
25
	@Result(name="success", location="cart-success.vm")
26
})
27
 
28
 
507 rajveer 29
public class CartController extends BaseController implements ParameterAware{
410 rajveer 30
 
31
	private static final long serialVersionUID = 1L;
832 rajveer 32
	private static Logger log = Logger.getLogger(Class.class);	
507 rajveer 33
	Map<String, String[]> reqparams = null;
410 rajveer 34
 
650 rajveer 35
 
572 chandransh 36
	private String errorMsg = "";
410 rajveer 37
 
38
	public CartController(){
507 rajveer 39
		super();
40
	}
41
 
42
	 // GET /cart
1614 rajveer 43
 
44
	@Action(value="cart",interceptorRefs={@InterceptorRef("myDefault")})
650 rajveer 45
	 public String index() {
572 chandransh 46
    	try {
47
			UserContextService.Client userClient = (new UserContextServiceClient()).getClient();
1466 ankur.sing 48
			errorMsg = userClient.validateCart(userinfo.getCartId());
572 chandransh 49
		} catch (Exception e) {
50
			// This exception can be ignored for showing the cart. Not so
51
			// innocent when this occurs at the time of checkout or when the
52
			// user is proceeding to pay.
53
			e.printStackTrace();
54
		}
410 rajveer 55
 
650 rajveer 56
		htmlSnippets.put("CART_HEADER", pageLoader.getCartHeaderHtml());
786 rajveer 57
		htmlSnippets.put("CART_DETAILS", pageLoader.getCartDetailsHtml(userinfo.getUserId(), userinfo.getCartId(), errorMsg));
650 rajveer 58
    	return "index";
507 rajveer 59
	 }
60
 
1614 rajveer 61
 
62
 
572 chandransh 63
	// POST /entity
1614 rajveer 64
 
65
	@Action(value="addtocart",interceptorRefs={@InterceptorRef("createuser"),@InterceptorRef("myDefault")})
572 chandransh 66
	public String create() {
67
		log.info("CartController.create");
555 chandransh 68
 
572 chandransh 69
		printParams();
70
 
71
		long userId = userinfo.getUserId();
72
		long cartId = userinfo.getCartId();
73
 
1614 rajveer 74
		log.info("user id is " + userId);
75
		log.info("cart id is " + cartId);
76
 
572 chandransh 77
		log.info("item id is " + this.reqparams.get("productid"));
1614 rajveer 78
 
79
		if (this.reqparams.get("historyitems") != null) {
80
			String historyItems = this.reqparams.get("productid")[0];
81
			updateHistory(historyItems);
82
		}
83
 
637 rajveer 84
		String itemIds = "";
572 chandransh 85
		if (this.reqparams.get("productid") != null) {
86
			itemIds = this.reqparams.get("productid")[0];
637 rajveer 87
		}else{
88
			return "failure";
572 chandransh 89
		}
637 rajveer 90
 
572 chandransh 91
 
92
		StringTokenizer tokenizer = new StringTokenizer(itemIds, "_");
93
		while (tokenizer.hasMoreTokens()) {
94
			long itemId = Long.parseLong(tokenizer.nextToken());
95
 
96
			try {
97
				UserContextServiceClient userServiceClient = new UserContextServiceClient();
98
				UserContextService.Client userClient = userServiceClient.getClient();
762 rajveer 99
				if (cartId == 0){
555 chandransh 100
					cartId = userClient.createCart(userId);
762 rajveer 101
				}
572 chandransh 102
				userClient.addItemToCart(cartId, itemId, 1);
762 rajveer 103
				userinfo.setCartId(cartId);
104
				int totalItems = userClient.getCart(cartId).getLinesSize();
105
				userinfo.setTotalItems(totalItems);
572 chandransh 106
			} catch (TException e) {
107
				e.printStackTrace();
108
			} catch (Exception e) {
109
				e.printStackTrace();
507 rajveer 110
			}
111
 
572 chandransh 112
		}
507 rajveer 113
 
572 chandransh 114
		return "success";
115
	}		
116
 
1614 rajveer 117
	private void updateHistory(String historyItems) {
118
		UserContextServiceClient userServiceClient;
119
		try {
120
			userServiceClient = new UserContextServiceClient();
121
			UserContextService.Client userClient = userServiceClient.getClient();
122
			StringTokenizer tokenizer = new StringTokenizer(historyItems, "_");
123
 
124
			while (tokenizer.hasMoreTokens()) {
125
				long itemId = Long.parseLong(tokenizer.nextToken());
126
				userClient.updateBrowseHistory(userinfo.getUserId(), itemId);
127
			}	
128
		} catch (Exception e) {
129
			e.printStackTrace();
130
		}
131
 
132
 
133
	}
134
 
507 rajveer 135
		// DELETE /entity
136
		public String destroy() {
137
	    	log.info("CartController.destroy");
138
	    	printParams();
139
	    	log.info("item id is " + this.request.getParameter("productid"));
140
			String itemIdString = this.request.getParameter("productid");
141
			long itemId = Long.parseLong(itemIdString);
517 rajveer 142
			if(userinfo.getCartId() == -1){
143
				log.info("Cart does not exist. Nothing to delete.");
144
			}else{
762 rajveer 145
				if(deleteItemFromCart(userinfo.getCartId(), itemId, userinfo.getUserId(), userinfo.isSessionId())){
146
					userinfo.setTotalItems(getNumberOfItemsInCart(userinfo.getCartId()));
801 rajveer 147
					return "redirect";	
517 rajveer 148
				}
507 rajveer 149
			}
801 rajveer 150
			return "redirect";
507 rajveer 151
		}
152
 
153
 
154
		// DELETE /entity
155
		public String update() {
156
	    	log.info("CartController.update");
157
	    	printParams();
158
	    	log.info("item id is " + this.request.getParameter("productid"));
159
	    	log.info("item id is " + this.request.getParameter("quantity"));
160
			String itemIdString = this.request.getParameter("productid");
161
			String quantityString = this.request.getParameter("quantity");
162
			long itemId = Long.parseLong(itemIdString);
163
			long quantity = Long.parseLong(quantityString);
517 rajveer 164
			if(quantity <= 0){
165
				log.info("Not valid item quantity. Unable to change item quantity.");
166
			}else{
762 rajveer 167
				if(updateItemQuantityInCart(userinfo.getCartId(), itemId, quantity)){
801 rajveer 168
					return "redirect";	
517 rajveer 169
				}
507 rajveer 170
			}
801 rajveer 171
			addActionError("Unable to update the quantity");
172
			return "redirect";
507 rajveer 173
		}
174
 
175
 
176
    public void printParams(){
177
    	for(String param : reqparams.keySet()) {
178
    		log.info("param name is " + param);
179
    		log.info("param first is " + reqparams.get(param)[0]);
180
    	}
181
    	log.info(this.reqparams);
182
    }
183
 
762 rajveer 184
	private boolean updateItemQuantityInCart(long cartId, long itemId, long quantity){
185
		try {
186
			UserContextServiceClient userContextServiceClient = new UserContextServiceClient();
187
			in.shop2020.model.v1.user.UserContextService.Client userClient = userContextServiceClient.getClient();
188
 
189
			userClient.changeQuantity(cartId, itemId, quantity);
190
			return true;
191
		} catch (ShoppingCartException e) {
192
			e.printStackTrace();
193
		} catch (TException e) {
194
			e.printStackTrace();
195
		} catch (Exception e) {
196
			e.printStackTrace();
197
		}
198
		return false;
199
	}
200
 
201
	private boolean deleteItemFromCart(long cartId, long catalogItemId, long userId, boolean isSessionId){
202
		try {
203
			UserContextServiceClient userContextServiceClient = new UserContextServiceClient();
204
			in.shop2020.model.v1.user.UserContextService.Client userClient = userContextServiceClient.getClient();
205
 
206
			userClient.deleteItemFromCart(cartId, catalogItemId);
207
			return true;	
208
		} catch (ShoppingCartException e) {
209
			e.printStackTrace();
210
		} catch (TException e) {
211
			e.printStackTrace();
212
		} catch (Exception e) {
213
			e.printStackTrace();
214
		}
215
 
216
		return false;
217
	}
218
 
219
 
220
 
221
	private int getNumberOfItemsInCart(long cartId) {
222
		int numberOfItems = 0;
223
		UserContextServiceClient userContextServiceClient = null;
224
		try {
225
			userContextServiceClient = new UserContextServiceClient();
226
			in.shop2020.model.v1.user.UserContextService.Client userClient = userContextServiceClient.getClient();
227
 
228
			numberOfItems = userClient.getCart(cartId).getLinesSize();
229
		} catch (ShoppingCartException e) {
230
			e.printStackTrace();
231
		} catch (TException e) {
232
			e.printStackTrace();
233
		} catch (Exception e) {
234
			e.printStackTrace();
235
		}
236
		return numberOfItems;
237
	}
238
 
239
 
507 rajveer 240
	public String getCartHeaderSnippet(){
241
		return htmlSnippets.get("CART_HEADER");
242
	}
243
 
244
	public String getCartDetailsSnippet(){
245
		return htmlSnippets.get("CART_DETAILS");
246
	}
247
 
248
	public long getNumberOfItems(){
249
		return userinfo.getTotalItems();
250
	}
650 rajveer 251
 
252
	@Override
253
	public void setParameters(Map<String, String[]> parameters) {
254
		this.reqparams = parameters;	
255
	}
507 rajveer 256
}