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