Advertisement
Guest User

Untitled

a guest
Nov 14th, 2018
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.81 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 exportToStreamWithoutEM(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.     List<HistoricalPrice> prices;
  65.     prices = service.findBySourceSince(PriceSource.BLOOMBERG, sincePriceDate);
  66.  
  67.     int lineCount = 0;
  68.     String[] line;
  69.     int col = 0;
  70.  
  71.     for (HistoricalPrice p : prices) {
  72.       line = new String[headers.length];
  73.       col = 0;
  74.       line[col++] = p.getInstrument().getInstrumentId();
  75.       line[col++] = p.getPriceDate().toString();
  76.       line[col++] = p.getClosePrice().toString();
  77.       // in bloomberg case, we never export dividends so we skip the field..
  78.       col++;
  79.       if (p.getCurrency() != null) {
  80.         line[col++] = p.getCurrency().toString();
  81.       } else {
  82.         line[col++] = null;
  83.       }
  84.       writer.writeNext(line);
  85.       lineCount++;
  86.       if (lineCount % 100 == 0) {
  87.         logger.info("Exported {} Bloomberg prices from EPR.", lineCount);
  88.         writer.flush();
  89.       }
  90.     }
  91.  
  92.     writer.close();
  93.     InputStream targetStream = new FileInputStream(tmpFile);
  94.     length = tmpFile.length();
  95.     return targetStream;
  96.   }
  97.  
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement