Subversion Repositories SmartDukaan

Rev

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

Rev Author Line No. Line
3427 chandransh 1
package in.shop2020.support.controllers;
2
 
3
import java.io.ByteArrayOutputStream;
4
import java.io.IOException;
5
import java.text.DateFormat;
6
import java.text.ParseException;
7
import java.text.SimpleDateFormat;
8
import java.util.Calendar;
9
import java.util.Date;
10
 
11
import in.shop2020.logistics.LogisticsServiceException;
12
import in.shop2020.logistics.Provider;
13
import in.shop2020.support.services.CourierReconciliationGenerator;
14
import in.shop2020.support.utils.ReportsUtils;
15
import in.shop2020.thrift.clients.LogisticsClient;
16
 
17
import javax.servlet.ServletContext;
18
import javax.servlet.ServletOutputStream;
19
import javax.servlet.http.HttpServletRequest;
20
import javax.servlet.http.HttpServletResponse;
21
import javax.servlet.http.HttpSession;
22
 
23
import org.apache.struts2.convention.annotation.InterceptorRef;
24
import org.apache.struts2.convention.annotation.InterceptorRefs;
25
import org.apache.struts2.interceptor.ServletRequestAware;
26
import org.apache.struts2.interceptor.ServletResponseAware;
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
@InterceptorRefs({
33
    @InterceptorRef("defaultStack"),
34
    @InterceptorRef("login")
35
})
36
public class CourierReconciliationController implements ServletRequestAware, ServletResponseAware, ServletContextAware{
37
 
38
    private static Logger logger = LoggerFactory.getLogger(CourierReconciliationController.class);
39
 
40
    private HttpServletRequest request;
41
    private HttpSession session;
42
    private HttpServletResponse response;
43
    private ServletContext context;
44
 
45
    private String errorMsg = "";
46
 
47
    public String index() {
48
        if(!ReportsUtils.canAccessReport((Long)session.getAttribute(ReportsUtils.ROLE), request.getServletPath())) {
49
            return "exception";
50
        }
51
        return "index";
52
    }
53
 
54
    public String create() {
55
        // Formatting Form input parameters
56
        String startDateStr = request.getParameter("startDate");
57
        String endDateStr = request.getParameter("endDate");
58
        String providerIdStr = request.getParameter("providerId");
3449 chandransh 59
        String payModeStr = request.getParameter("payMode");
3427 chandransh 60
        int providerId = 1;
3449 chandransh 61
        boolean payMode = false;
3427 chandransh 62
        DateFormat df = new SimpleDateFormat("MM/dd/yyyy");
63
        Date startDate = null, endDate = null;
64
        try {
65
            startDate = df.parse(startDateStr);
66
            endDate = df.parse(endDateStr);
67
            Calendar cal = Calendar.getInstance();
68
            cal.setTime(endDate);
69
            cal.add(Calendar.DATE, 1);
70
            endDate.setTime(cal.getTimeInMillis());
71
            providerId = Integer.parseInt(providerIdStr);
3449 chandransh 72
            payMode = Boolean.parseBoolean(payModeStr);
3427 chandransh 73
        } catch (ParseException pe) {
74
            errorMsg = "Please enter start and end dates in format MM/dd/yyyy";
75
            return "index";
76
        } catch (NumberFormatException nfe) {
3449 chandransh 77
            errorMsg = "Please select a provider and a payment mode";
3427 chandransh 78
            return "index";
79
        }
80
 
81
        Provider provider = null;
82
        try {
83
            LogisticsClient lsc = new LogisticsClient();
84
            in.shop2020.logistics.LogisticsService.Client logisticsClient = lsc.getClient();
85
            provider = logisticsClient.getProvider(providerId);
86
        } catch (TException e) {
87
            logger.error("Error getting information from one of the Thrift Services: ", e);
88
            return "index";
89
        } catch (LogisticsServiceException e) {
90
            logger.error("Error getting provider info from the logistics service: ", e);
91
            return "index";
92
        }
93
 
94
        CourierReconciliationGenerator crGenerator = new CourierReconciliationGenerator();
3449 chandransh 95
        ByteArrayOutputStream baos = crGenerator.generateCourierReconciliationReport(startDate, endDate, providerId, payMode);
3427 chandransh 96
 
97
        if (baos == null) {
98
            errorMsg = "No output for given date range";
99
            return "index";
100
        }
101
 
102
        // Preparing XLS file for output
103
        response.setContentType("application/vnd.ms-excel");
3449 chandransh 104
        response.setHeader("Content-disposition", "inline; filename=" + provider.getName() + (payMode ? "-cod" : "-prepaid") + "-courier-reconciliation-" + startDateStr + "-" + endDateStr + ".xls");
3427 chandransh 105
 
106
        ServletOutputStream sos;
107
        try {
108
            sos = response.getOutputStream();
109
            baos.writeTo(sos);
110
            sos.flush();
111
        } catch (IOException e) {
112
            errorMsg = "Failed to write to response.";
113
            logger.error("Unable to stream the payment details report", e);
114
        }
115
 
116
        return "index";
117
    }
118
 
119
 
120
    public String getErrorMsg() {
121
        return errorMsg;
122
    }
123
 
124
    @Override
125
    public void setServletRequest(HttpServletRequest req) {
126
        this.request = req;
127
        this.session = req.getSession();        
128
    }
129
 
130
    @Override
131
    public void setServletResponse(HttpServletResponse res) {
132
        this.response = res;
133
    }
134
 
135
    @Override
136
    public void setServletContext(ServletContext context) {
137
        this.context = context;
138
    }
139
 
140
    public String getServletContextPath() {
141
        return context.getContextPath();
142
    }
143
}