Subversion Repositories SmartDukaan

Rev

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

package in.shop2020.inventory.controllers;

import in.shop2020.googleadwords.feedshandler.ContentFeedsHandler;
import in.shop2020.googleadwords.model.Product;
import in.shop2020.googleadwords.util.UserInformation;
import in.shop2020.logistics.DeliveryType;
import in.shop2020.logistics.LogisticsService;
import in.shop2020.model.v1.catalog.CatalogService;
import in.shop2020.model.v1.catalog.CatalogServiceException;
import in.shop2020.model.v1.catalog.Item;
import in.shop2020.model.v1.catalog.ProductFeedSubmit;
import in.shop2020.logistics.ItemText;
import in.shop2020.thrift.clients.CatalogClient;
import in.shop2020.thrift.clients.LogisticsClient;

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

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

import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.thrift.TException;
import org.apache.thrift.transport.TTransportException;


public class ProductFeedsController extends BaseController{
        
        private String cataLogItemId;
        private String sentFeedResult;
        private String catalogItemIdDelete;
        private String deleteFeedResult;
        private String stockLinkFeed;
        private boolean stockLinkedFeed = false;
        public String index() {
                return "index";
        }
        
        public void downloadAllProductFeeds() throws IOException{
                File file = new File("/tmp/Product-Feeds.xls");
                HSSFWorkbook hwb=new HSSFWorkbook();
                HSSFSheet sheet =  hwb.createSheet("Product Feeds");
                HSSFRow rowhead=   sheet.createRow(0);
                rowhead.createCell(0).setCellValue("Catalog Item Id");
                rowhead.createCell(1).setCellValue("Product Name");
                rowhead.createCell(2).setCellValue("Stock Linked");
                
                CatalogClient catalogClient = null;
                try {
                        catalogClient = new CatalogClient();
                } catch (Exception e1) {
                        e1.printStackTrace();
                }
                
                List<ProductFeedSubmit> feedSubmits = new ArrayList<ProductFeedSubmit>();
                
                try{
                        feedSubmits = catalogClient.getClient().getAllProductFeedSubmit();
                } catch(Exception e){
                        e.printStackTrace();
                }
                
                int i = 1;
                try{
                        for(ProductFeedSubmit feedSubmit : feedSubmits){
                                HSSFRow rowToAdd = sheet.createRow(i);
                                rowToAdd.createCell(0).setCellValue(feedSubmit.getCatalogItemId());
                                Item item = catalogClient.getClient().getItemsByCatalogId(feedSubmit.getCatalogItemId()).get(0);
                                rowToAdd.createCell(1).setCellValue(item.getBrand() + " "+ item.getModelName() + " " + item.getModelNumber());
                                rowToAdd.createCell(2).setCellValue(feedSubmit.isStockLinkedFeed());
                                i++;
                        }
                } catch(Exception e){
                        e.printStackTrace();
                }
                
                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=\"Product-Feeds.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 String sendProductFeeds(){
                //boolean sendFeedsResult = false;
                processStockLinkFeed();
                Product insertedProduct = null;
                CatalogClient catalogClient = null;
                try {
                        catalogClient = new CatalogClient();
                } catch (TTransportException e1) {
                        e1.printStackTrace();
                }
                
                if(cataLogItemId!=null && !("").equalsIgnoreCase(cataLogItemId)){
                        List<Long> itemIdList = getEntityLogisticsEstimation(Long.parseLong(cataLogItemId));
                        UserInformation userInformation = new UserInformation("8587366",
                                        "http://www.saholic.com",
                                        "adwords@shop2020.in", "adwords_shop2020");
                        ContentFeedsHandler sample = new ContentFeedsHandler(userInformation,
                                        "https://content.googleapis.com/content/v1/");
                        
                        if(catalogClient!= null){
                                if(itemIdList!=null && itemIdList.size()>0){
                                        try {
                                                Item item = catalogClient.getClient().getItem(itemIdList.get(0));
                                                insertedProduct = sample.sendProductFeed(item);
                                        } catch (IOException e) {
                                                e.printStackTrace();
                                        } catch (CatalogServiceException e) {
                                                e.printStackTrace();
                                        } catch (TException e) {
                                                e.printStackTrace();
                                        }
                                }
                                else{
                                        sentFeedResult = "Error: There is No Product avaliable for given Catalog Item Id.";
                                        return "output";
                                }
                        }
                }
                if(insertedProduct!=null){
                        if(insertedProduct.content!=null && insertedProduct.content.value.indexOf("Error:")>-1){
                                sentFeedResult = insertedProduct.content.value;
                                return "output";
                        }
                        
                        sentFeedResult = "Product Feeds are sent Successfully and Product Details are Given Below:\nProduct Id- "
                                +insertedProduct.externalId+" Product Title- "+insertedProduct.title;
                        
                        if(catalogClient!=null){
                                ProductFeedSubmit feedSubmit = null;
                                try {
                                        feedSubmit= catalogClient.getClient().getProductFeedSubmit(Long.parseLong(cataLogItemId));
                                        if(feedSubmit!=null && feedSubmit.getCatalogItemId() > 0l){
                                                System.out.println("Comes into Update ");
                                                feedSubmit.setCatalogItemId(Long.parseLong(cataLogItemId));
                                                feedSubmit.setStockLinkedFeed(stockLinkedFeed);
                                                boolean resultUpdate = catalogClient.getClient().updateProductFeedSubmit(feedSubmit);
                                                if(resultUpdate){
                                                        sentFeedResult = sentFeedResult + "\n And Updated to Our System Successfully ";
                                                }
                                        }
                                        else{
                                                System.out.println("Comes into Add ");
                                                feedSubmit = new ProductFeedSubmit();
                                                feedSubmit.setCatalogItemId(Long.parseLong(cataLogItemId));
                                                feedSubmit.setStockLinkedFeed(stockLinkedFeed);
                                                boolean resultUpdate = catalogClient.getClient().addProductFeedSubmit(feedSubmit);
                                                if(resultUpdate){
                                                        sentFeedResult = sentFeedResult + "\n And Added to Our System Successfully ";
                                                }
                                        }
                                } catch (CatalogServiceException e) {
                                        e.printStackTrace();
                                } catch (TException e) {
                                        e.printStackTrace();
                                }
                        }
                        return "output";
                }
                else{
                        sentFeedResult = "Some Error occured while sending feeds. Please contact concerned Team";
                        return "output";
                }
                /*if(sendFeedsResult){
                        successmsg = "Product Feeds are sent Successfully";
                        return index();
                }
                else{
                        errorMsg = "Some Error occured while sending feeds. Please contact concerned Team";
                        return index();
                }*/
        }
        
