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();
572 chandransh 48
 
49
    	try {
50
			UserContextService.Client userClient = (new UserContextServiceClient()).getClient();
637 rajveer 51
			long defaultAddressId = userClient.getDefaultAddressId(userId);
52
			log.info("The default address id of this user is: " + defaultAddressId);
53
			if(defaultAddressId > 0)
54
				userClient.addAddressToCart(userinfo.getCartId(), defaultAddressId);
572 chandransh 55
			if(!userClient.validateCart(userinfo.getCartId()))
56
				errorMsg = "Your cart has been updated.";
57
 
58
		} catch (Exception e) {
59
			// This exception can be ignored for showing the cart. Not so
60
			// innocent when this occurs at the time of checkout or when the
61
			// user is proceeding to pay.
62
			e.printStackTrace();
63
		}
64
 
507 rajveer 65
 
650 rajveer 66
		htmlSnippets.put("SHIPPING_HEADER", pageLoader.getShippingHeaderHtml());
67
		htmlSnippets.put("SHIPPING_DETAILS", pageLoader.getShippingDetailsHtml(userinfo.getCartId(), errorMsg));
507 rajveer 68
 
637 rajveer 69
    	return "index";
507 rajveer 70
	 }
71
 
72
	// POST /entity
637 rajveer 73
	public String create(){
555 chandransh 74
		UserContextServiceClient userContextServiceClient = null;
75
		in.shop2020.model.v1.user.UserContextService.Client userClient = null;
76
 
637 rajveer 77
    	String action = this.request.getParameter("action");
517 rajveer 78
		if(action == null){
637 rajveer 79
			return "failure";
517 rajveer 80
		}
507 rajveer 81
 
555 chandransh 82
		try {
83
			userContextServiceClient = new UserContextServiceClient();
84
			userClient = userContextServiceClient.getClient();
517 rajveer 85
 
637 rajveer 86
			if(action != null){
87
				if(action.equals("add")){
88
					Address address = new Address();
89
					address.setName(this.request.getParameter("name"));
90
					address.setLine1(this.request.getParameter("line1"));
91
					address.setLine2(this.request.getParameter("line2"));
92
					address.setCity(this.request.getParameter("city"));
93
					address.setState(this.request.getParameter("state"));
94
					address.setPin(this.request.getParameter("pincode"));
95
					address.setPhone(this.request.getParameter("phone"));
96
					address.setCountry(this.request.getParameter("country"));
97
					address.setEnabled(true);
98
					address.setType(AddressType.HOME);
99
					long addressId = userClient.addAddressForUser(userinfo.getUserId(), address, false);
100
					userClient.addAddressToCart(userinfo.getCartId(), addressId);
101
					addActionMessage("Address added successfully.");
102
					return "redirect";
517 rajveer 103
				}
104
 
637 rajveer 105
				if(action.equals("change")){
106
					long addressId = Long.parseLong(this.request.getParameter("addressid"));
107
					userClient.addAddressToCart(userinfo.getCartId(), addressId);
108
					return "success";
109
				}
517 rajveer 110
			}
637 rajveer 111
		} catch (Exception e) {
112
			return "failure";
517 rajveer 113
		}
637 rajveer 114
		return null;
507 rajveer 115
	}
116
 
117
	@Override
118
	public void setParameters(Map<String, String[]> reqmap) {
119
		log.info("setParameters:" + reqmap);
120
 
121
		this.reqparams = reqmap;
122
	}
123
 
124
	public String getShippingHeaderSnippet(){
125
		return htmlSnippets.get("SHIPPING_HEADER");
126
	}
127
 
128
	public String getShippingDetailsSnippet(){
129
		return htmlSnippets.get("SHIPPING_DETAILS");
130
	}
131
 
517 rajveer 132
	public long getAddressId(){
133
		return this.addressId;
134
	}
572 chandransh 135
 
136
	public String getErrorMsg(){
137
		return this.errorMsg;
138
	}
507 rajveer 139
}