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