Subversion Repositories SmartDukaan

Rev

Rev 5572 | Rev 5716 | 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", 
5614 rajveer 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
					}
5076 varun.gupt 136
					if (this.pincode.isEmpty()) {
137
						addActionError("Pincode is empty.");
138
						invalidInput = true;
139
					}
839 vikas 140
					if (!invalidInput) {
141
						Address address = new Address();
142
						address.setName(this.name);
143
						address.setLine1(this.line1);
144
						address.setLine2(this.line2);
145
						address.setCity(this.city);
146
						address.setState(this.state);
147
						address.setPin(this.pincode);
148
						address.setPhone(this.phone);
149
						address.setCountry(this.country);
150
						address.setEnabled(true);
151
						address.setType(AddressType.HOME);
1446 varun.gupt 152
						long addressId = userClient.addAddressForUser(userinfo.getUserId(), address, false);
153
						userClient.addAddressToCart(userinfo.getCartId(), addressId);
839 vikas 154
						addActionMessage("Address added successfully.");
3185 vikas 155
                        DataLogger.logData(EventType.SHIPPINIG_ADD_ADDRESS, getSessionId(), userinfo.getUserId(), userinfo.getEmail(), address.getName(),
2157 vikas 156
                                        address.getCity(), address.getPhone(), address.getPin());
839 vikas 157
					}
158
					return "redirect";				
517 rajveer 159
				}
160
 
1446 varun.gupt 161
				if(action.equals("change"))	{
841 vikas 162
					addressId = Long.parseLong(this.request.getParameter("addressid"));
5614 rajveer 163
					userClient.addAddressToCart(userinfo.getCartId(), addressId);
1446 varun.gupt 164
 
3561 rajveer 165
					errorMsg = userClient.validateCart(userinfo.getCartId(), sourceId);
3224 vikas 166
                    DataLogger.logData(EventType.SHIPPINIG_ADD_CHANGE, getSessionId(), userinfo.getUserId(), userinfo.getEmail(),
2157 vikas 167
                            Long.toString(userinfo.getCartId()), Long.toString(addressId));
1446 varun.gupt 168
					return "index";
637 rajveer 169
				}
517 rajveer 170
			}
637 rajveer 171
		} catch (Exception e) {
3076 chandransh 172
		    log.error("Error while adding address to the cart", e);
637 rajveer 173
			return "failure";
517 rajveer 174
		}
637 rajveer 175
		return null;
507 rajveer 176
	}
177
 
1429 varun.gupt 178
	public String getShippingHeaderSnippet() {
179
		String htmlString = "";
180
		VelocityContext context = new VelocityContext();
181
		String templateFile = "templates/shippingheader.vm";
182
		htmlString = pageLoader.getHtmlFromVelocity(templateFile, context);
183
		return htmlString;
507 rajveer 184
	}
185
 
