Subversion Repositories SmartDukaan

Rev

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