Subversion Repositories SmartDukaan

Rev

Rev 3449 | Go to most recent revision | Details | 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");
59
        int providerId = 1;
60
        DateFormat df = new SimpleDateFormat("MM/dd/yyyy");
61
        Date startDate = null, endDate = null;
62
        try {
63
            startDate = df.parse(startDateStr);
64
            endDate = df.parse(endDateStr);
65
            Calendar cal = Calendar.getInstance();
66
            cal.setTime(endDate);
67
            cal.add(Calendar.DATE, 1);
68
            endDate.setTime(cal.getTimeInMillis());
69
            providerId = Integer.parseInt(providerIdStr);
70
        } catch (ParseException pe) {
71
            errorMsg = "Please enter start and end dates in format MM/dd/yyyy";
72
            return "index";
73
        } catch (NumberFormatException nfe) {
74
            errorMsg = "Please select a provider";
75
            return "index";
76
        }
77
 
78
        Provider provider = null;
79
        try {
80
            LogisticsClient lsc = new LogisticsClient();
81
            in.shop2020.logistics.LogisticsService.Client logisticsClient = lsc.getClient();
82
            provider = logisticsClient.getProvider(providerId);
83
        } catch (TException e) {
84
            logger.error("Error getting information from one of the Thrift Services: ", e);
85
            return "index";
86
        } catch (LogisticsServiceException e) {
87
            logger.error("Error getting provider info from the logistics service: ", e);
88
            return "index";
89
        }
90
 
91
        CourierReconciliationGenerator crGenerator = new CourierReconciliationGenerator();
92
        ByteArrayOutputStream baos = crGenerator.generateCourierReconciliationReport(startDate, endDate, providerId);
93
 
94
        if (baos == null) {
95
            errorMsg = "No output for given date range";
96
            return "index";
97
        }
98
 
99
        // Preparing XLS file for output
100
        response.setContentType("application/vnd.ms-excel");
101
        response.setHeader("Content-disposition", "inline; filename=" + provider.getName() + "-courier-reconciliation-" + startDateStr + "-" + endDateStr + ".xls");
102
 
103
        ServletOutputStream sos;
104
        try {
105
            sos = response.getOutputStream();
106
            baos.writeTo(sos);
107
            sos.flush();
108
        } catch (IOException e) {
109
            errorMsg = "Failed to write to response.";
110
            logger.error("Unable to stream the payment details report", e);
111
        }
112
 
113
        return "index";
114
    }
115
 
116
 
117
    public String getErrorMsg() {
118
        return errorMsg;
119
    }
120
 
121
    @Override
122
    public void setServletRequest(HttpServletRequest req) {
123
        this.request = req;
124
        this.session = req.getSession();        
125
    }
126
 
127
    @Override
128
    public void setServletResponse(HttpServletResponse res) {
129
        this.response = res;
130
    }
131
 
132
    @Override
133
    public void setServletContext(ServletContext context) {
134
        this.context = context;
135
    }
136
 
137
    public String getServletContextPath() {
138
        return context.getContextPath();
139
    }
140
}