Subversion Repositories SmartDukaan

Rev

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