Subversion Repositories SmartDukaan

Rev

Rev 4217 | Rev 5076 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
507 rajveer 1
package in.shop2020.serving.controllers;
2
 
2263 vikas 3
import in.shop2020.datalogger.EventType;
1429 varun.gupt 4
import in.shop2020.model.v1.catalog.Item;
507 rajveer 5
import in.shop2020.model.v1.user.Address;
6
import in.shop2020.model.v1.user.AddressType;
841 vikas 7
import in.shop2020.model.v1.user.Cart;
1429 varun.gupt 8
import in.shop2020.model.v1.user.Line;
1774 varun.gupt 9
import in.shop2020.model.v1.user.User;
572 chandransh 10
import in.shop2020.model.v1.user.UserContextService;
2137 chandransh 11
import in.shop2020.serving.utils.FormattingUtils;
839 vikas 12
import in.shop2020.serving.utils.Utils;
3126 rajveer 13
import in.shop2020.thrift.clients.CatalogClient;
14
import in.shop2020.thrift.clients.UserClient;
2511 vikas 15
import in.shop2020.utils.DataLogger;
507 rajveer 16
 
2419 vikas 17
import java.util.ArrayList;
18
import java.util.Collection;
19
import java.util.HashMap;
20
import java.util.List;
21
import java.util.Map;
507 rajveer 22
 
832 rajveer 23
import org.apache.log4j.Logger;
822 vikas 24
import org.apache.struts2.convention.annotation.InterceptorRef;
25
import org.apache.struts2.convention.annotation.InterceptorRefs;
637 rajveer 26
import org.apache.struts2.convention.annotation.Result;
27
import org.apache.struts2.convention.annotation.Results;
1429 varun.gupt 28
import org.apache.velocity.VelocityContext;
507 rajveer 29
 
3076 chandransh 30
@SuppressWarnings("serial")
822 vikas 31
@InterceptorRefs({
32
    @InterceptorRef("myDefault"),
33
    @InterceptorRef("login")
34
})
35
 
