Rev 9855 | Blame | Compare with Previous | Last modification | View Log | RSS feed
package in.shop2020.inventory.controllers;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;import com.google.api.services.content.handler.ContentFeedsHandler;import com.google.api.services.content.model.Product;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 blocke.printStackTrace();}try {hwb.write(fileOut);} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}try {fileOut.close();} catch (IOException e) {// TODO Auto-generated catch blocke.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));ContentFeedsHandler sample = new ContentFeedsHandler();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.getDescription()!=null && insertedProduct.getDescription().indexOf("Error:")>-1){sentFeedResult = insertedProduct.getDescription();return "output";}sentFeedResult = "Product Feeds are sent Successfully and Product Details are Given Below:\nProduct Id- "+insertedProduct.getId()+" Product Title- "+insertedProduct.getTitle();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){ContentFeedsHandler sample = new ContentFeedsHandler();boolean deleteFeedResultBool = sample.deleteProductFeed(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;}}