Subversion Repositories SmartDukaan

Rev

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