Subversion Repositories SmartDukaan

Rev

Rev 1981 | Rev 2141 | 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
 
1429 varun.gupt 3
import java.util.ArrayList;
839 vikas 4
import java.util.Collection;
1429 varun.gupt 5
import java.util.HashMap;
6
import java.util.List;
7
import java.util.Map;
839 vikas 8
 
1429 varun.gupt 9
import in.shop2020.model.v1.catalog.Item;
507 rajveer 10
import in.shop2020.model.v1.user.Address;
11
import in.shop2020.model.v1.user.AddressType;
841 vikas 12
import in.shop2020.model.v1.user.Cart;
1429 varun.gupt 13
import in.shop2020.model.v1.user.Line;
1774 varun.gupt 14
import in.shop2020.model.v1.user.User;
572 chandransh 15
import in.shop2020.model.v1.user.UserContextService;
507 rajveer 16
import in.shop2020.serving.controllers.BaseController;
1957 vikas 17
import in.shop2020.serving.utils.DataLogger;
2137 chandransh 18
import in.shop2020.serving.utils.FormattingUtils;
839 vikas 19
import in.shop2020.serving.utils.Utils;
1957 vikas 20
import in.shop2020.serving.utils.DataLogger.Event;
1429 varun.gupt 21
import in.shop2020.thrift.clients.CatalogServiceClient;
507 rajveer 22
import in.shop2020.thrift.clients.UserContextServiceClient;
23
 
24
 
1957 vikas 25
import org.apache.commons.lang.StringUtils;
832 rajveer 26
import org.apache.log4j.Logger;
822 vikas 27
import org.apache.struts2.convention.annotation.InterceptorRef;
28
import org.apache.struts2.convention.annotation.InterceptorRefs;
637 rajveer 29
import org.apache.struts2.convention.annotation.Result;
30
import org.apache.struts2.convention.annotation.Results;
841 vikas 31
import org.apache.thrift.TApplicationException;
1429 varun.gupt 32
import org.apache.velocity.VelocityContext;
507 rajveer 33
 
822 vikas 34
@InterceptorRefs({
35
    @InterceptorRef("myDefault"),
36
    @InterceptorRef("login")
37
})
38
 
637 rajveer 39
@Results({
40
    @Result(name="redirect", type="redirectAction", 
822 vikas 41
    		params = {"actionName" , "shipping"})
637 rajveer 42
})
822 vikas 43
public class ShippingController extends BaseController{
507 rajveer 44
 
45
	private static final long serialVersionUID = 1L;
832 rajveer 46
	private static Logger log = Logger.getLogger(Class.class);
1957 vikas 47
	private static Logger dataLog = DataLogger.getLogger();
507 rajveer 48
 
517 rajveer 49
	private long addressId = 0;
572 chandransh 50
	private String errorMsg = "";
507 rajveer 51
 
822 vikas 52
	private String name;
53
	private String line1;
54
	private String line2;
55
	private String city;
839 vikas 56
	private String state = "";
822 vikas 57
	private String pincode;
58
	private String phone;
59
	private String country;
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();
68
    	boolean isLoggedIn = userinfo.isLoggedIn();
692 chandransh 69
    	long cartId = userinfo.getCartId();
572 chandransh 70
    	try {
71
			UserContextService.Client userClient = (new UserContextServiceClient()).getClient();
692 chandransh 72
			userClient.checkOut(cartId);
841 vikas 73
			Cart cart = userClient.getCart(cartId);
74
			long defaultAddressId = cart.getAddressId();
75
			if (defaultAddressId == 0) {
76
			    defaultAddressId = userClient.getDefaultAddressId(userId);
77
			}
637 rajveer 78
			log.info("The default address id of this user is: " + defaultAddressId);
79
			if(defaultAddressId > 0)
692 chandransh 80
				userClient.addAddressToCart(cartId, defaultAddressId);
1466 ankur.sing 81
			errorMsg = userClient.validateCart(cartId);
572 chandransh 82
 
83
		} catch (Exception e) {
84
			// This exception can be ignored for showing the cart. Not so
85
			// innocent when this occurs at the time of checkout or when the
86
			// user is proceeding to pay.
87
			e.printStackTrace();
88
		}
712 rajveer 89
    	// in case this page is redirected from payment failure
822 vikas 90
 
839 vikas 91
		Collection<String> actionErrors = getActionErrors();
92
		if(actionErrors != null && !actionErrors.isEmpty()){
93
			for (String str : actionErrors) {
94
			    errorMsg += "<BR/>" + str;
95
			}
712 rajveer 96
		}
1957 vikas 97
		dataLog.info(StringUtils.join(
98
                new String[] { Event.SHIPPINIG_ACCESS.name(),
99
                        userinfo.getEmail(),
100
                        Long.toString(cartId) }, ", "));
822 vikas 101
 
637 rajveer 102
    	return "index";
507 rajveer 103
	 }
104
 
105
	// POST /entity
