Subversion Repositories SmartDukaan

Rev

Rev 1884 | Rev 1891 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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