Subversion Repositories SmartDukaan

Rev

Rev 1881 | Rev 1941 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 1881 Rev 1891
Line 1... Line 1...
1
package in.shop2020.support.controllers;
1
package in.shop2020.support.controllers;
2
 
2
 
3
import in.shop2020.support.services.PaymentDetailsGenerator;
3
import in.shop2020.support.services.PaymentDetailsGenerator;
4
import in.shop2020.thrift.clients.HelperServiceClient;
-
 
5
import in.shop2020.utils.StatisticsUser;
-
 
6
 
4
 
7
import java.io.ByteArrayOutputStream;
5
import java.io.ByteArrayOutputStream;
8
import java.io.IOException;
6
import java.io.IOException;
9
import java.text.DateFormat;
7
import java.text.DateFormat;
10
import java.text.ParseException;
8
import java.text.ParseException;
11
import java.text.SimpleDateFormat;
9
import java.text.SimpleDateFormat;
12
import java.util.Calendar;
10
import java.util.Calendar;
13
import java.util.Date;
11
import java.util.Date;
14
 
12
 
15
import javax.servlet.ServletOutputStream;
13
import javax.servlet.ServletOutputStream;
16
import javax.servlet.http.HttpServletRequest;
-
 
17
import javax.servlet.http.HttpServletResponse;
-
 
18
import javax.servlet.http.HttpSession;
-
 
19
 
14
 
20
import org.apache.struts2.interceptor.ServletRequestAware;
15
import org.apache.struts2.convention.annotation.InterceptorRef;
-
 
16
import org.apache.struts2.convention.annotation.InterceptorRefs;
21
import org.apache.struts2.interceptor.ServletResponseAware;
17
import org.apache.struts2.convention.annotation.Result;
22
 
18
 
