Subversion Repositories SmartDukaan

Rev

Rev 20050 | 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()){
20063 kshitij.so 109
            	awb = 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);
20063 kshitij.so 121
            addActionMessage("Successfully upload awb");
7567 rajveer 122
        } catch (TException e) {
123
        	logger.error("Unable to add AWBs", e);
20063 kshitij.so 124
            addActionError("Service error while processing request");
7567 rajveer 125
        }
126
 
127
        checkForErrors();
128
        return "authsuccess";
129
    }
130
 
131
    @Override
132
    public void setServletRequest(HttpServletRequest request) {
133
        this.request = request;
134
        this.session = request.getSession();
135
    }
136
 
137
    public File getAwbFile() {
138
        return awbFile;
139
    }
140
 
141
    public void setAwbFile(File awbFile) {
142
        this.awbFile = awbFile;
143
    }
144
 
145
    public String getAwbFileContentType() {
146
        return awbFileContentType;
147
    }
148
 
149
    public void setAwbFileContentType(String awbFileContentType) {
150
        this.awbFileContentType = awbFileContentType;
151
    }
152
 
153
    public String getAwbFileFileName() {
154
        return awbFileFileName;
155
    }
156
 
157
    public void setAwbFileFileName(String awbFileFileName) {
158
        this.awbFileFileName = awbFileFileName;
159
    }
160
 
161
    public String getErrorMsg(){
162
        return this.errorMsg;
163
    }
164
 
165
    private boolean checkForErrors(){
166
        Collection<String> actionErrors = getActionErrors();
167
        if(actionErrors != null && !actionErrors.isEmpty()){
168
            for (String str : actionErrors) {
169
                errorMsg += "<BR/>" + str;
170
            }
171
            return true;
172
        }
173
        return false;
174
    }
175
}