Subversion Repositories SmartDukaan

Rev

Rev 1429 | Rev 1466 | 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;
572 chandransh 14
import in.shop2020.model.v1.user.UserContextService;
507 rajveer 15
import in.shop2020.serving.controllers.BaseController;
839 vikas 16
import in.shop2020.serving.utils.Utils;
1429 varun.gupt 17
import in.shop2020.thrift.clients.CatalogServiceClient;
507 rajveer 18
import in.shop2020.thrift.clients.UserContextServiceClient;
19
 
20
 
832 rajveer 21
import org.apache.log4j.Logger;
822 vikas 22
import org.apache.struts2.convention.annotation.InterceptorRef;
23
import org.apache.struts2.convention.annotation.InterceptorRefs;
637 rajveer 24
import org.apache.struts2.convention.annotation.Result;
25
import org.apache.struts2.convention.annotation.Results;
841 vikas 26
import org.apache.thrift.TApplicationException;
1429 varun.gupt 27
import org.apache.velocity.VelocityContext;
507 rajveer 28
 
822 vikas 29
@InterceptorRefs({
30
    @InterceptorRef("myDefault"),
31
    @InterceptorRef("login")
32
})
33
 
637 rajveer 34
@Results({
35
    @Result(name="redirect", type="redirectAction", 
822 vikas 36
    		params = {"actionName" , "shipping"})
637 rajveer 37
})
822 vikas 38
public class ShippingController extends BaseController{
507 rajveer 39
 
40
	private static final long serialVersionUID = 1L;
832 rajveer 41
	private static Logger log = Logger.getLogger(Class.class);
507 rajveer 42
 
517 rajveer 43
	private long addressId = 0;
572 chandransh 44
	private String errorMsg = "";
507 rajveer 45
 
822 vikas 46
	private String name;
47
	private String line1;
48
	private String line2;
49
	private String city;
839 vikas 50
	private String state = "";
822 vikas 51
	private String pincode;
52
	private String phone;
53
	private String country;
54
 
507 rajveer 55
	public ShippingController(){
56
		super();
57
	}
58
 
59
	 // GET /shipping
637 rajveer 60
	 public String index() {
555 chandransh 61
    	long userId = userinfo.getUserId();
62
    	boolean isLoggedIn = userinfo.isLoggedIn();
692 chandransh 63
    	long cartId = userinfo.getCartId();
572 chandransh 64
    	try {
65
			UserContextService.Client userClient = (new UserContextServiceClient()).getClient();
692 chandransh 66
			userClient.checkOut(cartId);
841 vikas 67
			Cart cart = userClient.getCart(cartId);
68
			long defaultAddressId = cart.getAddressId();
69
			if (defaultAddressId == 0) {
70
			    defaultAddressId = userClient.getDefaultAddressId(userId);
71
			}
637 rajveer 72
			log.info("The default address id of this user is: " + defaultAddressId);
73
			if(defaultAddressId > 0)
692 chandransh 74
				userClient.addAddressToCart(cartId, defaultAddressId);
75
			if(!userClient.validateCart(cartId))
572 chandransh 76
				errorMsg = "Your cart has been updated.";
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
 
156
					if(!userClient.validateCart(userinfo.getCartId()))
157
						errorMsg = "Your cart has been updated.";
158
					return "index";
637 rajveer 159
				}
517 rajveer 160
			}
637 rajveer 161
		} catch (Exception e) {
162
			return "failure";
517 rajveer 163
		}
637 rajveer 164
		return null;
507 rajveer 165
	}
166
 
1429 varun.gupt 167
	public String getShippingHeaderSnippet() {
168
		String htmlString = "";
169
		VelocityContext context = new VelocityContext();
170
		String templateFile = "templates/shippingheader.vm";
171
		htmlString = pageLoader.getHtmlFromVelocity(templateFile, context);
172
		return htmlString;
507 rajveer 173
	}
174
 
