Subversion Repositories SmartDukaan

Rev

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

Rev Author Line No. Line
13709 manish.sha 1
package in.shop2020.support.controllers;
2
 
3
import in.shop2020.model.v1.catalog.HsItem;
4
import in.shop2020.support.utils.ReportsUtils;
5
import in.shop2020.thrift.clients.CatalogClient;
6
 
7
import java.io.File;
8
import java.io.FileInputStream;
9
import java.util.ArrayList;
10
import java.util.Collection;
11
import java.util.Date;
12
import java.util.List;
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.Sheet;
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 HsListingController extends ActionSupport implements ServletRequestAware {
42
	private HttpServletRequest request;
43
	private HttpSession session;
44
 
45
	private static Logger logger = LoggerFactory.getLogger(HsListingController.class);
46
 
47
	private File listingDataFile;
48
	private String listingDataFileName;
49
	private String errorMsg = "";
50
	private Long rowId = 0L;
51
 
52
	private static final int HS_ITEM_ID_INDEX = 0;
53
	private static final int ITEM_ID_INDEX = 1;
54
	private static final int HS_PRODUCT_ID_INDEX = 2;
55
	private static final int LISTING_PRICE_INDEX = 3;
56
	private static final int DEFAULT_WH_ID_INDEX = 4;
57
 
58
 
59
	public String index() {
60
        if(!ReportsUtils.canAccessReport((Long)session.getAttribute(ReportsUtils.ROLE), "/hs-dashboard"))
61
            return "authfail";
62
        checkForErrors();
63
        return "authsuccess";
64
    }
65
 
66
	public String create(){
67
		File fileToCreate = null;
68
		listingDataFileName = "ListingSheet_HomeShop18_"+(new Date().toString());
69
		try {
70
			fileToCreate = new File("/tmp/", this.listingDataFileName);
71
			FileUtils.copyFile(this.listingDataFile, fileToCreate);
72
		} catch (Exception e) {
73
			logger.error("Error while writing order data file to the local file system for Homeshop18", e);
74
			addActionError("Error while writing order data file to the local file system");
75
		}
76
 
77
		if(checkForErrors())
78
			return "authsuccess";
79
 
80
		Workbook wb = null;
81
		try {
82
			wb = new HSSFWorkbook(new FileInputStream(fileToCreate));
83
		} catch (Exception e) {
84
			logger.error("Unable to open the File for Order Creation for Homeshop18 ", e);
85
			setErrorMsg(getErrorMsg() + "Error in opening File for Order creation");
86
			addActionError("Unable to open the File for Order creation");
87
		}
88
		if(checkForErrors())
89
			return "authsuccess";
90
 
91
		Sheet sheet = wb.getSheetAt(0);
92
		Row firstRow = sheet.getRow(0);
93
 
94
		in.shop2020.model.v1.catalog.CatalogService.Client catalogClient = null;
95
		try {
96
			catalogClient = new CatalogClient().getClient();
97
		}
98
		catch (Exception e) {
99
             logger.error(e + "\n" + e.getMessage() + e.getCause());
100
             setErrorMsg(getErrorMsg() + "<br/>Unable to  Initialize Catalog Client ");
101
             addActionError("Unable to  Initialize Catalog Client ");
102
        }
103
 
104
		List<HsItem> hsItemList = new ArrayList<HsItem>();
105
		long time = System.currentTimeMillis();
106
		for (Row row : sheet) {
107
			rowId++;
108
			if(row.equals(firstRow))
109
				continue;
110
			try {
111
				row.getCell(HS_ITEM_ID_INDEX).setCellType(Cell.CELL_TYPE_STRING);
112
				row.getCell(HS_PRODUCT_ID_INDEX).setCellType(Cell.CELL_TYPE_STRING);
113
				HsItem hsItem = new HsItem();
114
				hsItem.setAddedBy(session.getAttribute(ReportsUtils.USER_EMAIL).toString());
115
				hsItem.setAddedTimestamp(time);
116
				hsItem.setDefaultWarehouseId((long)row.getCell(DEFAULT_WH_ID_INDEX).getNumericCellValue());
117
				hsItem.setHsItemId(row.getCell(HS_ITEM_ID_INDEX).getStringCellValue());
118
				hsItem.setHsProductId(row.getCell(HS_PRODUCT_ID_INDEX).getStringCellValue());
119
				hsItem.setItemId((long)row.getCell(ITEM_ID_INDEX).getNumericCellValue());
120
				hsItem.setListingPrice((long)row.getCell(LISTING_PRICE_INDEX).getNumericCellValue());
121
 
122
				hsItemList.add(hsItem);
123
				if(!catalogClient.isAlive()){
124
					catalogClient = new CatalogClient().getClient();
125
				}
126
				if(hsItemList.size()>=500){
127
					catalogClient.addHsItem(hsItemList);
128
					hsItemList = new ArrayList<HsItem>();
129
				}
130
			}
131
			catch (Exception e) {
132
	             logger.error(e + "\n" + e.getMessage() + e.getCause());
133
	             setErrorMsg(getErrorMsg() + "<br/>Error in row number " + rowId);
134
	             addActionError("Error in row number " + rowId);
135
	        }
136
		}
137
		if(errorMsg.isEmpty()) {
138
	    	setErrorMsg("Sucessfully uploaded listings");
139
	    }
140
		return "authsuccess";
141
	}
142
 
143
	private boolean checkForErrors(){
144
		Collection<String> actionErrors = getActionErrors();
145
		if(actionErrors != null && !actionErrors.isEmpty()){
146
			for (String str : actionErrors) {
147
				errorMsg += "<BR/>" + str;
148
			}
149
			return true;
150
		}
151
		return false;
152
	}
153
 
154
	@Override
155
	public void setServletRequest(HttpServletRequest request) {
156
		this.request = request;
157
		this.session = request.getSession();
158
	}
159
 
160
	public File getListingDataFile() {
161
		return listingDataFile;
162
	}
163
 
164
	public void setListingDataFile(File listingDataFile) {
165
		this.listingDataFile = listingDataFile;
166
	}
167
 
168
	public String getListingDataFileName() {
169
		return listingDataFileName;
170
	}
171
 
172
	public void setListingDataFileName(String listingDataFileName) {
173
		this.listingDataFileName = listingDataFileName;
174
	}
175
 
176
	public String getErrorMsg() {
177
		return errorMsg;
178
	}
179
 
180
	public void setErrorMsg(String errorMsg) {
181
		this.errorMsg = errorMsg;
182
	}
183
 
184
	public Long getRowId() {
185
		return rowId;
186
	}
187
 
188
	public void setRowId(Long rowId) {
189
		this.rowId = rowId;
190
	}
191
}