Subversion Repositories SmartDukaan

Rev

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