Subversion Repositories SmartDukaan

Rev

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

Rev Author Line No. Line
3936 chandransh 1
package in.shop2020.support.controllers;
2
 
3956 chandransh 3
import java.io.File;
4
import java.io.FileInputStream;
5
import java.io.FileNotFoundException;
6
import java.io.IOException;
7
import java.text.DateFormat;
8
import java.text.ParseException;
9
import java.text.SimpleDateFormat;
10
import java.util.Collection;
11
import java.util.Date;
12
import java.util.HashMap;
13
import java.util.Map;
14
import java.util.Map.Entry;
15
 
16
import in.shop2020.model.v1.order.TransactionService.Client;
17
import in.shop2020.model.v1.order.TransactionServiceException;
3936 chandransh 18
import in.shop2020.support.utils.ReportsUtils;
3956 chandransh 19
import in.shop2020.thrift.clients.TransactionClient;
3936 chandransh 20
 
21
import javax.servlet.http.HttpServletRequest;
22
import javax.servlet.http.HttpSession;
23
 
3956 chandransh 24
import org.apache.commons.io.FileUtils;
25
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
26
import org.apache.poi.ss.usermodel.Row;
27
import org.apache.poi.ss.usermodel.Sheet;
28
import org.apache.poi.ss.usermodel.Workbook;
3936 chandransh 29
import org.apache.struts2.convention.annotation.InterceptorRef;
30
import org.apache.struts2.convention.annotation.InterceptorRefs;
31
import org.apache.struts2.convention.annotation.Result;
32
import org.apache.struts2.convention.annotation.Results;
33
import org.apache.struts2.interceptor.ServletRequestAware;
3956 chandransh 34
import org.apache.thrift.TException;
35
import org.apache.thrift.transport.TTransportException;
3936 chandransh 36
import org.slf4j.Logger;
37
import org.slf4j.LoggerFactory;
38
 
39
import com.opensymphony.xwork2.ActionSupport;
40
 
