Subversion Repositories SmartDukaan

Rev

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