Subversion Repositories SmartDukaan

Rev

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