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