Subversion Repositories SmartDukaan

Rev

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