Rev 20050 | Blame | Compare with Previous | Last modification | View Log | RSS feed
package in.shop2020.support.controllers;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.IOException;import java.util.ArrayList;import java.util.Collection;import java.util.List;import in.shop2020.support.utils.ReportsUtils;import in.shop2020.thrift.clients.LogisticsClient;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpSession;import org.apache.commons.io.FileUtils;import org.apache.poi.hssf.usermodel.HSSFWorkbook;import org.apache.poi.ss.usermodel.Cell;import org.apache.poi.ss.usermodel.Row;import org.apache.poi.ss.usermodel.Sheet;import org.apache.poi.ss.usermodel.Workbook;import org.apache.struts2.convention.annotation.InterceptorRef;import org.apache.struts2.convention.annotation.InterceptorRefs;import org.apache.struts2.convention.annotation.Result;import org.apache.struts2.convention.annotation.Results;import org.apache.struts2.interceptor.ServletRequestAware;import org.apache.thrift.TException;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import com.opensymphony.xwork2.ActionSupport;@SuppressWarnings("serial")@InterceptorRefs({@InterceptorRef("defaultStack"),@InterceptorRef("login")})@Results({@Result(name="authfail", type="redirectAction", params = {"actionName" , "reports"})})public class AwbUploadController extends ActionSupport implements ServletRequestAware {private static Logger logger = LoggerFactory.getLogger(AwbUploadController.class);private HttpServletRequest request;private HttpSession session;private File awbFile;private String awbFileContentType;private String awbFileFileName;//private Stringprivate String errorMsg = "";public String index() {if(!ReportsUtils.canAccessReport((Long)session.getAttribute(ReportsUtils.ROLE), request.getServletPath()))return "authfail";checkForErrors();return "authsuccess";}public String create() {File fileToCreate = null;try {fileToCreate = new File("/tmp/", this.awbFileFileName);FileUtils.copyFile(this.awbFile, fileToCreate);} catch (Exception e) {logger.error("Error while writing AWB file to the local file system", e);addActionError("Error while writing AWB file to the local file system");}long providerId = Long.parseLong(this.request.getParameter("providerId"));boolean isCod = Boolean.parseBoolean(this.request.getParameter("awbType"));long awbUsedFor = Long.parseLong(this.request.getParameter("awbUsedFor"));if(checkForErrors())return "authsuccess";//Parse the file and submit the data for update to the logistics serviceWorkbook wb = null;try {wb = new HSSFWorkbook(new FileInputStream(fileToCreate));} catch (FileNotFoundException e) {logger.error("Unable to open the Awb report", e);addActionError("Unable to open the Awb report. Please check the report format.");} catch (IOException e) {logger.error("Unable to open the Awb report", e);addActionError("Unable to open the Awb report. Please check the report format.");}if(checkForErrors())return "authsuccess";List<String> awbs = new ArrayList<String>();Sheet sheet = wb.getSheetAt(0);Row firstRow = sheet.getRow(0);logger.info("Last row number is:" + sheet.getLastRowNum());for (Row row : sheet) {if(row.equals(firstRow))continue;logger.info("Row no. " + row.getRowNum());Cell awbCell = row.getCell(0);String awb ="";if(Cell.CELL_TYPE_STRING==row.getCell(0).getCellType()){awb = row.getCell(0).getStringCellValue();}if(Cell.CELL_TYPE_NUMERIC==row.getCell(0).getCellType()){awb = row.getCell(0).getNumericCellValue()+"";}awbs.add(awb);}if(checkForErrors())return "authsuccess";try {LogisticsClient logisticsClient = new LogisticsClient();in.shop2020.logistics.LogisticsService.Client lsc = logisticsClient.getClient();lsc.addNewAwbs(providerId, isCod, awbs, awbUsedFor);addActionMessage("Successfully upload awb");} catch (TException e) {logger.error("Unable to add AWBs", e);addActionError("Service error while processing request");}checkForErrors();return "authsuccess";}@Overridepublic void setServletRequest(HttpServletRequest request) {this.request = request;this.session = request.getSession();}public File getAwbFile() {return awbFile;}public void setAwbFile(File awbFile) {this.awbFile = awbFile;}public String getAwbFileContentType() {return awbFileContentType;}public void setAwbFileContentType(String awbFileContentType) {this.awbFileContentType = awbFileContentType;}public String getAwbFileFileName() {return awbFileFileName;}public void setAwbFileFileName(String awbFileFileName) {this.awbFileFileName = awbFileFileName;}public String getErrorMsg(){return this.errorMsg;}private boolean checkForErrors(){Collection<String> actionErrors = getActionErrors();if(actionErrors != null && !actionErrors.isEmpty()){for (String str : actionErrors) {errorMsg += "<BR/>" + str;}return true;}return false;}}