Subversion Repositories SmartDukaan

Rev

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