Subversion Repositories SmartDukaan

Rev

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