Subversion Repositories SmartDukaan

Rev

Rev 7240 | Rev 20780 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

package in.shop2020.support.controllers;

import in.shop2020.model.v1.order.LineItem;
import in.shop2020.model.v1.order.Order;
import in.shop2020.model.v1.order.OrderStatus;
import in.shop2020.model.v1.order.TransactionServiceException;
import in.shop2020.model.v1.user.UserType;
import in.shop2020.support.utils.ReportsUtils;
import in.shop2020.thrift.clients.PaymentClient;
import in.shop2020.thrift.clients.TransactionClient;
import in.shop2020.thrift.clients.UserClient;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.struts2.convention.annotation.InterceptorRef;
import org.apache.struts2.convention.annotation.InterceptorRefs;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.util.ServletContextAware;
import org.apache.thrift.TException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


@InterceptorRefs({
    @InterceptorRef("defaultStack"),
    @InterceptorRef("login")
})

@Results({
    @Result(name="authfail", type="redirectAction", params = {"actionName" , "reports"})
})
public class StatisticsController implements ServletRequestAware, ServletContextAware {

    private static Logger logger = LoggerFactory.getLogger(StatisticsController.class);
    
    private HttpServletRequest request;
    private HttpSession session;
    private ServletContext context;
    
        private long noOfRegisterUsers;
        private long noOfOrders;
        private long noOfCustomers;
        private double maxOrderAmount;
        private double minOrderAmount;
        private double maxPaymentAmount;
        private double minPaymentAmount;
        private List<Double> paymentAmountRange;
        private List<Double> orderAmountRange;
        private List<Order> validOrders;

    private UserClient usc;
    private in.shop2020.model.v1.user.UserContextService.Client uclient;
    
    private TransactionClient tsc;
    private in.shop2020.model.v1.order.TransactionService.Client tClient;
    
    private PaymentClient psc;
    private in.shop2020.payments.PaymentService.Client pClient;
    
    private final DateFormat formatter = new SimpleDateFormat("EEE, dd-MMM-yyyy hh:mm a");
        
        public StatisticsController(){
            try {
            usc = new UserClient();
            uclient = usc.getClient();
            
            tsc = new TransactionClient();
            tClient = tsc.getClient();
            
            psc = new PaymentClient();
            pClient = psc.getClient();
        } catch (Exception e) {
            logger.error("Error connecting to one of the user, order or payment service", e);
        }
        }
                
        public String index() {
        if(!ReportsUtils.canAccessReport((Long)session.getAttribute(ReportsUtils.ROLE), request.getServletPath())) {
            return "authfail";
        }
        getStats();
        return "authsuccess";
        
                /*if(getSessionUserName()==null) {
                        return "authfail";
                }
                else {
                    if(!canAccessReport()) {
                        return "exception";
                    }
                        getStats();
                        return "authsuccess";
                }*/
        }
        
        private void getStats() {
//              try {
//            noOfRegisterUsers = uclient.getUserCount(UserType.USER);
//              } catch(Exception e){
//                  logger.error("Error while getting the no. of registered users", e);
//              }
            
                try {
//            noOfOrders = tClient.getValidOrderCount();
//            noOfCustomers = tClient.getNoOfCustomersWithSuccessfulTransaction();
//            orderAmountRange = tClient.getValidOrdersAmountRange();
//            minOrderAmount = orderAmountRange.get(0);
//            maxOrderAmount = orderAmountRange.get(1);
//            
            validOrders = tClient.getValidOrders(200, false);
                } catch (Exception e){
                    logger.error("Error while getting order statistics", e);
                }
                
                try {
            paymentAmountRange = pClient.getSuccessfulPaymentsAmountRange();
            minPaymentAmount = paymentAmountRange.get(0);
            maxPaymentAmount = paymentAmountRange.get(1);
        } catch (Exception e) {
            logger.error("Error while getting payment statistics", e);
        }
        }
        
        public long getNoOfRegisterUsers() {
                return noOfRegisterUsers;
        }

        public long getNoOfOrders() {
                return noOfOrders;
        }
        
        public long getNoOfCustomers() {
                return noOfCustomers;
        }

        public double getMaxOrderAmount() {
                return maxOrderAmount;
        }

        public double getMinOrderAmount() {
                return minOrderAmount;
        }

        public double getMaxPaymentAmount() {
                return maxPaymentAmount;
        }

        public double getMinPaymentAmount() {
                return minPaymentAmount;
        }

    public List<Order> getValidOrders() {
        return validOrders;
    }
    
    public LineItem getItem(Order order) throws TransactionServiceException, TException {
        LineItem lItem = order.getLineitems().get(0);
        return lItem;
    }
    
    public String getOrderStatusString(OrderStatus status) {
        return status.getDescription();
    }
    
    public String getDateTime(long milliseconds) {
        Calendar cal = Calendar.getInstance();
        cal.setTimeInMillis(milliseconds);
        return formatter.format(cal.getTime());
    }

    @Override
    public void setServletRequest(HttpServletRequest req) {
        this.request = req;
        this.session = req.getSession();        
    }
    
    @Override
    public void setServletContext(ServletContext context) {
        this.context = context;
    }

    public String getServletContextPath() {
        return context.getContextPath();
    }
}