Subversion Repositories SmartDukaan

Rev

Rev 9332 | Blame | Compare with Previous | Last modification | View Log | RSS feed

package in.shop2020.support.controllers;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Collection;
import java.util.List;


import in.shop2020.model.v1.catalog.CatalogServiceException;
import in.shop2020.model.v1.catalog.Item;
import in.shop2020.model.v1.inventory.InventoryService.Client;
import in.shop2020.model.v1.inventory.InventoryServiceException;
import in.shop2020.model.v1.inventory.Vendor;
import in.shop2020.model.v1.inventory.VendorItemPricing;
import in.shop2020.support.utils.ReportsUtils;
import in.shop2020.thrift.clients.CatalogClient;
import in.shop2020.thrift.clients.InventoryClient;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.io.FileUtils;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.struts2.convention.annotation.InterceptorRef;
import org.apache.struts2.convention.annotation.InterceptorRefs;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.thrift.TException;
import org.apache.thrift.transport.TTransportException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.opensymphony.xwork2.ActionSupport;

@SuppressWarnings("serial")
@InterceptorRefs({
        @InterceptorRef("defaultStack"),
        @InterceptorRef("login")
})
@Results({
        @Result(name="authfail", type="redirectAction", params = {"actionName" , "reports"})
})
public class VendorPriceController extends ActionSupport implements ServletRequestAware {

        private static Logger logger = LoggerFactory.getLogger(VendorPriceController.class);

        private HttpServletRequest request;
        private HttpSession session;

        private String id;
        private String errorMsg = "";

        long vendorId;
        
        private File pricingFile;
    private String pricingFileContentType;
    private String pricingFileFileName;
    
        public String index() {
                if(!ReportsUtils.canAccessReport((Long)session.getAttribute(ReportsUtils.ROLE), request.getServletPath()))
                        return "authfail";
                return "authsuccess";
        }

        public String show() {
                return "authsuccess";
        }
        
        
        public String create(){
                File fileToCreate = null;
        try {
            fileToCreate = new File("/tmp/", this.pricingFileFileName);
            FileUtils.copyFile(this.pricingFile, fileToCreate);
        } catch (Exception e) {
           logger.error("Error while writing pricing file to the local file system", e);
           addActionError("Error while writing  pricing file report to the local file system");
        }
        
        Workbook wb = null;
        try {
            wb = new HSSFWorkbook(new FileInputStream(fileToCreate));
        } catch (FileNotFoundException e) {
            logger.error("Unable to open the pricing file", e);
            addActionError("Unable to open the pricing file. Please check the report format.");
        } catch (IOException e) {
                logger.error("Unable to open the pricing file", e);
            addActionError("Unable to open the pricing file. Please check the report format.");
        }
        
        
        InventoryClient isc = null;
        CatalogClient csc = null;
        try {
            isc = new InventoryClient();
            csc = new CatalogClient("catalog_service_server_host_amazon","catalog_service_server_port");
        } catch (TTransportException e) {
            logger.error("Unable to establish connection to the transaction service", e);
            addActionError("Unable to establish connection to the transaction service");
        }
        Client invClient = isc.getClient();
        in.shop2020.model.v1.catalog.CatalogService.Client catClient = csc.getClient(); 
        Sheet sheet = wb.getSheetAt(0);
        Row firstRow = sheet.getRow(0);
        logger.info("Last row number is:" + sheet.getLastRowNum());
        int successful = 0;
        for (Row row : sheet) {
            if(row.equals(firstRow))
                continue;
            logger.info("Row no. " + row.getRowNum());
            long itemId = (long)row.getCell(0).getNumericCellValue();
            double mrp = Math.round(row.getCell(1).getNumericCellValue());
            double sellingPrice = Math.round(row.getCell(2).getNumericCellValue());
            double mop = Math.round(row.getCell(3).getNumericCellValue());
            double dealerPrice = Math.round(row.getCell(4).getNumericCellValue());
            double transferPrice = Math.round(row.getCell(5).getNumericCellValue());
            double nlc = Math.round(row.getCell(6).getNumericCellValue());
            try{
                Item item = catClient.getItem(itemId);
                if(sellingPrice != item.getSellingPrice() || mrp != item.getMrp()){
                        item.setSellingPrice(sellingPrice);
                        item.setMrp(mrp);
                        catClient.updateItem(item);
                }
                VendorItemPricing pricing = invClient.getItemPricing(itemId, vendorId);
                pricing.setNlc(nlc);
                pricing.setTransferPrice(transferPrice);
                pricing.setDealerPrice(dealerPrice);
                pricing.setMop(mop);
                invClient.addVendorItemPricing(pricing);
                successful++;
            } catch (Exception e) {
                addActionError("Item Id is :" + itemId + " Reason is :" + e.getMessage());
                        }
        }
        addActionMessage("Updated pricing for " + successful + " items");
        return "authsuccess";
        }

        public File getPricingFile() {
        return pricingFile;
    }

    public void setPricingFile(File pricingFile) {
        this.pricingFile = pricingFile;
    }
        
        public String getPricingFileFileName() {
        return pricingFileFileName;
    }

    public void setPricingFileFileName(String pricingFileFileName) {
        this.pricingFileFileName = pricingFileFileName;
    }
    
        @Override
        public void setServletRequest(HttpServletRequest request) {
                this.request = request;
                this.session = request.getSession();
        }

        public String getErrorMsg(){
                Collection<String> actionErrors = getActionErrors();
        if(actionErrors != null && !actionErrors.isEmpty()){
            for (String str : actionErrors) {
                errorMsg += "<BR/>" + str;
            }   
        }
        return this.errorMsg;
        }

        public String getSuccessMsg(){
                String successMsg = "";
                Collection<String> actionMessages = getActionMessages();
        if(actionMessages != null && !actionMessages.isEmpty()){
            for (String str : actionMessages) {
                successMsg += "<BR/>" + str;
            }   
        }
        return successMsg;
        }

        public long getVendorId() {
                return vendorId;
        }

        public void setVendorId(long vendorId) {
                this.vendorId = vendorId;
        }

        public void setId(String id) {
                this.id = id;
        }

        public String getId() {
                return id;
        }

        public List<Vendor> getAllVendors() throws TException{
                InventoryClient isc = null;
        try {
            isc = new InventoryClient();
        
        } catch (TTransportException e) {
            logger.error("Unable to establish connection to the transaction service", e);
            addActionError("Unable to establish connection to the transaction service");
        }
        Client invClient = isc.getClient();
        return invClient.getAllVendors();
        }

        public void setPricingFileContentType(String pricingFileContentType) {
                this.pricingFileContentType = pricingFileContentType;
        }

        public String getPricingFileContentType() {
                return pricingFileContentType;
        }

}