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.serving.pages.PageContentKeys;
9
import in.shop2020.serving.pages.PageEnum;
10
import in.shop2020.serving.pages.PageManager;
11
import in.shop2020.thrift.clients.UserContextServiceClient;
12
 
13
import java.util.*;
14
 
15
import org.apache.juli.logging.Log;
16
import org.apache.juli.logging.LogFactory;
637 rajveer 17
import org.apache.struts2.convention.annotation.Result;
18
import org.apache.struts2.convention.annotation.Results;
507 rajveer 19
import org.apache.struts2.interceptor.ParameterAware;
20
import org.apache.struts2.rest.DefaultHttpHeaders;
21
import org.apache.struts2.rest.HttpHeaders;
22
import org.apache.thrift.TException;
23
 
637 rajveer 24
@Results({
25
    @Result(name="redirect", type="redirectAction", 
26
    		params = {"actionName" , "shipping"}),
27
    @Result(name="login", type="redirectAction", 
28
    		params = {"actionName" , "login"})		
29
})
507 rajveer 30
public class ShippingController extends BaseController implements ParameterAware{
31
 
32
	private static final long serialVersionUID = 1L;
33
	private static Log log = LogFactory.getLog(ShippingController.class);
34
	Map<String, String[]> reqparams = null;
35
 
36
	private PageManager pageManager = null;
517 rajveer 37
	private long addressId = 0;
572 chandransh 38
	private String errorMsg = "";
507 rajveer 39
 
40
	public ShippingController(){
41
		super();
42
		pageManager = PageManager.getPageManager();	
43
	}
44
 
45
	 // GET /shipping
637 rajveer 46
	 public String index() {
47
		if(!userinfo.isLoggedIn()){
650 rajveer 48
			setRedirectUrl();
637 rajveer 49
			return "login";
50
		}
555 chandransh 51
    	long userId = userinfo.getUserId();
52
    	boolean isLoggedIn = userinfo.isLoggedIn();
572 chandransh 53
 
54
    	try {
55
			UserContextService.Client userClient = (new UserContextServiceClient()).getClient();
637 rajveer 56
			long defaultAddressId = userClient.getDefaultAddressId(userId);
57
			log.info("The default address id of this user is: " + defaultAddressId);
58
			if(defaultAddressId > 0)
59
				userClient.addAddressToCart(userinfo.getCartId(), defaultAddressId);
572 chandransh 60
			if(!userClient.validateCart(userinfo.getCartId()))
61
				errorMsg = "Your cart has been updated.";
62
 
63
		} catch (Exception e) {
64
			// This exception can be ignored for showing the cart. Not so
65
			// innocent when this occurs at the time of checkout or when the
66
			// user is proceeding to pay.
67
			e.printStackTrace();
68
		}
69
 
507 rajveer 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
}