Subversion Repositories SmartDukaan

Rev

Rev 3126 | Rev 3209 | 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();
572 chandransh 64
    	try {
3126 rajveer 65
			UserContextService.Client userClient = (new UserClient()).getClient();
692 chandransh 66
			userClient.checkOut(cartId);
841 vikas 67
			Cart cart = userClient.getCart(cartId);
68
			long defaultAddressId = cart.getAddressId();
69
			if (defaultAddressId == 0) {
70
			    defaultAddressId = userClient.getDefaultAddressId(userId);
71
			}
637 rajveer 72
			log.info("The default address id of this user is: " + defaultAddressId);
73
			if(defaultAddressId > 0)
692 chandransh 74
				userClient.addAddressToCart(cartId, defaultAddressId);
1466 ankur.sing 75
			errorMsg = userClient.validateCart(cartId);
572 chandransh 76
 
77
		} catch (Exception e) {
2949 chandransh 78
			// This exception can be ignored for showing the shipping page. Not so
572 chandransh 79
			// innocent when this occurs at the time of checkout or when the
80
			// user is proceeding to pay.
2949 chandransh 81
			log.error("Unable to get all the data required for displaying the shipping page", e);
572 chandransh 82
		}
712 rajveer 83
    	// in case this page is redirected from payment failure
822 vikas 84
 
839 vikas 85
		Collection<String> actionErrors = getActionErrors();
86
		if(actionErrors != null && !actionErrors.isEmpty()){
87
			for (String str : actionErrors) {
88
			    errorMsg += "<BR/>" + str;
89
			}
712 rajveer 90
		}
3185 vikas 91
        DataLogger.logData(EventType.SHIPPINIG_ACCESS, getSessionId(), userinfo.getUserId(), userinfo.getEmail(),
2157 vikas 92
                Long.toString(cartId));
822 vikas 93
 
637 rajveer 94
    	return "index";
507 rajveer 95
	 }
96
 
97
	// POST /entity
637 rajveer 98
	public String create(){
3126 rajveer 99
		UserClient userContextServiceClient = null;
555 chandransh 100
		in.shop2020.model.v1.user.UserContextService.Client userClient = null;
101
 
637 rajveer 102
    	String action = this.request.getParameter("action");
517 rajveer 103
		if(action == null){
637 rajveer 104
			return "failure";
517 rajveer 105
		}
507 rajveer 106
 
555 chandransh 107
		try {
3126 rajveer 108
			userContextServiceClient = new UserClient();
555 chandransh 109
			userClient = userContextServiceClient.getClient();
517 rajveer 110
 
637 rajveer 111
			if(action != null){
112
				if(action.equals("add")){
839 vikas 113
					boolean invalidInput = false;
114
					if (!Utils.validatePin(this.pincode)) {
115
						addActionError("Invalid pincode.");
116
						invalidInput = true;
117
					}
118
					if (!Utils.validatePhone(this.phone)) {
119
						addActionError("Invalid phone number.");
120
						invalidInput = true;
121
					}
122
					if (this.line1.isEmpty()) {
123
						addActionError("Address line1 is empty.");
124
						invalidInput = true;
125
					}
126
					if (this.city.isEmpty()) {
127
						addActionError("City name is empty.");
128
						invalidInput = true;
129
					}
130
					if (this.state.isEmpty()) {
131
						addActionError("State name is empty.");
132
						invalidInput = true;
133
					}
134
					if (!invalidInput) {
135
						Address address = new Address();
136
						address.setName(this.name);
137
						address.setLine1(this.line1);
138
						address.setLine2(this.line2);
139
						address.setCity(this.city);
140
						address.setState(this.state);
141
						address.setPin(this.pincode);
142
						address.setPhone(this.phone);
143
						address.setCountry(this.country);
144
						address.setEnabled(true);
145
						address.setType(AddressType.HOME);
1446 varun.gupt 146
						long addressId = userClient.addAddressForUser(userinfo.getUserId(), address, false);
147
						userClient.addAddressToCart(userinfo.getCartId(), addressId);
839 vikas 148
						addActionMessage("Address added successfully.");
3185 vikas 149
                        DataLogger.logData(EventType.SHIPPINIG_ADD_ADDRESS, getSessionId(), userinfo.getUserId(), userinfo.getEmail(), address.getName(),
2157 vikas 150
                                        address.getCity(), address.getPhone(), address.getPin());
839 vikas 151
					}
152
					return "redirect";				
517 rajveer 153
				}
154
 
1446 varun.gupt 155
				if(action.equals("change"))	{
841 vikas 156
					addressId = Long.parseLong(this.request.getParameter("addressid"));
637 rajveer 157
					userClient.addAddressToCart(userinfo.getCartId(), addressId);
1446 varun.gupt 158
 
1466 ankur.sing 159
					errorMsg = userClient.validateCart(userinfo.getCartId());
2419 vikas 160
                    DataLogger.logData(EventType.SHIPPINIG_ADD_CHANGE, session.getId(), userinfo.getUserId(), userinfo.getEmail(),
2157 vikas 161
                            Long.toString(userinfo.getCartId()), Long.toString(addressId));
1446 varun.gupt 162
					return "index";
637 rajveer 163
				}
517 rajveer 164
			}
637 rajveer 165
		} catch (Exception e) {
3076 chandransh 166
		    log.error("Error while adding address to the cart", e);
637 rajveer 167
			return "failure";
517 rajveer 168
		}
637 rajveer 169
		return null;
507 rajveer 170
	}
