Subversion Repositories SmartDukaan

Rev

Rev 1597 | Rev 1630 | 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;
1597 ankur.sing 5
import in.shop2020.thrift.clients.TransactionServiceClient;
6
import in.shop2020.thrift.clients.UserContextServiceClient;
1611 ankur.sing 7
import in.shop2020.utils.StatisticsUser;
1597 ankur.sing 8
 
1611 ankur.sing 9
import javax.servlet.ServletContext;
1597 ankur.sing 10
import javax.servlet.http.HttpServletRequest;
11
import javax.servlet.http.HttpServletResponse;
1611 ankur.sing 12
import javax.servlet.http.HttpSession;
1597 ankur.sing 13
 
14
import org.apache.struts2.interceptor.ServletRequestAware;
15
import org.apache.struts2.interceptor.ServletResponseAware;
1611 ankur.sing 16
import org.apache.struts2.util.ServletContextAware;
1597 ankur.sing 17
 
1611 ankur.sing 18
public class StatisticsController implements ServletResponseAware, 
19
		ServletRequestAware, ServletContextAware{
1597 ankur.sing 20
 
1611 ankur.sing 21
	private ServletContext context;
1597 ankur.sing 22
	private HttpServletRequest request;
23
	private HttpServletResponse response;
1611 ankur.sing 24
	private HttpSession session;
1597 ankur.sing 25
 
26
	private String errorMsg = "";
27
	private long noOfRegisterUsers;
28
	private long noOfOrders;
29
	public StatisticsController(){
30
 
31
	}
32
 
33
	@Override
34
	public void setServletRequest(HttpServletRequest req) {
35
		this.request = req;
1611 ankur.sing 36
		this.session = req.getSession();
1597 ankur.sing 37
	}
38
 
39
	@Override
40
	public void setServletResponse(HttpServletResponse res) {
41
		this.response = res;
42
	}
43
 
44
	public String index()	{
1611 ankur.sing 45
		if(getSessionUserName()==null) {
46
			return "authfail";
47
		}
48
		else {
49
			getStats();
50
			return "authsuccess";
51
		}
52
	}
53
 
54
 
55
	private void getStats() {
1597 ankur.sing 56
		UserContextServiceClient usc;
57
		TransactionServiceClient tsc;
58
		try {
59
			usc = new UserContextServiceClient();
60
			in.shop2020.model.v1.user.UserContextService.Client uclient = usc.getClient();
61
			noOfRegisterUsers = uclient.getUserCount(UserType.USER);
62
 
63
			tsc = new TransactionServiceClient();
64
			in.shop2020.model.v1.order.TransactionService.Client tClient = tsc.getClient();
65
			noOfOrders = tClient.getValidOrderCount();
66
		} catch (Exception e) {
67
			e.printStackTrace();
68
		}
69
	}
70
 
1611 ankur.sing 71
	// Handler for POST /statistics
72
	public String create(){
73
		String username = request.getParameter("username");
74
		String password = request.getParameter("password");
75
		try{
76
			HelperServiceClient helperServiceClient = new HelperServiceClient();
77
			in.shop2020.utils.HelperService.Client client = helperServiceClient.getClient();
78
			StatisticsUser user = client.authenticateStatisticsUser(username, password);
79
			session.setAttribute("username", user.getUsername());
80
		}catch(Exception e){
81
			e.printStackTrace();
82
			return "authfail";
83
		}
84
		getStats();
85
		return "authsuccess";
86
	}
87
 
88
 
1597 ankur.sing 89
	public String show(){
90
		return null;
91
	}
92
 
93
	// Handles the POST request (Form Submission)
94
 
95
	public String getErrorMsg() {
96
		return errorMsg;
97
	}
98
 
99
	public long getNoOfRegisterUsers() {
100
		return noOfRegisterUsers;
101
	}
102
 
103
	public long getNoOfOrders() {
104
		return noOfOrders;
105
	}
106
 
1611 ankur.sing 107
	public String getSessionUserName(){
108
		return (String) session.getAttribute("username");
109
	}
110
 
111
	public String getServletContextPath(){
112
		return context.getContextPath();
113
	}
114
 
1597 ankur.sing 115
	public static void main(String[] args) {
116
		StatisticsController sc = new StatisticsController();
117
		sc.index();
118
	}
1611 ankur.sing 119
 
120
	@Override
121
	public void setServletContext(ServletContext context) {
122
		this.context = context;
123
 
124
	}
1597 ankur.sing 125
}
126