Subversion Repositories SmartDukaan

Rev

Rev 3903 | Rev 4453 | 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 fullName = "";
193
		String phoneNumber = "";
1429 varun.gupt 194
 
3126 rajveer 195
		CatalogClient catalogServiceClient  = null;
1429 varun.gupt 196
		in.shop2020.model.v1.catalog.InventoryService.Client catalogClient = null;
3126 rajveer 197
		UserClient userContextServiceClient = null;
1429 varun.gupt 198
		in.shop2020.model.v1.user.UserContextService.Client userClient = null;
199
 
2137 chandransh 200
		FormattingUtils formattingUtils = new FormattingUtils();
201
 
1429 varun.gupt 202
		try {
3126 rajveer 203
			catalogServiceClient = new CatalogClient();
1429 varun.gupt 204
			catalogClient = catalogServiceClient.getClient();
3126 rajveer 205
			userContextServiceClient = new UserClient();
1429 varun.gupt 206
			userClient = userContextServiceClient.getClient();
207
 
208
			Cart cart = userClient.getCart(cartId);
209
			List<Line> lineItems = cart.getLines();
210
 
211
			if( ! lineItems.isEmpty())	{
212
				items = new ArrayList<Map<String,String>>();
213
 
214
				for (Line line : lineItems) {
215
					Map<String, String> itemdetail = new HashMap<String, String>();
3561 rajveer 216
					Item item = catalogClient.getItemForSource(line.getItemId(), sourceId);
1429 varun.gupt 217
					String itemName = ((item.getBrand() != null) ? item.getBrand() + " " : "")
3830 chandransh 218
                                    + ((item.getModelName() != null) ? item.getModelName() + " " : "") 
219
                                    + (( item.getModelNumber() != null ) ? item.getModelNumber() + " " : "" );
220
 
221
                    String itemColor = "";
222
                    if(item.getColor() != null && !item.getColor().trim().equals("NA"))
223
                    itemColor = "Color - " + item.getColor();
224
 
225
                    itemdetail.put("ITEM_NAME", itemName);
226
                    itemdetail.put("ITEM_COLOR", itemColor);
1429 varun.gupt 227
					itemdetail.put("ITEM_ID", line.getItemId()+"");
3830 chandransh 228
					itemdetail.put("CATALOG_ID", item.getCatalogItemId() + "");
1429 varun.gupt 229
					itemdetail.put("ITEM_QUANTITY", ((int)line.getQuantity())+"");
2137 chandransh 230
					itemdetail.put("MRP", formattingUtils.formatPrice(item.getMrp()));
231
					itemdetail.put("SELLING_PRICE", formattingUtils.formatPrice(item.getSellingPrice()));
232
					itemdetail.put("TOTAL_PRICE", formattingUtils.formatPrice((item.getSellingPrice()*line.getQuantity())));
1429 varun.gupt 233
					itemdetail.put("SHIPPING_TIME", line.getEstimate()+"");
4217 varun.gupt 234
					itemdetail.put("BEST_DEAL_TEXT", item.getBestDealText());
2137 chandransh 235
					items.add(itemdetail);
1429 varun.gupt 236
				}
237
			}
2137 chandransh 238
			totalamount = cart.getTotalPrice();
1429 varun.gupt 239
			addresses = userClient.getAllAddressesForUser(cart.getUserId());
1774 varun.gupt 240
			User user = userClient.getUserById(cart.getUserId());
241
 
242
			fullName = user.getName();
243
			phoneNumber = user.getMobileNumber();
1429 varun.gupt 244
 
245
			if(cart.isSetAddressId())	{
246
				defaultAddressId = cart.getAddressId();
247
			} else	{
248
				defaultAddressId = userClient.getDefaultAddressId(cart.getUserId());
249
			}
1981 varun.gupt 250
 
251
			String couponCode = cart.getCouponCode();
252
	        context.put("couponcode", couponCode == null ? "" : couponCode);
2137 chandransh 253
	        context.put("discountedamount", formattingUtils.formatPrice(cart.getDiscountedPrice()));
1981 varun.gupt 254
 
1429 varun.gupt 255
		} catch (Exception e)	{
2949 chandransh 256
			log.error("Unable to get the shipping details", e);
1429 varun.gupt 257
		}
1774 varun.gupt 258
		System.out.println(fullName + " | " + phoneNumber);
1981 varun.gupt 259
 
1774 varun.gupt 260
		context.put("fullname", fullName);
261
		context.put("phonenumber", phoneNumber);
1429 varun.gupt 262
 
263
		context.put("items", items);
2137 chandransh 264
		context.put("totalamount", formattingUtils.formatPrice(totalamount));
1429 varun.gupt 265
		context.put("addresses", addresses);
266
		context.put("defaultAddressId", defaultAddressId+"");
267
		context.put("errorMsg", errorMsg);
268
		htmlString = pageLoader.getHtmlFromVelocity(templateFile, context);
269
		return htmlString;
507 rajveer 270
	}
271
 
517 rajveer 272
	public long getAddressId(){
273
		return this.addressId;
274
	}
572 chandransh 275
 
276
	public String getErrorMsg(){
277
		return this.errorMsg;
278
	}
822 vikas 279
 
280
	public String getName() {
281
		return name;
282
	}
283
 
284
	public void setName(String name) {
285
		this.name = name;
286
	}
287
 
288
	public String getLine1() {
289
		return line1;
290
	}
291
 
292
	public void setLine1(String line1) {
293
		this.line1 = line1;
294
	}
295
 
296
	public String getLine2() {
297
		return line2;
298
	}
299
 
300
	public void setLine2(String line2) {
301
		this.line2 = line2;
302
	}
303
 
304
	public String getCity() {
305
		return city;
306
	}
307
 
308
	public void setCity(String city) {
309
		this.city = city;
310
	}
311
 
312
	public String getState() {
313
		return state;
314
	}
315
 
316
	public void setState(String state) {
317
		this.state = state;
318
	}
319
 
320
	public String getPincode() {
321
		return pincode;
322
	}
323
 
324
	public void setPincode(String pincode) {
325
		this.pincode = pincode;
326
	}
327
 
328
	public String getCountry() {
329
		return country;
330
	}
331
 
332
	public void setCountry(String country) {
333
		this.country = country;
334
	}
335
 
336
	public String getPhone() {
337
		return phone;
338
	}
339
 
340
	public void setPhone(String phone) {
341
		this.phone = phone;
342
	}
3903 varun.gupt 343
 
344
	@Override
345
	public String getHeaderSnippet() {
346
		String url = request.getQueryString();
347
		if (url == null) {
348
			url = "";
349
		} else {
350
			url = "?" + url;
351
		}
352
		url = request.getRequestURI() + url;
353
		return pageLoader.getHeaderHtml(userinfo.isLoggedIn(), userinfo.getNameOfUser(), url , 0, false);
354
	}
1429 varun.gupt 355
}