171
 
1429 varun.gupt 172
	public String getShippingHeaderSnippet() {
173
		String htmlString = "";
174
		VelocityContext context = new VelocityContext();
175
		String templateFile = "templates/shippingheader.vm";
176
		htmlString = pageLoader.getHtmlFromVelocity(templateFile, context);
177
		return htmlString;
507 rajveer 178
	}
179
 
1429 varun.gupt 180
	public String getShippingDetailsSnippet() {
181
 
182
		long cartId = userinfo.getCartId();
183
		String htmlString = "";
184
		VelocityContext context = new VelocityContext();
185
		String templateFile = "templates/shippingdetails.vm";
186
		List<Map<String,String>> items = null;
187
		double totalamount= 0.0;
188
		List<Address> addresses = null;
189
		long defaultAddressId = 0;
1774 varun.gupt 190
		String fullName = "";
191
		String phoneNumber = "";
1429 varun.gupt 192
 
3126 rajveer 193
		CatalogClient catalogServiceClient  = null;
1429 varun.gupt 194
		in.shop2020.model.v1.catalog.InventoryService.Client catalogClient = null;
3126 rajveer 195
		UserClient userContextServiceClient = null;
1429 varun.gupt 196
		in.shop2020.model.v1.user.UserContextService.Client userClient = null;
197
 
2137 chandransh 198
		FormattingUtils formattingUtils = new FormattingUtils();
199
 
1429 varun.gupt 200
		try {
3126 rajveer 201
			catalogServiceClient = new CatalogClient();
1429 varun.gupt 202
			catalogClient = catalogServiceClient.getClient();
3126 rajveer 203
			userContextServiceClient = new UserClient();
1429 varun.gupt 204
			userClient = userContextServiceClient.getClient();
205
 
206
			Cart cart = userClient.getCart(cartId);
207
			List<Line> lineItems = cart.getLines();
208
 
209
			if( ! lineItems.isEmpty())	{
210
				items = new ArrayList<Map<String,String>>();
211
 
212
				for (Line line : lineItems) {
213
					Map<String, String> itemdetail = new HashMap<String, String>();
214
					Item item = catalogClient.getItem(line.getItemId());
215
					String itemName = ((item.getBrand() != null) ? item.getBrand() + " " : "")
216
										+ ((item.getModelName() != null) ? item.getModelName() + " " : "") 
217
										+ (( item.getModelNumber() != null ) ? item.getModelNumber() + " " : "" )
218
										+ (( (item.getColor() != null && !item.getColor().trim().equals("NA"))) ? "("+item.getColor()+")" : "" );
219
 
220
					itemdetail.put("ITEM_NAME", itemName);
221
					itemdetail.put("ITEM_ID", line.getItemId()+"");
222
					itemdetail.put("ITEM_QUANTITY", ((int)line.getQuantity())+"");
2137 chandransh 223
					itemdetail.put("MRP", formattingUtils.formatPrice(item.getMrp()));
224
					itemdetail.put("SELLING_PRICE", formattingUtils.formatPrice(item.getSellingPrice()));
225
					itemdetail.put("TOTAL_PRICE", formattingUtils.formatPrice((item.getSellingPrice()*line.getQuantity())));
1429 varun.gupt 226
					itemdetail.put("SHIPPING_TIME", line.getEstimate()+"");
2137 chandransh 227
					items.add(itemdetail);
1429 varun.gupt 228
				}
229
			}
2137 chandransh 230
			totalamount = cart.getTotalPrice();
1429 varun.gupt 231
			addresses = userClient.getAllAddressesForUser(cart.getUserId());
1774 varun.gupt 232
			User user = userClient.getUserById(cart.getUserId());
233
 
234
			fullName = user.getName();
235
			phoneNumber = user.getMobileNumber();
1429 varun.gupt 236
 
237
			if(cart.isSetAddressId())	{
238
				defaultAddressId = cart.getAddressId();
239
			} else	{
240
				defaultAddressId = userClient.getDefaultAddressId(cart.getUserId());
241
			}
1981 varun.gupt 242
 
243
			String couponCode = cart.getCouponCode();
244
	        context.put("couponcode", couponCode == null ? "" : couponCode);
2137 chandransh 245
	        context.put("discountedamount", formattingUtils.formatPrice(cart.getDiscountedPrice()));
1981 varun.gupt 246
 
1429 varun.gupt 247
		} catch (Exception e)	{
2949 chandransh 248
			log.error("Unable to get the shipping details", e);
1429 varun.gupt 249
		}
1774 varun.gupt 250
		System.out.println(fullName + " | " + phoneNumber);
1981 varun.gupt 251
 
1774 varun.gupt 252
		context.put("fullname", fullName);
253
		context.put("phonenumber", phoneNumber);
1429 varun.gupt 254
 
255
		context.put("items", items);
2137 chandransh 256
		context.put("totalamount", formattingUtils.formatPrice(totalamount));
1429 varun.gupt 257
		context.put("addresses", addresses);
258
		context.put("defaultAddressId", defaultAddressId+"");
259
		context.put("errorMsg", errorMsg);
260
		htmlString = pageLoader.getHtmlFromVelocity(templateFile, context);
261
		return htmlString;
507 rajveer 262
	}
