use of org.hisp.dhis.period.PeriodType in project dhis2-core by dhis2.
the class DataElement method getLatestOpenFuturePeriod.
/**
* Returns the latest period which is open for data input. Returns null if
* data element is not associated with any data sets.
*
* @return the latest period which is open for data input.
*/
public Period getLatestOpenFuturePeriod() {
int periods = getOpenFuturePeriods();
PeriodType periodType = getPeriodType();
if (periodType != null) {
Period period = periodType.createPeriod();
// Rewind one as 0 open periods implies current period is locked
period = periodType.getPreviousPeriod(period);
return periodType.getNextPeriod(period, periods);
}
return null;
}
use of org.hisp.dhis.period.PeriodType in project dhis2-core by dhis2.
the class DefaultAnalyticsService method addReportingRates.
/**
* Adds reporting rates to the given grid based on the given data query
* parameters and reporting rate metric.
*
* @param params the {@link DataQueryParams}.
* @param grid the grid.
* @param metric the reporting rate metric.
*/
private void addReportingRates(DataQueryParams params, Grid grid, ReportingRateMetric metric) {
if (!params.getReportingRates().isEmpty() && !params.isSkipData()) {
if (!COMPLETENESS_DIMENSION_TYPES.containsAll(params.getDimensionTypes())) {
return;
}
DataQueryParams targetParams = DataQueryParams.newBuilder(params).withSkipPartitioning(true).withTimely(false).withRestrictByOrgUnitOpeningClosedDate(true).withRestrictByCategoryOptionStartEndDate(true).withAggregationType(AggregationType.SUM).build();
Map<String, Double> targetMap = getAggregatedCompletenessTargetMap(targetParams);
Map<String, Double> dataMap = metric != EXPECTED_REPORTS ? getAggregatedCompletenessValueMap(params) : new HashMap<>();
Integer periodIndex = params.getPeriodDimensionIndex();
Integer dataSetIndex = DataQueryParams.DX_INDEX;
Map<String, PeriodType> dsPtMap = params.getDataSetPeriodTypeMap();
PeriodType filterPeriodType = params.getFilterPeriodType();
for (Map.Entry<String, Double> entry : targetMap.entrySet()) {
List<String> dataRow = Lists.newArrayList(entry.getKey().split(DIMENSION_SEP));
Double target = entry.getValue();
Double actual = dataMap.get(entry.getKey());
if (target != null && (actual != null || metric == EXPECTED_REPORTS)) {
// ---------------------------------------------------------
// Multiply target value by number of periods in time span
// ---------------------------------------------------------
PeriodType queryPt = filterPeriodType != null ? filterPeriodType : getPeriodTypeFromIsoString(dataRow.get(periodIndex));
PeriodType dataSetPt = dsPtMap.get(dataRow.get(dataSetIndex));
target = target * queryPt.getPeriodSpan(dataSetPt);
// ---------------------------------------------------------
// Calculate reporting rate and replace data set with rate
// ---------------------------------------------------------
Double value = 0d;
if (EXPECTED_REPORTS == metric) {
value = target;
} else if (ACTUAL_REPORTS == metric || ACTUAL_REPORTS_ON_TIME == metric) {
value = actual;
} else if (// REPORTING_RATE or REPORTING_RATE_ON_TIME
!MathUtils.isZero(target)) {
value = (actual * PERCENT) / target;
}
String reportingRate = DimensionalObjectUtils.getDimensionItem(dataRow.get(DX_INDEX), metric);
dataRow.set(DX_INDEX, reportingRate);
grid.addRow().addValues(dataRow.toArray()).addValue(params.isSkipRounding() ? value : MathUtils.getRounded(value));
if (params.isIncludeNumDen()) {
grid.addValue(actual).addValue(target).addValue(PERCENT);
}
}
}
}
}
use of org.hisp.dhis.period.PeriodType in project dhis2-core by dhis2.
the class PeriodResourceTable method getPopulateTempTableContent.
@Override
public Optional<List<Object[]>> getPopulateTempTableContent() {
Calendar calendar = PeriodType.getCalendar();
List<Object[]> batchArgs = new ArrayList<>();
Set<String> uniqueIsoDates = new HashSet<>();
for (Period period : objects) {
if (period != null && period.isValid()) {
final PeriodType rowType = period.getPeriodType();
final String isoDate = period.getIsoDate();
if (!uniqueIsoDates.add(isoDate)) {
log.warn("Duplicate ISO date for period, ignoring: " + period + ", ISO date: " + isoDate);
continue;
}
List<Object> values = new ArrayList<>();
values.add(period.getId());
values.add(isoDate);
values.add(period.getDaysInPeriod());
values.add(period.getStartDate());
values.add(period.getEndDate());
for (PeriodType periodType : PeriodType.PERIOD_TYPES) {
if (rowType.getFrequencyOrder() < periodType.getFrequencyOrder() || rowType.equals(periodType)) {
Period targetPeriod = IdentifiableObjectUtils.getPeriodByPeriodType(period, periodType, calendar);
values.add(IdentifiableObjectUtils.getLocalPeriodIdentifier(targetPeriod, calendar));
} else {
values.add(null);
}
}
batchArgs.add(values.toArray());
}
}
return Optional.of(batchArgs);
}
use of org.hisp.dhis.period.PeriodType in project dhis2-core by dhis2.
the class PeriodResourceTable method getCreateTempTableStatement.
@Override
public String getCreateTempTableStatement() {
String sql = "CREATE TABLE " + getTempTableName() + " (periodid INTEGER NOT NULL PRIMARY KEY, iso VARCHAR(15) NOT NULL, daysno INTEGER NOT NULL, startdate DATE NOT NULL, enddate DATE NOT NULL";
for (PeriodType periodType : PeriodType.PERIOD_TYPES) {
sql += ", " + columnQuote + periodType.getName().toLowerCase() + columnQuote + " VARCHAR(15)";
}
sql += ")";
return sql;
}
use of org.hisp.dhis.period.PeriodType in project dhis2-core by dhis2.
the class DataElementResourceTable method getPopulateTempTableContent.
@Override
public Optional<List<Object[]>> getPopulateTempTableContent() {
List<Object[]> batchArgs = new ArrayList<>();
for (DataElement dataElement : objects) {
List<Object> values = new ArrayList<>();
final DataSet dataSet = dataElement.getApprovalDataSet();
final PeriodType periodType = dataElement.getPeriodType();
// -----------------------------------------------------------------
// Use highest approval level if data set does not require approval,
// or null if approval is required.
// -----------------------------------------------------------------
values.add(dataElement.getId());
values.add(dataElement.getUid());
values.add(dataElement.getName());
values.add(dataSet != null ? dataSet.getId() : null);
values.add(dataSet != null ? dataSet.getUid() : null);
values.add(dataSet != null ? dataSet.getName() : null);
values.add(dataSet != null && dataSet.isApproveData() ? null : APPROVAL_LEVEL_HIGHEST);
values.add(dataSet != null && dataSet.isApproveData() ? dataSet.getWorkflow().getId() : null);
values.add(periodType != null ? periodType.getId() : null);
values.add(periodType != null ? periodType.getName() : null);
batchArgs.add(values.toArray());
}
return Optional.of(batchArgs);
}
Aggregations