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