1429 varun.gupt 175
	public String getShippingDetailsSnippet() {
176
 
177
		long cartId = userinfo.getCartId();
178
		String htmlString = "";
179
		VelocityContext context = new VelocityContext();
180
		String templateFile = "templates/shippingdetails.vm";
181
		List<Map<String,String>> items = null;
182
		double totalamount= 0.0;
183
		List<Address> addresses = null;
184
		long defaultAddressId = 0;
185
 
186
		CatalogServiceClient catalogServiceClient  = null;
187
		in.shop2020.model.v1.catalog.InventoryService.Client catalogClient = null;
188
		UserContextServiceClient userContextServiceClient = null;
189
		in.shop2020.model.v1.user.UserContextService.Client userClient = null;
190
 
191
		try {
192
			catalogServiceClient = new CatalogServiceClient();
193
			catalogClient = catalogServiceClient.getClient();
194
			userContextServiceClient = new UserContextServiceClient();
195
			userClient = userContextServiceClient.getClient();
196
 
197
			Cart cart = userClient.getCart(cartId);
198
			List<Line> lineItems = cart.getLines();
199
 
200
			if( ! lineItems.isEmpty())	{
201
				items = new ArrayList<Map<String,String>>();
202
 
203
				for (Line line : lineItems) {
204
					Map<String, String> itemdetail = new HashMap<String, String>();
205
					Item item = catalogClient.getItem(line.getItemId());
206
					String itemName = ((item.getBrand() != null) ? item.getBrand() + " " : "")
207
										+ ((item.getModelName() != null) ? item.getModelName() + " " : "") 
208
										+ (( item.getModelNumber() != null ) ? item.getModelNumber() + " " : "" )
209
										+ (( (item.getColor() != null && !item.getColor().trim().equals("NA"))) ? "("+item.getColor()+")" : "" );
210
 
211
					itemdetail.put("ITEM_NAME", itemName);
212
					itemdetail.put("ITEM_ID", line.getItemId()+"");
213
					itemdetail.put("ITEM_QUANTITY", ((int)line.getQuantity())+"");
214
					itemdetail.put("MRP", ((int)item.getMrp())+"");
215
					itemdetail.put("SELLING_PRICE", (int)item.getSellingPrice()+"");
216
					itemdetail.put("TOTAL_PRICE", (int)(item.getSellingPrice()*line.getQuantity())+"");
217
					itemdetail.put("SHIPPING_TIME", line.getEstimate()+"");
218
					totalamount = totalamount + line.getQuantity()*item.getSellingPrice();
219
					items.add(itemdetail);				
220
				}
221
			}
222
 
223
			addresses = userClient.getAllAddressesForUser(cart.getUserId());
224
 
225
			if(cart.isSetAddressId())	{
226
				defaultAddressId = cart.getAddressId();
227
			} else	{
228
				defaultAddressId = userClient.getDefaultAddressId(cart.getUserId());
229
			}
230
		} catch (Exception e)	{
231
			e.printStackTrace();
232
		}
233
 
234
		context.put("items", items);
235
		context.put("totalamount", (int)totalamount+"");
236
		context.put("addresses", addresses);
237
		context.put("defaultAddressId", defaultAddressId+"");
238
		context.put("errorMsg", errorMsg);
239
		htmlString = pageLoader.getHtmlFromVelocity(templateFile, context);
240
		return htmlString;
507 rajveer 241
	}
242
 
517 rajveer 243
	public long getAddressId(){
244
		return this.addressId;
245
	}
572 chandransh 246
 
247
	public String getErrorMsg(){
248
		return this.errorMsg;
249
	}
822 vikas 250
 
251
	public String getName() {
252
		return name;
253
	}
254
 
255
	public void setName(String name) {
256
		this.name = name;
257
	}
258
 
259
	public String getLine1() {
260
		return line1;
261
	}
262
 
263
	public void setLine1(String line1) {
264
		this.line1 = line1;
265
	}
266
 
267
	public String getLine2() {
268
		return line2;
269
	}
270
 
271
	public void setLine2(String line2) {
272
		this.line2 = line2;
273
	}
274
 
275
	public String getCity() {
276
		return city;
277
	}
278
 
279
	public void setCity(String city) {
280
		this.city = city;
281
	}
282
 
283
	public String getState() {
284
		return state;
285
	}
286
 
287
	public void setState(String state) {
288
		this.state = state;
289
	}
290
 
291
	public String getPincode() {
292
		return pincode;
293
	}
294
 
295
	public void setPincode(String pincode) {
296
		this.pincode = pincode;
297
	}
298
 
299
	public String getCountry() {
300
		return country;
301
	}
302
 
303
	public void setCountry(String country) {
304
		this.country = country;
305
	}
306
 
307
	public String getPhone() {
308
		return phone;
309
	}
310
 
311
	public void setPhone(String phone) {
312
		this.phone = phone;
313
	}
1429 varun.gupt 314
}