Subversion Repositories SmartDukaan

Rev

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