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.UserContextService;
507 rajveer 6
import in.shop2020.serving.controllers.BaseController;
7
import in.shop2020.thrift.clients.UserContextServiceClient;
8
 
9
 
10
import org.apache.juli.logging.Log;
11
import org.apache.juli.logging.LogFactory;
832 rajveer 12
import org.apache.log4j.Logger;
822 vikas 13
import org.apache.struts2.convention.annotation.InterceptorRef;
14
import org.apache.struts2.convention.annotation.InterceptorRefs;
637 rajveer 15
import org.apache.struts2.convention.annotation.Result;
16
import org.apache.struts2.convention.annotation.Results;
507 rajveer 17
 
822 vikas 18
@InterceptorRefs({
19
    @InterceptorRef("myDefault"),
20
    @InterceptorRef("login")
21
})
22
 
637 rajveer 23
@Results({
24
    @Result(name="redirect", type="redirectAction", 
822 vikas 25
    		params = {"actionName" , "shipping"})
637 rajveer 26
})
822 vikas 27
public class ShippingController extends BaseController{
507 rajveer 28
 
29
	private static final long serialVersionUID = 1L;
832 rajveer 30
	private static Logger log = Logger.getLogger(Class.class);
507 rajveer 31
 
517 rajveer 32
	private long addressId = 0;
572 chandransh 33
	private String errorMsg = "";
507 rajveer 34
 
822 vikas 35
	private String name;
36
	private String line1;
37
	private String line2;
38
	private String city;
39
	private String state;
40
	private String pincode;
41
	private String phone;
42
	private String country;
43
 
507 rajveer 44
	public ShippingController(){
45
		super();
46
	}
47
 
48
	 // GET /shipping
637 rajveer 49
	 public String index() {
555 chandransh 50
    	long userId = userinfo.getUserId();
51
    	boolean isLoggedIn = userinfo.isLoggedIn();
692 chandransh 52
    	long cartId = userinfo.getCartId();
572 chandransh 53
    	try {
54
			UserContextService.Client userClient = (new UserContextServiceClient()).getClient();
692 chandransh 55
			userClient.checkOut(cartId);
637 rajveer 56
			long defaultAddressId = userClient.getDefaultAddressId(userId);
57
			log.info("The default address id of this user is: " + defaultAddressId);
58
			if(defaultAddressId > 0)
692 chandransh 59
				userClient.addAddressToCart(cartId, defaultAddressId);
60
			if(!userClient.validateCart(cartId))
572 chandransh 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
		}
712 rajveer 69
    	// in case this page is redirected from payment failure
822 vikas 70
 
712 rajveer 71
		if(getActionErrors()!=null){
72
			this.errorMsg = getActionErrors().toArray()[0].toString();
73
		}
822 vikas 74
 
650 rajveer 75
		htmlSnippets.put("SHIPPING_HEADER", pageLoader.getShippingHeaderHtml());
76
		htmlSnippets.put("SHIPPING_DETAILS", pageLoader.getShippingDetailsHtml(userinfo.getCartId(), errorMsg));
507 rajveer 77
 
637 rajveer 78
    	return "index";
507 rajveer 79
	 }
80
 
81
	// POST /entity
637 rajveer 82
	public String create(){
555 chandransh 83
		UserContextServiceClient userContextServiceClient = null;
84
		in.shop2020.model.v1.user.UserContextService.Client userClient = null;
85
 
637 rajveer 86
    	String action = this.request.getParameter("action");
517 rajveer 87
		if(action == null){
637 rajveer 88
			return "failure";
517 rajveer 89
		}
507 rajveer 90
 
555 chandransh 91
		try {
92
			userContextServiceClient = new UserContextServiceClient();
93
			userClient = userContextServiceClient.getClient();
517 rajveer 94
 
637 rajveer 95
			if(action != null){
96
				if(action.equals("add")){
97
					Address address = new Address();
822 vikas 98
					address.setName(this.name);
99
					address.setLine1(this.line1);
100
					address.setLine2(this.line2);
101
					address.setCity(this.city);
102
					address.setState(this.state);
103
					address.setPin(this.pincode);
104
					address.setPhone(this.phone);
105
					address.setCountry(this.country);
637 rajveer 106
					address.setEnabled(true);
107
					address.setType(AddressType.HOME);
108
					long addressId = userClient.addAddressForUser(userinfo.getUserId(), address, false);
109
					userClient.addAddressToCart(userinfo.getCartId(), addressId);
110
					addActionMessage("Address added successfully.");
111
					return "redirect";
517 rajveer 112
				}
113
 
637 rajveer 114
				if(action.equals("change")){
115
					long addressId = Long.parseLong(this.request.getParameter("addressid"));
116
					userClient.addAddressToCart(userinfo.getCartId(), addressId);
117
					return "success";
118
				}
517 rajveer 119
			}
637 rajveer 120
		} catch (Exception e) {
121
			return "failure";
517 rajveer 122
		}
637 rajveer 123
		return null;
507 rajveer 124
	}
125
 
126
	public String getShippingHeaderSnippet(){
127
		return htmlSnippets.get("SHIPPING_HEADER");
128
	}
129
 
130
	public String getShippingDetailsSnippet(){
131
		return htmlSnippets.get("SHIPPING_DETAILS");
132
	}
133
 
517 rajveer 134
	public long getAddressId(){
135
		return this.addressId;
136
	}
572 chandransh 137
 
138
	public String getErrorMsg(){
139
		return this.errorMsg;
140
	}
822 vikas 141
 
142
	public String getName() {
143
		return name;
144
	}
145
 
146
	public void setName(String name) {
147
		this.name = name;
148
	}
149
 
150
	public String getLine1() {
151
		return line1;
152
	}
153
 
154
	public void setLine1(String line1) {
155
		this.line1 = line1;
156
	}
157
 
158
	public String getLine2() {
159
		return line2;
160
	}
161
 
162
	public void setLine2(String line2) {
163
		this.line2 = line2;
164
	}
165
 
166
	public String getCity() {
167
		return city;
168
	}
169
 
170
	public void setCity(String city) {
171
		this.city = city;
172
	}
173
 
174
	public String getState() {
175
		return state;
176
	}
177
 
178
	public void setState(String state) {
179
		this.state = state;
180
	}
181
 
182
	public String getPincode() {
183
		return pincode;
184
	}
185
 
186
	public void setPincode(String pincode) {
187
		this.pincode = pincode;
188
	}
189
 
190
	public String getCountry() {
191
		return country;
192
	}
193
 
194
	public void setCountry(String country) {
195
		this.country = country;
196
	}
197
 
198
	public String getPhone() {
199
		return phone;
200
	}
201
 
202
	public void setPhone(String phone) {
203
		this.phone = phone;
204
	}
507 rajveer 205
}