Subversion Repositories SmartDukaan

Rev

Rev 8739 | Rev 8887 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

package in.shop2020.support.controllers;

import in.shop2020.model.v1.catalog.Amazonlisted;
import in.shop2020.model.v1.catalog.CatalogService.Client;
import in.shop2020.model.v1.catalog.SnapdealItem;
import in.shop2020.model.v1.order.AmazonFbaSalesSnapshot;
import in.shop2020.support.utils.ReportsUtils;
import in.shop2020.thrift.clients.CatalogClient;
import in.shop2020.thrift.clients.TransactionClient;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.xwork.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.struts2.convention.annotation.InterceptorRef;
import org.apache.struts2.convention.annotation.InterceptorRefs;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.apache.struts2.util.ServletContextAware;
import org.apache.thrift.TException;
import org.apache.thrift.transport.TTransportException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.opensymphony.xwork2.ValidationAwareSupport;

public class SnapdealListController extends ValidationAwareSupport implements ServletRequestAware ,ServletResponseAware, ServletContextAware{
        private static Logger logger = LoggerFactory.getLogger(SnapdealListController.class);


        private HttpServletRequest request;
        private HttpServletResponse response;
        private HttpSession session;
        private ServletContext context;
        private String url;
        private String itemId;
        private String isSnapdealListed;
        private String exceptionPrice;
        private String warehouseId;
        private File file;
        private String errMsg;
        private String next;
        private String id;

        public String index() {
                if (!ReportsUtils.canAccessReport((Long) session.getAttribute(ReportsUtils.ROLE),request.getServletPath())) {
                        return "authfail";
                }
                return "index";
        }

        public String uploadBulkSheet(){
                if(!ReportsUtils.canAccessReport((Long)session.getAttribute(ReportsUtils.ROLE), request.getRequestURI().substring(request.getContextPath().length()).split("/")[1].split("!")[0])) {
                        logger.info(request.getRequestURI().substring(request.getContextPath().length()).split("/")[1].split("!")[0]);
                        return "authfail";
                }
                return "snapdeal-bulk-upload";
        }
        
