Subversion Repositories SmartDukaan

Rev

Rev 793 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 793 Rev 822
Line 2... Line 2...
2
 
2
 
3
import in.shop2020.model.v1.user.Address;
3
import in.shop2020.model.v1.user.Address;
4
import in.shop2020.model.v1.user.AddressType;
4
import in.shop2020.model.v1.user.AddressType;
5
import in.shop2020.model.v1.user.UserContextService.Client;
5
import in.shop2020.model.v1.user.UserContextService.Client;
6
import in.shop2020.serving.controllers.BaseController;
6
import in.shop2020.serving.controllers.BaseController;
-
 
7
import in.shop2020.serving.utils.Utils;
7
import in.shop2020.thrift.clients.UserContextServiceClient;
8
import in.shop2020.thrift.clients.UserContextServiceClient;
8
 
9
 
9
import java.util.*;
10
import java.util.*;
10
 
11
 
11
import org.apache.juli.logging.Log;
12
import org.apache.juli.logging.Log;
12
import org.apache.juli.logging.LogFactory;
13
import org.apache.juli.logging.LogFactory;
-
 
14
import org.apache.struts2.convention.annotation.InterceptorRef;
-
 
15
import org.apache.struts2.convention.annotation.InterceptorRefs;
13
import org.apache.struts2.convention.annotation.Result;
16
import org.apache.struts2.convention.annotation.Result;
14
import org.apache.struts2.convention.annotation.Results;
17
import org.apache.struts2.convention.annotation.Results;
15
import org.apache.struts2.interceptor.ParameterAware;
18
import org.apache.struts2.interceptor.ParameterAware;
-
 
19
 
-
 
20
@InterceptorRefs({
16
import org.apache.struts2.rest.DefaultHttpHeaders;
21
    @InterceptorRef("myDefault"),
17
import org.apache.struts2.rest.HttpHeaders;
22
    @InterceptorRef("login")
-
 
23
})
18
 
24
 
