Subversion Repositories SmartDukaan

Rev

Rev 5876 | Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
5874 rajveer 1
package in.shop2020.support.controllers;
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;
7
import in.shop2020.support.utils.ReportsUtils;
8
import in.shop2020.thrift.clients.TransactionClient;
9
 
10
import java.text.DateFormat;
11
import java.text.SimpleDateFormat;
12
import java.util.Calendar;
13
import java.util.List;
14
 
15
import javax.servlet.ServletContext;
16
import javax.servlet.http.HttpServletRequest;
17
import javax.servlet.http.HttpSession;
18
 
19
import org.apache.struts2.convention.annotation.InterceptorRef;
20
import org.apache.struts2.convention.annotation.InterceptorRefs;
21
import org.apache.struts2.convention.annotation.Result;
22
import org.apache.struts2.convention.annotation.Results;
23
import org.apache.struts2.interceptor.ServletRequestAware;
24
import org.apache.struts2.util.ServletContextAware;
25
import org.apache.thrift.TException;
26
import org.slf4j.Logger;
27
import org.slf4j.LoggerFactory;
28
 
29
 
30
@InterceptorRefs({
31
    @InterceptorRef("defaultStack"),
32
    @InterceptorRef("login")
33
})
34
 
35
@Results({
36
    @Result(name="authfail", type="redirectAction", params = {"actionName" , "reports"})
37
})
38
public class PickupStoreStatisticsController implements ServletRequestAware, ServletContextAware {
39
 
40
    private static Logger logger = LoggerFactory.getLogger(PickupStoreStatisticsController.class);
41
 
42
    private HttpServletRequest request;
43
    private HttpSession session;
44
    private ServletContext context;
45
 
46
	private List<Order> validOrders;
47
 
48
 
49
    private TransactionClient tsc;
50
    private in.shop2020.model.v1.order.TransactionService.Client tClient;
51
 
52
    private final DateFormat formatter = new SimpleDateFormat("EEE, dd-MMM-yyyy hh:mm a");
53
 
54
	public PickupStoreStatisticsController(){
55
	    try {
56
            tsc = new TransactionClient();
57
            tClient = tsc.getClient();
58
        } catch (Exception e) {
59
            logger.error("Error connecting to one of the user, order or payment service", e);
60
        }
61
	}
62
 
63
	public String index() {
64
        if(!ReportsUtils.canAccessReport((Long)session.getAttribute(ReportsUtils.ROLE), request.getServletPath())) {
65
            return "authfail";
66
        }
67
        getStats();
68
        return "authsuccess";
69
 
70
	}
71
 
72
	private void getStats() {
73
		try {
74
            validOrders = tClient.getValidOrders(50, true);
75
		} catch (Exception e){
76
		    logger.error("Error while getting order statistics", e);
77
		}
78
	}
79
 
80
    public List<Order> getValidOrders() {
81
        return validOrders;
82
    }
83
 
84
    public LineItem getItem(Order order) throws TransactionServiceException, TException {
85
        LineItem lItem = order.getLineitems().get(0);
86
        return lItem;
87
    }
88
 
89
    public String getOrderStatusString(OrderStatus status) {
90
        return status.getDescription();
91
    }
92
 
93
    public String getDateTime(long milliseconds) {
94
        Calendar cal = Calendar.getInstance();
95
        cal.setTimeInMillis(milliseconds);
96
        return formatter.format(cal.getTime());
97
    }
98
 
99
    @Override
100
    public void setServletRequest(HttpServletRequest req) {
101
        this.request = req;
102
        this.session = req.getSession();        
103
    }
104
 
105
    @Override
106
    public void setServletContext(ServletContext context) {
107
        this.context = context;
108
    }
109
 
110
    public String getServletContextPath() {
111
        return context.getContextPath();
112
    }
113
}
114