Subversion Repositories SmartDukaan

Rev

Rev 2511 | Rev 3063 | 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;
1429 varun.gupt 13
import in.shop2020.thrift.clients.CatalogServiceClient;
507 rajveer 14
import in.shop2020.thrift.clients.UserContextServiceClient;
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
 
822 vikas 30
@InterceptorRefs({
31
    @InterceptorRef("myDefault"),
32
    @InterceptorRef("login")
33
})
34
 
637 rajveer 35
@Results({
36
    @Result(name="redirect", type="redirectAction", 
822 vikas 37
    		params = {"actionName" , "shipping"})
637 rajveer 38
})
822 vikas 39
public class ShippingController extends BaseController{
507 rajveer 40
 
41
	private static final long serialVersionUID = 1L;
832 rajveer 42
	private static Logger log = Logger.getLogger(Class.class);
507 rajveer 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 {
65
			UserContextService.Client userClient = (new UserContextServiceClient()).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
		}
2419 vikas 91
        DataLogger.logData(EventType.SHIPPINIG_ACCESS, session.getId(), 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(){
555 chandransh 99
		UserContextServiceClient userContextServiceClient = null;
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 {
108
			userContextServiceClient = new UserContextServiceClient();
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.");
2419 vikas 149
                        DataLogger.logData(EventType.SHIPPINIG_ADD_ADDRESS, session.getId(), 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) {
166
			return "failure";
517 rajveer 167
		}
637 rajveer 168
		return null;
507 rajveer 169
	}
170
 
1429 varun.gupt 171
	public String getShippingHeaderSnippet() {
172
		String htmlString = "";
173
		VelocityContext context = new VelocityContext();
174
		String templateFile = "templates/shippingheader.vm";
175
		htmlString = pageLoader.getHtmlFromVelocity(templateFile, context);
176
		return htmlString;
507 rajveer 177
	}
178
 
1429 varun.gupt 179
	public String getShippingDetailsSnippet() {
180
 
181
		long cartId = userinfo.getCartId();
182
		String htmlString = "";
183
		VelocityContext context = new VelocityContext();
184
		String templateFile = "templates/shippingdetails.vm";
185
		List<Map<String,String>> items = null;
186
		double totalamount= 0.0;
187
		List<Address> addresses = null;
188
		long defaultAddressId = 0;
1774 varun.gupt 189
		String fullName = "";
190
		String phoneNumber = "";
1429 varun.gupt 191
 
192
		CatalogServiceClient catalogServiceClient  = null;
193
		in.shop2020.model.v1.catalog.InventoryService.Client catalogClient = null;
194
		UserContextServiceClient userContextServiceClient = null;
195
		in.shop2020.model.v1.user.UserContextService.Client userClient = null;
196
 
2137 chandransh 197
		FormattingUtils formattingUtils = new FormattingUtils();
198
 
1429 varun.gupt 199
		try {
200
			catalogServiceClient = new CatalogServiceClient();
201
			catalogClient = catalogServiceClient.getClient();
202
			userContextServiceClient = new UserContextServiceClient();
203
			userClient = userContextServiceClient.getClient();
204
 
205
			Cart cart = userClient.getCart(cartId);
206
			List<Line> lineItems = cart.getLines();
207
 
208
			if( ! lineItems.isEmpty())	{
209
				items = new ArrayList<Map<String,String>>();
210
 
211
				for (Line line : lineItems) {
212
					Map<String, String> itemdetail = new HashMap<String, String>();
213
					Item item = catalogClient.getItem(line.getItemId());
214
					String itemName = ((item.getBrand() != null) ? item.getBrand() + " " : "")
215
										+ ((item.getModelName() != null) ? item.getModelName() + " " : "") 
216
										+ (( item.getModelNumber() != null ) ? item.getModelNumber() + " " : "" )
217
										+ (( (item.getColor() != null && !item.getColor().trim().equals("NA"))) ? "("+item.getColor()+")" : "" );
218
 
219
					itemdetail.put("ITEM_NAME", itemName);
220
					itemdetail.put("ITEM_ID", line.getItemId()+"");
221
					itemdetail.put("ITEM_QUANTITY", ((int)line.getQuantity())+"");
2137 chandransh 222
					itemdetail.put("MRP", formattingUtils.formatPrice(item.getMrp()));
223
					itemdetail.put("SELLING_PRICE", formattingUtils.formatPrice(item.getSellingPrice()));
224
					itemdetail.put("TOTAL_PRICE", formattingUtils.formatPrice((item.getSellingPrice()*line.getQuantity())));
1429 varun.gupt 225
					itemdetail.put("SHIPPING_TIME", line.getEstimate()+"");
2137 chandransh 226
					items.add(itemdetail);
1429 varun.gupt 227
				}
228
			}
2137 chandransh 229
			totalamount = cart.getTotalPrice();
1429 varun.gupt 230
			addresses = userClient.getAllAddressesForUser(cart.getUserId());
1774 varun.gupt 231
			User user = userClient.getUserById(cart.getUserId());
232
 
233
			fullName = user.getName();
234
			phoneNumber = user.getMobileNumber();
1429 varun.gupt 235
 
236
			if(cart.isSetAddressId())	{
237
				defaultAddressId = cart.getAddressId();
238
			} else	{
239
				defaultAddressId = userClient.getDefaultAddressId(cart.getUserId());
240
			}
1981 varun.gupt 241
 
242
			String couponCode = cart.getCouponCode();
243
	        context.put("couponcode", couponCode == null ? "" : couponCode);
2137 chandransh 244
	        context.put("discountedamount", formattingUtils.formatPrice(cart.getDiscountedPrice()));
1981 varun.gupt 245
 
1429 varun.gupt 246
		} catch (Exception e)	{
2949 chandransh 247
			log.error("Unable to get the shipping details", e);
1429 varun.gupt 248
		}
1774 varun.gupt 249
		System.out.println(fullName + " | " + phoneNumber);
1981 varun.gupt 250
 
1774 varun.gupt 251
		context.put("fullname", fullName);
252
		context.put("phonenumber", phoneNumber);
1429 varun.gupt 253
 
254
		context.put("items", items);
2137 chandransh 255
		context.put("totalamount", formattingUtils.formatPrice(totalamount));
1429 varun.gupt 256
		context.put("addresses", addresses);
257
		context.put("defaultAddressId", defaultAddressId+"");
258
		context.put("errorMsg", errorMsg);
259
		htmlString = pageLoader.getHtmlFromVelocity(templateFile, context);
260
		return htmlString;
507 rajveer 261
	}
262
 
517 rajveer 263
	public long getAddressId(){
264
		return this.addressId;
265
	}
572 chandransh 266
 
267
	public String getErrorMsg(){
268
		return this.errorMsg;
269
	}
822 vikas 270
 
271
	public String getName() {
272
		return name;
273
	}
274
 
275
	public void setName(String name) {
276
		this.name = name;
277
	}
278
 
279
	public String getLine1() {
280
		return line1;
281
	}
282
 
283
	public void setLine1(String line1) {
284
		this.line1 = line1;
285
	}
286
 
287
	public String getLine2() {
288
		return line2;
289
	}
290
 
291
	public void setLine2(String line2) {
292
		this.line2 = line2;
293
	}
294
 
295
	public String getCity() {
296
		return city;
297
	}
298
 
299
	public void setCity(String city) {
300
		this.city = city;
301
	}
302
 
303
	public String getState() {
304
		return state;
305
	}
306
 
307
	public void setState(String state) {
308
		this.state = state;
309
	}
310
 
311
	public String getPincode() {
312
		return pincode;
313
	}
314
 
315
	public void setPincode(String pincode) {
316
		this.pincode = pincode;
317
	}
318
 
319
	public String getCountry() {
320
		return country;
321
	}
322
 
323
	public void setCountry(String country) {
324
		this.country = country;
325
	}
326
 
327
	public String getPhone() {
328
		return phone;
329
	}
330
 
331
	public void setPhone(String phone) {
332
		this.phone = phone;
333
	}
1429 varun.gupt 334
}