Subversion Repositories SmartDukaan

Rev

Details | 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;
419 rajveer 9
import in.shop2020.serving.utils.Utils;
410 rajveer 10
import in.shop2020.thrift.clients.UserContextServiceClient;
11
 
12
import org.apache.juli.logging.Log;
13
import org.apache.juli.logging.LogFactory;
14
import org.apache.struts2.interceptor.ParameterAware;
15
import org.apache.thrift.TException;
16
 
507 rajveer 17
public class CartController extends BaseController implements ParameterAware{
410 rajveer 18
 
19
	private static final long serialVersionUID = 1L;
507 rajveer 20
	private static Log log = LogFactory.getLog(CartController.class);
21
	Map<String, String[]> reqparams = null;
410 rajveer 22
 
650 rajveer 23
 
572 chandransh 24
	private String errorMsg = "";
410 rajveer 25
 
26
	public CartController(){
507 rajveer 27
		super();
28
	}
29
 
30
	 // GET /cart
650 rajveer 31
	 public String index() {
572 chandransh 32
    	try {
33
			UserContextService.Client userClient = (new UserContextServiceClient()).getClient();
34
			if(!userClient.validateCart(userinfo.getCartId()))
35
				errorMsg = "Your cart has been updated.";
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());
44
		htmlSnippets.put("CART_DETAILS", pageLoader.getCartDetailsHtml(userinfo.getCartId(), errorMsg));
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()));
517 rajveer 104
					return "delsuccess";	
105
				}
507 rajveer 106
			}
107
			return "delfailure";
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)){
517 rajveer 125
					return "delsuccess";	
126
				}
507 rajveer 127
			}
128
			return "delfailure";
129
		}
130
 
131
 
132
    public void printParams(){
133
    	for(String param : reqparams.keySet()) {
134
    		log.info("param name is " + param);
135
    		log.info("param first is " + reqparams.get(param)[0]);
136
    	}
137
    	log.info(this.reqparams);
138
    }
139
 
762 rajveer 140
	private boolean updateItemQuantityInCart(long cartId, long itemId, long quantity){
141
		try {
142
			UserContextServiceClient userContextServiceClient = new UserContextServiceClient();
143
			in.shop2020.model.v1.user.UserContextService.Client userClient = userContextServiceClient.getClient();
144
 
145
			userClient.changeQuantity(cartId, itemId, quantity);
146
			return true;
147
		} catch (ShoppingCartException e) {
148
			e.printStackTrace();
149
		} catch (TException e) {
150
			e.printStackTrace();
151
		} catch (Exception e) {
152
			e.printStackTrace();
153
		}
154
		return false;
155
	}
156
 
157
	private boolean deleteItemFromCart(long cartId, long catalogItemId, long userId, boolean isSessionId){
158
		try {
159
			UserContextServiceClient userContextServiceClient = new UserContextServiceClient();
160
			in.shop2020.model.v1.user.UserContextService.Client userClient = userContextServiceClient.getClient();
161
 
162
			userClient.deleteItemFromCart(cartId, catalogItemId);
163
			return true;	
164
		} catch (ShoppingCartException e) {
165
			e.printStackTrace();
166
		} catch (TException e) {
167
			e.printStackTrace();
168
		} catch (Exception e) {
169
			e.printStackTrace();
170
		}
171
 
172
		return false;
173
	}
174
 
175
 
176
 
177
	private int getNumberOfItemsInCart(long cartId) {
178
		int numberOfItems = 0;
179
		UserContextServiceClient userContextServiceClient = null;
180
		try {
181
			userContextServiceClient = new UserContextServiceClient();
182
			in.shop2020.model.v1.user.UserContextService.Client userClient = userContextServiceClient.getClient();
183
 
184
			numberOfItems = userClient.getCart(cartId).getLinesSize();
185
		} catch (ShoppingCartException e) {
186
			e.printStackTrace();
187
		} catch (TException e) {
188
			e.printStackTrace();
189
		} catch (Exception e) {
190
			e.printStackTrace();
191
		}
192
		return numberOfItems;
193
	}
194
 
195
 
507 rajveer 196
	public String getCartHeaderSnippet(){
197
		return htmlSnippets.get("CART_HEADER");
198
	}
199
 
200
	public String getCartDetailsSnippet(){
201
		return htmlSnippets.get("CART_DETAILS");
202
	}
203
 
204
	public long getNumberOfItems(){
205
		return userinfo.getTotalItems();
206
	}
650 rajveer 207
 
208
	@Override
209
	public void setParameters(Map<String, String[]> parameters) {
210
		this.reqparams = parameters;	
211
	}
507 rajveer 212
}