Subversion Repositories SmartDukaan

Rev

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