Rev 8303 | Blame | Compare with Previous | Last modification | View Log | RSS feed
package in.shop2020.support.controllers;import in.shop2020.model.v1.catalog.CatalogService;import in.shop2020.model.v1.catalog.EbayItem;import in.shop2020.support.utils.ReportsUtils;import in.shop2020.thrift.clients.CatalogClient;import java.io.File;import java.io.FileInputStream;import java.io.IOException;import java.text.SimpleDateFormat;import java.util.Date;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.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.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 EbayListingUploaderController extends ActionSupport implements ServletRequestAware {private static Logger logger = LoggerFactory.getLogger(EbayListingUploaderController.class);private static final int LISTING_ID_INDEX = 0;private static final int ITEM_ID_INDEX = 1;private static final int LISTING_NAME_INDEX = 2;private static final int PRICE_INDEX = 3;private static final int EXPIRY_DATE_INDEX = 4;private static final int SUBSIDY_INDEX = 5;private static final int DEFAULT_WAREHOUSE_INDEX = 6;private HttpServletRequest request;private HttpSession session;private String errorMsg = "";private SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");private File listingFile;public String create() throws IOException {FileUtils.copyFile(listingFile, new File("/tmp/listing_file " + sdf.format(new Date()) + ".xls"));Workbook wb = new HSSFWorkbook(new FileInputStream(listingFile));int totalNumRows = 0;int successfullyProcessedNumRows = 0;for (Row row : wb.getSheetAt(0)) {String key = "";String listingId = null;String listingName = null;Long price =0L;Long itemId = 0L;Double subsidy = 0.0;Date expiryDate = null;try {totalNumRows++;if(totalNumRows==1) {continue;}long warehouseId = 0L;try {row.getCell(LISTING_ID_INDEX).setCellType(Cell.CELL_TYPE_STRING);listingId = row.getCell(LISTING_ID_INDEX).getStringCellValue();itemId = new Double(row.getCell(ITEM_ID_INDEX).getNumericCellValue()).longValue();listingName = row.getCell(LISTING_NAME_INDEX).getStringCellValue();price = new Double(row.getCell(PRICE_INDEX).getNumericCellValue()).longValue();Cell subsidyCell = row.getCell(SUBSIDY_INDEX);if(subsidyCell != null && subsidyCell.getCellType() != Cell.CELL_TYPE_BLANK) {subsidy = row.getCell(SUBSIDY_INDEX).getNumericCellValue();} else {subsidy = 0.0;}Cell warehouseCell = row.getCell(DEFAULT_WAREHOUSE_INDEX);if(warehouseCell != null && warehouseCell.getCellType() != Cell.CELL_TYPE_BLANK) {warehouseId = new Double(row.getCell(DEFAULT_WAREHOUSE_INDEX).getNumericCellValue()).longValue();} else {warehouseId = 0L;}} catch (Exception e) {logger.error("Error in reading input for row number " + totalNumRows, e);setErrorMsg(getErrorMsg() + "<br/>Error in reading input for row number " + totalNumRows);continue;}CatalogService.Client catalogClient = new CatalogClient().getClient();EbayItem ebayItem = new EbayItem();ebayItem.setEbayListingId(listingId);ebayItem.setItemId(itemId);ebayItem.setListingName(listingName);ebayItem.setListingPrice(price);try {//TODO Check for Date and Time sanityebayItem.setListingExpiryDate(interchangeDateAndMonth(row.getCell(EXPIRY_DATE_INDEX).getDateCellValue()).getTime());} catch (Exception e) {logger.warn("Error while setting expiry time for Ebay Listing", e);}ebayItem.setSubsidy(subsidy);ebayItem.setDefaultWarehouseId(warehouseId);catalogClient.addEbayItem(ebayItem);successfullyProcessedNumRows++;} catch (Exception e) {logger.error(e + "\n" + e.getMessage() + e.getCause());setErrorMsg(getErrorMsg() + "<br/>Error in row number " + totalNumRows);addActionError("Error in row number " + totalNumRows);}}if(errorMsg.isEmpty()) {setErrorMsg("Sucessfully uploaded listings");}return "authsuccess";}public String index() {if(!ReportsUtils.canAccessReport((Long)session.getAttribute(ReportsUtils.ROLE), "/ebay-dashboard"))return "authfail";return "authsuccess";}private Date interchangeDateAndMonth(Date date) {Date updatedDate = new Date(date.getTime());updatedDate.setDate(date.getMonth() + 1);updatedDate.setMonth(date.getDate() - 1);return updatedDate;}@Overridepublic void setServletRequest(HttpServletRequest request) {this.request = request;this.session = request.getSession();}public String getErrorMsg() {return errorMsg;}public void setErrorMsg(String errorMsg) {this.errorMsg = errorMsg;}public File getListingFile() {return listingFile;}public void setListingFile(File listingFile) {this.listingFile = listingFile;}}