Subversion Repositories SmartDukaan

Rev

Details | 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
 
12
import org.apache.juli.logging.Log;
13
import org.apache.juli.logging.LogFactory;
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
import org.apache.struts2.interceptor.ParameterAware;
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;
32
	private static Log log = LogFactory.getLog(AddressController.class);
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
						}
83
						if (!Utils.validatePin(this.phone)) {
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()) {
96
							addActionError("City name is empty.");
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")){
569 rajveer 114
							userClient.addAddressForUser(userinfo.getUserId(), address, true);
822 vikas 115
							userinfo.setPincode(this.pincode);
517 rajveer 116
						}else{
569 rajveer 117
							userClient.addAddressForUser(userinfo.getUserId(), address, false);
517 rajveer 118
						}
822 vikas 119
						addActionMessage("Address added successfully.");
650 rajveer 120
						return "redirect";
517 rajveer 121
					}
507 rajveer 122
 
517 rajveer 123
					if(action.equals("delete")){
124
						Long addressId = Long.parseLong(this.request.getParameter("addressid"));
125
						userClient.removeAddressForUser(userinfo.getUserId(), addressId);
595 rajveer 126
						addActionMessage("Address deleted successfully.");
650 rajveer 127
						return "success";	
517 rajveer 128
					}
129
 
130
					if(action.equals("setdefault")){
131
						Long addressId = Long.parseLong(this.request.getParameter("addressid"));
132
						userClient.setDefaultAddress(userinfo.getUserId(), addressId);
793 rajveer 133
						//FIXME update pincode
595 rajveer 134
						addActionMessage("Address set default successfully.");
650 rajveer 135
						return "success";	
517 rajveer 136
					}
507 rajveer 137
				}
595 rajveer 138
 
507 rajveer 139
			} catch (Exception e) {
140
				e.printStackTrace();
595 rajveer 141
				addActionError("Unable to update address.");
650 rajveer 142
				return"redirect";
507 rajveer 143
			}
144
 
650 rajveer 145
			return "redirect";
507 rajveer 146
		}else{
650 rajveer 147
			return "failure";
507 rajveer 148
		}
149
	}
150
 
768 rajveer 151
	public String getMyaccountHeaderSnippet(){
152
		return htmlSnippets.get("MYACCOUNT_HEADER");
507 rajveer 153
	}
154
 
155
	public String getShippingAddressDetailsSnippet(){
156
		return htmlSnippets.get("SHIPPING_ADDRESS_DETAILS");
157
	}
822 vikas 158
 
159
	public String getName() {
160
		return name;
161
	}
162
 
163
	public void setName(String name) {
164
		this.name = name;
165
	}
166
 
167
	public String getLine1() {
168
		return line1;
169
	}
170
 
171
	public void setLine1(String line1) {
172
		this.line1 = line1;
173
	}
174
 
175
	public String getLine2() {
176
		return line2;
177
	}
178
 
179
	public void setLine2(String line2) {
180
		this.line2 = line2;
181
	}
182
 
183
	public String getCity() {
184
		return city;
185
	}
186
 
187
	public void setCity(String city) {
188
		this.city = city;
189
	}
190
 
191
	public String getState() {
192
		return state;
193
	}
194
 
195
	public void setState(String state) {
196
		this.state = state;
197
	}
198
 
199
	public String getPincode() {
200
		return pincode;
201
	}
202
 
203
	public void setPincode(String pincode) {
204
		this.pincode = pincode;
205
	}
206
 
207
	public String getCountry() {
208
		return country;
209
	}
210
 
211
	public void setCountry(String country) {
212
		this.country = country;
213
	}
214
 
215
	public String getPhone() {
216
		return phone;
217
	}
218
 
219
	public void setPhone(String phone) {
220
		this.phone = phone;
221
	}
222
 
223
	public String getAction() {
224
		return action;
225
	}
226
 
227
	public void setAction(String action) {
228
		this.action = action;
229
	}
230
 
231
	public String getDefault() {
232
		return isDefault;
233
	}
234
 
235
	public void setDefault(String isDefault) {
236
		this.isDefault = isDefault;
237
	}
507 rajveer 238
}