Subversion Repositories SmartDukaan

Rev

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