Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
507 rajveer 1
package in.shop2020.serving.controllers;
2
 
3
import in.shop2020.model.v1.user.Address;
4
import in.shop2020.model.v1.user.AddressType;
572 chandransh 5
import in.shop2020.model.v1.user.User;
6
import in.shop2020.model.v1.user.UserContextService;
507 rajveer 7
import in.shop2020.serving.controllers.BaseController;
8
import in.shop2020.thrift.clients.UserContextServiceClient;
9
 
10
import java.util.*;
11
 
12
import org.apache.juli.logging.Log;
13
import org.apache.juli.logging.LogFactory;
637 rajveer 14
import org.apache.struts2.convention.annotation.Result;
15
import org.apache.struts2.convention.annotation.Results;
507 rajveer 16
import org.apache.struts2.interceptor.ParameterAware;
17
import org.apache.struts2.rest.DefaultHttpHeaders;
18
import org.apache.struts2.rest.HttpHeaders;
19
import org.apache.thrift.TException;
20
 
637 rajveer 21
@Results({
22
    @Result(name="redirect", type="redirectAction", 
23
    		params = {"actionName" , "shipping"}),
24
    @Result(name="login", type="redirectAction", 
25
    		params = {"actionName" , "login"})		
26
})
507 rajveer 27
public class ShippingController extends BaseController implements ParameterAware{
28
 
29
	private static final long serialVersionUID = 1L;
30
	private static Log log = LogFactory.getLog(ShippingController.class);
31
	Map<String, String[]> reqparams = null;
32
 
517 rajveer 33
	private long addressId = 0;
572 chandransh 34
	private String errorMsg = "";
507 rajveer 35
 
36
	public ShippingController(){
37
		super();
38
	}
39
 
40
	 // GET /shipping
637 rajveer 41
	 public String index() {
42
		if(!userinfo.isLoggedIn()){
650 rajveer 43
			setRedirectUrl();
637 rajveer 44
			return "login";
45
		}
555 chandransh 46
    	long userId = userinfo.getUserId();
47
    	boolean isLoggedIn = userinfo.isLoggedIn();
692 chandransh 48
    	long cartId = userinfo.getCartId();
572 chandransh 49
    	try {
50
			UserContextService.Client userClient = (new UserContextServiceClient()).getClient();
692 chandransh 51
			userClient.checkOut(cartId);
637 rajveer 52
			long defaultAddressId = userClient.getDefaultAddressId(userId);
53
			log.info("The default address id of this user is: " + defaultAddressId);
54
			if(defaultAddressId > 0)
692 chandransh 55
				userClient.addAddressToCart(cartId, defaultAddressId);
56
			if(!userClient.validateCart(cartId))
572 chandransh 57
				errorMsg = "Your cart has been updated.";
58
 
59
		} catch (Exception e) {
60
			// This exception can be ignored for showing the cart. Not so
61
			// innocent when this occurs at the time of checkout or when the
62
			// user is proceeding to pay.
63
			e.printStackTrace();
64
		}
712 rajveer 65
    	// in case this page is redirected from payment failure
66
		/*
67
		if(getActionErrors()!=null){
68
			this.errorMsg = getActionErrors().toArray()[0].toString();
69
		}
70
		*/
650 rajveer 71
		htmlSnippets.put("SHIPPING_HEADER", pageLoader.getShippingHeaderHtml());
72
		htmlSnippets.put("SHIPPING_DETAILS", pageLoader.getShippingDetailsHtml(userinfo.getCartId(), errorMsg));
507 rajveer 73
 
637 rajveer 74
    	return "index";
507 rajveer 75
	 }
76
 
77
	// POST /entity
637 rajveer 78
	public String create(){
555 chandransh 79
		UserContextServiceClient userContextServiceClient = null;
80
		in.shop2020.model.v1.user.UserContextService.Client userClient = null;
81
 
637 rajveer 82
    	String action = this.request.getParameter("action");
517 rajveer 83
		if(action == null){
637 rajveer 84
			return "failure";
517 rajveer 85
		}
507 rajveer 86
 
555 chandransh 87
		try {
88
			userContextServiceClient = new UserContextServiceClient();
89
			userClient = userContextServiceClient.getClient();
517 rajveer 90
 
637 rajveer 91
			if(action != null){
92
				if(action.equals("add")){
93
					Address address = new Address();
94
					address.setName(this.request.getParameter("name"));
95
					address.setLine1(this.request.getParameter("line1"));
96
					address.setLine2(this.request.getParameter("line2"));
97
					address.setCity(this.request.getParameter("city"));
98
					address.setState(this.request.getParameter("state"));
99
					address.setPin(this.request.getParameter("pincode"));
100
					address.setPhone(this.request.getParameter("phone"));
101
					address.setCountry(this.request.getParameter("country"));
102
					address.setEnabled(true);
103
					address.setType(AddressType.HOME);
104
					long addressId = userClient.addAddressForUser(userinfo.getUserId(), address, false);
105
					userClient.addAddressToCart(userinfo.getCartId(), addressId);
106
					addActionMessage("Address added successfully.");
107
					return "redirect";
517 rajveer 108
				}
109
 
637 rajveer 110
				if(action.equals("change")){
111
					long addressId = Long.parseLong(this.request.getParameter("addressid"));
112
					userClient.addAddressToCart(userinfo.getCartId(), addressId);
113
					return "success";
114
				}
517 rajveer 115
			}
637 rajveer 116
		} catch (Exception e) {
117
			return "failure";
517 rajveer 118
		}
637 rajveer 119
		return null;
507 rajveer 120
	}
121
 
122
	@Override
123
	public void setParameters(Map<String, String[]> reqmap) {
124
		log.info("setParameters:" + reqmap);
125
 
126
		this.reqparams = reqmap;
127
	}
128
 
129
	public String getShippingHeaderSnippet(){
130
		return htmlSnippets.get("SHIPPING_HEADER");
131
	}
132
 
133
	public String getShippingDetailsSnippet(){
134
		return htmlSnippets.get("SHIPPING_DETAILS");
135
	}
136
 
517 rajveer 137
	public long getAddressId(){
138
		return this.addressId;
139
	}
572 chandransh 140
 
141
	public String getErrorMsg(){
142
		return this.errorMsg;
143
	}
507 rajveer 144
}