/** * Produces a ListMap based on the given list of values. The key for * each entry is produced by applying the given keyMapper function. * * @param values the values of the map. * @param keyMapper the function producing the key for each entry. * @return a ListMap. */ public static <T, V> ListMap<T, V> getListMap( List<V> values, Function<V, T> keyMapper ) { ListMap<T, V> map = new ListMap<>(); for ( V value : values ) { T key = keyMapper.apply( value ); map.putValue( key, value ); } return map; }
public boolean containsValue( T key, V value ) { List<V> list = this.get( key ); if ( list == null ) { return false; } if ( list.contains( value ) ) { return true; } return false; }
@Override public List<Period> getDuplicatePeriods() { Collection<Period> periods = periodService.getAllPeriods(); List<Period> duplicates = new ArrayList<>(); ListMap<String, Period> map = new ListMap<>(); for ( Period period : periods ) { String key = period.getPeriodType().getName() + period.getStartDate().toString(); period.setName( period.toString() ); map.putValue( key, period ); } for ( String key : map.keySet() ) { List<Period> values = map.get( key ); if ( values != null && values.size() > 1 ) { duplicates.addAll( values ); } } return duplicates; }
/** * Groups queries by their data type. * * @param params the data query parameters. * @return a list of {@link DataQueryParams}. */ private List<DataQueryParams> groupByDataType( DataQueryParams params ) { List<DataQueryParams> queries = new ArrayList<>(); if ( !params.getDataElements().isEmpty() ) { ListMap<DataType, DimensionalItemObject> dataTypeDataElementMap = QueryPlannerUtils.getDataTypeDataElementMap( params.getDataElements() ); for ( DataType dataType : dataTypeDataElementMap.keySet() ) { DataQueryParams query = DataQueryParams.newBuilder( params ) .withDataElements( dataTypeDataElementMap.get( dataType ) ) .withDataType( dataType ).build(); queries.add( query ); } } else { DataQueryParams query = DataQueryParams.newBuilder( params ) .withDataType( DataType.NUMERIC ).build(); queries.add( query ); } logQuerySplit( queries, "data type" ); return queries; }
PeriodType periodType = predictor.getPeriodType(); ListMap<Period, Period> samplePeriodsMap = new ListMap<Period, Period>(); samplePeriodsMap.put( outputPeriod, new ArrayList<Period>() ); samplePeriodsMap.putValue( outputPeriod, p ); Period pNext = pPrev; samplePeriodsMap.putValue( outputPeriod, pPrev ); pNext = periodType.getNextPeriod( pNext ); samplePeriodsMap.putValue( outputPeriod, pPrev ); samplePeriodsMap.putValue( outputPeriod, pNext );
public List<V> putValues( T key, Collection<V> values ) { for ( V value : values ) { putValue( key, value ); } return null; }
Set<Period> existingOutputPeriods = getExistingPeriods( outputPeriods ); ListMap<Period, Period> samplePeriodsMap = getSamplePeriodsMap( outputPeriods, predictor ); Set<Period> allSamplePeriods = samplePeriodsMap.uniqueValues(); Set<Period> existingSamplePeriods = getExistingPeriods( allSamplePeriods ); Set<DimensionalItemObject> aggregateDimensionItems = getDimensionItems( aggregates, skipTest ); aggregates, samplePeriodsMap.get( period ), constantMap, generator.getMissingValueStrategy() ); ListMap<String, Double> aggregateValueMap = ListMap.union( aggregateSampleMap.get( aoc ), aggregateSampleMapNonAoc );
@Override public void replaceDataPeriodsWithAggregationPeriods( Map<String, Object> dataValueMap, DataQueryParams params, ListMap<DimensionalItemObject, DimensionalItemObject> dataPeriodAggregationPeriodMap ) { if ( params.isDisaggregation() ) { int periodIndex = params.getPeriodDimensionIndex(); if ( periodIndex == -1 ) { return; // Period is filter, nothing to replace } Set<String> keys = new HashSet<>( dataValueMap.keySet() ); for ( String key : keys ) { String[] keyArray = key.split( DIMENSION_SEP ); String periodKey = keyArray[periodIndex]; Assert.notNull( periodKey, String.format( "Period key cannot be null, key: '%s'", key ) ); List<DimensionalItemObject> periods = dataPeriodAggregationPeriodMap.get( PeriodType.getPeriodFromIsoString( periodKey ) ); Assert.notNull( periods, String.format( "Period list cannot be null, key: '%s', map: '%s'", key, dataPeriodAggregationPeriodMap.toString() ) ); Object value = dataValueMap.get( key ); for ( DimensionalItemObject period : periods ) { String[] keyCopy = keyArray.clone(); keyCopy[periodIndex] = ((Period) period).getIsoDate(); dataValueMap.put( TextUtils.toString( keyCopy, DIMENSION_SEP ), value ); } dataValueMap.remove( key ); } } }
/** * Replaces the periods of this query with the corresponding data periods. * Sets the period type to the data period type. This method is relevant only * when then the data period type has lower frequency than the aggregation * period type. This is valid because disaggregation is allowed for data * with average aggregation operator. */ private void replaceAggregationPeriodsWithDataPeriods( ListMap<DimensionalItemObject, DimensionalItemObject> dataPeriodAggregationPeriodMap ) { this.periodType = this.dataPeriodType.getName(); if ( !getPeriods().isEmpty() ) // Period is dimension { setDimensionOptions( PERIOD_DIM_ID, DimensionType.PERIOD, dataPeriodType.getName().toLowerCase(), new ArrayList<>( dataPeriodAggregationPeriodMap.keySet() ) ); } else // Period is filter { setFilterOptions( PERIOD_DIM_ID, DimensionType.PERIOD, dataPeriodType.getName().toLowerCase(), new ArrayList<>( dataPeriodAggregationPeriodMap.keySet() ) ); } }
public Set<V> uniqueValues( ) { Set<V> results = new HashSet<>(); for ( Map.Entry<T, List<V>> entry: entrySet() ) { results.addAll( entry.getValue() ); } return results; }
@Override public List<Grid> getDefaultDataSetReport( DataSet dataSet, Period period, OrganisationUnit unit, Set<String> dimensions, boolean selectedUnitOnly, I18nFormat format, I18n i18n ) { ListMap<CategoryCombo, DataElement> map = new ListMap<>(); for ( DataSetElement element : dataSet.getDataSetElements() ) { map.putValue( element.getResolvedCategoryCombo(), element.getDataElement() ); } DataSet tmpDataSet = new DataSet( dataSet.getName(), dataSet.getShortName(), dataSet.getPeriodType() ); tmpDataSet.setDataSetElements( dataSet.getDataSetElements() ); for ( CategoryCombo categoryCombo : map.keySet() ) { List<DataElement> dataElements = map.get( categoryCombo ); String name = categoryCombo.isDefault() ? dataSet.getName() : categoryCombo.getName(); Section section = new Section( name, dataSet, dataElements, null ); tmpDataSet.getSections().add( section ); } return getSectionDataSetReport( tmpDataSet, period, unit, dimensions, selectedUnitOnly, format, i18n ); }
for ( String periodType : periodTypePeriodMap.keySet() ) .addOrSetDimensionOptions( PERIOD_DIM_ID, DimensionType.PERIOD, periodType.toLowerCase(), periodTypePeriodMap.get( periodType ) ) .withPeriodType( periodType ).build(); .withPeriodType( periodTypePeriodMap.keySet().iterator().next() ); // Using first period type for ( String periodType : periodTypePeriodMap.keySet() ) periodType.toLowerCase(), filter.getDisplayName(), periodTypePeriodMap.get( periodType ) ) );
public void putValueMap( Map<T, V> map ) { for ( Map.Entry<T, V> entry : map.entrySet() ) { putValue( entry.getKey(), entry.getValue() ); } }
public Collection<V> allValues() { Collection<V> results = new ArrayList<>(); for ( Map.Entry<T, List<V>> entry: entrySet() ) { results.addAll( entry.getValue() ); } return results; }
QueryPlannerUtils.getLevelOrgUnitMap( params.getOrganisationUnits() ); for ( Integer level : levelOrgUnitMap.keySet() ) .addOrSetDimensionOptions( ORGUNIT_DIM_ID, DimensionType.ORGANISATION_UNIT, LEVEL_PREFIX + level, levelOrgUnitMap.get( level ) ).build(); .removeFilter( ORGUNIT_DIM_ID ); for ( Integer level : levelOrgUnitMap.keySet() ) filter.getDimensionType(), LEVEL_PREFIX + level, filter.getDisplayName(), levelOrgUnitMap.get( level ) ) );
public List<V> putValue( T key, V value ) { List<V> list = this.get( key ); list = list == null ? new ArrayList<>() : list; list.add( value ); super.put( key, list ); return null; }
/** * Returns a mapping from data approval key to data approval status for the given * list of data approvals. */ private Map<String, DataApprovalStatus> getStatusMap( List<DataApproval> dataApprovalList ) { Map<String, DataApprovalStatus> statusMap = new HashMap<>(); DataApprovalPermissionsEvaluator evaluator = makePermissionsEvaluator(); ListMap<String, DataApproval> listMap = getIndexedListMap( dataApprovalList ); for ( Map.Entry<String, List<DataApproval>> entry : listMap.entrySet() ) { List<DataApproval> dataApprovals = entry.getValue(); Set<OrganisationUnit> orgUnits = dataApprovals.stream().map( DataApproval::getOrganisationUnit ).collect( Collectors.toSet() ); DataApproval da = dataApprovals.get( 0 ); List<DataApprovalStatus> statuses = dataApprovalStore.getDataApprovalStatuses( da.getWorkflow(), da.getPeriod(), orgUnits, da.getOrganisationUnit().getHierarchyLevel(), null, getCategoryOptionCombos( dataApprovals ) ); for ( DataApprovalStatus status : statuses ) { evaluator.evaluatePermissions( status, da.getWorkflow() ); statusMap.put( daKey( da, status.getOrganisationUnitUid(), status.getAttributeOptionComboUid() ), status ); } } return statusMap; }