Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.amfine.mdi.web.export;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileWriter;
- import java.io.IOException;
- import java.io.InputStream;
- import java.time.LocalDateTime;
- import java.util.List;
- import javax.persistence.EntityManager;
- import javax.persistence.EntityManagerFactory;
- import javax.persistence.EntityTransaction;
- import javax.persistence.FlushModeType;
- import javax.persistence.NoResultException;
- import javax.persistence.TypedQuery;
- import org.mdi.common.enums.PriceSource;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- import com.opencsv.CSVWriter;
- import com.amfine.mdi.domain.HistoricalPrice;
- import com.amfine.mdi.service.HistoricalPriceService;
- public class BloombergExport {
- private final char cellSeparator;
- private final char quoteChar;
- private HistoricalPriceService service;
- private static Logger logger = LoggerFactory.getLogger(BloombergExport.class);
- private EntityManagerFactory emFactory;
- private final String[] headers = new String[] {
- "instrumentId",
- "date",
- "instrumentData",
- "instrumentDividend",
- "currency"
- };
- public BloombergExport(
- final char cellSeparator,
- final char quoteChar,
- final HistoricalPriceService service,
- final EntityManagerFactory emFactory) {
- this.cellSeparator = cellSeparator;
- this.quoteChar = quoteChar;
- this.service = service;
- this.emFactory = emFactory;
- }
- public InputStream exportToStream(Long length, LocalDateTime sincePriceDate) throws IOException {
- File tmpFile = File.createTempFile("bloomberg-temporary", ".tmp");
- CSVWriter writer = new CSVWriter(new FileWriter(tmpFile), cellSeparator, quoteChar, '\'', "\n");
- writer.writeNext(headers);
- int lineCount = 0;
- String[] line;
- int col = 0;
- final String request = "select p from HistoricalPrice p where p.source = :priceSource AND p.writeDate >= :priceDate order by p.id";
- final int pageSize = 2;
- EntityManager eMgr = emFactory.createEntityManager();
- eMgr.setFlushMode(FlushModeType.COMMIT);
- EntityTransaction tx = eMgr.getTransaction();
- TypedQuery<HistoricalPrice> tq = eMgr.createQuery(request, HistoricalPrice.class);
- tq.setMaxResults(pageSize);
- tq.setParameter("priceSource", PriceSource.BLOOMBERG);
- tq.setParameter("priceDate", sincePriceDate);
- int startAt = 0;
- List<HistoricalPrice> pagedResults = null;
- do {
- tx.begin();
- tq.setFirstResult(startAt);
- pagedResults = tq.getResultList();
- try {
- System.out.println("startAt " + startAt);
- for (HistoricalPrice price : pagedResults) {
- line = new String[headers.length];
- col = 0;
- line[col++] = price.getInstrument().getInstrumentId();
- line[col++] = price.getPriceDate().toString();
- line[col++] = price.getClosePrice().toString();
- // in bloomberg case, we never export dividends so we skip the field..
- col++;
- if (price.getCurrency() != null) {
- line[col++] = price.getCurrency().toString();
- } else {
- line[col++] = null;
- }
- writer.writeNext(line);
- lineCount++;
- if (lineCount % pageSize == 0) {
- logger.info("Exported {} Bloomberg prices from EPR.", lineCount);
- writer.flush();
- }
- // System.out.println(price.getId() + "-" + price.getSource());
- }
- } catch (NoResultException e) {
- pagedResults = null;
- }
- eMgr.flush();
- eMgr.clear();
- tx.rollback();
- startAt += pageSize;
- } while ((pagedResults != null) && (!pagedResults.isEmpty()));
- eMgr.close();
- writer.close();
- InputStream targetStream = new FileInputStream(tmpFile);
- length = tmpFile.length();
- return targetStream;
- }
- public InputStream exportToStreamWithoutEM(Long length, LocalDateTime sincePriceDate) throws IOException {
- File tmpFile = File.createTempFile("bloomberg-temporary", ".tmp");
- CSVWriter writer = new CSVWriter(new FileWriter(tmpFile), cellSeparator, quoteChar, '\'', "\n");
- writer.writeNext(headers);
- List<HistoricalPrice> prices;
- prices = service.findBySourceSince(PriceSource.BLOOMBERG, sincePriceDate);
- int lineCount = 0;
- String[] line;
- int col = 0;
- for (HistoricalPrice p : prices) {
- line = new String[headers.length];
- col = 0;
- line[col++] = p.getInstrument().getInstrumentId();
- line[col++] = p.getPriceDate().toString();
- line[col++] = p.getClosePrice().toString();
- // in bloomberg case, we never export dividends so we skip the field..
- col++;
- if (p.getCurrency() != null) {
- line[col++] = p.getCurrency().toString();
- } else {
- line[col++] = null;
- }
- writer.writeNext(line);
- lineCount++;
- if (lineCount % 100 == 0) {
- logger.info("Exported {} Bloomberg prices from EPR.", lineCount);
- writer.flush();
- }
- }
- writer.close();
- InputStream targetStream = new FileInputStream(tmpFile);
- length = tmpFile.length();
- return targetStream;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement