Subversion Repositories SmartDukaan

Rev

Rev 9332 | 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());
9333 rajveer 112
        int successful = 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) {
9333 rajveer 139
            	addActionError("Item Id is :" + itemId + " Reason is :" + e.getMessage());
9332 rajveer 140
			}
9322 rajveer 141
        }
9332 rajveer 142
        addActionMessage("Updated pricing for " + successful + " items");
9322 rajveer 143
        return "authsuccess";
144
	}
145
 
146
	public File getPricingFile() {
147
        return pricingFile;
148
    }
149
 
150
    public void setPricingFile(File pricingFile) {
151
        this.pricingFile = pricingFile;
152
    }
153
 
9325 rajveer 154
	public String getPricingFileFileName() {
155
        return pricingFileFileName;
9322 rajveer 156
    }
157
 
9325 rajveer 158
    public void setPricingFileFileName(String pricingFileFileName) {
159
        this.pricingFileFileName = pricingFileFileName;
9322 rajveer 160
    }
161
 
162
	@Override
163
	public void setServletRequest(HttpServletRequest request) {
164
		this.request = request;
165
		this.session = request.getSession();
166
	}
167
 
168
	public String getErrorMsg(){
9328 rajveer 169
		Collection<String> actionErrors = getActionErrors();
170
        if(actionErrors != null && !actionErrors.isEmpty()){
171
            for (String str : actionErrors) {
172
                errorMsg += "<BR/>" + str;
173
            }   
174
        }
175
        return this.errorMsg;
9322 rajveer 176
	}
177
 
9328 rajveer 178
	public String getSuccessMsg(){
179
		String successMsg = "";
180
		Collection<String> actionMessages = getActionMessages();
181
        if(actionMessages != null && !actionMessages.isEmpty()){
182
            for (String str : actionMessages) {
183
            	successMsg += "<BR/>" + str;
184
            }   
185
        }
186
        return successMsg;
187
	}
188
 
9322 rajveer 189
	public long getVendorId() {
190
		return vendorId;
191
	}
192
 
9326 rajveer 193
	public void setVendorId(long vendorId) {
9322 rajveer 194
		this.vendorId = vendorId;
195
	}
196
 
197
	public void setId(String id) {
198
		this.id = id;
199
	}
200
 
201
	public String getId() {
202
		return id;
203
	}
204
 
205
	public List<Vendor> getAllVendors() throws TException{
206
		InventoryClient isc = null;
207
        try {
208
            isc = new InventoryClient();
209
 
210
        } catch (TTransportException e) {
211
            logger.error("Unable to establish connection to the transaction service", e);
212
            addActionError("Unable to establish connection to the transaction service");
213
        }
214
        Client invClient = isc.getClient();
215
        return invClient.getAllVendors();
216
	}
217
 
9325 rajveer 218
	public void setPricingFileContentType(String pricingFileContentType) {
219
		this.pricingFileContentType = pricingFileContentType;
220
	}
221
 
222
	public String getPricingFileContentType() {
223
		return pricingFileContentType;
224
	}
225
 
9322 rajveer 226
}