Subversion Repositories SmartDukaan

Rev

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