Subversion Repositories SmartDukaan

Rev

Rev 8250 | Rev 8281 | Go to most recent revision | Details | Compare with Previous | 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
 
8251 amar.kumar 58
    private SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
8182 amar.kumar 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();
8250 amar.kumar 82
            	Date expiryDate = null;
83
            	try {
84
            		//TODO Test getDateCellValue
85
            		//row.getCell(EXPIRY_DATE_INDEX).setCellType(Cell.CELL_TYPE_STRING);
86
            		String expiryDateString = row.getCell(EXPIRY_DATE_INDEX).getStringCellValue();
87
            		expiryDate = sdf.parse(expiryDateString);
88
            	} catch (Exception e) {
89
            		logger.warn("Error in updating expiry timestamp for Ebay Listing", e);
90
            	}
8182 amar.kumar 91
            	Double subsidy = row.getCell(SUBSIDY_INDEX).getNumericCellValue();
92
            	Cell warehouseCell = row.getCell(DEFAULT_WAREHOUSE_INDEX);
93
            	if(warehouseCell != null && warehouseCell.getCellType() != Cell.CELL_TYPE_BLANK) {
94
            		warehouseId = new Double(row.getCell(DEFAULT_WAREHOUSE_INDEX).getNumericCellValue()).longValue();
95
            	} else {
96
            		warehouseId = 0L;
97
            	}
98
 
99
            	CatalogService.Client catalogClient = new CatalogClient().getClient();
100
            	EbayItem ebayItem = new EbayItem();
101
            	ebayItem.setEbayListingId(listingId);
102
            	ebayItem.setItemId(itemId);
103
            	ebayItem.setListingName(listingName);
104
            	ebayItem.setListingPrice(price);
8250 amar.kumar 105
            	try {
106
            		ebayItem.setListingExpiryDate(expiryDate.getTime());
107
            	} catch (Exception e) {
108
            		logger.warn("Error while setting expiry time for Ebay Listing", e);
109
            	}
8182 amar.kumar 110
            	ebayItem.setSubsidy(subsidy);
111
            	ebayItem.setDefaultWarehouseId(warehouseId);
112
            	catalogClient.addEbayItem(ebayItem);
113
 
114
            	successfullyProcessedNumRows++;
115
	         } catch (Exception e) {
116
	             logger.error(e + "\n" + e.getMessage() + e.getCause());
117
	             setErrorMsg(getErrorMsg() + "<br/>Error in row number " + totalNumRows);
118
	             addActionError("Error in row number " + totalNumRows);
119
	         }
120
	    }
121
	    if(errorMsg.isEmpty()) {
122
	    	setErrorMsg("Sucessfully uploaded listings");
123
	    }
124
	    return "authsuccess";
125
	}
126
 
127
	public String index() {
128
        if(!ReportsUtils.canAccessReport((Long)session.getAttribute(ReportsUtils.ROLE), "/ebay-dashboard"))
129
            return "authfail";
130
        return "authsuccess";
131
    }
132
 
133
	@Override
134
	public void setServletRequest(HttpServletRequest request) {
135
		this.request = request;
136
        this.session = request.getSession();
137
	}
138
 
139
	public String getErrorMsg() {
140
		return errorMsg;
141
	}
142
 
143
	public void setErrorMsg(String errorMsg) {
144
		this.errorMsg = errorMsg;
145
	}
146
 
147
	public File getListingFile() {
148
		return listingFile;
149
	}
150
 
151
	public void setListingFile(File listingFile) {
152
		this.listingFile = listingFile;
153
	}
154
 
155
}