Subversion Repositories SmartDukaan

Rev

Rev 13146 | Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
7567 rajveer 1
package in.shop2020.support.controllers;
2
 
3
import java.io.File;
4
import java.io.FileInputStream;
5
import java.io.FileNotFoundException;
6
import java.io.IOException;
7
import java.util.ArrayList;
8
import java.util.Collection;
9
import java.util.List;
10
import in.shop2020.support.utils.ReportsUtils;
11
import in.shop2020.thrift.clients.LogisticsClient;
12
 
13
import javax.servlet.http.HttpServletRequest;
14
import javax.servlet.http.HttpSession;
15
 
16
import org.apache.commons.io.FileUtils;
17
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
18
import org.apache.poi.ss.usermodel.Row;
19
import org.apache.poi.ss.usermodel.Sheet;
20
import org.apache.poi.ss.usermodel.Workbook;
21
import org.apache.struts2.convention.annotation.InterceptorRef;
22
import org.apache.struts2.convention.annotation.InterceptorRefs;
23
import org.apache.struts2.convention.annotation.Result;
24
import org.apache.struts2.convention.annotation.Results;
25
import org.apache.struts2.interceptor.ServletRequestAware;
26
import org.apache.thrift.TException;
27
import org.slf4j.Logger;
28
import org.slf4j.LoggerFactory;
29
 
30
import com.opensymphony.xwork2.ActionSupport;
31
 
32
@SuppressWarnings("serial")
33
@InterceptorRefs({
34
    @InterceptorRef("defaultStack"),
35
    @InterceptorRef("login")
36
})
37
@Results({
38
    @Result(name="authfail", type="redirectAction", params = {"actionName" , "reports"})
39
})
40
public class AwbUploadController extends ActionSupport implements ServletRequestAware {
41
 
42
    private static Logger logger = LoggerFactory.getLogger(AwbUploadController.class);
43
 
44
    private HttpServletRequest request;
45
    private HttpSession session;
46
 
47
    private File awbFile;
48
    private String awbFileContentType;
49
    private String awbFileFileName;
50
 
51
    private String errorMsg = "";
52
 
53
    public String index() {
54
        if(!ReportsUtils.canAccessReport((Long)session.getAttribute(ReportsUtils.ROLE), request.getServletPath()))
55
            return "authfail";
56
        checkForErrors();
57
        return "authsuccess";
58
    }
59
 
60
    public String create() {
61
        File fileToCreate = null;
62
        try {
63
            fileToCreate = new File("/tmp/", this.awbFileFileName);
64
            FileUtils.copyFile(this.awbFile, fileToCreate);
65
        } catch (Exception e) {
66
           logger.error("Error while writing AWB file to the local file system", e);
67
           addActionError("Error while writing AWB file to the local file system");
68
        }
69
 
70
        long providerId = Long.parseLong(this.request.getParameter("providerId"));
71
        boolean isCod = Boolean.parseBoolean(this.request.getParameter("awbType"));
72
 
73
 
74
        if(checkForErrors())
75
            return "authsuccess";
76
 
77
        //Parse the file and submit the data for update to the logistics service
78
        Workbook wb = null;
79
        try {
80
            wb = new HSSFWorkbook(new FileInputStream(fileToCreate));
81
        } catch (FileNotFoundException e) {
82
            logger.error("Unable to open the Awb report", e);
83
            addActionError("Unable to open the Awb report. Please check the report format.");
84
        } catch (IOException e) {
85
            logger.error("Unable to open the Awb report", e);
86
            addActionError("Unable to open the Awb report. Please check the report format.");
87
        }
88
        if(checkForErrors())
89
            return "authsuccess";
90
 
91
        List<String> awbs = new ArrayList<String>();
92
 
93
        Sheet sheet = wb.getSheetAt(0);
94
        Row firstRow = sheet.getRow(0);
95
        logger.info("Last row number is:" + sheet.getLastRowNum());
96
        for (Row row : sheet) {
97
            if(row.equals(firstRow))
98
                continue;
99
            logger.info("Row no. " + row.getRowNum());
100
            String awb = row.getCell(0).getStringCellValue();
101
            awbs.add(awb);
102
        }
103
        if(checkForErrors())
104
            return "authsuccess";
105
 
106
 
107
        try {
108
            LogisticsClient logisticsClient = new LogisticsClient();
109
            in.shop2020.logistics.LogisticsService.Client lsc = logisticsClient.getClient();
110
            lsc.addNewAwbs(providerId, isCod, awbs);
111
        } catch (TException e) {
112
        	logger.error("Unable to add AWBs", e);
113
            addActionError(e.getMessage());
114
        }
115
 
116
        checkForErrors();
117
        return "authsuccess";
118
    }
119
 
120
    @Override
121
    public void setServletRequest(HttpServletRequest request) {
122
        this.request = request;
123
        this.session = request.getSession();
124
    }
125
 
126
    public File getAwbFile() {
127
        return awbFile;
128
    }
129
 
130
    public void setAwbFile(File awbFile) {
131
        this.awbFile = awbFile;
132
    }
133
 
134
    public String getAwbFileContentType() {
135
        return awbFileContentType;
136
    }
137
 
138
    public void setAwbFileContentType(String awbFileContentType) {
139
        this.awbFileContentType = awbFileContentType;
140
    }
141
 
142
    public String getAwbFileFileName() {
143
        return awbFileFileName;
144
    }
145
 
146
    public void setAwbFileFileName(String awbFileFileName) {
147
        this.awbFileFileName = awbFileFileName;
148
    }
149
 
150
    public String getErrorMsg(){
151
        return this.errorMsg;
152
    }
153
 
154
    private boolean checkForErrors(){
155
        Collection<String> actionErrors = getActionErrors();
156
        if(actionErrors != null && !actionErrors.isEmpty()){
157
            for (String str : actionErrors) {
158
                errorMsg += "<BR/>" + str;
159
            }
160
            return true;
161
        }
162
        return false;
163
    }
164
}