CommandLine commandLine = getCommandLine();
String findProvider = getCommandLineOption(commandLine, FIND_PROVIDER_OPTION);
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());