Subversion Repositories SmartDukaan

Rev

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