19
@Results({
25
@Results({
20
    @Result(name="redirect", type="redirectAction", 
26
    @Result(name="redirect", type="redirectAction", 
21
    		params = {"actionName" , "address"}),
27
    		params = {"actionName" , "address"})
22
    @Result(name="login", type="redirectAction", 
-
 
23
       		params = {"actionName" , "login"})
-
 
24
})
28
})
25
public class AddressController extends BaseController implements ParameterAware{
29
public class AddressController extends BaseController{
26
	
30
	
27
	private static final long serialVersionUID = 1L;
31
	private static final long serialVersionUID = 1L;
28
	private static Log log = LogFactory.getLog(AddressController.class);
32
	private static Log log = LogFactory.getLog(AddressController.class);
-
 
33
	
29
	Map<String, String[]> reqparams = null;
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;
30
	
47
	
31
	public AddressController(){
48
	public AddressController(){
32
		super();
49
		super();
33
	}
50
	}
34
	
51
	
35
	 // GET /address
52
	 // GET /address
36
	 public String index() {
53
	 public String index() {
-
 
54
		log.info("Check error msgs");
-
 
55
		Collection<String> errorMsgs = getActionErrors();
37
		if(!userinfo.isLoggedIn()){
56
		if(errorMsgs !=null && !errorMsgs.isEmpty()){
38
			setRedirectUrl();
57
			for (String errMsg : errorMsgs) { 
39
			return "login";
58
			    this.errorMsg += errMsg + "<br/>";
-
 
59
			}
40
		}
60
		}
-
 
61
		log.info(this.errorMsg);
41
		htmlSnippets.put("MYACCOUNT_HEADER",pageLoader.getMyaccountHeaderHtml());
62
		htmlSnippets.put("MYACCOUNT_HEADER",pageLoader.getMyaccountHeaderHtml());
42
		htmlSnippets.put("SHIPPING_ADDRESS_DETAILS",pageLoader.getShippingAddressDetailsHtml(userinfo.getUserId()));
63
		htmlSnippets.put("SHIPPING_ADDRESS_DETAILS",pageLoader.getShippingAddressDetailsHtml(userinfo.getUserId(), this.errorMsg));
43
		return "index";
64
		return "index";
44
	 }
65
	 }
45
 
66
 
46
	// POST /address
67
	// POST /address
47
	public String create(){
68
	public String create(){
48
    	printParams();
-
 
49
 
69
 
50
		if(userinfo.isLoggedIn()){
70
		if(userinfo.isLoggedIn()){
51
			UserContextServiceClient userContextServiceClient;
71
			UserContextServiceClient userContextServiceClient;
52
			try {
72
			try {
53
				userContextServiceClient = new UserContextServiceClient();
73
				userContextServiceClient = new UserContextServiceClient();
54
				Client userClient = userContextServiceClient.getClient();
74
				Client userClient = userContextServiceClient.getClient();
55
				
75
				
56
				String action = this.request.getParameter("action");
-
 
57
				String isDefault = this.request.getParameter("default");
-
 
58
				
-
 
59
				if(action != null){
76
				if(action != null){
60
					if(action.equals("add")){
77
					if(action.equals("add")){
-
 
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
						}
61
						Address address = new Address();
102
						Address address = new Address();
62
						address.setName(this.request.getParameter("name"));
103
						address.setName(this.name);
63
						address.setLine1(this.request.getParameter("line1"));
104
						address.setLine1(this.line1);
64
						address.setLine2(this.request.getParameter("line2"));
105
						address.setLine2(this.line2);
65
						address.setCity(this.request.getParameter("city"));
106
						address.setCity(this.city);
66
						address.setState(this.request.getParameter("state"));
107
						address.setState(this.state);
67
						address.setPin(this.request.getParameter("pincode"));
108
						address.setPin(this.pincode);
68
						address.setPhone(this.request.getParameter("phone"));
109
						address.setPhone(this.phone);
69
						address.setCountry(this.request.getParameter("country"));
110
						address.setCountry(this.country);
70
						address.setEnabled(true);
111
						address.setEnabled(true);
71
						address.setType(AddressType.HOME);
112
						address.setType(AddressType.HOME);
72
						if(isDefault.equals("true")){
113
						if(isDefault.equals("true")){
73
							userClient.addAddressForUser(userinfo.getUserId(), address, true);
114
							userClient.addAddressForUser(userinfo.getUserId(), address, true);
74
							userinfo.setPincode(this.request.getParameter("pincode"));
115
							userinfo.setPincode(this.pincode);
75
							addActionMessage("Address added successfully.");
-
 
76
						}else{
116
						}else{
77
							userClient.addAddressForUser(userinfo.getUserId(), address, false);
117
							userClient.addAddressForUser(userinfo.getUserId(), address, false);
78
							addActionMessage("Address added successfully.");
-
 
79
						}
118
						}
-
 
119
						addActionMessage("Address added successfully.");
80
						return "redirect";
120
						return "redirect";
81
					}
121
					}
82
					
122
					
83
					if(action.equals("delete")){
123
					if(action.equals("delete")){
84
						Long addressId = Long.parseLong(this.request.getParameter("addressid"));
124
						Long addressId = Long.parseLong(this.request.getParameter("addressid"));
Line 104... Line 144...
104
			
144
			
105
			return "redirect";
145
			return "redirect";
106
		}else{
146
		}else{
107
			return "failure";
147
			return "failure";
108
		}
148
		}
109
		
-
 
110
		
-
 
111
	}
-
 
112
 
-
 
113
    public void printParams(){
-
 
114
    	for(String param : reqparams.keySet()) {
-
 
115
    		log.info("param name is " + param);
-
 
116
    		log.info("param first is " + reqparams.get(param)[0]);
-
 
117
    	}
-
 
118
    	log.info(this.reqparams);
-
 
119
    }
-
 
120
 
-
 
121
	@Override
-
 
122
	public void setParameters(Map<String, String[]> reqmap) {
-
 
123
		log.info("setParameters:" + reqmap);
-
 
124
		
-
 
125
		this.reqparams = reqmap;
-
 
126
	}
149
	}
127
 
150
 
128
	public String getMyaccountHeaderSnippet(){
151
	public String getMyaccountHeaderSnippet(){
129
		return htmlSnippets.get("MYACCOUNT_HEADER");
152
		return htmlSnippets.get("MYACCOUNT_HEADER");
130
	}
153
	}
131
	
154
	
132
	public String getShippingAddressDetailsSnippet(){
155
	public String getShippingAddressDetailsSnippet(){
133
		return htmlSnippets.get("SHIPPING_ADDRESS_DETAILS");
156
		return htmlSnippets.get("SHIPPING_ADDRESS_DETAILS");
134
	}
157
	}
-
 
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
	}
135
}
238
}