Subversion Repositories SmartDukaan

Rev

Rev 9325 | Go to most recent revision | 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.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 = "";

        private String pricingFileName;
        long vendorId;
        private File pricingFile;
        
        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() throws InventoryServiceException, TException, CatalogServiceException {
                File fileToCreate = null;
        try {
            fileToCreate = new File("/tmp/", this.pricingFileName);
            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();
        } 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());
        for (Row row : sheet) {
            if(row.equals(firstRow))
                continue;
            logger.info("Row no. " + row.getRowNum());
            long itemId = (long)row.getCell(0).getNumericCellValue();
            double mrp = row.getCell(1).getNumericCellValue();
            double sellingPrice = row.getCell(2).getNumericCellValue();
            double dealerPrice = row.getCell(3).getNumericCellValue();
            double mop = row.getCell(4).getNumericCellValue();
            double transferPrice = row.getCell(5).getNumericCellValue();
            double nlc = row.getCell(6).getNumericCellValue();
            Item item = catClient.getItem(itemId);
            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);
        }
        return "authsuccess";
        }

        public File getPricingFile() {
        return pricingFile;
    }

    public void setPricingFile(File pricingFile) {
        this.pricingFile = pricingFile;
    }
        
        public String getPricingFileName() {
        return pricingFileName;
    }

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


        public String getErrorMsg(){
                return this.errorMsg;
        }

        public long getVendorId() {
                return vendorId;
        }

        public void setItemId(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();
        }

}