637 rajveer 106
	public String create(){
555 chandransh 107
		UserContextServiceClient userContextServiceClient = null;
108
		in.shop2020.model.v1.user.UserContextService.Client userClient = null;
109
 
637 rajveer 110
    	String action = this.request.getParameter("action");
517 rajveer 111
		if(action == null){
637 rajveer 112
			return "failure";
517 rajveer 113
		}
507 rajveer 114
 
555 chandransh 115
		try {
116
			userContextServiceClient = new UserContextServiceClient();
117
			userClient = userContextServiceClient.getClient();
517 rajveer 118
 
637 rajveer 119
			if(action != null){
120
				if(action.equals("add")){
839 vikas 121
					boolean invalidInput = false;
122
					if (!Utils.validatePin(this.pincode)) {
123
						addActionError("Invalid pincode.");
124
						invalidInput = true;
125
					}
126
					if (!Utils.validatePhone(this.phone)) {
127
						addActionError("Invalid phone number.");
128
						invalidInput = true;
129
					}
130
					if (this.line1.isEmpty()) {
131
						addActionError("Address line1 is empty.");
132
						invalidInput = true;
133
					}
134
					if (this.city.isEmpty()) {
135
						addActionError("City name is empty.");
136
						invalidInput = true;
137
					}
138
					if (this.state.isEmpty()) {
139
						addActionError("State name is empty.");
140
						invalidInput = true;
141
					}
142
					if (!invalidInput) {
143
						Address address = new Address();
144
						address.setName(this.name);
145
						address.setLine1(this.line1);
146
						address.setLine2(this.line2);
147
						address.setCity(this.city);
148
						address.setState(this.state);
149
						address.setPin(this.pincode);
150
						address.setPhone(this.phone);
151
						address.setCountry(this.country);
152
						address.setEnabled(true);
153
						address.setType(AddressType.HOME);
1446 varun.gupt 154
						long addressId = userClient.addAddressForUser(userinfo.getUserId(), address, false);
155
						userClient.addAddressToCart(userinfo.getCartId(), addressId);
839 vikas 156
						addActionMessage("Address added successfully.");
1957 vikas 157
                        dataLog.info(StringUtils.join(
158
                                new String[] {
159
                                        Event.SHIPPINIG_ADD_ADDRESS.name(),
160
                                        userinfo.getEmail(), address.getName(),
161
                                        address.getCity(), address.getPhone(),
162
                                        address.getPin() }, ", "));
839 vikas 163
					}
164
					return "redirect";				
517 rajveer 165
				}
166
 
1446 varun.gupt 167
				if(action.equals("change"))	{
841 vikas 168
					addressId = Long.parseLong(this.request.getParameter("addressid"));
637 rajveer 169
					userClient.addAddressToCart(userinfo.getCartId(), addressId);
1446 varun.gupt 170
 
1466 ankur.sing 171
					errorMsg = userClient.validateCart(userinfo.getCartId());
1957 vikas 172
                    dataLog.info(StringUtils.join(
173
                            new String[] { Event.SHIPPINIG_ADD_CHANGE.name(),
174
                                    userinfo.getEmail(),
175
                                    Long.toString(userinfo.getCartId()),
176
                                    Long.toString(addressId) }, ", "));
1446 varun.gupt 177
					return "index";
637 rajveer 178
				}
517 rajveer 179
			}
637 rajveer 180
		} catch (Exception e) {
181
			return "failure";
517 rajveer 182
		}
637 rajveer 183
		return null;
507 rajveer 184
	}
185
 
1429 varun.gupt 186
	public String getShippingHeaderSnippet() {
187
		String htmlString = "";
188
		VelocityContext context = new VelocityContext();
189
		String templateFile = "templates/shippingheader.vm";
190
		htmlString = pageLoader.getHtmlFromVelocity(templateFile, context);
191
		return htmlString;
507 rajveer 192
	}
193
 
