String replaceWithProvider = getCommandLineOption(commandLine, REPLACE_WITH_PROVIDER_OPTION);
HistoricalTimeSeriesMaster htsMaster = getToolContext().getHistoricalTimeSeriesMaster();
HistoricalTimeSeriesProvider htsProvider = getToolContext().getHistoricalTimeSeriesProvider();
BloombergHistoricalTimeSeriesLoader loader = new BloombergHistoricalTimeSeriesLoader(htsMaster, htsProvider,
new BloombergIdentifierProvider(getToolContext().getBloombergReferenceDataProvider()));
for (HistoricalTimeSeriesInfoDocument infoDoc : HistoricalTimeSeriesInfoSearchIterator.iterable(htsMaster, getHistoricalSearchRequest(findProvider))) {
ExternalIdBundle bundle = infoDoc.getInfo().getExternalIdBundle().toBundle();
ExternalId buid = bundle.getExternalId(ExternalSchemes.BLOOMBERG_BUID);
ExternalId ticker = bundle.getExternalId(ExternalSchemes.BLOOMBERG_TICKER);
String dataField = infoDoc.getInfo().getDataField();
if (buid != null) {
s_logger.info("replacing {} {}", buid, ticker);
try {
LocalDateDoubleTimeSeries hts = htsProvider.getHistoricalTimeSeries(ExternalIdBundle.of(buid),
BLOOMBERG_DATA_SOURCE_NAME, replaceWithProvider, dataField);
if (hts != null) {
Map<ExternalId, UniqueId> addedTS = loader.loadTimeSeries(Sets.newHashSet(buid), replaceWithProvider, dataField, null, null);
if (addedTS.get(buid) != null) {
htsMaster.remove(infoDoc.getUniqueId());
s_logger.info("removed TS with buid={}, ticker={}, dataProvider={}, dataField={}", new Object[] {buid, ticker, findProvider, dataField});
}
}