Subversion Repositories SmartDukaan

Rev

Rev 1677 | Rev 1884 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1597 ankur.sing 1
package in.shop2020.support.controllers;
2
 
3
import in.shop2020.model.v1.user.UserType;
1611 ankur.sing 4
import in.shop2020.thrift.clients.HelperServiceClient;
1630 ankur.sing 5
import in.shop2020.thrift.clients.PaymentServiceClient;
1597 ankur.sing 6
import in.shop2020.thrift.clients.TransactionServiceClient;
7
import in.shop2020.thrift.clients.UserContextServiceClient;
1611 ankur.sing 8
import in.shop2020.utils.StatisticsUser;
1597 ankur.sing 9
 
1731 ankur.sing 10
import java.util.List;
11
 
1611 ankur.sing 12
import javax.servlet.ServletContext;
1597 ankur.sing 13
import javax.servlet.http.HttpServletRequest;
14
import javax.servlet.http.HttpServletResponse;
1611 ankur.sing 15
import javax.servlet.http.HttpSession;
1597 ankur.sing 16
 
17
import org.apache.struts2.interceptor.ServletRequestAware;
18
import org.apache.struts2.interceptor.ServletResponseAware;
1611 ankur.sing 19
import org.apache.struts2.util.ServletContextAware;
1597 ankur.sing 20
 
1611 ankur.sing 21
public class StatisticsController implements ServletResponseAware, 
22
		ServletRequestAware, ServletContextAware{
1597 ankur.sing 23
 
1611 ankur.sing 24
	private ServletContext context;
1597 ankur.sing 25
	private HttpServletRequest request;
26
	private HttpServletResponse response;
1611 ankur.sing 27
	private HttpSession session;
1597 ankur.sing 28
 
29
	private String errorMsg = "";
30
	private long noOfRegisterUsers;
31
	private long noOfOrders;
1630 ankur.sing 32
	private long noOfCustomers;
33
	private double maxOrderAmount;
34
	private double minOrderAmount;
35
	private double maxPaymentAmount;
36
	private double minPaymentAmount;
1731 ankur.sing 37
	private List<Double> paymentAmountRange;
38
	private List<Double> orderAmountRange;
1630 ankur.sing 39
 
1597 ankur.sing 40
	public StatisticsController(){
41
 
42
	}
43
 
44
	@Override
45
	public void setServletRequest(HttpServletRequest req) {
46
		this.request = req;
1611 ankur.sing 47
		this.session = req.getSession();
1597 ankur.sing 48
	}
49
 
50
	@Override
51
	public void setServletResponse(HttpServletResponse res) {
52
		this.response = res;
53
	}
54
 
55
	public String index()	{
1611 ankur.sing 56
		if(getSessionUserName()==null) {
57
			return "authfail";
58
		}
59
		else {
60
			getStats();
61
			return "authsuccess";
62
		}
63
	}
64
 
65
 
66
	private void getStats() {
1597 ankur.sing 67
		UserContextServiceClient usc;
68
		TransactionServiceClient tsc;
1630 ankur.sing 69
		PaymentServiceClient psc;
1597 ankur.sing 70
		try {
71
			usc = new UserContextServiceClient();
72
			in.shop2020.model.v1.user.UserContextService.Client uclient = usc.getClient();
73
			noOfRegisterUsers = uclient.getUserCount(UserType.USER);
74
 
75
			tsc = new TransactionServiceClient();
76
			in.shop2020.model.v1.order.TransactionService.Client tClient = tsc.getClient();
77
			noOfOrders = tClient.getValidOrderCount();
1630 ankur.sing 78
			noOfCustomers = tClient.getNoOfCustomersWithSuccessfulTransaction();
1731 ankur.sing 79
			orderAmountRange = tClient.getValidOrdersAmountRange();
80
			minOrderAmount = orderAmountRange.get(0);
81
			maxOrderAmount = orderAmountRange.get(1);
1630 ankur.sing 82
 
83
			psc = new PaymentServiceClient();
84
			in.shop2020.payments.PaymentService.Client pClient = psc.getClient();
1731 ankur.sing 85
			paymentAmountRange = pClient.getSuccessfulPaymentsAmountRange();
86
			minPaymentAmount = paymentAmountRange.get(0);
87
			maxPaymentAmount = paymentAmountRange.get(1);
1597 ankur.sing 88
		} catch (Exception e) {
89
			e.printStackTrace();
90
		}
91
	}
92
 
1611 ankur.sing 93
	// Handler for POST /statistics
94
	public String create(){
95
		String username = request.getParameter("username");
96
		String password = request.getParameter("password");
97
		try{
98
			HelperServiceClient helperServiceClient = new HelperServiceClient();
99
			in.shop2020.utils.HelperService.Client client = helperServiceClient.getClient();
100
			StatisticsUser user = client.authenticateStatisticsUser(username, password);
101
			session.setAttribute("username", user.getUsername());
102
		}catch(Exception e){
103
			e.printStackTrace();
104
			return "authfail";
105
		}
106
		getStats();
107
		return "authsuccess";
108
	}
109
 
110
 
1597 ankur.sing 111
	public String show(){
112
		return null;
113
	}
114
 
115
	// Handles the POST request (Form Submission)
116
 
117
	public String getErrorMsg() {
118
		return errorMsg;
119
	}
120
 
121
	public long getNoOfRegisterUsers() {
122
		return noOfRegisterUsers;
123
	}
124
 
125
	public long getNoOfOrders() {
126
		return noOfOrders;
127
	}
128
 
1630 ankur.sing 129
	public long getNoOfCustomers() {
130
		return noOfCustomers;
131
	}
132
 
133
	public double getMaxOrderAmount() {
134
		return maxOrderAmount;
135
	}
136
 
137
	public double getMinOrderAmount() {
138
		return minOrderAmount;
139
	}
140
 
141
	public double getMaxPaymentAmount() {
142
		return maxPaymentAmount;
143
	}
144
 
145
	public double getMinPaymentAmount() {
146
		return minPaymentAmount;
147
	}
148
 
1611 ankur.sing 149
	public String getSessionUserName(){
150
		return (String) session.getAttribute("username");
151
	}
152
 
153
	public String getServletContextPath(){
154
		return context.getContextPath();
155
	}
156
 
157
	@Override
158
	public void setServletContext(ServletContext context) {
159
		this.context = context;
160
 
161
	}
1597 ankur.sing 162
}
163