Subversion Repositories SmartDukaan

Rev

Rev 21363 | Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
21101 kshitij.so 1
package in.shop2020.support.controllers;
2
 
3
import in.shop2020.model.v1.order.UserWallet;
4
import in.shop2020.model.v1.user.User;
5
import in.shop2020.model.v1.user.UserContextService.Client;
6
import in.shop2020.support.utils.ReportsUtils;
7
import in.shop2020.thrift.clients.TransactionClient;
8
import in.shop2020.thrift.clients.UserClient;
9
 
10
import javax.servlet.ServletContext;
11
import javax.servlet.http.HttpServletRequest;
12
import javax.servlet.http.HttpServletResponse;
13
import javax.servlet.http.HttpSession;
14
 
15
import org.apache.struts2.convention.annotation.InterceptorRef;
16
import org.apache.struts2.convention.annotation.InterceptorRefs;
17
import org.apache.struts2.convention.annotation.Result;
18
import org.apache.struts2.convention.annotation.Results;
19
import org.apache.struts2.interceptor.ServletRequestAware;
20
import org.apache.struts2.interceptor.ServletResponseAware;
21
import org.apache.struts2.util.ServletContextAware;
22
import org.apache.thrift.TException;
23
import org.slf4j.Logger;
24
import org.slf4j.LoggerFactory;
25
 
26
import com.opensymphony.xwork2.ValidationAwareSupport;
27
 
28
@SuppressWarnings("serial")
29
 
30
@InterceptorRefs({
31
	@InterceptorRef("defaultStack"),
32
	@InterceptorRef("login")
33
})
34
@Results({
35
	@Result(name = "redirect", location = "${url}", type = "redirect"),
36
	@Result(name="authsuccess", type="redirectAction", params = {"actionName" , "reports"})
37
})
38
public class UserWalletCreditController extends ValidationAwareSupport implements ServletRequestAware ,ServletResponseAware, ServletContextAware{
39
 
40
	private static Logger logger = LoggerFactory.getLogger(UserWalletCreditController.class);
41
 
42
	private HttpServletRequest request;
43
	private HttpSession session;
44
	private HttpServletResponse response;
45
	private String id;
46
	private String email;
47
	private User user;
48
	private String amount;
49
	private String url;
50
	private String result;
51
 
52
	public String getResult() {
53
		return result;
54
	}
55
 
56
	public void setResult(String result) {
57
		this.result = result;
58
	}
59
 
60
	public String getUrl() {
61
		return url;
62
	}
63
 
64
	public void setUrl(String url) {
65
		this.url = url;
66
	}
67
 
68
	public String getAmount() {
69
		return amount;
70
	}
71
 
72
	public void setAmount(String amount) {
73
		this.amount = amount;
74
	}
75
 
76
	public User getUser() {
77
		return user;
78
	}
79
 
80
	public void setUser(User user) {
81
		this.user = user;
82
	}
83
 
84
	private UserWallet userWallet;
85
 
86
 
87
	public UserWallet getUserWallet() {
88
		return userWallet;
89
	}
90
 
91
	public void setUserWallet(UserWallet userWallet) {
92
		this.userWallet = userWallet;
93
	}
94
 
95
	public String getEmail() {
96
		return email;
97
	}
98
 
99
	public void setEmail(String email) {
100
		this.email = email;
101
	}
102
 
103
	public String index() throws TException {
104
		if (!ReportsUtils.canAccessReport((Long) session.getAttribute(ReportsUtils.ROLE),request.getServletPath())) {
105
			return "authfail";
106
		}
107
		return "index";
108
	}
109
 
110
	public String getWallet(){
111
		try{
112
			Client uc = new UserClient().getClient();
113
			user = uc.getUserByEmail(email);
114
			if (user.getUserId()==-1){
115
				addActionError("User email not valid");
116
				return "index";
117
			}
118
		}
119
		catch(Exception e){
120
			e.printStackTrace();
121
			addActionError("Service exception.");
122
			return "index";
123
		}
124
		try{
125
			in.shop2020.model.v1.order.TransactionService.Client tc = new TransactionClient().getClient();
126
			userWallet = tc.getUserWallet(user.getUserId());
127
		}
128
		catch(Exception e){
129
			e.printStackTrace();
130
			addActionError("Service exception.");
131
			return "index";
132
		}
133
		return "user-wallet-credit-edit";
134
	}
135
 
136
	public String creditWallet(){
137
		long credit_amount;
138
		try{
139
			credit_amount = Long.valueOf(amount);
140
			if (credit_amount <=0){
141
				setResult("Amount cant be negative");
142
				return "pmsa-result";
143
			}
144
		}
145
		catch(Exception e){
146
			setResult("Invalid amount");
147
			return "pmsa-result";
148
		}
149
		try{
150
			Client uc = new UserClient().getClient();
151
			user = uc.getUserByEmail(email);
152
		}
153
		catch(Exception e){
154
			setResult("Service error");
155
			return "pmsa-result";
156
		}
157
 
158
		if (user.getUserId()==-1){
159
			setResult("User email not valid");
160
			return "pmsa-result";
161
		}
162
		try {
163
			in.shop2020.model.v1.order.TransactionService.Client tc = new TransactionClient().getClient();
164
			tc.creditUserWallet(user.getUserId(), credit_amount);
165
		} catch (Exception e) {
166
			e.printStackTrace();
167
			setResult("Service error");
168
			return "pmsa-result";
169
		}
170
		setResult("Wallet updated successfully");
171
		return "pmsa-result";
172
	}
173
 
174
 
175
	public String getId() {
176
		return id;
177
	}
178
 
179
	public void setId(String id) {
180
		this.id = id;
181
	}
182
 
183
 
184
 
185
	public void setServletRequest(HttpServletRequest req) {
186
		this.request = req;
187
		this.session = req.getSession();        
188
	}
189
 
190
	@Override
191
	public void setServletContext(ServletContext arg0) {
192
		// TODO Auto-generated method stub
193
 
194
	}
195
 
196
	public void setServletResponse(HttpServletResponse response) {
197
		this.response = response;
198
	}
199
 
200
 
201
}