Subversion Repositories SmartDukaan

Rev

Rev 5876 | Rev 5878 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

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