Subversion Repositories SmartDukaan

Rev

Rev 1034 | Rev 1957 | Go to most recent revision | Details | Compare with Previous | 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;
5
import in.shop2020.model.v1.user.UserContextService.Client;
6
import in.shop2020.serving.controllers.BaseController;
822 vikas 7
import in.shop2020.serving.utils.Utils;
507 rajveer 8
import in.shop2020.thrift.clients.UserContextServiceClient;
9
 
10
import java.util.*;
11
 
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;
507 rajveer 15
import org.apache.struts2.convention.annotation.Result;
16
import org.apache.struts2.convention.annotation.Results;
17
 
822 vikas 18
@InterceptorRefs({
19
    @InterceptorRef("myDefault"),
20
    @InterceptorRef("login")
21
})
22
 
595 rajveer 23
@Results({
24
    @Result(name="redirect", type="redirectAction", 
822 vikas 25
    		params = {"actionName" , "address"})
595 rajveer 26
})
822 vikas 27
public class AddressController 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
 
822 vikas 32
	private String errorMsg = "";
33
 
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
 
43
	private String action;
44
	private String isDefault;
45
 
507 rajveer 46
	public AddressController(){
47
		super();
48
	}
49
 
50
	 // GET /address
650 rajveer 51
	 public String index() {
822 vikas 52
		log.info("Check error msgs");
53
		Collection<String> errorMsgs = getActionErrors();
54
		if(errorMsgs !=null && !errorMsgs.isEmpty()){
55
			for (String errMsg : errorMsgs) { 
56
			    this.errorMsg += errMsg + "<br/>";
57
			}
650 rajveer 58
		}
822 vikas 59
		log.info(this.errorMsg);
768 rajveer 60
		htmlSnippets.put("MYACCOUNT_HEADER",pageLoader.getMyaccountHeaderHtml());
822 vikas 61
		htmlSnippets.put("SHIPPING_ADDRESS_DETAILS",pageLoader.getShippingAddressDetailsHtml(userinfo.getUserId(), this.errorMsg));
650 rajveer 62
		return "index";
507 rajveer 63
	 }
64
 
65
	// POST /address
650 rajveer 66
	public String create(){
507 rajveer 67
 
68
		if(userinfo.isLoggedIn()){
69
			UserContextServiceClient userContextServiceClient;
70
			try {
71
				userContextServiceClient = new UserContextServiceClient();
72
				Client userClient = userContextServiceClient.getClient();
73
 
517 rajveer 74
				if(action != null){
75
					if(action.equals("add")){
822 vikas 76
						boolean invalidInput = false;
77
						if (!Utils.validatePin(this.pincode)) {
78
							addActionError("Invalid pincode.");
79
							invalidInput = true;
80
						}
839 vikas 81
						if (!Utils.validatePhone(this.phone)) {
822 vikas 82
							addActionError("Invalid phone number.");
83
							invalidInput = true;
84
						}
85
						if (this.line1.isEmpty()) {
86
							addActionError("Address line1 is empty.");
87
							invalidInput = true;
88
						}
89
						if (this.city.isEmpty()) {
90
							addActionError("City name is empty.");
91
							invalidInput = true;
92
						}
93
						if (this.state.isEmpty()) {
839 vikas 94
							addActionError("State name is empty.");
822 vikas 95
							invalidInput = true;
96
						}
97
						if (invalidInput) {
98
							return "redirect";
99
						}
517 rajveer 100
						Address address = new Address();
822 vikas 101
						address.setName(this.name);
102
						address.setLine1(this.line1);
103
						address.setLine2(this.line2);
104
						address.setCity(this.city);
105
						address.setState(this.state);
106
						address.setPin(this.pincode);
107
						address.setPhone(this.phone);
108
						address.setCountry(this.country);
517 rajveer 109
						address.setEnabled(true);
595 rajveer 110
						address.setType(AddressType.HOME);
517 rajveer 111
						if(isDefault.equals("true")){
569 rajveer 112
							userClient.addAddressForUser(userinfo.getUserId(), address, true);
822 vikas 113
							userinfo.setPincode(this.pincode);
517 rajveer 114
						}else{
569 rajveer 115
							userClient.addAddressForUser(userinfo.getUserId(), address, false);
517 rajveer 116
						}
822 vikas 117
						addActionMessage("Address added successfully.");
650 rajveer 118
						return "redirect";
517 rajveer 119
					}
507 rajveer 120
 
517 rajveer 121
					if(action.equals("delete")){
122
						Long addressId = Long.parseLong(this.request.getParameter("addressid"));
123
						userClient.removeAddressForUser(userinfo.getUserId(), addressId);
595 rajveer 124
						addActionMessage("Address deleted successfully.");
650 rajveer 125
						return "success";	
517 rajveer 126
					}
127
 
128
					if(action.equals("setdefault")){
129
						Long addressId = Long.parseLong(this.request.getParameter("addressid"));
130
						userClient.setDefaultAddress(userinfo.getUserId(), addressId);
793 rajveer 131
						//FIXME update pincode
595 rajveer 132
						addActionMessage("Address set default successfully.");
650 rajveer 133
						return "success";	
517 rajveer 134
					}
507 rajveer 135
				}
595 rajveer 136
 
507 rajveer 137
			} catch (Exception e) {
138
				e.printStackTrace();
595 rajveer 139
				addActionError("Unable to update address.");
650 rajveer 140
				return"redirect";
507 rajveer 141
			}
142
 
650 rajveer 143
			return "redirect";
507 rajveer 144
		}else{
650 rajveer 145
			return "failure";
507 rajveer 146
		}
147
	}
148
 
768 rajveer 149
	public String getMyaccountHeaderSnippet(){
150
		return htmlSnippets.get("MYACCOUNT_HEADER");
507 rajveer 151
	}
152
 
153
	public String getShippingAddressDetailsSnippet(){
154
		return htmlSnippets.get("SHIPPING_ADDRESS_DETAILS");
155
	}
822 vikas 156
 
157
	public String getName() {
158
		return name;
159
	}
160
 
161
	public void setName(String name) {
162
		this.name = name;
163
	}
164
 
165
	public String getLine1() {
166
		return line1;
167
	}
168
 
169
	public void setLine1(String line1) {
170
		this.line1 = line1;
171
	}
172
 
173
	public String getLine2() {
174
		return line2;
175
	}
176
 
177
	public void setLine2(String line2) {
178
		this.line2 = line2;
179
	}
180
 
181
	public String getCity() {
182
		return city;
183
	}
184
 
185
	public void setCity(String city) {
186
		this.city = city;
187
	}
188
 
189
	public String getState() {
190
		return state;
191
	}
192
 
193
	public void setState(String state) {
194
		this.state = state;
195
	}
196
 
197
	public String getPincode() {
198
		return pincode;
199
	}
200
 
201
	public void setPincode(String pincode) {
202
		this.pincode = pincode;
203
	}
204
 
205
	public String getCountry() {
206
		return country;
207
	}
208
 
209
	public void setCountry(String country) {
210
		this.country = country;
211
	}
212
 
213
	public String getPhone() {
214
		return phone;
215
	}
216
 
217
	public void setPhone(String phone) {
218
		this.phone = phone;
219
	}
220
 
221
	public String getAction() {
222
		return action;
223
	}
224
 
225
	public void setAction(String action) {
226
		this.action = action;
227
	}
228
 
229
	public String getDefault() {
230
		return isDefault;
231
	}
232
 
233
	public void setDefault(String isDefault) {
234
		this.isDefault = isDefault;
235
	}
507 rajveer 236
}