use of org.hisp.dhis.common.GridHeader in project dhis2-core by dhis2.
the class ListGrid method addRegressionColumn.
@Override
public Grid addRegressionColumn(int columnIndex, boolean addHeader) {
verifyGridState();
SimpleRegression regression = new SimpleRegression();
List<Object> column = getColumn(columnIndex);
int index = 0;
for (Object value : column) {
if (value != null && !MathUtils.isEqual(Double.parseDouble(String.valueOf(value)), 0d)) {
regression.addData(index++, Double.parseDouble(String.valueOf(value)));
}
}
List<Object> regressionColumn = new ArrayList<>();
for (int i = 0; i < column.size(); i++) {
final double predicted = regression.predict(i);
if (!Double.isNaN(predicted)) {
regressionColumn.add(Precision.round(predicted, 1));
} else {
regressionColumn.add(null);
}
}
addColumn(regressionColumn);
if (addHeader && columnIndex < headers.size()) {
GridHeader header = headers.get(columnIndex);
if (header != null) {
GridHeader regressionHeader = new GridHeader(header.getName() + REGRESSION_SUFFIX, header.getColumn() + REGRESSION_SUFFIX, header.getValueType(), header.getType(), header.isHidden(), header.isMeta());
addHeader(regressionHeader);
}
}
return this;
}
use of org.hisp.dhis.common.GridHeader in project dhis2-core by dhis2.
the class ListGrid method addCumulativeColumn.
@Override
public Grid addCumulativeColumn(int columnIndex, boolean addHeader) {
verifyGridState();
List<Object> column = getColumn(columnIndex);
List<Object> cumulativeColumn = new ArrayList<>();
double sum = 0d;
for (Object value : column) {
double number = value != null ? Double.parseDouble(String.valueOf(value)) : 0d;
sum += number;
cumulativeColumn.add(sum);
}
addColumn(cumulativeColumn);
if (addHeader && columnIndex < headers.size()) {
GridHeader header = headers.get(columnIndex);
if (header != null) {
GridHeader regressionHeader = new GridHeader(header.getName() + CUMULATIVE_SUFFIX, header.getColumn() + CUMULATIVE_SUFFIX, header.getValueType(), header.getType(), header.isHidden(), header.isMeta());
addHeader(regressionHeader);
}
}
return this;
}
use of org.hisp.dhis.common.GridHeader in project dhis2-core by dhis2.
the class DefaultDataSetReportService method getSectionDataSetReport.
@Override
public List<Grid> getSectionDataSetReport(DataSet dataSet, Period period, OrganisationUnit unit, Set<String> dimensions, boolean selectedUnitOnly, I18nFormat format, I18n i18n) {
List<Section> sections = new ArrayList<>(dataSet.getSections());
Collections.sort(sections, new SectionOrderComparator());
Map<String, Object> valueMap = dataSetReportStore.getAggregatedValues(dataSet, period, unit, dimensions);
Map<String, Object> subTotalMap = dataSetReportStore.getAggregatedSubTotals(dataSet, period, unit, dimensions);
Map<String, Object> totalMap = dataSetReportStore.getAggregatedTotals(dataSet, period, unit, dimensions);
List<Grid> grids = new ArrayList<>();
for (Section section : sections) {
for (DataElementCategoryCombo categoryCombo : section.getCategoryCombos()) {
Grid grid = new ListGrid().setTitle(section.getName() + SPACE + categoryCombo.getName()).setSubtitle(unit.getName() + SPACE + format.formatPeriod(period));
// -----------------------------------------------------------------
// Grid headers
// -----------------------------------------------------------------
grid.addHeader(new GridHeader(i18n.getString("dataelement"), false, true));
List<DataElementCategoryOptionCombo> optionCombos = categoryCombo.getSortedOptionCombos();
for (DataElementCategoryOptionCombo optionCombo : optionCombos) {
grid.addHeader(new GridHeader(optionCombo.isDefault() ? DEFAULT_HEADER : optionCombo.getName(), false, false));
}
if (// Sub-total
categoryCombo.doSubTotals() && !selectedUnitOnly) {
for (DataElementCategoryOption categoryOption : categoryCombo.getCategoryOptions()) {
grid.addHeader(new GridHeader(categoryOption.getName(), false, false));
}
}
if (// Total
categoryCombo.doTotal() && !selectedUnitOnly) {
grid.addHeader(new GridHeader(TOTAL_HEADER, false, false));
}
// -----------------------------------------------------------------
// Grid values
// -----------------------------------------------------------------
List<DataElement> dataElements = new ArrayList<>(section.getDataElementsByCategoryCombo(categoryCombo));
FilterUtils.filter(dataElements, AggregatableDataElementFilter.INSTANCE);
for (DataElement dataElement : dataElements) {
grid.addRow();
// Data element name
grid.addValue(new GridValue(dataElement.getFormNameFallback()));
for (// Values
DataElementCategoryOptionCombo optionCombo : // Values
optionCombos) {
Map<Object, Object> attributes = new HashMap<>();
attributes.put(ATTR_DE, dataElement.getUid());
attributes.put(ATTR_CO, optionCombo.getUid());
Object value = null;
if (selectedUnitOnly) {
DataValue dataValue = dataValueService.getDataValue(dataElement, period, unit, optionCombo);
value = dataValue != null && dataValue.getValue() != null ? Double.parseDouble(dataValue.getValue()) : null;
} else {
value = valueMap.get(dataElement.getUid() + SEPARATOR + optionCombo.getUid());
}
grid.addValue(new GridValue(value, attributes));
}
if (// Sub-total
categoryCombo.doSubTotals() && !selectedUnitOnly) {
for (DataElementCategoryOption categoryOption : categoryCombo.getCategoryOptions()) {
Object value = subTotalMap.get(dataElement.getUid() + SEPARATOR + categoryOption.getUid());
grid.addValue(new GridValue(value));
}
}
if (// Total
categoryCombo.doTotal() && !selectedUnitOnly) {
Object value = totalMap.get(String.valueOf(dataElement.getUid()));
grid.addValue(new GridValue(value));
}
}
grids.add(grid);
}
}
return grids;
}
use of org.hisp.dhis.common.GridHeader in project dhis2-core by dhis2.
the class AbstractGridView method renderMergedOutputModel.
@Override
protected void renderMergedOutputModel(Map<String, Object> model, HttpServletRequest request, HttpServletResponse response) throws Exception {
Object object = model.get("model");
List<Grid> grids = new ArrayList<>();
if (WebMetadata.class.isAssignableFrom(object.getClass())) {
WebMetadata metadata = (WebMetadata) object;
Collection<Field> fields = ReflectionUtils.collectFields(WebMetadata.class, PredicateUtils.idObjectCollections);
for (Field field : fields) {
List<IdentifiableObject> identifiableObjects = ReflectionUtils.invokeGetterMethod(field.getName(), metadata);
if (identifiableObjects == null || identifiableObjects.isEmpty()) {
continue;
}
Grid grid = new ListGrid();
grid.setTitle(identifiableObjects.get(0).getClass().getSimpleName() + "s");
boolean nameable = false;
grid.addHeader(new GridHeader("UID", false, false));
grid.addHeader(new GridHeader("Name", false, false));
if (NameableObject.class.isAssignableFrom(identifiableObjects.get(0).getClass())) {
grid.addHeader(new GridHeader("ShortName", false, false));
nameable = true;
}
grid.addHeader(new GridHeader("Code", false, false));
for (IdentifiableObject identifiableObject : identifiableObjects) {
grid.addRow();
grid.addValue(identifiableObject.getUid());
grid.addValue(identifiableObject.getName());
if (nameable) {
grid.addValue(((NameableObject) identifiableObject).getShortName());
}
grid.addValue(identifiableObject.getCode());
}
grids.add(grid);
}
} else {
IdentifiableObject identifiableObject = (IdentifiableObject) object;
Grid grid = new ListGrid();
grid.setTitle(identifiableObject.getClass().getSimpleName());
grid.addEmptyHeaders(2);
grid.addRow().addValue("UID").addValue(identifiableObject.getUid());
grid.addRow().addValue("Name").addValue(identifiableObject.getName());
if (NameableObject.class.isAssignableFrom(identifiableObject.getClass())) {
grid.addRow().addValue("ShortName").addValue(((NameableObject) identifiableObject).getShortName());
grid.addRow().addValue("Description").addValue(((NameableObject) identifiableObject).getDescription());
}
grid.addRow().addValue("Code").addValue(identifiableObject.getCode());
grids.add(grid);
}
renderGrids(grids, response);
}
use of org.hisp.dhis.common.GridHeader in project dhis2-core by dhis2.
the class GridTest method testAddHeaderList.
@Test
public void testAddHeaderList() {
Grid grid = new ListGrid();
GridHeader headerA = new GridHeader("DataElementA", "Data element A");
GridHeader headerB = new GridHeader("DataElementB", "Data element B");
GridHeader headerC = new GridHeader("DataElementC", "Data element C");
GridHeader headerD = new GridHeader("DataElementC", "Data element D");
GridHeader headerE = new GridHeader("DataElementC", "Data element E");
grid.addHeader(headerA);
grid.addHeader(headerB);
assertEquals(2, grid.getHeaders().size());
List<GridHeader> headers = Lists.newArrayList(headerC, headerD, headerE);
grid.addHeaders(1, headers);
assertEquals(5, grid.getHeaders().size());
assertEquals(headerA, grid.getHeaders().get(0));
assertEquals(headerC, grid.getHeaders().get(1));
assertEquals(headerD, grid.getHeaders().get(2));
assertEquals(headerE, grid.getHeaders().get(3));
assertEquals(headerB, grid.getHeaders().get(4));
}
Aggregations