637 rajveer 36
@Results({
37
    @Result(name="redirect", type="redirectAction", 
822 vikas 38
    		params = {"actionName" , "shipping"})
637 rajveer 39
})
822 vikas 40
public class ShippingController extends BaseController{
507 rajveer 41
 
3101 chandransh 42
	private static Logger log = Logger.getLogger(ShippingController.class);
3063 chandransh 43
 
517 rajveer 44
	private long addressId = 0;
572 chandransh 45
	private String errorMsg = "";
507 rajveer 46
 
822 vikas 47
	private String name;
48
	private String line1;
49
	private String line2;
50
	private String city;
839 vikas 51
	private String state = "";
822 vikas 52
	private String pincode;
53
	private String phone;
54
	private String country;
55
 
507 rajveer 56
	public ShippingController(){
57
		super();
58
	}
59
 
60
	 // GET /shipping
637 rajveer 61
	 public String index() {
555 chandransh 62
    	long userId = userinfo.getUserId();
692 chandransh 63
    	long cartId = userinfo.getCartId();
3209 vikas 64
    	String itemIdString = "";
572 chandransh 65
    	try {
3126 rajveer 66
			UserContextService.Client userClient = (new UserClient()).getClient();
692 chandransh 67
			userClient.checkOut(cartId);
841 vikas 68
			Cart cart = userClient.getCart(cartId);
3209 vikas 69
			itemIdString = Utils.getItemIdStringInCart(cart);
841 vikas 70
			long defaultAddressId = cart.getAddressId();
71
			if (defaultAddressId == 0) {
72
			    defaultAddressId = userClient.getDefaultAddressId(userId);
73
			}
637 rajveer 74
			log.info("The default address id of this user is: " + defaultAddressId);
75
			if(defaultAddressId > 0)
692 chandransh 76
				userClient.addAddressToCart(cartId, defaultAddressId);
3561 rajveer 77
			errorMsg = userClient.validateCart(cartId, sourceId);
572 chandransh 78
 
79
		} catch (Exception e) {
2949 chandransh 80
			// This exception can be ignored for showing the shipping page. Not so
572 chandransh 81
			// innocent when this occurs at the time of checkout or when the
82
			// user is proceeding to pay.
2949 chandransh 83
			log.error("Unable to get all the data required for displaying the shipping page", e);
572 chandransh 84
		}
712 rajveer 85
    	// in case this page is redirected from payment failure
822 vikas 86
 
839 vikas 87
		Collection<String> actionErrors = getActionErrors();
88
		if(actionErrors != null && !actionErrors.isEmpty()){
89
			for (String str : actionErrors) {
90
			    errorMsg += "<BR/>" + str;
91
			}
712 rajveer 92
		}
3185 vikas 93
        DataLogger.logData(EventType.SHIPPINIG_ACCESS, getSessionId(), userinfo.getUserId(), userinfo.getEmail(),
3209 vikas 94
                Long.toString(cartId), itemIdString);
822 vikas 95
 
637 rajveer 96
    	return "index";
507 rajveer 97
	 }
98
 
99
	// POST /entity
637 rajveer 100
	public String create(){
3126 rajveer 101
		UserClient userContextServiceClient = null;
555 chandransh 102
		in.shop2020.model.v1.user.UserContextService.Client userClient = null;
103
 
637 rajveer 104
    	String action = this.request.getParameter("action");
517 rajveer 105
		if(action == null){
637 rajveer 106
			return "failure";
517 rajveer 107
		}
507 rajveer 108
 
555 chandransh 109
		try {
3126 rajveer 110
			userContextServiceClient = new UserClient();
555 chandransh 111
			userClient = userContextServiceClient.getClient();
517 rajveer 112
 
637 rajveer 113
			if(action != null){
114
				if(action.equals("add")){
839 vikas 115
					boolean invalidInput = false;
116
					if (!Utils.validatePin(this.pincode)) {
117
						addActionError("Invalid pincode.");
118
						invalidInput = true;
119
					}
120
					if (!Utils.validatePhone(this.phone)) {
121
						addActionError("Invalid phone number.");
122
						invalidInput = true;
123
					}
124
					if (this.line1.isEmpty()) {
125
						addActionError("Address line1 is empty.");
126
						invalidInput = true;
127
					}
128
					if (this.city.isEmpty()) {
129
						addActionError("City name is empty.");
130
						invalidInput = true;
131
					}
132
					if (this.state.isEmpty()) {
133
						addActionError("State name is empty.");
134
						invalidInput = true;
135
					}
136
					if (!invalidInput) {
137
						Address address = new Address();
138
						address.setName(this.name);
139
						address.setLine1(this.line1);
140
						address.setLine2(this.line2);
141
						address.setCity(this.city);
142
						address.setState(this.state);
143
						address.setPin(this.pincode);
144
						address.setPhone(this.phone);
145
						address.setCountry(this.country);
146
						address.setEnabled(true);
147
						address.setType(AddressType.HOME);
1446 varun.gupt 148
						long addressId = userClient.addAddressForUser(userinfo.getUserId(), address, false);
149
						userClient.addAddressToCart(userinfo.getCartId(), addressId);
839 vikas 150
						addActionMessage("Address added successfully.");
3185 vikas 151
                        DataLogger.logData(EventType.SHIPPINIG_ADD_ADDRESS, getSessionId(), userinfo.getUserId(), userinfo.getEmail(), address.getName(),
2157 vikas 152
                                        address.getCity(), address.getPhone(), address.getPin());
839 vikas 153
					}
154
					return "redirect";				
517 rajveer 155
				}
156
 
1446 varun.gupt 157
				if(action.equals("change"))	{
841 vikas 158
					addressId = Long.parseLong(this.request.getParameter("addressid"));
637 rajveer 159
					userClient.addAddressToCart(userinfo.getCartId(), addressId);
1446 varun.gupt 160
 
3561 rajveer 161
					errorMsg = userClient.validateCart(userinfo.getCartId(), sourceId);
3224 vikas 162
                    DataLogger.logData(EventType.SHIPPINIG_ADD_CHANGE, getSessionId(), userinfo.getUserId(), userinfo.getEmail(),
2157 vikas 163
                            Long.toString(userinfo.getCartId()), Long.toString(addressId));
1446 varun.gupt 164
					return "index";
637 rajveer 165
				}
517 rajveer 166
			}
637 rajveer 167
		} catch (Exception e) {
3076 chandransh 168
		    log.error("Error while adding address to the cart", e);
637 rajveer 169
			return "failure";
517 rajveer 170
		}
637 rajveer 171
		return null;
507 rajveer 172
	}
173
 
1429 varun.gupt 174
	public String getShippingHeaderSnippet() {
175
		String htmlString = "";
176
		VelocityContext context = new VelocityContext();
177
		String templateFile = "templates/shippingheader.vm";
178
		htmlString = pageLoader.getHtmlFromVelocity(templateFile, context);
179
		return htmlString;
507 rajveer 180
	}
181
 
1429 varun.gupt 182
	public String getShippingDetailsSnippet() {
183
 
184
		long cartId = userinfo.getCartId();
185
		String htmlString = "";
186
		VelocityContext context = new VelocityContext();
187
		String templateFile = "templates/shippingdetails.vm";
188
		List<Map<String,String>> items = null;
189
		double totalamount= 0.0;
190
		List<Address> addresses = null;
191
		long defaultAddressId = 0;
1774 varun.gupt 192
		String phoneNumber = "";
1429 varun.gupt 193
 
3126 rajveer 194
		CatalogClient catalogServiceClient  = null;
1429 varun.gupt 195
		in.shop2020.model.v1.catalog.InventoryService.Client catalogClient = null;
3126 rajveer 196
		UserClient userContextServiceClient = null;
1429 varun.gupt 197
		in.shop2020.model.v1.user.UserContextService.Client userClient = null;
198
 
2137 chandransh 199
		FormattingUtils formattingUtils = new FormattingUtils();
200
 
1429 varun.gupt 201
		try {
3126 rajveer 202
			catalogServiceClient = new CatalogClient();
1429 varun.gupt 203
			catalogClient = catalogServiceClient.getClient();
3126 rajveer 204
			userContextServiceClient = new UserClient();
1429 varun.gupt 205
			userClient = userContextServiceClient.getClient();
206
 
207
			Cart cart = userClient.getCart(cartId);
208
			List<Line> lineItems = cart.getLines();
209
 
210
			if( ! lineItems.isEmpty())	{
211
				items = new ArrayList<Map<String,String>>();
212
 
213
				for (Line line : lineItems) {
214
					Map<String, String> itemdetail = new HashMap<String, String>();
3561 rajveer 215
					Item item = catalogClient.getItemForSource(line.getItemId(), sourceId);
1429 varun.gupt 216
					String itemName = ((item.getBrand() != null) ? item.getBrand() + " " : "")
3830 chandransh 217
                                    + ((item.getModelName() != null) ? item.getModelName() + " " : "") 
218
                                    + (( item.getModelNumber() != null ) ? item.getModelNumber() + " " : "" );
219
 
220
                    String itemColor = "";
221
                    if(item.getColor() != null && !item.getColor().trim().equals("NA"))
222
                    itemColor = "Color - " + item.getColor();
223
 
224
                    itemdetail.put("ITEM_NAME", itemName);
225
                    itemdetail.put("ITEM_COLOR", itemColor);
1429 varun.gupt 226
					itemdetail.put("ITEM_ID", line.getItemId()+"");
3830 chandransh 227
					itemdetail.put("CATALOG_ID", item.getCatalogItemId() + "");
1429 varun.gupt 228
					itemdetail.put("ITEM_QUANTITY", ((int)line.getQuantity())+"");
2137 chandransh 229
					itemdetail.put("MRP", formattingUtils.formatPrice(item.getMrp()));
230
					itemdetail.put("SELLING_PRICE", formattingUtils.formatPrice(item.getSellingPrice()));
231
					itemdetail.put("TOTAL_PRICE", formattingUtils.formatPrice((item.getSellingPrice()*line.getQuantity())));
1429 varun.gupt 232
					itemdetail.put("SHIPPING_TIME", line.getEstimate()+"");
4217 varun.gupt 233
					itemdetail.put("BEST_DEAL_TEXT", item.getBestDealText());
2137 chandransh 234
					items.add(itemdetail);
1429 varun.gupt 235
				}
236
			}
2137 chandransh 237
			totalamount = cart.getTotalPrice();
1429 varun.gupt 238
			addresses = userClient.getAllAddressesForUser(cart.getUserId());
1774 varun.gupt 239
			User user = userClient.getUserById(cart.getUserId());
240
			phoneNumber = user.getMobileNumber();
1429 varun.gupt 241
 
242
			if(cart.isSetAddressId())	{
243
				defaultAddressId = cart.getAddressId();
244
			} else	{
245
				defaultAddressId = userClient.getDefaultAddressId(cart.getUserId());
246
			}
1981 varun.gupt 247
 
248
			String couponCode = cart.getCouponCode();
249
	        context.put("couponcode", couponCode == null ? "" : couponCode);
2137 chandransh 250
	        context.put("discountedamount", formattingUtils.formatPrice(cart.getDiscountedPrice()));
1981 varun.gupt 251
 
1429 varun.gupt 252
		} catch (Exception e)	{
2949 chandransh 253
			log.error("Unable to get the shipping details", e);
1429 varun.gupt 254
		}
1981 varun.gupt 255
 
1774 varun.gupt 256
		context.put("phonenumber", phoneNumber);
1429 varun.gupt 257
		context.put("items", items);
2137 chandransh 258
		context.put("totalamount", formattingUtils.formatPrice(totalamount));
1429 varun.gupt 259
		context.put("addresses", addresses);
260
		context.put("defaultAddressId", defaultAddressId+"");
261
		context.put("errorMsg", errorMsg);
262
		htmlString = pageLoader.getHtmlFromVelocity(templateFile, context);
263
		return htmlString;
507 rajveer 264
	}
265
 
517 rajveer 266
	public long getAddressId(){
267
		return this.addressId;
268
	}
572 chandransh 269
 
270
	public String getErrorMsg(){
271
		return this.errorMsg;
272
	}
822 vikas 273
 
274
	public String getName() {
275
		return name;
276
	}
277
 
278
	public void setName(String name) {
279
		this.name = name;
280
	}
281
 
282
	public String getLine1() {
283
		return line1;
284
	}
285
 
286
	public void setLine1(String line1) {
287
		this.line1 = line1;
288
	}
289
 
290
	public String getLine2() {
291
		return line2;
292
	}
293
 
294
	public void setLine2(String line2) {
295
		this.line2 = line2;
296
	}
297
 
298
	public String getCity() {
299
		return city;
300
	}
301
 
302
	public void setCity(String city) {
303
		this.city = city;
304
	}
305
 
306
	public String getState() {
307
		return state;
308
	}
309
 
310
	public void setState(String state) {
311
		this.state = state;
312
	}
313
 
314
	public String getPincode() {
315
		return pincode;
316
	}
317
 
318
	public void setPincode(String pincode) {
319
		this.pincode = pincode;
320
	}
321
 
322
	public String getCountry() {
323
		return country;
324
	}
325
 
326
	public void setCountry(String country) {
327
		this.country = country;
328
	}
329
 
330
	public String getPhone() {
331
		return phone;
332
	}
333
 
334
	public void setPhone(String phone) {
335
		this.phone = phone;
336
	}
3903 varun.gupt 337
 
338
	@Override
339
	public String getHeaderSnippet() {
340
		String url = request.getQueryString();
341
		if (url == null) {
342
			url = "";
343
		} else {
344
			url = "?" + url;
345
		}
346
		url = request.getRequestURI() + url;
4453 varun.gupt 347
		return pageLoader.getHeaderHtml(userinfo.isLoggedIn(), userinfo.getEmail(), url , 0, false);
3903 varun.gupt 348
	}
1429 varun.gupt 349
}