263
 
517 rajveer 264
	public long getAddressId(){
265
		return this.addressId;
266
	}
572 chandransh 267
 
268
	public String getErrorMsg(){
269
		return this.errorMsg;
270
	}
822 vikas 271
 
272
	public String getName() {
273
		return name;
274
	}
275
 
276
	public void setName(String name) {
277
		this.name = name;
278
	}
279
 
280
	public String getLine1() {
281
		return line1;
282
	}
283
 
284
	public void setLine1(String line1) {
285
		this.line1 = line1;
286
	}
287
 
288
	public String getLine2() {
289
		return line2;
290
	}
291
 
292
	public void setLine2(String line2) {
293
		this.line2 = line2;
294
	}
295
 
296
	public String getCity() {
297
		return city;
298
	}
299
 
300
	public void setCity(String city) {
301
		this.city = city;
302
	}
303
 
304
	public String getState() {
305
		return state;
306
	}
307
 
308
	public void setState(String state) {
309
		this.state = state;
310
	}
311
 
312
	public String getPincode() {
313
		return pincode;
314
	}
315
 
316
	public void setPincode(String pincode) {
317
		this.pincode = pincode;
318
	}
319
 
320
	public String getCountry() {
321
		return country;
322
	}
323
 
324
	public void setCountry(String country) {
325
		this.country = country;
326
	}
327
 
328
	public String getPhone() {
329
		return phone;
330
	}
331
 
332
	public void setPhone(String phone) {
333
		this.phone = phone;
334
	}
1429 varun.gupt 335
}