        public String deleteProductFeeds(){
                CatalogClient catalogClient = null;
                try {
                        catalogClient = new CatalogClient();
                } catch (TTransportException e1) {
                        e1.printStackTrace();
                }
                if(catalogClient!=null){
                        ProductFeedSubmit feedSubmit = null;
                        try {
                                feedSubmit= catalogClient.getClient().getProductFeedSubmit(Long.parseLong(catalogItemIdDelete));
                                if(feedSubmit!=null && feedSubmit.getCatalogItemId() > 0l){
                                        UserInformation userInformation = new UserInformation("8587366",
                                                        "http://www.saholic.com",
                                                        "adwords@shop2020.in", "adwords_shop2020");
                                        ContentFeedsHandler sample = new ContentFeedsHandler(userInformation,
                                                        "https://content.googleapis.com/content/v1/");
                                        boolean deleteFeedResultBool = sample.deleteProduct(Long.parseLong(catalogItemIdDelete));
                                        if(deleteFeedResultBool){
                                                deleteFeedResult = "Product Feeds are deleted Successfully For Product Id "+ catalogItemIdDelete;
                                                boolean deleteResult = catalogClient.getClient().deleteProductFeedSubmit(Long.parseLong(catalogItemIdDelete));
                                                if(deleteResult){
                                                        deleteFeedResult = deleteFeedResult + "\n And Deleted from our System Successfully";
                                                }
                                        }
                                        else{
                                                deleteFeedResult = "Error Occurred: Product Feeds Deletion Unsuccessful";
                                        }
                                }
                        } catch (CatalogServiceException e) {
                                e.printStackTrace();
                        } catch (TException e) {
                                e.printStackTrace();
                        }
                }       
                
                return "output";
        }
        
        public List<Long> getEntityLogisticsEstimation(long productId){
        List<ItemText> items=null;
        List<Long> itemIds = new ArrayList<Long>();
        try {
                LogisticsClient cl = new LogisticsClient();
            LogisticsService.Client client = cl.getClient();
            items = client.getEntityLogisticsEstimation(productId, "110001", DeliveryType.PREPAID );
            for(ItemText text : items)
                itemIds.add(text.getItemId());
        } catch (Exception e1) {
            System.out.println("Unable to get items by catalog item id"+e1.getMessage());
        }
        return itemIds;
    }
        
        public void processStockLinkFeed(){
                if(stockLinkFeed!=null && ("on").equalsIgnoreCase(stockLinkFeed)){
                        stockLinkedFeed = true;
                }
        }
        
        public String getCatalogItemIdDelete() {
                return catalogItemIdDelete;
        }

        public void setCatalogItemIdDelete(String catalogItemIdDelete) {
                this.catalogItemIdDelete = catalogItemIdDelete;
        }

        public String getDeleteFeedResult() {
                return deleteFeedResult;
        }

        public void setDeleteFeedResult(String deleteFeedResult) {
                this.deleteFeedResult = deleteFeedResult;
        }

        public String getCataLogItemId() {
                return cataLogItemId;
        }

        public void setCataLogItemId(String cataLogItemId) {
                this.cataLogItemId = cataLogItemId;
        }
        
        public String getSentFeedResult() {
                return sentFeedResult;
        }

        public void setSentFeedResult(String sentFeedResult) {
                this.sentFeedResult = sentFeedResult;
        }

        public String getStockLinkFeed() {
                return stockLinkFeed;
        }

        public void setStockLinkFeed(String stockLinkFeed) {
                this.stockLinkFeed = stockLinkFeed;
        }
}