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
		}
65
 
507 rajveer 66
 
650 rajveer 67
		htmlSnippets.put("SHIPPING_HEADER", pageLoader.getShippingHeaderHtml());
68
		htmlSnippets.put("SHIPPING_DETAILS", pageLoader.getShippingDetailsHtml(userinfo.getCartId(), errorMsg));
507 rajveer 69
 
637 rajveer 70
    	return "index";
507 rajveer 71
	 }
72
 
73
	// POST /entity
637 rajveer 74
	public String create(){
555 chandransh 75
		UserContextServiceClient userContextServiceClient = null;
76
		in.shop2020.model.v1.user.UserContextService.Client userClient = null;
77
 
637 rajveer 78
    	String action = this.request.getParameter("action");
517 rajveer 79
		if(action == null){
637 rajveer 80
			return "failure";
517 rajveer 81
		}
507 rajveer 82
 
555 chandransh 83
		try {
84
			userContextServiceClient = new UserContextServiceClient();
85
			userClient = userContextServiceClient.getClient();
517 rajveer 86
 
637 rajveer 87
			if(action != null){
88
				if(action.equals("add")){
89
					Address address = new Address();
90
					address.setName(this.request.getParameter("name"));
91
					address.setLine1(this.request.getParameter("line1"));
92
					address.setLine2(this.request.getParameter("line2"));
93
					address.setCity(this.request.getParameter("city"));
94
					address.setState(this.request.getParameter("state"));
95
					address.setPin(this.request.getParameter("pincode"));
96
					address.setPhone(this.request.getParameter("phone"));
97
					address.setCountry(this.request.getParameter("country"));
98
					address.setEnabled(true);
99
					address.setType(AddressType.HOME);
100
					long addressId = userClient.addAddressForUser(userinfo.getUserId(), address, false);
101
					userClient.addAddressToCart(userinfo.getCartId(), addressId);
102
					addActionMessage("Address added successfully.");
103
					return "redirect";
517 rajveer 104
				}
105
 
637 rajveer 106
				if(action.equals("change")){
107
					long addressId = Long.parseLong(this.request.getParameter("addressid"));
108
					userClient.addAddressToCart(userinfo.getCartId(), addressId);
109
					return "success";
110
				}
517 rajveer 111
			}
637 rajveer 112
		} catch (Exception e) {
113
			return "failure";
517 rajveer 114
		}
637 rajveer 115
		return null;
507 rajveer 116
	}
117
 
118
	@Override
119
	public void setParameters(Map<String, String[]> reqmap) {
120
		log.info("setParameters:" + reqmap);
121
 
122
		this.reqparams = reqmap;
123
	}
124
 
125
	public String getShippingHeaderSnippet(){
126
		return htmlSnippets.get("SHIPPING_HEADER");
127
	}
128
 
129
	public String getShippingDetailsSnippet(){
130
		return htmlSnippets.get("SHIPPING_DETAILS");
131
	}
132
 
517 rajveer 133
	public long getAddressId(){
134
		return this.addressId;
135
	}
572 chandransh 136
 
137
	public String getErrorMsg(){
138
		return this.errorMsg;
139
	}
507 rajveer 140
}