Subversion Repositories SmartDukaan

Rev

Rev 5874 | Rev 5877 | 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
 
5876 rajveer 45
    private static 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();
5876 rajveer 65
            if(stores == null){
66
            	lsc = new LogisticsClient();
67
            	lClient = lsc.getClient();
68
            	for(PickupStore store: lClient.getAllPickupStores()){
69
            		stores.put(store.getId(), store.getName());
70
            	}
71
            }
5874 rajveer 72
        } catch (Exception e) {
73
            logger.error("Error connecting to one of the user, order or payment service", e);
74
        }
75
	}
76
 
77
	public String index() {
78
        if(!ReportsUtils.canAccessReport((Long)session.getAttribute(ReportsUtils.ROLE), request.getServletPath())) {
79
            return "authfail";
80
        }
81
        getStats();
82
        return "authsuccess";
83
 
84
	}
85
 
86
	private void getStats() {
87
		try {
88
            validOrders = tClient.getValidOrders(50, true);
89
		} catch (Exception e){
90
		    logger.error("Error while getting order statistics", e);
91
		}
92
	}
93
 
94
    public List<Order> getValidOrders() {
95
        return validOrders;
96
    }
97
 
98
    public LineItem getItem(Order order) throws TransactionServiceException, TException {
99
        LineItem lItem = order.getLineitems().get(0);
100
        return lItem;
101
    }
102
 
103
    public String getOrderStatusString(OrderStatus status) {
104
        return status.getDescription();
105
    }
106
 
107
    public String getDateTime(long milliseconds) {
108
        Calendar cal = Calendar.getInstance();
109
        cal.setTimeInMillis(milliseconds);
110
        return formatter.format(cal.getTime());
111
    }
112
 
113
    @Override
114
    public void setServletRequest(HttpServletRequest req) {
115
        this.request = req;
116
        this.session = req.getSession();        
117
    }
118
 
119
    @Override
120
    public void setServletContext(ServletContext context) {
121
        this.context = context;
122
    }
123
 
5876 rajveer 124
    public String getStoreName(long storeId){
125
    	return stores.get(storeId);
126
    }
127
 
5874 rajveer 128
    public String getServletContextPath() {
129
        return context.getContextPath();
130
    }
131
}
132