1429 varun.gupt 186
	public String getShippingDetailsSnippet() {
5326 rajveer 187
		long userId = userinfo.getUserId();
1429 varun.gupt 188
		long cartId = userinfo.getCartId();
189
		String htmlString = "";
190
		VelocityContext context = new VelocityContext();
191
		String templateFile = "templates/shippingdetails.vm";
192
		List<Map<String,String>> items = null;
193
		double totalamount= 0.0;
194
		List<Address> addresses = null;
195
		long defaultAddressId = 0;
1774 varun.gupt 196
		String phoneNumber = "";
1429 varun.gupt 197
 
3126 rajveer 198
		CatalogClient catalogServiceClient  = null;
1429 varun.gupt 199
		in.shop2020.model.v1.catalog.InventoryService.Client catalogClient = null;
3126 rajveer 200
		UserClient userContextServiceClient = null;
1429 varun.gupt 201
		in.shop2020.model.v1.user.UserContextService.Client userClient = null;
202
 
2137 chandransh 203
		FormattingUtils formattingUtils = new FormattingUtils();
204
 
1429 varun.gupt 205
		try {
3126 rajveer 206
			catalogServiceClient = new CatalogClient();
1429 varun.gupt 207
			catalogClient = catalogServiceClient.getClient();
3126 rajveer 208
			userContextServiceClient = new UserClient();
1429 varun.gupt 209
			userClient = userContextServiceClient.getClient();
210
 
211
			Cart cart = userClient.getCart(cartId);
212
			List<Line> lineItems = cart.getLines();
213
 
214
			if( ! lineItems.isEmpty())	{
215
				items = new ArrayList<Map<String,String>>();
216
 
217
				for (Line line : lineItems) {
218
					Map<String, String> itemdetail = new HashMap<String, String>();
3561 rajveer 219
					Item item = catalogClient.getItemForSource(line.getItemId(), sourceId);
1429 varun.gupt 220
					String itemName = ((item.getBrand() != null) ? item.getBrand() + " " : "")
3830 chandransh 221
                                    + ((item.getModelName() != null) ? item.getModelName() + " " : "") 
222
                                    + (( item.getModelNumber() != null ) ? item.getModelNumber() + " " : "" );
223
 
224
                    String itemColor = "";
225
                    if(item.getColor() != null && !item.getColor().trim().equals("NA"))
226
                    itemColor = "Color - " + item.getColor();
227
 
228
                    itemdetail.put("ITEM_NAME", itemName);
229
                    itemdetail.put("ITEM_COLOR", itemColor);
1429 varun.gupt 230
					itemdetail.put("ITEM_ID", line.getItemId()+"");
3830 chandransh 231
					itemdetail.put("CATALOG_ID", item.getCatalogItemId() + "");
1429 varun.gupt 232
					itemdetail.put("ITEM_QUANTITY", ((int)line.getQuantity())+"");
2137 chandransh 233
					itemdetail.put("MRP", formattingUtils.formatPrice(item.getMrp()));
234
					itemdetail.put("SELLING_PRICE", formattingUtils.formatPrice(item.getSellingPrice()));
235
					itemdetail.put("TOTAL_PRICE", formattingUtils.formatPrice((item.getSellingPrice()*line.getQuantity())));
1429 varun.gupt 236
					itemdetail.put("SHIPPING_TIME", line.getEstimate()+"");
4217 varun.gupt 237
					itemdetail.put("BEST_DEAL_TEXT", item.getBestDealText());
2137 chandransh 238
					items.add(itemdetail);
1429 varun.gupt 239
				}
240
			}
2137 chandransh 241
			totalamount = cart.getTotalPrice();
5326 rajveer 242
			addresses = userClient.getAllAddressesForUser(userId);
243
			User user = userClient.getUserById(userId);
1774 varun.gupt 244
			phoneNumber = user.getMobileNumber();
1429 varun.gupt 245
 
246
			if(cart.isSetAddressId())	{
247
				defaultAddressId = cart.getAddressId();
248
			} else	{
5326 rajveer 249
				defaultAddressId = userClient.getDefaultAddressId(userId);
1429 varun.gupt 250
			}
1981 varun.gupt 251
 
252
			String couponCode = cart.getCouponCode();
253
	        context.put("couponcode", couponCode == null ? "" : couponCode);
2137 chandransh 254
	        context.put("discountedamount", formattingUtils.formatPrice(cart.getDiscountedPrice()));
1981 varun.gupt 255
 
1429 varun.gupt 256
		} catch (Exception e)	{
2949 chandransh 257
			log.error("Unable to get the shipping details", e);
1429 varun.gupt 258
		}
1981 varun.gupt 259
 
1774 varun.gupt 260
		context.put("phonenumber", phoneNumber);
1429 varun.gupt 261
		context.put("items", items);
2137 chandransh 262
		context.put("totalamount", formattingUtils.formatPrice(totalamount));
1429 varun.gupt 263
		context.put("addresses", addresses);
264
		context.put("defaultAddressId", defaultAddressId+"");
265
		context.put("errorMsg", errorMsg);
266
		htmlString = pageLoader.getHtmlFromVelocity(templateFile, context);
267
		return htmlString;
507 rajveer 268
	}
269
 
517 rajveer 270
	public long getAddressId(){
271
		return this.addressId;
272
	}
572 chandransh 273
 
274
	public String getErrorMsg(){
275
		return this.errorMsg;
276
	}
822 vikas 277
 
278
	public String getName() {
279
		return name;
280
	}
281
 
282
	public void setName(String name) {
283
		this.name = name;
284
	}
285
 
286
	public String getLine1() {
287
		return line1;
288
	}
289
 
290
	public void setLine1(String line1) {
291
		this.line1 = line1;
292
	}
293
 
294
	public String getLine2() {
295
		return line2;
296
	}
297
 
298
	public void setLine2(String line2) {
299
		this.line2 = line2;
300
	}
301
 
302
	public String getCity() {
303
		return city;
304
	}
305
 
306
	public void setCity(String city) {
307
		this.city = city;
308
	}
309
 
310
	public String getState() {
311
		return state;
312
	}
313
 
314
	public void setState(String state) {
315
		this.state = state;
316
	}
317
 
318
	public String getPincode() {
319
		return pincode;
320
	}
321
 
322
	public void setPincode(String pincode) {
323
		this.pincode = pincode;
324
	}
325
 
326
	public String getCountry() {
327
		return country;
328
	}
329
 
330
	public void setCountry(String country) {
331
		this.country = country;
332
	}
333
 
334
	public String getPhone() {
335
		return phone;
336
	}
337
 
338
	public void setPhone(String phone) {
339
		this.phone = phone;
340
	}
3903 varun.gupt 341
 
342
	@Override
343
	public String getHeaderSnippet() {
344
		String url = request.getQueryString();
345
		if (url == null) {
346
			url = "";
347
		} else {
348
			url = "?" + url;
349
		}
350
		url = request.getRequestURI() + url;
4453 varun.gupt 351
		return pageLoader.getHeaderHtml(userinfo.isLoggedIn(), userinfo.getEmail(), url , 0, false);
3903 varun.gupt 352
	}
1429 varun.gupt 353
}