FieldMappingHistoricalTimeSeriesResolver extends DefaultHistoricalTimeSeriesResolver { @Override public HistoricalTimeSeriesResolutionResult resolve(ExternalIdBundle identifierBundle, LocalDate identifierValidityDate, String dataSource, String dataProvider, String dataField, String resolutionKey) { ArgumentChecker.notNull(dataField, "dataField"); Map<String, HistoricalTimeSeriesFieldAdjustment> fieldMappings = getFieldAdjustments(dataSource, dataField); if (fieldMappings.size() == 1) { Map.Entry<String, HistoricalTimeSeriesFieldAdjustment> fieldMappingEntry = Iterables.getOnlyElement(fieldMappings.entrySet()); dataSource = fieldMappingEntry.getKey(); dataProvider = fieldMappingEntry.getValue().getUnderlyingDataProvider(); dataField = fieldMappingEntry.getValue().getUnderlyingDataField(); } else if (fieldMappings.size() > 1) { dataField = null; dataProvider = null; } if (identifierBundle != null) { Collection<ManageableHistoricalTimeSeriesInfo> timeSeriesCandidates = search(identifierBundle, identifierValidityDate, dataSource, dataProvider, dataField); if (!fieldMappings.isEmpty()) { Iterator<ManageableHistoricalTimeSeriesInfo> it = timeSeriesCandidates.iterator(); while (it.hasNext()) { ManageableHistoricalTimeSeriesInfo candidate = it.next(); HistoricalTimeSeriesFieldAdjustment fieldAdjustment = fieldMappings.get(candidate.getDataSource()); if (fieldAdjustment == null || ((fieldAdjustment.getUnderlyingDataProvider() != null && !fieldAdjustment.getUnderlyingDataProvider().equals(candidate.getDataProvider())) || !fieldAdjustment.getUnderlyingDataField().equals(candidate.getDataField()))) { it.remove(); } } } ManageableHistoricalTimeSeriesInfo selectedResult = select(timeSeriesCandidates, resolutionKey); if (selectedResult == null) { s_logger.debug("Resolver failed to find any time-series for {} using {}/{}", new Object[] {identifierBundle, dataField, resolutionKey }); return null; } HistoricalTimeSeriesFieldAdjustment fieldAdjustment = fieldMappings.get(selectedResult.getDataSource()); HistoricalTimeSeriesAdjuster adjuster = fieldAdjustment != null ? fieldAdjustment.getAdjuster() : null; return new HistoricalTimeSeriesResolutionResult(selectedResult, adjuster); } else { return search(dataSource, dataProvider, dataField); } } FieldMappingHistoricalTimeSeriesResolver(Collection<HistoricalTimeSeriesFieldAdjustmentMap> fieldMaps, HistoricalTimeSeriesSelector selector, HistoricalTimeSeriesMaster master); @Override HistoricalTimeSeriesResolutionResult resolve(ExternalIdBundle identifierBundle, LocalDate identifierValidityDate, String dataSource, String dataProvider, String dataField,
      String resolutionKey); Collection<HistoricalTimeSeriesFieldAdjustmentMap> getFieldMaps();  }
@Test public void testNoMaps() { FieldMappingHistoricalTimeSeriesResolver resolver = new FieldMappingHistoricalTimeSeriesResolver(ImmutableList.<HistoricalTimeSeriesFieldAdjustmentMap>of(), _selector, _htsMaster); for (ExternalIdBundleWithDates identifierBundleWithDates : _identifiers) { resolver.resolve(identifierBundleWithDates.toBundle(), null, null, null, "PX_LAST", null); assertEquals(9, _selector.getLastCandidates().size()); resolver.resolve(identifierBundleWithDates.toBundle(), null, null, null, "Something else", null); assertEquals(0, _selector.getLastCandidates().size()); } }
@Test public void testMappings() { String syntheticFieldName = "Synthetic"; HistoricalTimeSeriesFieldAdjustmentMap map1 = new HistoricalTimeSeriesFieldAdjustmentMap("BLOOMBERG"); map1.addFieldAdjustment(syntheticFieldName, "CMPL", "PX_LAST", null); HistoricalTimeSeriesFieldAdjustmentMap map2 = new HistoricalTimeSeriesFieldAdjustmentMap("REUTERS"); HistoricalTimeSeriesAdjuster reutersAdjuster = mock(HistoricalTimeSeriesAdjuster.class); map2.addFieldAdjustment(syntheticFieldName, null, "VOLUME", reutersAdjuster); FieldMappingHistoricalTimeSeriesResolver resolver = new FieldMappingHistoricalTimeSeriesResolver(ImmutableList.<HistoricalTimeSeriesFieldAdjustmentMap>of(map1, map2), _selector, _htsMaster); for (ExternalIdBundleWithDates identifierBundleWithDates : _identifiers) { resolver.resolve(identifierBundleWithDates.toBundle(), null, null, null, "PX_LAST", null); assertEquals(9, _selector.getLastCandidates().size()); resolver.resolve(identifierBundleWithDates.toBundle(), null, null, null, "Something else", null); assertEquals(0, _selector.getLastCandidates().size()); resolver.resolve(identifierBundleWithDates.toBundle(), null, null, null, syntheticFieldName, null); Set<Triple<String, String, String>> candidateInfo = getCandidateInfo(_selector.getLastCandidates()); assertEquals(candidateInfo.toString(), 4, candidateInfo.size()); assertTrue(candidateInfo.contains(Triple.of("BLOOMBERG", "CMPL", "PX_LAST"))); assertTrue(candidateInfo.contains(Triple.of("REUTERS", "UNKNOWN", "VOLUME"))); assertTrue(candidateInfo.contains(Triple.of("REUTERS", "CMPL", "VOLUME"))); assertTrue(candidateInfo.contains(Triple.of("REUTERS", "CMPT", "VOLUME"))); HistoricalTimeSeriesResolutionResult resolutionResult = resolver.resolve(identifierBundleWithDates.toBundle(), null, "REUTERS", "Ignored for mapped fields", syntheticFieldName, null); assertEquals(3, _selector.getLastCandidates().size()); assertEquals(reutersAdjuster, resolutionResult.getAdjuster()); } }