Subversion Repositories SmartDukaan

Rev

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

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