Subversion Repositories SmartDukaan

Rev

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