41
@SuppressWarnings("serial")
42
@InterceptorRefs({
43
    @InterceptorRef("defaultStack"),
44
    @InterceptorRef("login")
45
})
46
@Results({
47
    @Result(name="authfail", type="redirectAction", params = {"actionName" , "reports"})
48
})
49
public class CodReconciliationController extends ActionSupport implements ServletRequestAware {
50
 
51
    private static Logger logger = LoggerFactory.getLogger(CodReconciliationController.class);
52
 
53
    private HttpServletRequest request;
54
    private HttpSession session;
3956 chandransh 55
 
56
    private File codReport;
57
    private String codReportContentType;
58
    private String codReportFileName;
59
 
60
    private String errorMsg = "";
3936 chandransh 61
 
62
    public String index() {
63
        if(!ReportsUtils.canAccessReport((Long)session.getAttribute(ReportsUtils.ROLE), request.getServletPath()))
64
            return "authfail";
3956 chandransh 65
        checkForErrors();
3936 chandransh 66
        return "authsuccess";
67
    }
68
 
69
    public String create() {
3956 chandransh 70
        File fileToCreate = null;
71
        try {
72
            fileToCreate = new File("/tmp/", this.codReportFileName);
73
            FileUtils.copyFile(this.codReport, fileToCreate);
74
        } catch (Exception e) {
75
           logger.error("Error while writing COD report to the local file system", e);
76
           addActionError("Error while writing COD report to the local file system");
77
        }
78
 
79
        String xferBy = this.request.getParameter("xferBy");
80
        String xferTxnId = this.request.getParameter("xferTxnId");
81
        String xferDateStr = this.request.getParameter("xferTxnDate"); 
82
        DateFormat df = new SimpleDateFormat("dd/MM/yyyy");
83
        Date xferDate = null;
84
        try {
85
            xferDate = df.parse(xferDateStr);
86
        } catch (ParseException e1) {
87
            logger.error("Invalid date: " + xferDateStr);
88
            addActionError("Invalid date supplied");
89
        }
90
 
91
        if(checkForErrors())
92
            return "authsuccess";
93
 
94
        //Parse the file and submit the data for update to the transaction service
95
        Workbook wb = null;
96
        try {
97
            wb = new HSSFWorkbook(new FileInputStream(fileToCreate));
98
        } catch (FileNotFoundException e) {
99
            logger.error("Unable to open the COD report", e);
100
            addActionError("Unable to open the COD report. Please check the report format.");
101
        } catch (IOException e) {
102
            logger.error("Unable to open the COD report", e);
103
            addActionError("Unable to open the COD report. Please check the report format.");
104
        }
105
        if(checkForErrors())
106
            return "authsuccess";
107
 
108
        HashMap<String, Double> collectedAmountMap = new HashMap<String, Double>(); 
109
 
110
        Sheet sheet = wb.getSheetAt(0);
111
        Row firstRow = sheet.getRow(0);
112
        logger.info("Last row number is:" + sheet.getLastRowNum());
113
        for (Row row : sheet) {
114
            if(row.equals(firstRow))
115
                continue;
116
            logger.info("Row no. " + row.getRowNum());
117
            String awb = row.getCell(0).getStringCellValue();
118
            double collectedAmount = row.getCell(1).getNumericCellValue();
119
            collectedAmountMap.put(awb, collectedAmount);
120
        }
121
 
122
        TransactionClient tsc = null;
123
        try {
124
            tsc = new TransactionClient();
125
        } catch (TTransportException e) {
126
            logger.error("Unable to establish connection to the transaction service", e);
127
            addActionError("Unable to establish connection to the transaction service");
128
        }
129
        if(checkForErrors())
130
            return "authsuccess";
131
 
132
        Client txnClient = tsc.getClient();
133
        try {
134
            Map<String, String> unprocessedAwbs = txnClient.reconcileCodCollection(collectedAmountMap, xferBy, xferTxnId, xferDate.getTime());
135
            if(!unprocessedAwbs.isEmpty()){
136
                for(Entry<String, String> entry: unprocessedAwbs.entrySet())
137
                    addActionError("Unable to process AWB no. " + entry.getKey() + " bcoz of " + entry.getValue());
138
            }
139
        } catch (TransactionServiceException e) {
140
            logger.error("Unable to reconcile COD collection", e);
141
            addActionError(e.getMessage());
142
        } catch (TException e) {
143
            logger.error("Unable to reconcile COD collection", e);
144
            addActionError(e.getMessage());
145
        }
146
 
147
        checkForErrors();
3936 chandransh 148
        return "authsuccess";
149
    }
150
 
151
    @Override
152
    public void setServletRequest(HttpServletRequest request) {
153
        this.request = request;
154
        this.session = request.getSession();
155
    }
156
 
3956 chandransh 157
    public File getCodReport() {
158
        return codReport;
159
    }
160
 
161
    public void setCodReport(File codReport) {
162
        this.codReport = codReport;
163
    }
164
 
165
    public String getCodReportContentType() {
166
        return codReportContentType;
167
    }
168
 
169
    public void setCodReportContentType(String codReportContentType) {
170
        this.codReportContentType = codReportContentType;
171
    }
172
 
173
    public String getCodReportFileName() {
174
        return codReportFileName;
175
    }
176
 
177
    public void setCodReportFileName(String codReportFileName) {
178
        this.codReportFileName = codReportFileName;
179
    }
180
 
181
    public String getErrorMsg(){
182
        return this.errorMsg;
183
    }
184
 
185
    private boolean checkForErrors(){
186
        Collection<String> actionErrors = getActionErrors();
187
        if(actionErrors != null && !actionErrors.isEmpty()){
188
            for (String str : actionErrors) {
189
                errorMsg += "<BR/>" + str;
190
            }
191
            return true;
192
        }
193
        return false;
194
    }
3936 chandransh 195
}