Subversion Repositories SmartDukaan

Rev

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

Rev 3936 Rev 3956
Line 1... Line 1...
1
package in.shop2020.support.controllers;
1
package in.shop2020.support.controllers;
2
 
2
 
-
 
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;
3
import in.shop2020.support.utils.ReportsUtils;
18
import in.shop2020.support.utils.ReportsUtils;
-
 
19
import in.shop2020.thrift.clients.TransactionClient;
4
 
20
 
5
import javax.servlet.http.HttpServletRequest;
21
import javax.servlet.http.HttpServletRequest;
6
import javax.servlet.http.HttpSession;
22
import javax.servlet.http.HttpSession;
7
 
23
 
-
 
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;
8
import org.apache.struts2.convention.annotation.InterceptorRef;
29
import org.apache.struts2.convention.annotation.InterceptorRef;
9
import org.apache.struts2.convention.annotation.InterceptorRefs;
30
import org.apache.struts2.convention.annotation.InterceptorRefs;
10
import org.apache.struts2.convention.annotation.Result;
31
import org.apache.struts2.convention.annotation.Result;
11
import org.apache.struts2.convention.annotation.Results;
32
import org.apache.struts2.convention.annotation.Results;
12
import org.apache.struts2.interceptor.ServletRequestAware;
33
import org.apache.struts2.interceptor.ServletRequestAware;
-
 
34
import org.apache.thrift.TException;
-
 
35
import org.apache.thrift.transport.TTransportException;
13
import org.slf4j.Logger;
36
import org.slf4j.Logger;
14
import org.slf4j.LoggerFactory;
37
import org.slf4j.LoggerFactory;
15
 
38
 
16
import com.opensymphony.xwork2.ActionSupport;
39
import com.opensymphony.xwork2.ActionSupport;
17
 
40
 
Line 27... Line 50...
27
 
50
 
28
    private static Logger logger = LoggerFactory.getLogger(CodReconciliationController.class);
51
    private static Logger logger = LoggerFactory.getLogger(CodReconciliationController.class);
29
    
52
    
30
    private HttpServletRequest request;
53
    private HttpServletRequest request;
31
    private HttpSession session;
54
    private HttpSession session;
-
 
55
    
-
 
56
    private File codReport;
-
 
57
    private String codReportContentType;
-
 
58
    private String codReportFileName;
-
 
59
    
-
 
60
    private String errorMsg = "";
32
 
61
 
33
    public String index() {
62
    public String index() {
34
        if(!ReportsUtils.canAccessReport((Long)session.getAttribute(ReportsUtils.ROLE), request.getServletPath()))
63
        if(!ReportsUtils.canAccessReport((Long)session.getAttribute(ReportsUtils.ROLE), request.getServletPath()))
35
            return "authfail";
64
            return "authfail";
-
 
65
        checkForErrors();
36
        return "authsuccess";
66
        return "authsuccess";
37
    }
67
    }
38
    
68
    
39
    public String create() {
69
    public String create() {
-
 
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();
40
        return "authsuccess";
148
        return "authsuccess";
41
    }
149
    }
42
    
150
    
43
    @Override
151
    @Override
44
    public void setServletRequest(HttpServletRequest request) {
152
    public void setServletRequest(HttpServletRequest request) {
45
        this.request = request;
153
        this.request = request;
46
        this.session = request.getSession();
154
        this.session = request.getSession();
47
    }
155
    }
48
 
156
 
-
 
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
    }
49
}
195
}