Subversion Repositories SmartDukaan

Rev

Rev 9331 | 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
 
9332 rajveer 76
	public String create(){
9322 rajveer 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());
9332 rajveer 112
        int successful = 0, failed = 0;
9322 rajveer 113
        for (Row row : sheet) {
114
            if(row.equals(firstRow))
115
                continue;
116
            logger.info("Row no. " + row.getRowNum());
117
            long itemId = (long)row.getCell(0).getNumericCellValue();
9331 rajveer 118
            double mrp = Math.round(row.getCell(1).getNumericCellValue());
119
            double sellingPrice = Math.round(row.getCell(2).getNumericCellValue());
120
            double mop = Math.round(row.getCell(3).getNumericCellValue());
121
            double dealerPrice = Math.round(row.getCell(4).getNumericCellValue());
122
            double transferPrice = Math.round(row.getCell(5).getNumericCellValue());
123
            double nlc = Math.round(row.getCell(6).getNumericCellValue());
9332 rajveer 124
            try{
125
            	Item item = catClient.getItem(itemId);
126
            	if(sellingPrice != item.getSellingPrice() || mrp != item.getMrp()){
127
            		item.setSellingPrice(sellingPrice);
128
            		item.setMrp(mrp);
129
            		catClient.updateItem(item);
130
            	}
131
            	VendorItemPricing pricing = invClient.getItemPricing(itemId, vendorId);
132
            	pricing.setNlc(nlc);
133
            	pricing.setTransferPrice(transferPrice);
134
            	pricing.setDealerPrice(dealerPrice);
135
            	pricing.setMop(mop);
136
            	invClient.addVendorItemPricing(pricing);
137
            	successful++;
138
            } catch (Exception e) {
139
            	failed++;
140
            	addActionError(e.getMessage());
141
			}
9322 rajveer 142
        }
9332 rajveer 143
        addActionMessage("Updated pricing for " + successful + " items");
144
        addActionError("Unable to update " + failed + " items and received following errors.");
9322 rajveer 145
        return "authsuccess";
146
	}
147
 
148
	public File getPricingFile() {
149
        return pricingFile;
150
    }
151
 
152
    public void setPricingFile(File pricingFile) {
153
        this.pricingFile = pricingFile;
154
    }
155
 
9325 rajveer 156
	public String getPricingFileFileName() {
157
        return pricingFileFileName;
9322 rajveer 158
    }
159
 
9325 rajveer 160
    public void setPricingFileFileName(String pricingFileFileName) {
161
        this.pricingFileFileName = pricingFileFileName;
9322 rajveer 162
    }
163
 
164
	@Override
165
	public void setServletRequest(HttpServletRequest request) {
166
		this.request = request;
167
		this.session = request.getSession();
168
	}
169
 
170
	public String getErrorMsg(){
9328 rajveer 171
		Collection<String> actionErrors = getActionErrors();
172
        if(actionErrors != null && !actionErrors.isEmpty()){
173
            for (String str : actionErrors) {
174
                errorMsg += "<BR/>" + str;
175
            }   
176
        }
177
        return this.errorMsg;
9322 rajveer 178
	}
179
 
9328 rajveer 180
	public String getSuccessMsg(){
181
		String successMsg = "";
182
		Collection<String> actionMessages = getActionMessages();
183
        if(actionMessages != null && !actionMessages.isEmpty()){
184
            for (String str : actionMessages) {
185
            	successMsg += "<BR/>" + str;
186
            }   
187
        }
188
        return successMsg;
189
	}
190
 
9322 rajveer 191
	public long getVendorId() {
192
		return vendorId;
193
	}
194
 
9326 rajveer 195
	public void setVendorId(long vendorId) {
9322 rajveer 196
		this.vendorId = vendorId;
197
	}
198
 
199
	public void setId(String id) {
200
		this.id = id;
201
	}
202
 
203
	public String getId() {
204
		return id;
205
	}
206
 
207
	public List<Vendor> getAllVendors() throws TException{
208
		InventoryClient isc = null;
209
        try {
210
            isc = new InventoryClient();
211
 
212
        } catch (TTransportException e) {
213
            logger.error("Unable to establish connection to the transaction service", e);
214
            addActionError("Unable to establish connection to the transaction service");
215
        }
216
        Client invClient = isc.getClient();
217
        return invClient.getAllVendors();
218
	}
219
 
9325 rajveer 220
	public void setPricingFileContentType(String pricingFileContentType) {
221
		this.pricingFileContentType = pricingFileContentType;
222
	}
223
 
224
	public String getPricingFileContentType() {
225
		return pricingFileContentType;
226
	}
227
 
9322 rajveer 228
}