Subversion Repositories SmartDukaan

Rev

Rev 9328 | Rev 9332 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
9322 rajveer 1
package in.shop2020.support.controllers;
2
 
3
import java.io.File;
4
import java.io.FileInputStream;
5
import java.io.FileNotFoundException;
6
import java.io.IOException;
9328 rajveer 7
import java.util.Collection;
9322 rajveer 8
import java.util.List;
9
 
10
 
11
import in.shop2020.model.v1.catalog.CatalogServiceException;
12
import in.shop2020.model.v1.catalog.Item;
13
import in.shop2020.model.v1.inventory.InventoryService.Client;
14
import in.shop2020.model.v1.inventory.InventoryServiceException;
15
import in.shop2020.model.v1.inventory.Vendor;
16
import in.shop2020.model.v1.inventory.VendorItemPricing;
17
import in.shop2020.support.utils.ReportsUtils;
18
import in.shop2020.thrift.clients.CatalogClient;
19
import in.shop2020.thrift.clients.InventoryClient;
20
 
21
import javax.servlet.http.HttpServletRequest;
22
import javax.servlet.http.HttpSession;
23
 
24
import org.apache.commons.io.FileUtils;
25
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
26
import org.apache.poi.ss.usermodel.Row;
27
import org.apache.poi.ss.usermodel.Sheet;
28
import org.apache.poi.ss.usermodel.Workbook;
29
import org.apache.struts2.convention.annotation.InterceptorRef;
30
import org.apache.struts2.convention.annotation.InterceptorRefs;
31
import org.apache.struts2.convention.annotation.Result;
32
import org.apache.struts2.convention.annotation.Results;
33
import org.apache.struts2.interceptor.ServletRequestAware;
34
import org.apache.thrift.TException;
35
import org.apache.thrift.transport.TTransportException;
36
import org.slf4j.Logger;
37
import org.slf4j.LoggerFactory;
38
 
39
import com.opensymphony.xwork2.ActionSupport;
40
 
