Subversion Repositories SmartDukaan

Rev

Rev 1614 | Rev 1957 | 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
 
637 rajveer 79
		String itemIds = "";
572 chandransh 80
		if (this.reqparams.get("productid") != null) {
81
			itemIds = this.reqparams.get("productid")[0];
637 rajveer 82
		}else{
83
			return "failure";
572 chandransh 84
		}
637 rajveer 85
 
572 chandransh 86
 
87
		StringTokenizer tokenizer = new StringTokenizer(itemIds, "_");
88
		while (tokenizer.hasMoreTokens()) {
89
			long itemId = Long.parseLong(tokenizer.nextToken());
90
 
91
			try {
92
				UserContextServiceClient userServiceClient = new UserContextServiceClient();
93
				UserContextService.Client userClient = userServiceClient.getClient();
762 rajveer 94
				if (cartId == 0){
555 chandransh 95
					cartId = userClient.createCart(userId);
762 rajveer 96
				}
572 chandransh 97
				userClient.addItemToCart(cartId, itemId, 1);
762 rajveer 98
				userinfo.setCartId(cartId);
99
				int totalItems = userClient.getCart(cartId).getLinesSize();
100
				userinfo.setTotalItems(totalItems);
572 chandransh 101
			} catch (TException e) {
102
				e.printStackTrace();
103
			} catch (Exception e) {
104
				e.printStackTrace();
507 rajveer 105
			}
106
 
572 chandransh 107
		}
507 rajveer 108
 
572 chandransh 109
		return "success";
110
	}		
111
 
1614 rajveer 112
 
507 rajveer 113
		// DELETE /entity
114
		public String destroy() {
115
	    	log.info("CartController.destroy");
116
	    	printParams();
117
	    	log.info("item id is " + this.request.getParameter("productid"));
118
			String itemIdString = this.request.getParameter("productid");
119
			long itemId = Long.parseLong(itemIdString);
517 rajveer 120
			if(userinfo.getCartId() == -1){
121
				log.info("Cart does not exist. Nothing to delete.");
122
			}else{
762 rajveer 123
				if(deleteItemFromCart(userinfo.getCartId(), itemId, userinfo.getUserId(), userinfo.isSessionId())){
124
					userinfo.setTotalItems(getNumberOfItemsInCart(userinfo.getCartId()));
801 rajveer 125
					return "redirect";	
517 rajveer 126
				}
507 rajveer 127
			}
801 rajveer 128
			return "redirect";
507 rajveer 129
		}
130
 
131
 
132
		// DELETE /entity
133
		public String update() {
134
	    	log.info("CartController.update");
135
	    	printParams();
136
	    	log.info("item id is " + this.request.getParameter("productid"));
137
	    	log.info("item id is " + this.request.getParameter("quantity"));
138
			String itemIdString = this.request.getParameter("productid");
139
			String quantityString = this.request.getParameter("quantity");
140
			long itemId = Long.parseLong(itemIdString);
141
			long quantity = Long.parseLong(quantityString);
517 rajveer 142
			if(quantity <= 0){
143
				log.info("Not valid item quantity. Unable to change item quantity.");
144
			}else{
762 rajveer 145
				if(updateItemQuantityInCart(userinfo.getCartId(), itemId, quantity)){
801 rajveer 146
					return "redirect";	
517 rajveer 147
				}
507 rajveer 148
			}
801 rajveer 149
			addActionError("Unable to update the quantity");
150
			return "redirect";
507 rajveer 151
		}
152
 
153
 
154
    public void printParams(){
155
    	for(String param : reqparams.keySet()) {
156
    		log.info("param name is " + param);
157
    		log.info("param first is " + reqparams.get(param)[0]);
158
    	}
159
    	log.info(this.reqparams);
160
    }
161
 
762 rajveer 162
	private boolean updateItemQuantityInCart(long cartId, long itemId, long quantity){
163
		try {
164
			UserContextServiceClient userContextServiceClient = new UserContextServiceClient();
165
			in.shop2020.model.v1.user.UserContextService.Client userClient = userContextServiceClient.getClient();
166
 
167
			userClient.changeQuantity(cartId, itemId, quantity);
168
			return true;
169
		} catch (ShoppingCartException e) {
170
			e.printStackTrace();
171
		} catch (TException e) {
172
			e.printStackTrace();
173
		} catch (Exception e) {
174
			e.printStackTrace();
175
		}
176
		return false;
177
	}
178
 
179
	private boolean deleteItemFromCart(long cartId, long catalogItemId, long userId, boolean isSessionId){
180
		try {
181
			UserContextServiceClient userContextServiceClient = new UserContextServiceClient();
182
			in.shop2020.model.v1.user.UserContextService.Client userClient = userContextServiceClient.getClient();
183
 
184
			userClient.deleteItemFromCart(cartId, catalogItemId);
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
 
194
		return false;
195
	}
196
 
197
 
198
 
199
	private int getNumberOfItemsInCart(long cartId) {
200
		int numberOfItems = 0;
201
		UserContextServiceClient userContextServiceClient = null;
202
		try {
203
			userContextServiceClient = new UserContextServiceClient();
204
			in.shop2020.model.v1.user.UserContextService.Client userClient = userContextServiceClient.getClient();
205
 
206
			numberOfItems = userClient.getCart(cartId).getLinesSize();
207
		} catch (ShoppingCartException e) {
208
			e.printStackTrace();
209
		} catch (TException e) {
210
			e.printStackTrace();
211
		} catch (Exception e) {
212
			e.printStackTrace();
213
		}
214
		return numberOfItems;
215
	}
216
 
217
 
507 rajveer 218
	public String getCartHeaderSnippet(){
219
		return htmlSnippets.get("CART_HEADER");
220
	}
221
 
222
	public String getCartDetailsSnippet(){
223
		return htmlSnippets.get("CART_DETAILS");
224
	}
225
 
226
	public long getNumberOfItems(){
227
		return userinfo.getTotalItems();
228
	}
650 rajveer 229
 
230
	@Override
231
	public void setParameters(Map<String, String[]> parameters) {
232
		this.reqparams = parameters;	
233
	}
507 rajveer 234
}