        public void downloadSnapdealListings() throws IOException, TException{
                File file = new File("/tmp/snapdeal-bulk-upload-template.xls");
                HSSFWorkbook hwb=new HSSFWorkbook();
                HSSFSheet sheet =  hwb.createSheet("Snapdeal-Listings");
                HSSFRow rowhead=   sheet.createRow((short)0);
                rowhead.createCell((short) 0).setCellValue("ITEM-ID");
                rowhead.createCell((short) 1).setCellValue("WAREHOUSE-ID");
                rowhead.createCell((short) 2).setCellValue("EXCEPTIONAL-PRICE");
                rowhead.createCell((short) 3).setCellValue("SNAPDEAL-LISTED");
                CatalogClient catalogServiceClient = null;
                List<SnapdealItem> snapdealItems = null;
                try {
                        catalogServiceClient = new CatalogClient("catalog_service_server_host_amazon","catalog_service_server_port");
                        in.shop2020.model.v1.catalog.CatalogService.Client catalogClient= catalogServiceClient.getClient();
                        snapdealItems = catalogClient.getAllSnapdealItems();
                } catch (Exception e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                }
                int iterator=1;
                for(SnapdealItem snapdealItem:snapdealItems){
                        HSSFRow row = sheet.createRow((short)iterator);
                        row.createCell((short) 0).setCellValue(snapdealItem.getItem_id());
                        row.createCell((short) 1).setCellValue(snapdealItem.getWarehouseId());
                        row.createCell((short) 2).setCellValue(snapdealItem.getExceptionPrice());
                        if(snapdealItem.isIsListedOnSnapdeal()){
                                row.createCell((short) 3).setCellValue(1);
                        }
                        else{
                                row.createCell((short) 3).setCellValue(0);
                        }
                        iterator++;
                }
                
                FileOutputStream fileOut = null;
                try {
                        fileOut = new FileOutputStream(file);
                } catch (FileNotFoundException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                }
                try {
                        hwb.write(fileOut);
                } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                }
                try {
                        fileOut.close();
                } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                }
                byte[] buffer = new byte[(int)file.length()];
                InputStream input = null;
                try {
                        int totalBytesRead = 0;
                        input = new BufferedInputStream(new FileInputStream(file));
                        while(totalBytesRead < buffer.length){
                                int bytesRemaining = buffer.length - totalBytesRead;
                                int bytesRead = input.read(buffer, totalBytesRead, bytesRemaining); 
                                if (bytesRead > 0){
                                        totalBytesRead = totalBytesRead + bytesRead;
                                }
                        }
                }
                finally {
                        input.close();
                        file.delete();
                }

                response.setHeader("Content-Disposition", "attachment; filename=\"Snapdeal-Bulk-Listings.xls\"");
                response.setContentType("application/octet-stream");
                ServletOutputStream sos;
                try {
                        sos = response.getOutputStream();
                        sos.write(buffer);
                        sos.flush();
                } catch (IOException e) {
                        System.out.println("Unable to stream the manifest file");
                }   

                
        }

        public void uploadsnapdealBulkSheet() throws IOException, TException{
                File fileToCreate = new File("/tmp/", "Snapdeal-bulk-upload.xls");
                FileUtils.copyFile(this.file, fileToCreate);
                FileInputStream iFile = new FileInputStream(new File(file.getAbsolutePath()));
                HSSFWorkbook workbook = new HSSFWorkbook(iFile);
                HSSFSheet sheet = workbook.getSheetAt(0);
                Client CatalogClient=null;
                StringBuilder sb = new StringBuilder();
                try {
                        CatalogClient = new CatalogClient("catalog_service_server_host_amazon","catalog_service_server_port").getClient();
                } catch (TTransportException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                }
                for (int iterator=(sheet.getFirstRowNum()+1);iterator<=sheet.getLastRowNum();iterator++){
                        SnapdealItem snapdealItem =null;
                        Long sku;
                        if (checkEmptyString(sheet.getRow(iterator).getCell(0))){
                                continue;
                        }
                        else {
                                sku=(long) sheet.getRow(iterator).getCell(0).getNumericCellValue();
                                snapdealItem = CatalogClient.getSnapdealItem(sku);
                        }
                        
                        if (!checkEmptyString(sheet.getRow(iterator).getCell(1))){
                                long warehouseId = (long) sheet.getRow(iterator).getCell(1).getNumericCellValue();
                                snapdealItem.setWarehouseId(warehouseId);
                        }

                        if (!checkEmptyString(sheet.getRow(iterator).getCell(2))){
                                double exceptionPrice = sheet.getRow(iterator).getCell(2).getNumericCellValue();
                                snapdealItem.setExceptionPrice(exceptionPrice);
                        }

                        if (!checkEmptyString(sheet.getRow(iterator).getCell(3))){
                                if ((long)sheet.getRow(iterator).getCell(3).getNumericCellValue()==1){
                                        snapdealItem.setIsListedOnSnapdeal(true);
                                }
                                if ((long)sheet.getRow(iterator).getCell(3).getNumericCellValue()==0){
                                        snapdealItem.setIsListedOnSnapdeal(false);
                                }
                        }
                        if(!CatalogClient.addOrUpdateSnapdealItem(snapdealItem)){
                                sb.append(sku + "\n");
                        }       
                }
                //logger.info("Amazon Bulk Map "+amazonBulkUpdate.toString());
                File file = new File("/tmp/amazonbulk");
                FileWriter writer = new FileWriter(file);
                writer.append(sb.toString());
                writer.close();
                byte[] buffer = new byte[(int)file.length()];
                InputStream input = null;
                try {
                        int totalBytesRead = 0;
                        input = new BufferedInputStream(new FileInputStream(file));
                        while(totalBytesRead < buffer.length){
                                int bytesRemaining = buffer.length - totalBytesRead;
                                int bytesRead = input.read(buffer, totalBytesRead, bytesRemaining); 
                                if (bytesRead > 0){
                                        totalBytesRead = totalBytesRead + bytesRead;
                                }
                        }
                }
                finally {
                        input.close();
                        file.delete();
                }

                response.setHeader("Content-Type", "text/javascript");

                ServletOutputStream sos;
                try {
                        sos = response.getOutputStream();
                        sos.write(buffer);
                        sos.flush();
                } catch (IOException e) {
                        System.out.println("Unable to stream the manifest file");
                }   
        }

        public boolean checkEmptyString(Cell cell){
                if (cell==null){
                        return true;
                }
                return false;
        }

        public String show() {
                logger.info("Before fetching role");
                logger.info(request.getSession().toString());
                logger.info(ReportsUtils.ROLE);
                logger.info(session.getAttribute(ReportsUtils.ROLE).toString());
                logger.info(request.getRequestURI().substring(request.getContextPath().length()).split("/")[1]);
                if(!ReportsUtils.canAccessReport((Long)session.getAttribute(ReportsUtils.ROLE), request.getRequestURI().substring(request.getContextPath().length()).split("/")[1])) {
                        logger.info(request.getRequestURI().substring(request.getContextPath().length()).split("/")[1]);
                        return "authfail";
                }

                if (StringUtils.equals(id, "snapdeal-options")){
                        return "snapdeal-options";
                }

                return "id";
        }

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

        public HttpServletRequest getRequest() {
                logger.info("set request"+request.toString());
                return request;
        }

        public void setRequest(HttpServletRequest request) {
                this.request = request;
        }

        public HttpServletResponse getResponse() {
                return response;
        }

        public void setResponse(HttpServletResponse response) {
                this.response = response;
        }

        public HttpSession getSession() {
                return session;
        }

        public void setSession(HttpSession session) {
                logger.info("set session"+session.toString());
                this.session = session;
        }

        public ServletContext getContext() {
                return context;
        }

        public void setContext(ServletContext context) {
                this.context = context;
        }

        public String getUrl() {
                return url;
        }

        public void setUrl(String url) {
                this.url = url;
        }

        public String getItemId() {
                return itemId;
        }

        public void setItemId(String itemId) {
                this.itemId = itemId;
        }

        public String getIsSnapdealListed() {
                return isSnapdealListed;
        }

        public void setIsSnapdealListed(String isSnapdealListed) {
                this.isSnapdealListed = isSnapdealListed;
        }

        public String getExceptionPrice() {
                return exceptionPrice;
        }

        public void setExceptionPrice(String exceptionPrice) {
                this.exceptionPrice = exceptionPrice;
        }

        public String getWarehouseId() {
                return warehouseId;
        }

        public void setWarehouseId(String warehouseId) {
                this.warehouseId = warehouseId;
        }

        public File getFile() {
                return file;
        }

        public void setFile(File file) {
                this.file = file;
        }

        public String getErrMsg() {
                return errMsg;
        }

        public void setErrMsg(String errMsg) {
                this.errMsg = errMsg;
        }

        public String getNext() {
                return next;
        }

        public void setNext(String next) {
                this.next = next;
        }

        public String getId() {
                return id;
        }

        public void setServletRequest(HttpServletRequest req) {
                this.request = req;
                this.session = req.getSession();        
        }

        public void setServletContext(ServletContext arg0) {
                // TODO Auto-generated method stub

        }

        public void setServletResponse(HttpServletResponse response) {
                this.response = response;
        }

}