| 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.Date;
|
|
|
13 |
|
|
|
14 |
import javax.servlet.http.HttpServletRequest;
|
|
|
15 |
import javax.servlet.http.HttpSession;
|
|
|
16 |
|
|
|
17 |
import org.apache.commons.io.FileUtils;
|
|
|
18 |
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
|
|
|
19 |
import org.apache.poi.ss.usermodel.Cell;
|
|
|
20 |
import org.apache.poi.ss.usermodel.Row;
|
|
|
21 |
import org.apache.poi.ss.usermodel.Workbook;
|
|
|
22 |
import org.apache.struts2.convention.annotation.InterceptorRef;
|
|
|
23 |
import org.apache.struts2.convention.annotation.InterceptorRefs;
|
|
|
24 |
import org.apache.struts2.convention.annotation.Result;
|
|
|
25 |
import org.apache.struts2.convention.annotation.Results;
|
|
|
26 |
import org.apache.struts2.interceptor.ServletRequestAware;
|
|
|
27 |
import org.slf4j.Logger;
|
|
|
28 |
import org.slf4j.LoggerFactory;
|
|
|
29 |
|
|
|
30 |
import com.opensymphony.xwork2.ActionSupport;
|
|
|
31 |
|
|
|
32 |
@SuppressWarnings("serial")
|
|
|
33 |
@InterceptorRefs({
|
|
|
34 |
@InterceptorRef("defaultStack"),
|
|
|
35 |
@InterceptorRef("login")
|
|
|
36 |
})
|
|
|
37 |
@Results({
|
|
|
38 |
@Result(name="authfail", type="redirectAction", params = {"actionName" , "reports"})
|
|
|
39 |
})
|
|
|
40 |
public class EbayListingUploaderController extends ActionSupport implements ServletRequestAware {
|
|
|
41 |
|
|
|
42 |
private static Logger logger = LoggerFactory.getLogger(EbayListingUploaderController.class);
|
|
|
43 |
|
|
|
44 |
private static final int LISTING_ID_INDEX = 0;
|
|
|
45 |
private static final int ITEM_ID_INDEX = 1;
|
|
|
46 |
private static final int LISTING_NAME_INDEX = 2;
|
|
|
47 |
private static final int PRICE_INDEX = 3;
|
|
|
48 |
private static final int EXPIRY_DATE_INDEX = 4;
|
|
|
49 |
private static final int SUBSIDY_INDEX = 5;
|
|
|
50 |
private static final int DEFAULT_WAREHOUSE_INDEX = 6;
|
|
|
51 |
|
|
|
52 |
private HttpServletRequest request;
|
|
|
53 |
private HttpSession session;
|
|
|
54 |
|
|
|
55 |
private String errorMsg = "";
|
|
|
56 |
|
| 8251 |
amar.kumar |
57 |
private SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
|
| 8182 |
amar.kumar |
58 |
|
|
|
59 |
private File listingFile;
|
|
|
60 |
|
|
|
61 |
|
|
|
62 |
public String create() throws IOException {
|
|
|
63 |
FileUtils.copyFile(listingFile, new File("/tmp/listing_file " + sdf.format(new Date()) + ".xls"));
|
|
|
64 |
Workbook wb = new HSSFWorkbook(new FileInputStream(listingFile));
|
|
|
65 |
|
|
|
66 |
int totalNumRows = 0;
|
|
|
67 |
int successfullyProcessedNumRows = 0;
|
|
|
68 |
for (Row row : wb.getSheetAt(0)) {
|
|
|
69 |
String key = "";
|
| 8303 |
amar.kumar |
70 |
String listingId = null;
|
|
|
71 |
String listingName = null;
|
|
|
72 |
Long price =0L;
|
|
|
73 |
Long itemId = 0L;
|
|
|
74 |
Double subsidy = 0.0;
|
|
|
75 |
Date expiryDate = null;
|
| 8182 |
amar.kumar |
76 |
try {
|
|
|
77 |
totalNumRows++;
|
|
|
78 |
if(totalNumRows==1) {
|
|
|
79 |
continue;
|
|
|
80 |
}
|
|
|
81 |
long warehouseId = 0L;
|
| 8303 |
amar.kumar |
82 |
try {
|
|
|
83 |
row.getCell(LISTING_ID_INDEX).setCellType(Cell.CELL_TYPE_STRING);
|
|
|
84 |
listingId = row.getCell(LISTING_ID_INDEX).getStringCellValue();
|
|
|
85 |
itemId = new Double(row.getCell(ITEM_ID_INDEX).getNumericCellValue()).longValue();
|
|
|
86 |
listingName = row.getCell(LISTING_NAME_INDEX).getStringCellValue();
|
|
|
87 |
price = new Double(row.getCell(PRICE_INDEX).getNumericCellValue()).longValue();
|
|
|
88 |
subsidy = row.getCell(SUBSIDY_INDEX).getNumericCellValue();
|
|
|
89 |
Cell warehouseCell = row.getCell(DEFAULT_WAREHOUSE_INDEX);
|
|
|
90 |
if(warehouseCell != null && warehouseCell.getCellType() != Cell.CELL_TYPE_BLANK) {
|
|
|
91 |
warehouseId = new Double(row.getCell(DEFAULT_WAREHOUSE_INDEX).getNumericCellValue()).longValue();
|
|
|
92 |
} else {
|
|
|
93 |
warehouseId = 0L;
|
|
|
94 |
}
|
|
|
95 |
} catch (Exception e) {
|
|
|
96 |
setErrorMsg(getErrorMsg() + "<br/>Error in reading input for row number " + totalNumRows);
|
|
|
97 |
continue;
|
|
|
98 |
}
|
| 8182 |
amar.kumar |
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 {
|
| 8303 |
amar.kumar |
107 |
//TODO Check for Date and Time sanity
|
| 8281 |
amar.kumar |
108 |
ebayItem.setListingExpiryDate(interchangeDateAndMonth(row.getCell(EXPIRY_DATE_INDEX).getDateCellValue()).getTime());
|
| 8250 |
amar.kumar |
109 |
} catch (Exception e) {
|
|
|
110 |
logger.warn("Error while setting expiry time for Ebay Listing", e);
|
|
|
111 |
}
|
| 8182 |
amar.kumar |
112 |
ebayItem.setSubsidy(subsidy);
|
|
|
113 |
ebayItem.setDefaultWarehouseId(warehouseId);
|
|
|
114 |
catalogClient.addEbayItem(ebayItem);
|
|
|
115 |
|
|
|
116 |
successfullyProcessedNumRows++;
|
|
|
117 |
} catch (Exception e) {
|
|
|
118 |
logger.error(e + "\n" + e.getMessage() + e.getCause());
|
|
|
119 |
setErrorMsg(getErrorMsg() + "<br/>Error in row number " + totalNumRows);
|
|
|
120 |
addActionError("Error in row number " + totalNumRows);
|
|
|
121 |
}
|
|
|
122 |
}
|
|
|
123 |
if(errorMsg.isEmpty()) {
|
|
|
124 |
setErrorMsg("Sucessfully uploaded listings");
|
|
|
125 |
}
|
|
|
126 |
return "authsuccess";
|
|
|
127 |
}
|
|
|
128 |
|
|
|
129 |
public String index() {
|
|
|
130 |
if(!ReportsUtils.canAccessReport((Long)session.getAttribute(ReportsUtils.ROLE), "/ebay-dashboard"))
|
|
|
131 |
return "authfail";
|
|
|
132 |
return "authsuccess";
|
|
|
133 |
}
|
|
|
134 |
|
| 8281 |
amar.kumar |
135 |
private Date interchangeDateAndMonth(Date date) {
|
|
|
136 |
Date updatedDate = new Date(date.getTime());
|
| 8286 |
amar.kumar |
137 |
updatedDate.setDate(date.getMonth() + 1);
|
|
|
138 |
updatedDate.setMonth(date.getDate() - 1);
|
| 8281 |
amar.kumar |
139 |
return updatedDate;
|
|
|
140 |
}
|
|
|
141 |
|
| 8182 |
amar.kumar |
142 |
@Override
|
|
|
143 |
public void setServletRequest(HttpServletRequest request) {
|
|
|
144 |
this.request = request;
|
|
|
145 |
this.session = request.getSession();
|
|
|
146 |
}
|
|
|
147 |
|
|
|
148 |
public String getErrorMsg() {
|
|
|
149 |
return errorMsg;
|
|
|
150 |
}
|
|
|
151 |
|
|
|
152 |
public void setErrorMsg(String errorMsg) {
|
|
|
153 |
this.errorMsg = errorMsg;
|
|
|
154 |
}
|
|
|
155 |
|
|
|
156 |
public File getListingFile() {
|
|
|
157 |
return listingFile;
|
|
|
158 |
}
|
|
|
159 |
|
|
|
160 |
public void setListingFile(File listingFile) {
|
|
|
161 |
this.listingFile = listingFile;
|
|
|
162 |
}
|
|
|
163 |
|
|
|
164 |
}
|