41
@SuppressWarnings("serial")
42
@InterceptorRefs({
43
	@InterceptorRef("defaultStack"),
44
	@InterceptorRef("login")
45
})
46
@Results({
47
	@Result(name="authfail", type="redirectAction", params = {"actionName" , "reports"})
48
})
49
public class VendorPriceController extends ActionSupport implements ServletRequestAware {
50
 
51
	private static Logger logger = LoggerFactory.getLogger(VendorPriceController.class);
52
 
53
	private HttpServletRequest request;
54
	private HttpSession session;
55
 
56
	private String id;
57
	private String errorMsg = "";
58
 
59
	long vendorId;
9325 rajveer 60
 
9322 rajveer 61
	private File pricingFile;
9325 rajveer 62
    private String pricingFileContentType;
63
    private String pricingFileFileName;
64
 
9322 rajveer 65
	public String index() {
66
		if(!ReportsUtils.canAccessReport((Long)session.getAttribute(ReportsUtils.ROLE), request.getServletPath()))
67
			return "authfail";
68
		return "authsuccess";
69
	}
70
 
71
	public String show() {
72
		return "authsuccess";
73
	}
74
 
75
 
76
	public String create() throws InventoryServiceException, TException, CatalogServiceException {
77
		File fileToCreate = null;
78
        try {
9325 rajveer 79
            fileToCreate = new File("/tmp/", this.pricingFileFileName);
9322 rajveer 80
            FileUtils.copyFile(this.pricingFile, fileToCreate);
81
        } catch (Exception e) {
82
           logger.error("Error while writing pricing file to the local file system", e);
83
           addActionError("Error while writing  pricing file report to the local file system");
84
        }
85
 
86
        Workbook wb = null;
87
        try {
88
            wb = new HSSFWorkbook(new FileInputStream(fileToCreate));
89
        } catch (FileNotFoundException e) {
90
            logger.error("Unable to open the pricing file", e);
91
            addActionError("Unable to open the pricing file. Please check the report format.");
92
        } catch (IOException e) {
93
        	logger.error("Unable to open the pricing file", e);
94
            addActionError("Unable to open the pricing file. Please check the report format.");
95
        }
96
 
97
 
98
        InventoryClient isc = null;
99
        CatalogClient csc = null;
100
        try {
101
            isc = new InventoryClient();
9328 rajveer 102
            csc = new CatalogClient("catalog_service_server_host_amazon","catalog_service_server_port");
9322 rajveer 103
        } catch (TTransportException e) {
104
            logger.error("Unable to establish connection to the transaction service", e);
105
            addActionError("Unable to establish connection to the transaction service");
106
        }
107
        Client invClient = isc.getClient();
108
        in.shop2020.model.v1.catalog.CatalogService.Client catClient = csc.getClient(); 
109
        Sheet sheet = wb.getSheetAt(0);
110
        Row firstRow = sheet.getRow(0);
111
        logger.info("Last row number is:" + sheet.getLastRowNum());
112
        for (Row row : sheet) {
113
            if(row.equals(firstRow))
114
                continue;
115
            logger.info("Row no. " + row.getRowNum());
116
            long itemId = (long)row.getCell(0).getNumericCellValue();
9331 rajveer 117
            double mrp = Math.round(row.getCell(1).getNumericCellValue());
118
            double sellingPrice = Math.round(row.getCell(2).getNumericCellValue());
119
            double mop = Math.round(row.getCell(3).getNumericCellValue());
120
            double dealerPrice = Math.round(row.getCell(4).getNumericCellValue());
121
            double transferPrice = Math.round(row.getCell(5).getNumericCellValue());
122
            double nlc = Math.round(row.getCell(6).getNumericCellValue());
9322 rajveer 123
            Item item = catClient.getItem(itemId);
9328 rajveer 124
            if(sellingPrice != item.getSellingPrice() || mrp != item.getMrp()){
125
            	item.setSellingPrice(sellingPrice);
126
            	item.setMrp(mrp);
127
            	catClient.updateItem(item);
128
            }
9322 rajveer 129
            VendorItemPricing pricing = invClient.getItemPricing(itemId, vendorId);
130
            pricing.setNlc(nlc);
131
            pricing.setTransferPrice(transferPrice);
132
            pricing.setDealerPrice(dealerPrice);
133
            pricing.setMop(mop);
134
            invClient.addVendorItemPricing(pricing);
135
        }
9328 rajveer 136
        addActionMessage("Updated pricing for " +sheet.getLastRowNum() + " items");
9322 rajveer 137
        return "authsuccess";
138
	}
139
 
140
	public File getPricingFile() {
141
        return pricingFile;
142
    }
143
 
144
    public void setPricingFile(File pricingFile) {
145
        this.pricingFile = pricingFile;
146
    }
147
 
9325 rajveer 148
	public String getPricingFileFileName() {
149
        return pricingFileFileName;
9322 rajveer 150
    }
151
 
9325 rajveer 152
    public void setPricingFileFileName(String pricingFileFileName) {
153
        this.pricingFileFileName = pricingFileFileName;
9322 rajveer 154
    }
155
 
156
	@Override
157
	public void setServletRequest(HttpServletRequest request) {
158
		this.request = request;
159
		this.session = request.getSession();
160
	}
161
 
162
	public String getErrorMsg(){
9328 rajveer 163
		Collection<String> actionErrors = getActionErrors();
164
        if(actionErrors != null && !actionErrors.isEmpty()){
165
            for (String str : actionErrors) {
166
                errorMsg += "<BR/>" + str;
167
            }   
168
        }
169
        return this.errorMsg;
9322 rajveer 170
	}
171
 
9328 rajveer 172
	public String getSuccessMsg(){
173
		String successMsg = "";
174
		Collection<String> actionMessages = getActionMessages();
175
        if(actionMessages != null && !actionMessages.isEmpty()){
176
            for (String str : actionMessages) {
177
            	successMsg += "<BR/>" + str;
178
            }   
179
        }
180
        return successMsg;
181
	}
182
 
9322 rajveer 183
	public long getVendorId() {
184
		return vendorId;
185
	}
186
 
9326 rajveer 187
	public void setVendorId(long vendorId) {
9322 rajveer 188
		this.vendorId = vendorId;
189
	}
190
 
191
	public void setId(String id) {
192
		this.id = id;
193
	}
194
 
195
	public String getId() {
196
		return id;
197
	}
198
 
199
	public List<Vendor> getAllVendors() throws TException{
200
		InventoryClient isc = null;
201
        try {
202
            isc = new InventoryClient();
203
 
204
        } catch (TTransportException e) {
205
            logger.error("Unable to establish connection to the transaction service", e);
206
            addActionError("Unable to establish connection to the transaction service");
207
        }
208
        Client invClient = isc.getClient();
209
        return invClient.getAllVendors();
210
	}
211
 
9325 rajveer 212
	public void setPricingFileContentType(String pricingFileContentType) {
213
		this.pricingFileContentType = pricingFileContentType;
214
	}
215
 
216
	public String getPricingFileContentType() {
217
		return pricingFileContentType;
218
	}
219
 
9322 rajveer 220
}