Subversion Repositories SmartDukaan

Rev

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