Subversion Repositories SmartDukaan

Rev

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