Subversion Repositories SmartDukaan

Rev

Rev 2183 | Rev 2419 | 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
 
2263 vikas 9
import in.shop2020.datalogger.EventType;
1429 varun.gupt 10
import in.shop2020.model.v1.catalog.Item;
507 rajveer 11
import in.shop2020.model.v1.user.Address;
12
import in.shop2020.model.v1.user.AddressType;
841 vikas 13
import in.shop2020.model.v1.user.Cart;
1429 varun.gupt 14
import in.shop2020.model.v1.user.Line;
1774 varun.gupt 15
import in.shop2020.model.v1.user.User;
572 chandransh 16
import in.shop2020.model.v1.user.UserContextService;
507 rajveer 17
import in.shop2020.serving.controllers.BaseController;
1957 vikas 18
import in.shop2020.serving.utils.DataLogger;
2137 chandransh 19
import in.shop2020.serving.utils.FormattingUtils;
839 vikas 20
import in.shop2020.serving.utils.Utils;
1429 varun.gupt 21
import in.shop2020.thrift.clients.CatalogServiceClient;
507 rajveer 22
import in.shop2020.thrift.clients.UserContextServiceClient;
23
 
24
 
832 rajveer 25
import org.apache.log4j.Logger;
822 vikas 26
import org.apache.struts2.convention.annotation.InterceptorRef;
27
import org.apache.struts2.convention.annotation.InterceptorRefs;
637 rajveer 28
import org.apache.struts2.convention.annotation.Result;
29
import org.apache.struts2.convention.annotation.Results;
1429 varun.gupt 30
import org.apache.velocity.VelocityContext;
507 rajveer 31
 
822 vikas 32
@InterceptorRefs({
33
    @InterceptorRef("myDefault"),
34
    @InterceptorRef("login")
35
})
36
 
637 rajveer 37
@Results({
38
    @Result(name="redirect", type="redirectAction", 
822 vikas 39
    		params = {"actionName" , "shipping"})
637 rajveer 40
})
822 vikas 41
public class ShippingController extends BaseController{
507 rajveer 42
 
43
	private static final long serialVersionUID = 1L;
832 rajveer 44
	private static Logger log = Logger.getLogger(Class.class);
507 rajveer 45
 
517 rajveer 46
	private long addressId = 0;
572 chandransh 47
	private String errorMsg = "";
507 rajveer 48
 
822 vikas 49
	private String name;
50
	private String line1;
51
	private String line2;
52
	private String city;
839 vikas 53
	private String state = "";
822 vikas 54
	private String pincode;
55
	private String phone;
56
	private String country;
57
 
507 rajveer 58
	public ShippingController(){
59
		super();
60
	}
61
 
62
	 // GET /shipping
637 rajveer 63
	 public String index() {
555 chandransh 64
    	long userId = userinfo.getUserId();
692 chandransh 65
    	long cartId = userinfo.getCartId();
572 chandransh 66
    	try {
67
			UserContextService.Client userClient = (new UserContextServiceClient()).getClient();
692 chandransh 68
			userClient.checkOut(cartId);
841 vikas 69
			Cart cart = userClient.getCart(cartId);
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);
1466 ankur.sing 77
			errorMsg = userClient.validateCart(cartId);
572 chandransh 78
 
79
		} catch (Exception e) {
80
			// This exception can be ignored for showing the cart. Not so
81
			// innocent when this occurs at the time of checkout or when the
82
			// user is proceeding to pay.
83
			e.printStackTrace();
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
		}
2263 vikas 93
        DataLogger.logData(EventType.SHIPPINIG_ACCESS.name(), session.getId(), Long.toString(userinfo.getUserId()), userinfo.getEmail(),
2157 vikas 94
                Long.toString(cartId));
822 vikas 95
 
637 rajveer 96
    	return "index";
507 rajveer 97
	 }
98
 
99
	// POST /entity
