Subversion Repositories SmartDukaan

Rev

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

Rev Author Line No. Line
1586 ankur.sing 1
package in.shop2020.support.controllers;
2
 
1631 ankur.sing 3
import in.shop2020.support.services.PaymentDetailsGenerator;
1586 ankur.sing 4
 
5
import java.io.ByteArrayOutputStream;
6
import java.io.IOException;
7
import java.text.DateFormat;
8
import java.text.ParseException;
9
import java.text.SimpleDateFormat;
10
import java.util.Calendar;
11
import java.util.Date;
12
 
13
import javax.servlet.ServletOutputStream;
14
 
1891 ankur.sing 15
import org.apache.struts2.convention.annotation.InterceptorRef;
16
import org.apache.struts2.convention.annotation.InterceptorRefs;
17
import org.apache.struts2.convention.annotation.Result;
1586 ankur.sing 18
 
19
 
1891 ankur.sing 20
@InterceptorRefs({
21
    @InterceptorRef("myDefault"),
22
    @InterceptorRef("login")
23
})
1631 ankur.sing 24
 
1891 ankur.sing 25
public class PaymentDetailsController extends ReportsController {
26
 
1677 ankur.sing 27
    private String errorMsg = "";
28
    private final String authsuccess = "authsuccess";
29
    private final String authfail = "authfail";
30
    public PaymentDetailsController() {
1631 ankur.sing 31
 
1677 ankur.sing 32
    }
1631 ankur.sing 33
 
1677 ankur.sing 34
    public String index() {
1891 ankur.sing 35
        if(!canAccessReport()) {
36
            return "exception";
1677 ankur.sing 37
        }
1891 ankur.sing 38
        return authsuccess;
1677 ankur.sing 39
    }
1631 ankur.sing 40
 
1677 ankur.sing 41
    // Handles the POST request (Form Submission)
42
    public String create() {
43
        // Formatting Form input parameters
1891 ankur.sing 44
        String startDateStr = getServletRequest().getParameter("startDate");
45
        String endDateStr = getServletRequest().getParameter("endDate");
46
        String statusStr = getServletRequest().getParameter("status");
1881 ankur.sing 47
        String filenameStr = "";
48
        int status = 1;
1677 ankur.sing 49
        DateFormat df = new SimpleDateFormat("MM/dd/yyyy");
50
        Date startDate = null, endDate = null;
51
        try {
52
            startDate = df.parse(startDateStr);
53
            endDate = df.parse(endDateStr);
54
            Calendar cal = Calendar.getInstance();
55
            cal.setTime(endDate);
56
            cal.add(Calendar.DATE, 1);
57
            endDate.setTime(cal.getTimeInMillis());
1881 ankur.sing 58
            status = Integer.parseInt(statusStr);
1891 ankur.sing 59
            filenameStr = (status == 0 ? "success" : "failed-pending");
1677 ankur.sing 60
        } catch (ParseException pe) {
61
            errorMsg = "Please enter start and end dates in format MM/dd/yyyy";
62
            return authsuccess;
1881 ankur.sing 63
        } catch (NumberFormatException nfe) {
64
            errorMsg = "Please select payment status";
65
            return authsuccess;
1677 ankur.sing 66
        }
1631 ankur.sing 67
 
1677 ankur.sing 68
        PaymentDetailsGenerator paymentDetailGenerator = new PaymentDetailsGenerator();
1881 ankur.sing 69
        ByteArrayOutputStream baos = paymentDetailGenerator.generatePaymentDetailsReport(startDate, endDate, status);
1631 ankur.sing 70
 
1677 ankur.sing 71
        if (baos == null) {
72
            errorMsg = "No output for given date range";
73
            return authsuccess;
74
        } else {
1891 ankur.sing 75
            errorMsg = "Generating report...";
1677 ankur.sing 76
        }
1586 ankur.sing 77
 
1677 ankur.sing 78
        // Preparing XLS file for output
1891 ankur.sing 79
        getServletResponse().setContentType("application/vnd.ms-excel");
80
        getServletResponse().setHeader("Content-disposition", "inline; filename=payments-report-" + filenameStr + ".xls");
1677 ankur.sing 81
        ServletOutputStream sos;
82
        try {
1891 ankur.sing 83
            sos = getServletResponse().getOutputStream();
1677 ankur.sing 84
            baos.writeTo(sos);
85
            sos.flush();
1891 ankur.sing 86
            errorMsg = "Report generated";
1677 ankur.sing 87
        } catch (IOException e) {
88
            errorMsg = "Failed to write to response.";
89
            e.printStackTrace();
90
        }
91
        return authsuccess;
92
    }
1586 ankur.sing 93
 
1677 ankur.sing 94
    public String getErrorMsg() {
95
        return errorMsg;
96
    }
1586 ankur.sing 97
}