Subversion Repositories SmartDukaan

Rev

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 EbaySubsidyUploaderController extends ActionSupport implements ServletRequestAware {
42
 
43
	private static Logger logger = LoggerFactory.getLogger(EbaySubsidyUploaderController.class);
44
 
45
	private static final int LISTING_INDEX=0;
46
	private static final int SUBSIDY_INDEX=1;
47
	private HttpServletRequest request;
48
    private HttpSession session;
49
 
50
    private String errorMsg = "";
51
 
52
    private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd-HH:mm:ss");
53
 
54
    private File subsidyFile;
55
 
56
	public String index() {
57
        if(!ReportsUtils.canAccessReport((Long)session.getAttribute(ReportsUtils.ROLE), "/ebay-dashboard"))
58
            return "authfail";
59
        return "authsuccess";
60
    }
61
 
62
	public String create() throws IOException {
63
		FileUtils.copyFile(subsidyFile, new File("/tmp/subsidy_file " + sdf.format(new Date()) + ".xls"));
64
		Workbook wb = new HSSFWorkbook(new FileInputStream(subsidyFile));
65
 
66
		int totalNumRows = 0;
67
	    int successfullyProcessedNumRows = 0;
68
	    for (Row row : wb.getSheetAt(0)) {
69
	    	String key = "";
70
	        try {
71
	            totalNumRows++;
72
	            if(totalNumRows==1) {
73
	            	continue;
74
	            }
75
	            row.getCell(LISTING_INDEX).setCellType(Cell.CELL_TYPE_STRING);
76
            	String listingId = row.getCell(LISTING_INDEX).getStringCellValue();
77
            	Double subsidy = row.getCell(SUBSIDY_INDEX).getNumericCellValue();
78
            	CatalogService.Client catalogClient = new CatalogClient().getClient();
79
            	EbayItem ebayItem = catalogClient.getEbayItem(listingId);
80
            	ebayItem.setSubsidy(subsidy);
81
            	catalogClient.updateEbayItem(ebayItem);
82
 
83
            	successfullyProcessedNumRows++;
84
	         } catch (Exception e) {
85
	             logger.error(e + "\n" + e.getMessage() + e.getCause());
86
	             setErrorMsg(getErrorMsg() + "</br>Error in row number " + totalNumRows);
87
	             addActionError("Error in row number " + totalNumRows);
88
	         }
89
	    }
90
	    if(errorMsg.isEmpty()) {
91
	    	setErrorMsg("Sucessfully uploaded subsidy");
92
	    }
93
	    return "authsuccess";
94
	}
95
 
96
	public String getErrorMsg() {
97
		return errorMsg;
98
	}
99
 
100
	public void setErrorMsg(String errorMsg) {
101
		this.errorMsg = errorMsg;
102
	}
103
 
104
	public File getSubsidyFile() {
105
		return subsidyFile;
106
	}
107
 
108
	public void setSubsidyFile(File subsidyFile) {
109
		this.subsidyFile = subsidyFile;
110
	}
111
 
112
	@Override
113
	public void setServletRequest(HttpServletRequest request) {
114
		this.request = request;
115
        this.session = request.getSession();
116
	}
117
 
118
}