Subversion Repositories SmartDukaan

Rev

Rev 13709 | Details | Compare with Previous | 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
		}
13712 manish.sha 137
 
138
		try{
139
			if(!catalogClient.isAlive()){
140
				catalogClient = new CatalogClient().getClient();
141
			}if(hsItemList.size()>0){
142
				catalogClient.addHsItem(hsItemList);
143
			}
144
		}catch (Exception e) {
145
            logger.error(e + "\n" + e.getMessage() + e.getCause());
146
            setErrorMsg(getErrorMsg() + "<br/>Error while updating Data in System");
147
            addActionError("Error while updating Data in System");
148
       }
13709 manish.sha 149
		if(errorMsg.isEmpty()) {
150
	    	setErrorMsg("Sucessfully uploaded listings");
151
	    }
152
		return "authsuccess";
153
	}
154
 
155
	private boolean checkForErrors(){
156
		Collection<String> actionErrors = getActionErrors();
157
		if(actionErrors != null && !actionErrors.isEmpty()){
158
			for (String str : actionErrors) {
159
				errorMsg += "<BR/>" + str;
160
			}
161
			return true;
162
		}
163
		return false;
164
	}
165
 
166
	@Override
167
	public void setServletRequest(HttpServletRequest request) {
168
		this.request = request;
169
		this.session = request.getSession();
170
	}
171
 
172
	public File getListingDataFile() {
173
		return listingDataFile;
174
	}
175
 
176
	public void setListingDataFile(File listingDataFile) {
177
		this.listingDataFile = listingDataFile;
178
	}
179
 
180
	public String getListingDataFileName() {
181
		return listingDataFileName;
182
	}
183
 
184
	public void setListingDataFileName(String listingDataFileName) {
185
		this.listingDataFileName = listingDataFileName;
186
	}
187
 
188
	public String getErrorMsg() {
189
		return errorMsg;
190
	}
191
 
192
	public void setErrorMsg(String errorMsg) {
193
		this.errorMsg = errorMsg;
194
	}
195
 
196
	public Long getRowId() {
197
		return rowId;
198
	}
199
 
200
	public void setRowId(Long rowId) {
201
		this.rowId = rowId;
202
	}
203
}