1429 varun.gupt 194
	public String getShippingDetailsSnippet() {
195
 
196
		long cartId = userinfo.getCartId();
197
		String htmlString = "";
198
		VelocityContext context = new VelocityContext();
199
		String templateFile = "templates/shippingdetails.vm";
200
		List<Map<String,String>> items = null;
201
		double totalamount= 0.0;
202
		List<Address> addresses = null;
203
		long defaultAddressId = 0;
1774 varun.gupt 204
		String fullName = "";
205
		String phoneNumber = "";
1429 varun.gupt 206
 
207
		CatalogServiceClient catalogServiceClient  = null;
208
		in.shop2020.model.v1.catalog.InventoryService.Client catalogClient = null;
209
		UserContextServiceClient userContextServiceClient = null;
210
		in.shop2020.model.v1.user.UserContextService.Client userClient = null;
211
 
2137 chandransh 212
		FormattingUtils formattingUtils = new FormattingUtils();
213
 
1429 varun.gupt 214
		try {
215
			catalogServiceClient = new CatalogServiceClient();
216
			catalogClient = catalogServiceClient.getClient();
217
			userContextServiceClient = new UserContextServiceClient();
218
			userClient = userContextServiceClient.getClient();
219
 
220
			Cart cart = userClient.getCart(cartId);
221
			List<Line> lineItems = cart.getLines();
222
 
223
			if( ! lineItems.isEmpty())	{
224
				items = new ArrayList<Map<String,String>>();
225
 
226
				for (Line line : lineItems) {
227
					Map<String, String> itemdetail = new HashMap<String, String>();
228
					Item item = catalogClient.getItem(line.getItemId());
229
					String itemName = ((item.getBrand() != null) ? item.getBrand() + " " : "")
230
										+ ((item.getModelName() != null) ? item.getModelName() + " " : "") 
231
										+ (( item.getModelNumber() != null ) ? item.getModelNumber() + " " : "" )
232
										+ (( (item.getColor() != null && !item.getColor().trim().equals("NA"))) ? "("+item.getColor()+")" : "" );
233
 
234
					itemdetail.put("ITEM_NAME", itemName);
235
					itemdetail.put("ITEM_ID", line.getItemId()+"");
236
					itemdetail.put("ITEM_QUANTITY", ((int)line.getQuantity())+"");
2137 chandransh 237
					itemdetail.put("MRP", formattingUtils.formatPrice(item.getMrp()));
238
					itemdetail.put("SELLING_PRICE", formattingUtils.formatPrice(item.getSellingPrice()));
239
					itemdetail.put("TOTAL_PRICE", formattingUtils.formatPrice((item.getSellingPrice()*line.getQuantity())));
1429 varun.gupt 240
					itemdetail.put("SHIPPING_TIME", line.getEstimate()+"");
2137 chandransh 241
					items.add(itemdetail);
1429 varun.gupt 242
				}
243
			}
2137 chandransh 244
			totalamount = cart.getTotalPrice();
1429 varun.gupt 245
			addresses = userClient.getAllAddressesForUser(cart.getUserId());
1774 varun.gupt 246
			User user = userClient.getUserById(cart.getUserId());
247
 
248
			fullName = user.getName();
249
			phoneNumber = user.getMobileNumber();
1429 varun.gupt 250
 
251
			if(cart.isSetAddressId())	{
252
				defaultAddressId = cart.getAddressId();
253
			} else	{
254
				defaultAddressId = userClient.getDefaultAddressId(cart.getUserId());
255
			}
1981 varun.gupt 256
 
257
			String couponCode = cart.getCouponCode();
258
	        context.put("couponcode", couponCode == null ? "" : couponCode);
2137 chandransh 259
	        context.put("discountedamount", formattingUtils.formatPrice(cart.getDiscountedPrice()));
1981 varun.gupt 260
 
1429 varun.gupt 261
		} catch (Exception e)	{
262
			e.printStackTrace();
263
		}
1774 varun.gupt 264
		System.out.println(fullName + " | " + phoneNumber);
1981 varun.gupt 265
 
1774 varun.gupt 266
		context.put("fullname", fullName);
267
		context.put("phonenumber", phoneNumber);
1429 varun.gupt 268
 
269
		context.put("items", items);
2137 chandransh 270
		context.put("totalamount", formattingUtils.formatPrice(totalamount));
1429 varun.gupt 271
		context.put("addresses", addresses);
272
		context.put("defaultAddressId", defaultAddressId+"");
273
		context.put("errorMsg", errorMsg);
274
		htmlString = pageLoader.getHtmlFromVelocity(templateFile, context);
275
		return htmlString;
507 rajveer 276
	}
277
 
517 rajveer 278
	public long getAddressId(){
279
		return this.addressId;
280
	}
572 chandransh 281
 
282
	public String getErrorMsg(){
283
		return this.errorMsg;
284
	}
822 vikas 285
 
286
	public String getName() {
287
		return name;
288
	}
289
 
290
	public void setName(String name) {
291
		this.name = name;
292
	}
293
 
294
	public String getLine1() {
295
		return line1;
296
	}
297
 
298
	public void setLine1(String line1) {
299
		this.line1 = line1;
300
	}
301
 
302
	public String getLine2() {
303
		return line2;
304
	}
305
 
306
	public void setLine2(String line2) {
307
		this.line2 = line2;
308
	}
309
 
310
	public String getCity() {
311
		return city;
312
	}
313
 
314
	public void setCity(String city) {
315
		this.city = city;
316
	}
317
 
318
	public String getState() {
319
		return state;
320
	}
321
 
322
	public void setState(String state) {
323
		this.state = state;
324
	}
325
 
326
	public String getPincode() {
327
		return pincode;
328
	}
329
 
330
	public void setPincode(String pincode) {
331
		this.pincode = pincode;
332
	}
333
 
334
	public String getCountry() {
335
		return country;
336
	}
337
 
338
	public void setCountry(String country) {
339
		this.country = country;
340
	}
341
 
342
	public String getPhone() {
343
		return phone;
344
	}
345
 
346
	public void setPhone(String phone) {
347
		this.phone = phone;
348
	}
1429 varun.gupt 349
}