23
public class PaymentDetailsController implements ServletResponseAware, ServletRequestAware {
-
 
24
 
19
 
25
    private HttpServletRequest request;
20
@InterceptorRefs({
26
    private HttpServletResponse response;
21
    @InterceptorRef("myDefault"),
27
    private HttpSession session;
22
    @InterceptorRef("login")
-
 
23
})
-
 
24
 
-
 
25
public class PaymentDetailsController extends ReportsController {
28
 
26
 
29
    private String errorMsg = "";
27
    private String errorMsg = "";
30
    private final String authsuccess = "authsuccess";
28
    private final String authsuccess = "authsuccess";
31
    private final String authfail = "authfail";
29
    private final String authfail = "authfail";
32
    public PaymentDetailsController() {
30
    public PaymentDetailsController() {
33
 
31
 
34
    }
32
    }
35
 
33
 
36
    @Override
-
 
37
    public void setServletRequest(HttpServletRequest req) {
-
 
38
        this.request = req;
-
 
39
        this.session = req.getSession();
-
 
40
    }
-
 
41
 
-
 
42
    @Override
-
 
43
    public void setServletResponse(HttpServletResponse res) {
-
 
44
        this.response = res;
-
 
45
    }
-
 
46
 
-
 
47
    public String index() {
34
    public String index() {
48
        if (getSessionUserName() == null) {
35
        if(!canAccessReport()) {
49
            return authfail;
36
            return "exception";
50
        } else {
-
 
51
            return authsuccess;
-
 
52
        }
37
        }
-
 
38
        return authsuccess;
53
    }
39
    }
54
 
40
 
55
    // Handles the POST request (Form Submission)
41
    // Handles the POST request (Form Submission)
56
    public String create() {
42
    public String create() {
57
        String username = request.getParameter("username");
-
 
58
        String password = request.getParameter("password");
-
 
59
        if(username != null && password != null) {
-
 
60
            try{
-
 
61
                HelperServiceClient hsc = new HelperServiceClient();
-
 
62
                in.shop2020.utils.HelperService.Client client = hsc.getClient();
-
 
63
                StatisticsUser user = client.authenticateStatisticsUser(username, password);
-
 
64
                session.setAttribute("username", user.getUsername());
-
 
65
                return authsuccess;
-
 
66
            }catch(Exception e){
-
 
67
                e.printStackTrace();
-
 
68
                return authfail;
-
 
69
            }
-
 
70
        }
-
 
71
        if (getSessionUserName() == null) {
-
 
72
            return authfail;
-
 
73
        }
-
 
74
        
-
 
75
        // Formatting Form input parameters
43
        // Formatting Form input parameters
76
        String startDateStr = request.getParameter("startDate");
44
        String startDateStr = getServletRequest().getParameter("startDate");
77
        String endDateStr = request.getParameter("endDate");
45
        String endDateStr = getServletRequest().getParameter("endDate");
78
        String statusStr = request.getParameter("status");
46
        String statusStr = getServletRequest().getParameter("status");
79
        String filenameStr = "";
47
        String filenameStr = "";
80
        int status = 1;
48
        int status = 1;
81
        DateFormat df = new SimpleDateFormat("MM/dd/yyyy");
49
        DateFormat df = new SimpleDateFormat("MM/dd/yyyy");
82
        Date startDate = null, endDate = null;
50
        Date startDate = null, endDate = null;
83
        try {
51
        try {
Line 86... Line 54...
86
            Calendar cal = Calendar.getInstance();
54
            Calendar cal = Calendar.getInstance();
87
            cal.setTime(endDate);
55
            cal.setTime(endDate);
88
            cal.add(Calendar.DATE, 1);
56
            cal.add(Calendar.DATE, 1);
89
            endDate.setTime(cal.getTimeInMillis());
57
            endDate.setTime(cal.getTimeInMillis());
90
            status = Integer.parseInt(statusStr);
58
            status = Integer.parseInt(statusStr);
91
            filenameStr = (status == 0 ? "success" : "failed");
59
            filenameStr = (status == 0 ? "success" : "failed-pending");
92
        } catch (ParseException pe) {
60
        } catch (ParseException pe) {
93
            errorMsg = "Please enter start and end dates in format MM/dd/yyyy";
61
            errorMsg = "Please enter start and end dates in format MM/dd/yyyy";
94
            return authsuccess;
62
            return authsuccess;
95
        } catch (NumberFormatException nfe) {
63
        } catch (NumberFormatException nfe) {
96
            errorMsg = "Please select payment status";
64
            errorMsg = "Please select payment status";
Line 102... Line 70...
102
 
70
 
103
        if (baos == null) {
71
        if (baos == null) {
104
            errorMsg = "No output for given date range";
72
            errorMsg = "No output for given date range";
105
            return authsuccess;
73
            return authsuccess;
106
        } else {
74
        } else {
107
            errorMsg = "";
75
            errorMsg = "Generating report...";
108
        }
76
        }
109
 
77
 
110
        // Preparing XLS file for output
78
        // Preparing XLS file for output
111
        response.setContentType("application/vnd.ms-excel");
79
        getServletResponse().setContentType("application/vnd.ms-excel");
112
        response.setHeader("Content-disposition", "inline; filename=payments-report-" + filenameStr + ".xls");
80
        getServletResponse().setHeader("Content-disposition", "inline; filename=payments-report-" + filenameStr + ".xls");
113
        ServletOutputStream sos;
81
        ServletOutputStream sos;
114
        try {
82
        try {
115
            sos = response.getOutputStream();
83
            sos = getServletResponse().getOutputStream();
116
            baos.writeTo(sos);
84
            baos.writeTo(sos);
117
            sos.flush();
85
            sos.flush();
-
 
86
            errorMsg = "Report generated";
118
        } catch (IOException e) {
87
        } catch (IOException e) {
119
            errorMsg = "Failed to write to response.";
88
            errorMsg = "Failed to write to response.";
120
            e.printStackTrace();
89
            e.printStackTrace();
121
        }
90
        }
122
        return authsuccess;
91
        return authsuccess;
123
    }
92
    }
124
 
93
 
125
    public String getErrorMsg() {
94
    public String getErrorMsg() {
126
        return errorMsg;
95
        return errorMsg;
127
    }
96
    }
128
 
-
 
129
    public String getSessionUserName() {
-
 
130
        return (String) session.getAttribute("username");
-
 
131
    }
-
 
132
}
97
}