637 rajveer 100
	public String create(){
555 chandransh 101
		UserContextServiceClient userContextServiceClient = null;
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 {
110
			userContextServiceClient = new UserContextServiceClient();
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.");
2263 vikas 151
                        DataLogger.logData(EventType.SHIPPINIG_ADD_ADDRESS.name(), session.getId(), Long.toString(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
 
1466 ankur.sing 161
					errorMsg = userClient.validateCart(userinfo.getCartId());
2263 vikas 162
                    DataLogger.logData(EventType.SHIPPINIG_ADD_CHANGE.name(), session.getId(), Long.toString(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) {
168
			return "failure";
517 rajveer 169
		}
637 rajveer 170
		return null;
507 rajveer 171
	}
172
 
1429 varun.gupt 173
	public String getShippingHeaderSnippet() {
174
		String htmlString = "";
175
		VelocityContext context = new VelocityContext();
176
		String templateFile = "templates/shippingheader.vm";
177
		htmlString = pageLoader.getHtmlFromVelocity(templateFile, context);
178
		return htmlString;
507 rajveer 179
	}
180
 
1429 varun.gupt 181
	public String getShippingDetailsSnippet() {
182
 
183
		long cartId = userinfo.getCartId();
184
		String htmlString = "";
185
		VelocityContext context = new VelocityContext();
186
		String templateFile = "templates/shippingdetails.vm";
187
		List<Map<String,String>> items = null;
188
		double totalamount= 0.0;
189
		List<Address> addresses = null;
190
		long defaultAddressId = 0;
1774 varun.gupt 191
		String fullName = "";
192
		String phoneNumber = "";
1429 varun.gupt 193
 
194
		CatalogServiceClient catalogServiceClient  = null;
195
		in.shop2020.model.v1.catalog.InventoryService.Client catalogClient = null;
196
		UserContextServiceClient userContextServiceClient = null;
197
		in.shop2020.model.v1.user.UserContextService.Client userClient = null;
198
 
2137 chandransh 199
		FormattingUtils formattingUtils = new FormattingUtils();
200
 
1429 varun.gupt 201
		try {
202
			catalogServiceClient = new CatalogServiceClient();
203
			catalogClient = catalogServiceClient.getClient();
204
			userContextServiceClient = new UserContextServiceClient();
205
			userClient = userContextServiceClient.getClient();
206
 
207
			Cart cart = userClient.getCart(cartId);
208
			List<Line> lineItems = cart.getLines();
209
 
210
			if( ! lineItems.isEmpty())	{
211
				items = new ArrayList<Map<String,String>>();
212
 
213
				for (Line line : lineItems) {
214
					Map<String, String> itemdetail = new HashMap<String, String>();
215
					Item item = catalogClient.getItem(line.getItemId());
216
					String itemName = ((item.getBrand() != null) ? item.getBrand() + " " : "")
217
										+ ((item.getModelName() != null) ? item.getModelName() + " " : "") 
218
										+ (( item.getModelNumber() != null ) ? item.getModelNumber() + " " : "" )
219
										+ (( (item.getColor() != null && !item.getColor().trim().equals("NA"))) ? "("+item.getColor()+")" : "" );
220
 
221
					itemdetail.put("ITEM_NAME", itemName);
222
					itemdetail.put("ITEM_ID", line.getItemId()+"");
223
					itemdetail.put("ITEM_QUANTITY", ((int)line.getQuantity())+"");
2137 chandransh 224
					itemdetail.put("MRP", formattingUtils.formatPrice(item.getMrp()));
225
					itemdetail.put("SELLING_PRICE", formattingUtils.formatPrice(item.getSellingPrice()));
226
					itemdetail.put("TOTAL_PRICE", formattingUtils.formatPrice((item.getSellingPrice()*line.getQuantity())));
1429 varun.gupt 227
					itemdetail.put("SHIPPING_TIME", line.getEstimate()+"");
2137 chandransh 228
					items.add(itemdetail);
1429 varun.gupt 229
				}
230
			}
2137 chandransh 231
			totalamount = cart.getTotalPrice();
1429 varun.gupt 232
			addresses = userClient.getAllAddressesForUser(cart.getUserId());
1774 varun.gupt 233
			User user = userClient.getUserById(cart.getUserId());
234
 
235
			fullName = user.getName();
236
			phoneNumber = user.getMobileNumber();
1429 varun.gupt 237
 
238
			if(cart.isSetAddressId())	{
239
				defaultAddressId = cart.getAddressId();
240
			} else	{
241
				defaultAddressId = userClient.getDefaultAddressId(cart.getUserId());
242
			}
1981 varun.gupt 243
 
244
			String couponCode = cart.getCouponCode();
245
	        context.put("couponcode", couponCode == null ? "" : couponCode);
2137 chandransh 246
	        context.put("discountedamount", formattingUtils.formatPrice(cart.getDiscountedPrice()));
1981 varun.gupt 247
 
1429 varun.gupt 248
		} catch (Exception e)	{
249
			e.printStackTrace();
250
		}
1774 varun.gupt 251
		System.out.println(fullName + " | " + phoneNumber);
1981 varun.gupt 252
 
1774 varun.gupt 253
		context.put("fullname", fullName);
254
		context.put("phonenumber", phoneNumber);
1429 varun.gupt 255
 
256
		context.put("items", items);
2137 chandransh 257
		context.put("totalamount", formattingUtils.formatPrice(totalamount));
1429 varun.gupt 258
		context.put("addresses", addresses);
259
		context.put("defaultAddressId", defaultAddressId+"");
260
		context.put("errorMsg", errorMsg);
261
		htmlString = pageLoader.getHtmlFromVelocity(templateFile, context);
262
		return htmlString;
507 rajveer 263
	}
264
 
517 rajveer 265
	public long getAddressId(){
266
		return this.addressId;
267
	}
572 chandransh 268
 
269
	public String getErrorMsg(){
270
		return this.errorMsg;
271
	}
822 vikas 272
 
273
	public String getName() {
274
		return name;
275
	}
276
 
277
	public void setName(String name) {
278
		this.name = name;
279
	}
280
 
281
	public String getLine1() {
282
		return line1;
283
	}
284
 
285
	public void setLine1(String line1) {
286
		this.line1 = line1;
287
	}
288
 
289
	public String getLine2() {
290
		return line2;
291
	}
292
 
293
	public void setLine2(String line2) {
294
		this.line2 = line2;
295
	}
296
 
297
	public String getCity() {
298
		return city;
299
	}
300
 
301
	public void setCity(String city) {
302
		this.city = city;
303
	}
304
 
305
	public String getState() {
306
		return state;
307
	}
308
 
309
	public void setState(String state) {
310
		this.state = state;
311
	}
312
 
313
	public String getPincode() {
314
		return pincode;
315
	}
316
 
317
	public void setPincode(String pincode) {
318
		this.pincode = pincode;
319
	}
320
 
321
	public String getCountry() {
322
		return country;
323
	}
324
 
325
	public void setCountry(String country) {
326
		this.country = country;
327
	}
328
 
329
	public String getPhone() {
330
		return phone;
331
	}
332
 
333
	public void setPhone(String phone) {
334
		this.phone = phone;
335
	}
1429 varun.gupt 336
}