Subversion Repositories SmartDukaan

Rev

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