Advertisement
Guest User

Untitled

a guest
Nov 14th, 2018
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.22 KB | None | 0 0
  1. package com.amfine.mdi.web.export;
  2.  
  3. import java.io.File;
  4. import java.io.FileInputStream;
  5. import java.io.FileWriter;
  6. import java.io.IOException;
  7. import java.io.InputStream;
  8. import java.time.LocalDateTime;
  9. import java.util.List;
  10.  
  11. import javax.persistence.EntityManager;
  12. import javax.persistence.EntityManagerFactory;
  13. import javax.persistence.EntityTransaction;
  14. import javax.persistence.FlushModeType;
  15. import javax.persistence.NoResultException;
  16. import javax.persistence.TypedQuery;
  17.  
  18. import org.mdi.common.enums.PriceSource;
  19. import org.slf4j.Logger;
  20. import org.slf4j.LoggerFactory;
  21.  
  22. import com.opencsv.CSVWriter;
  23.  
  24. import com.amfine.mdi.domain.HistoricalPrice;
  25. import com.amfine.mdi.service.HistoricalPriceService;
  26.  
  27. public class BloombergExport {
  28.  
  29.   private final char             cellSeparator;
  30.  
  31.   private final char             quoteChar;
  32.  
  33.   private HistoricalPriceService service;
  34.  
  35.   private static Logger          logger  = LoggerFactory.getLogger(BloombergExport.class);
  36.  
  37.   private EntityManagerFactory   emFactory;
  38.  
  39.   private final String[]         headers = new String[] {
  40.     "instrumentId",
  41.     "date",
  42.     "instrumentData",
  43.     "instrumentDividend",
  44.     "currency"
  45.   };
  46.  
  47.   public BloombergExport(
  48.   final char cellSeparator,
  49.   final char quoteChar,
  50.   final HistoricalPriceService service,
  51.   final EntityManagerFactory emFactory) {
  52.     this.cellSeparator = cellSeparator;
  53.     this.quoteChar = quoteChar;
  54.     this.service = service;
  55.     this.emFactory = emFactory;
  56.   }
  57.  
  58.   public InputStream exportToStream(Long length, LocalDateTime sincePriceDate) throws IOException {
  59.  
  60.     File tmpFile = File.createTempFile("bloomberg-temporary", ".tmp");
  61.     CSVWriter writer = new CSVWriter(new FileWriter(tmpFile), cellSeparator, quoteChar, '\'', "\n");
  62.     writer.writeNext(headers);
  63.  
  64.     int lineCount = 0;
  65.     String[] line;
  66.     int col = 0;
  67.  
  68.     final String request = "select p from HistoricalPrice p where p.source = :priceSource AND p.writeDate >= :priceDate order by p.id";
  69.     final int pageSize = 2;
  70.     EntityManager eMgr = emFactory.createEntityManager();
  71.  
  72.     eMgr.setFlushMode(FlushModeType.COMMIT);
  73.     EntityTransaction tx = eMgr.getTransaction();
  74.     TypedQuery<HistoricalPrice> tq = eMgr.createQuery(request, HistoricalPrice.class);
  75.     tq.setMaxResults(pageSize);
  76.     tq.setParameter("priceSource", PriceSource.BLOOMBERG);
  77.     tq.setParameter("priceDate", sincePriceDate);
  78.     int startAt = 0;
  79.     List<HistoricalPrice> pagedResults = null;
  80.     do {
  81.       tx.begin();
  82.       tq.setFirstResult(startAt);
  83.       pagedResults = tq.getResultList();
  84.       try {
  85.         System.out.println("startAt " + startAt);
  86.         for (HistoricalPrice price : pagedResults) {
  87.  
  88.           line = new String[headers.length];
  89.           col = 0;
  90.           line[col++] = price.getInstrument().getInstrumentId();
  91.           line[col++] = price.getPriceDate().toString();
  92.           line[col++] = price.getClosePrice().toString();
  93.           // in bloomberg case, we never export dividends so we skip the field..
  94.           col++;
  95.           if (price.getCurrency() != null) {
  96.             line[col++] = price.getCurrency().toString();
  97.           } else {
  98.             line[col++] = null;
  99.           }
  100.           writer.writeNext(line);
  101.           lineCount++;
  102.           if (lineCount % pageSize == 0) {
  103.             logger.info("Exported {} Bloomberg prices from EPR.", lineCount);
  104.             writer.flush();
  105.           }
  106.           // System.out.println(price.getId() + "-" + price.getSource());
  107.         }
  108.       } catch (NoResultException e) {
  109.         pagedResults = null;
  110.       }
  111.       eMgr.flush();
  112.       eMgr.clear();
  113.       tx.rollback();
  114.       startAt += pageSize;
  115.     } while ((pagedResults != null) && (!pagedResults.isEmpty()));
  116.     eMgr.close();
  117.  
  118.     writer.close();
  119.     InputStream targetStream = new FileInputStream(tmpFile);
  120.     length = tmpFile.length();
  121.     return targetStream;
  122.  
  123.   }
  124.  
  125.   public InputStream exportToStreamWithoutEM(Long length, LocalDateTime sincePriceDate) throws IOException {
  126.  
  127.     File tmpFile = File.createTempFile("bloomberg-temporary", ".tmp");
  128.     CSVWriter writer = new CSVWriter(new FileWriter(tmpFile), cellSeparator, quoteChar, '\'', "\n");
  129.     writer.writeNext(headers);
  130.  
  131.     List<HistoricalPrice> prices;
  132.     prices = service.findBySourceSince(PriceSource.BLOOMBERG, sincePriceDate);
  133.  
  134.     int lineCount = 0;
  135.     String[] line;
  136.     int col = 0;
  137.  
  138.     for (HistoricalPrice p : prices) {
  139.       line = new String[headers.length];
  140.       col = 0;
  141.       line[col++] = p.getInstrument().getInstrumentId();
  142.       line[col++] = p.getPriceDate().toString();
  143.       line[col++] = p.getClosePrice().toString();
  144.       // in bloomberg case, we never export dividends so we skip the field..
  145.       col++;
  146.       if (p.getCurrency() != null) {
  147.         line[col++] = p.getCurrency().toString();
  148.       } else {
  149.         line[col++] = null;
  150.       }
  151.       writer.writeNext(line);
  152.       lineCount++;
  153.       if (lineCount % 100 == 0) {
  154.         logger.info("Exported {} Bloomberg prices from EPR.", lineCount);
  155.         writer.flush();
  156.       }
  157.     }
  158.  
  159.     writer.close();
  160.     InputStream targetStream = new FileInputStream(tmpFile);
  161.     length = tmpFile.length();
  162.     return targetStream;
  163.   }
  164.  
  165. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement