Subversion Repositories SmartDukaan

Rev

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