Subversion Repositories SmartDukaan

Rev

Rev 9325 | Go to most recent revision | Details | 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
	private String pricingFileName;
59
	long vendorId;
60
	private File pricingFile;
61
 
62
	public String index() {
63
		if(!ReportsUtils.canAccessReport((Long)session.getAttribute(ReportsUtils.ROLE), request.getServletPath()))
64
			return "authfail";
65
		return "authsuccess";
66
	}
67
 
68
	public String show() {
69
		return "authsuccess";
70
	}
71
 
72
 
73
	public String create() throws InventoryServiceException, TException, CatalogServiceException {
74
		File fileToCreate = null;
75
        try {
76
            fileToCreate = new File("/tmp/", this.pricingFileName);
77
            FileUtils.copyFile(this.pricingFile, fileToCreate);
78
        } catch (Exception e) {
79
           logger.error("Error while writing pricing file to the local file system", e);
80
           addActionError("Error while writing  pricing file report to the local file system");
81
        }
82
 
83
        Workbook wb = null;
84
        try {
85
            wb = new HSSFWorkbook(new FileInputStream(fileToCreate));
86
        } catch (FileNotFoundException e) {
87
            logger.error("Unable to open the pricing file", e);
88
            addActionError("Unable to open the pricing file. Please check the report format.");
89
        } catch (IOException 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
        }
93
 
94
 
95
        InventoryClient isc = null;
96
        CatalogClient csc = null;
97
        try {
98
            isc = new InventoryClient();
99
            csc = new CatalogClient();
100
        } catch (TTransportException e) {
101
            logger.error("Unable to establish connection to the transaction service", e);
102
            addActionError("Unable to establish connection to the transaction service");
103
        }
104
        Client invClient = isc.getClient();
105
        in.shop2020.model.v1.catalog.CatalogService.Client catClient = csc.getClient(); 
106
        Sheet sheet = wb.getSheetAt(0);
107
        Row firstRow = sheet.getRow(0);
108
        logger.info("Last row number is:" + sheet.getLastRowNum());
109
        for (Row row : sheet) {
110
            if(row.equals(firstRow))
111
                continue;
112
            logger.info("Row no. " + row.getRowNum());
113
            long itemId = (long)row.getCell(0).getNumericCellValue();
114
            double mrp = row.getCell(1).getNumericCellValue();
115
            double sellingPrice = row.getCell(2).getNumericCellValue();
116
            double dealerPrice = row.getCell(3).getNumericCellValue();
117
            double mop = row.getCell(4).getNumericCellValue();
118
            double transferPrice = row.getCell(5).getNumericCellValue();
119
            double nlc = row.getCell(6).getNumericCellValue();
120
            Item item = catClient.getItem(itemId);
121
            item.setSellingPrice(sellingPrice);
122
            item.setMrp(mrp);
123
            catClient.updateItem(item);
124
            VendorItemPricing pricing = invClient.getItemPricing(itemId, vendorId);
125
            pricing.setNlc(nlc);
126
            pricing.setTransferPrice(transferPrice);
127
            pricing.setDealerPrice(dealerPrice);
128
            pricing.setMop(mop);
129
            invClient.addVendorItemPricing(pricing);
130
        }
131
        return "authsuccess";
132
	}
133
 
134
	public File getPricingFile() {
135
        return pricingFile;
136
    }
137
 
138
    public void setPricingFile(File pricingFile) {
139
        this.pricingFile = pricingFile;
140
    }
141
 
142
	public String getPricingFileName() {
143
        return pricingFileName;
144
    }
145
 
146
    public void setPricingFileName(String pricingFileName) {
147
        this.pricingFileName = pricingFileName;
148
    }
149
 
150
	@Override
151
	public void setServletRequest(HttpServletRequest request) {
152
		this.request = request;
153
		this.session = request.getSession();
154
	}
155
 
156
 
157
	public String getErrorMsg(){
158
		return this.errorMsg;
159
	}
160
 
161
	public long getVendorId() {
162
		return vendorId;
163
	}
164
 
165
	public void setItemId(long vendorId) {
166
		this.vendorId = vendorId;
167
	}
168
 
169
 
170
	public void setId(String id) {
171
		this.id = id;
172
	}
173
 
174
	public String getId() {
175
		return id;
176
	}
177
 
178
	public List<Vendor> getAllVendors() throws TException{
179
		InventoryClient isc = null;
180
        try {
181
            isc = new InventoryClient();
182
 
183
        } catch (TTransportException e) {
184
            logger.error("Unable to establish connection to the transaction service", e);
185
            addActionError("Unable to establish connection to the transaction service");
186
        }
187
        Client invClient = isc.getClient();
188
        return invClient.getAllVendors();
189
	}
190
 
191
}