Subversion Repositories SmartDukaan

Rev

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