Subversion Repositories SmartDukaan

Rev

Rev 1034 | Rev 1446 | 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);
145
						long addressId = userClient.addAddressForUser(
146
								userinfo.getUserId(), address, false);
147
						userClient.addAddressToCart(userinfo.getCartId(),
148
								addressId);
149
						addActionMessage("Address added successfully.");
150
					}
151
					return "redirect";				
517 rajveer 152
				}
153
 
637 rajveer 154
				if(action.equals("change")){
841 vikas 155
					addressId = Long.parseLong(this.request.getParameter("addressid"));
637 rajveer 156
					userClient.addAddressToCart(userinfo.getCartId(), addressId);
157
					return "success";
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;
184
 
185
		CatalogServiceClient catalogServiceClient  = null;
186
		in.shop2020.model.v1.catalog.InventoryService.Client catalogClient = null;
187
		UserContextServiceClient userContextServiceClient = null;
188
		in.shop2020.model.v1.user.UserContextService.Client userClient = null;
189
 
190
		try {
191
			catalogServiceClient = new CatalogServiceClient();
192
			catalogClient = catalogServiceClient.getClient();
193
			userContextServiceClient = new UserContextServiceClient();
194
			userClient = userContextServiceClient.getClient();
195
 
196
			Cart cart = userClient.getCart(cartId);
197
			List<Line> lineItems = cart.getLines();
198
 
199
			if( ! lineItems.isEmpty())	{
200
				items = new ArrayList<Map<String,String>>();
201
 
202
				for (Line line : lineItems) {
203
					Map<String, String> itemdetail = new HashMap<String, String>();
204
					Item item = catalogClient.getItem(line.getItemId());
205
					String itemName = ((item.getBrand() != null) ? item.getBrand() + " " : "")
206
										+ ((item.getModelName() != null) ? item.getModelName() + " " : "") 
207
										+ (( item.getModelNumber() != null ) ? item.getModelNumber() + " " : "" )
208
										+ (( (item.getColor() != null && !item.getColor().trim().equals("NA"))) ? "("+item.getColor()+")" : "" );
209
 
210
					itemdetail.put("ITEM_NAME", itemName);
211
					itemdetail.put("ITEM_ID", line.getItemId()+"");
212
					itemdetail.put("ITEM_QUANTITY", ((int)line.getQuantity())+"");
213
					itemdetail.put("MRP", ((int)item.getMrp())+"");
214
					itemdetail.put("SELLING_PRICE", (int)item.getSellingPrice()+"");
215
					itemdetail.put("TOTAL_PRICE", (int)(item.getSellingPrice()*line.getQuantity())+"");
216
					itemdetail.put("SHIPPING_TIME", line.getEstimate()+"");
217
					totalamount = totalamount + line.getQuantity()*item.getSellingPrice();
218
					items.add(itemdetail);				
219
				}
220
			}
221
 
222
			addresses = userClient.getAllAddressesForUser(cart.getUserId());
223
 
224
			if(cart.isSetAddressId())	{
225
				defaultAddressId = cart.getAddressId();
226
			} else	{
227
				defaultAddressId = userClient.getDefaultAddressId(cart.getUserId());
228
			}
229
		} catch (Exception e)	{
230
			e.printStackTrace();
231
		}
232
 
233
		context.put("items", items);
234
		context.put("totalamount", (int)totalamount+"");
235
		context.put("addresses", addresses);
236
		context.put("defaultAddressId", defaultAddressId+"");
237
		context.put("errorMsg", errorMsg);
238
		htmlString = pageLoader.getHtmlFromVelocity(templateFile, context);
239
		return htmlString;
507 rajveer 240
	}
241
 
517 rajveer 242
	public long getAddressId(){
243
		return this.addressId;
244
	}
572 chandransh 245
 
246
	public String getErrorMsg(){
247
		return this.errorMsg;
248
	}
822 vikas 249
 
250
	public String getName() {
251
		return name;
252
	}
253
 
254
	public void setName(String name) {
255
		this.name = name;
256
	}
257
 
258
	public String getLine1() {
259
		return line1;
260
	}
261
 
262
	public void setLine1(String line1) {
263
		this.line1 = line1;
264
	}
265
 
266
	public String getLine2() {
267
		return line2;
268
	}
269
 
270
	public void setLine2(String line2) {
271
		this.line2 = line2;
272
	}
273
 
274
	public String getCity() {
275
		return city;
276
	}
277
 
278
	public void setCity(String city) {
279
		this.city = city;
280
	}
281
 
282
	public String getState() {
283
		return state;
284
	}
285
 
286
	public void setState(String state) {
287
		this.state = state;
288
	}
289
 
290
	public String getPincode() {
291
		return pincode;
292
	}
293
 
294
	public void setPincode(String pincode) {
295
		this.pincode = pincode;
296
	}
297
 
298
	public String getCountry() {
299
		return country;
300
	}
301
 
302
	public void setCountry(String country) {
303
		this.country = country;
304
	}
305
 
306
	public String getPhone() {
307
		return phone;
308
	}
309
 
310
	public void setPhone(String phone) {
311
		this.phone = phone;
312
	}
1429 varun.gupt 313
}