Subversion Repositories SmartDukaan

Rev

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

Rev Author Line No. Line
8182 amar.kumar 1
package in.shop2020.support.controllers;
2
 
3
import in.shop2020.model.v1.catalog.CatalogService;
4
import in.shop2020.model.v1.catalog.EbayItem;
5
import in.shop2020.support.utils.ReportsUtils;
6
import in.shop2020.thrift.clients.CatalogClient;
7
 
8
import java.io.File;
9
import java.io.FileInputStream;
10
import java.io.IOException;
11
import java.text.SimpleDateFormat;
12
import java.util.Collection;
13
import java.util.Date;
14
 
15
import javax.servlet.http.HttpServletRequest;
16
import javax.servlet.http.HttpSession;
17
 
18
import org.apache.commons.io.FileUtils;
19
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
20
import org.apache.poi.ss.usermodel.Cell;
21
import org.apache.poi.ss.usermodel.Row;
22
import org.apache.poi.ss.usermodel.Workbook;
23
import org.apache.struts2.convention.annotation.InterceptorRef;
24
import org.apache.struts2.convention.annotation.InterceptorRefs;
25
import org.apache.struts2.convention.annotation.Result;
26
import org.apache.struts2.convention.annotation.Results;
27
import org.apache.struts2.interceptor.ServletRequestAware;
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 EbayListingUploaderController extends ActionSupport implements ServletRequestAware {
42
 
43
	private static Logger logger = LoggerFactory.getLogger(EbayListingUploaderController.class);
44
 
45
	private static final int LISTING_ID_INDEX = 0;
46
	private static final int ITEM_ID_INDEX = 1;
47
	private static final int LISTING_NAME_INDEX = 2;
48
	private static final int PRICE_INDEX = 3;
49
	private static final int EXPIRY_DATE_INDEX = 4;
50
	private static final int SUBSIDY_INDEX = 5;
51
	private static final int DEFAULT_WAREHOUSE_INDEX = 6;
52
 
53
	private HttpServletRequest request;
54
    private HttpSession session;
55
 
56
    private String errorMsg = "";
57
 
58
    private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
59
 
60
    private File listingFile;
61
 
62
 
63
    public String create() throws IOException {
64
		FileUtils.copyFile(listingFile, new File("/tmp/listing_file " + sdf.format(new Date()) + ".xls"));
65
		Workbook wb = new HSSFWorkbook(new FileInputStream(listingFile));
66
 
67
		int totalNumRows = 0;
68
	    int successfullyProcessedNumRows = 0;
69
	    for (Row row : wb.getSheetAt(0)) {
70
	    	String key = "";
71
	        try {
72
	            totalNumRows++;
73
	            if(totalNumRows==1) {
74
	            	continue;
75
	            }
76
	            long warehouseId = 0L;
77
	            row.getCell(LISTING_ID_INDEX).setCellType(Cell.CELL_TYPE_STRING);
78
            	String listingId = row.getCell(LISTING_ID_INDEX).getStringCellValue();
79
            	Long itemId = new Double(row.getCell(ITEM_ID_INDEX).getNumericCellValue()).longValue();
80
            	String listingName = row.getCell(LISTING_NAME_INDEX).getStringCellValue();
81
            	Long price = new Double(row.getCell(PRICE_INDEX).getNumericCellValue()).longValue();
82
            	//TODO Test getDateCellValue
83
            	//row.getCell(EXPIRY_DATE_INDEX).setCellType(Cell.CELL_TYPE_STRING);
84
            	//String expiryDateString = row.getCell(EXPIRY_DATE_INDEX).getStringCellValue();
85
            	//Date expiryDate = sdf.parse(expiryDateString);
86
            	Double subsidy = row.getCell(SUBSIDY_INDEX).getNumericCellValue();
87
            	Cell warehouseCell = row.getCell(DEFAULT_WAREHOUSE_INDEX);
88
            	if(warehouseCell != null && warehouseCell.getCellType() != Cell.CELL_TYPE_BLANK) {
89
            		warehouseId = new Double(row.getCell(DEFAULT_WAREHOUSE_INDEX).getNumericCellValue()).longValue();
90
            	} else {
91
            		warehouseId = 0L;
92
            	}
93
 
94
            	CatalogService.Client catalogClient = new CatalogClient().getClient();
95
            	EbayItem ebayItem = new EbayItem();
96
            	ebayItem.setEbayListingId(listingId);
97
            	ebayItem.setItemId(itemId);
98
            	ebayItem.setListingName(listingName);
99
            	ebayItem.setListingPrice(price);
100
            	//ebayItem.setListingExpiryDate(expiryDate.getTime());
101
            	ebayItem.setSubsidy(subsidy);
102
            	ebayItem.setDefaultWarehouseId(warehouseId);
103
            	catalogClient.addEbayItem(ebayItem);
104
 
105
            	successfullyProcessedNumRows++;
106
	         } catch (Exception e) {
107
	             logger.error(e + "\n" + e.getMessage() + e.getCause());
108
	             setErrorMsg(getErrorMsg() + "<br/>Error in row number " + totalNumRows);
109
	             addActionError("Error in row number " + totalNumRows);
110
	         }
111
	    }
112
	    if(errorMsg.isEmpty()) {
113
	    	setErrorMsg("Sucessfully uploaded listings");
114
	    }
115
	    return "authsuccess";
116
	}
117
 
118
	public String index() {
119
        if(!ReportsUtils.canAccessReport((Long)session.getAttribute(ReportsUtils.ROLE), "/ebay-dashboard"))
120
            return "authfail";
121
        return "authsuccess";
122
    }
123
 
124
	@Override
125
	public void setServletRequest(HttpServletRequest request) {
126
		this.request = request;
127
        this.session = request.getSession();
128
	}
129
 
130
	public String getErrorMsg() {
131
		return errorMsg;
132
	}
133
 
134
	public void setErrorMsg(String errorMsg) {
135
		this.errorMsg = errorMsg;
136
	}
137
 
138
	public File getListingFile() {
139
		return listingFile;
140
	}
141
 
142
	public void setListingFile(File listingFile) {
143
		this.listingFile = listingFile;
144
	}
145
 
146
}