1
package org.hisp.dhis.reporttable;
4
* Copyright (c) 2004-2007, University of Oslo
7
* Redistribution and use in source and binary forms, with or without
8
* modification, are permitted provided that the following conditions are met:
9
* * Redistributions of source code must retain the above copyright notice, this
10
* list of conditions and the following disclaimer.
11
* * Redistributions in binary form must reproduce the above copyright notice,
12
* this list of conditions and the following disclaimer in the documentation
13
* and/or other materials provided with the distribution.
14
* * Neither the name of the HISP project nor the names of its contributors may
15
* be used to endorse or promote products derived from this software without
16
* specific prior written permission.
18
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
19
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
22
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
25
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
import static org.hisp.dhis.system.util.ConversionUtils.getIdentifiers;
32
import java.util.List;
35
import org.apache.commons.logging.Log;
36
import org.apache.commons.logging.LogFactory;
37
import org.hisp.dhis.common.MetaObject;
38
import org.hisp.dhis.completeness.DataSetCompletenessExportService;
39
import org.hisp.dhis.dataelement.DataElement;
40
import org.hisp.dhis.dataelement.DataElementCategoryOptionCombo;
41
import org.hisp.dhis.datamart.DataMartService;
42
import org.hisp.dhis.datamart.DataMartStore;
43
import org.hisp.dhis.dataset.DataSet;
44
import org.hisp.dhis.indicator.Indicator;
45
import org.hisp.dhis.jdbc.BatchHandler;
46
import org.hisp.dhis.jdbc.BatchHandlerFactory;
47
import org.hisp.dhis.jdbc.batchhandler.GenericBatchHandler;
48
import org.hisp.dhis.organisationunit.OrganisationUnit;
49
import org.hisp.dhis.period.Period;
50
import org.hisp.dhis.period.RelativePeriodType;
51
import org.hisp.dhis.reporttable.jdbc.ReportTableManager;
52
import org.hisp.dhis.system.grid.Grid;
55
* @author Lars Helge Overland
58
public class DefaultReportTableCreator
59
extends ReportTableInternalProcess
61
private static final Log log = LogFactory.getLog( DefaultReportTableCreator.class );
63
private static final String NULL_REPLACEMENT = "0.0";
65
// ---------------------------------------------------------------------
67
// ---------------------------------------------------------------------
69
private ReportTableManager reportTableManager;
71
public void setReportTableManager( ReportTableManager reportTableManager )
73
this.reportTableManager = reportTableManager;
76
private ReportTableService reportTableService;
78
public void setReportTableService( ReportTableService reportTableService )
80
this.reportTableService = reportTableService;
83
private BatchHandlerFactory batchHandlerFactory;
85
public void setBatchHandlerFactory( BatchHandlerFactory batchHandlerFactory )
87
this.batchHandlerFactory = batchHandlerFactory;
90
private DataMartService dataMartService;
92
public void setDataMartService( DataMartService dataMartService )
94
this.dataMartService = dataMartService;
97
private DataMartStore dataMartStore;
99
public void setDataMartStore( DataMartStore dataMartStore )
101
this.dataMartStore = dataMartStore;
104
private DataSetCompletenessExportService completenessExportService;
106
public void setCompletenessExportService( DataSetCompletenessExportService completenessExportService )
108
this.completenessExportService = completenessExportService;
111
public void createReportTable( ReportTable reportTable, boolean doDataMart )
113
log.info( "Process started for report table: '" + reportTable.getName() + "'" );
115
setMessage( "aggregating_data" );
117
reportTableService.saveOrUpdateReportTable( reportTable );
119
// ---------------------------------------------------------------------
120
// Exporting relevant data to data mart
121
// ---------------------------------------------------------------------
125
String mode = reportTable.getMode();
127
if ( mode.equals( ReportTable.MODE_DATAELEMENTS ) || mode.equals( ReportTable.MODE_INDICATORS ) )
129
dataMartService.export( getIdentifiers( DataElement.class, reportTable.getDataElements() ),
130
getIdentifiers( Indicator.class, reportTable.getIndicators() ),
131
getIdentifiers( Period.class, reportTable.getAllPeriods() ),
132
getIdentifiers( OrganisationUnit.class, reportTable.getUnits() ) );
134
else if ( mode.equals( ReportTable.MODE_DATASETS ) )
136
completenessExportService.exportDataSetCompleteness( getIdentifiers( DataSet.class, reportTable.getDataSets() ),
137
getIdentifiers( Period.class, reportTable.getAllPeriods() ),
138
getIdentifiers( OrganisationUnit.class, reportTable.getUnits() ),
139
reportTable.getId() );
143
// ---------------------------------------------------------------------
144
// Creating report table
145
// ---------------------------------------------------------------------
147
setMessage( "creating_report_datasource" );
149
reportTableManager.createReportTable( reportTable );
151
// ---------------------------------------------------------------------
152
// Updating existingt table name after deleting the database table
153
// ---------------------------------------------------------------------
155
reportTable.updateExistingTableName();
157
reportTableService.saveOrUpdateReportTable( reportTable );
159
log.info( "Created report table" );
161
// ---------------------------------------------------------------------
163
// ---------------------------------------------------------------------
165
Grid grid = getGrid( reportTable );
167
if ( reportTable.isRegression() )
169
// -----------------------------------------------------------------
170
// The start index of the crosstab columns is derived by
171
// subtracting the total number of columns with the number of
172
// crosstab columns, since they come last in the report table.
173
// -----------------------------------------------------------------
175
int numberOfColumns = reportTable.getCrossTabIdentifiers().size();
176
int startColumnIndex = grid.getWidth() - numberOfColumns;
178
addRegressionToGrid( grid, startColumnIndex, numberOfColumns );
181
// ---------------------------------------------------------------------
182
// Populating report table from grid
183
// ---------------------------------------------------------------------
185
BatchHandler batchHandler = batchHandlerFactory.createBatchHandler( GenericBatchHandler.class );
187
batchHandler.setTableName( reportTable.getTableName() );
191
for ( List<String> row : grid.getRows() )
193
batchHandler.addObject( row );
196
batchHandler.flush();
198
setMessage( "process_done" );
200
log.info( "Populated report table: '" + reportTable.getTableName() + "'" );
203
public void removeReportTable( ReportTable reportTable )
205
reportTableManager.removeReportTable( reportTable );
209
public void deleteRelativePeriods()
211
dataMartStore.deleteRelativePeriods();
213
log.info( "Deleted relative periods" );
216
// -------------------------------------------------------------------------
217
// Supportive methods
218
// -------------------------------------------------------------------------
220
private Grid addRegressionToGrid( Grid grid, int startColumnIndex, int numberOfColumns )
222
for ( int i = 0; i < numberOfColumns; i++ )
224
int columnIndex = i + startColumnIndex;
226
grid.addRegressionColumn( columnIndex );
232
private Grid getGrid( ReportTable reportTable )
234
Grid grid = new Grid();
236
Map<String, Double> map = null;
238
for ( final MetaObject metaObject : reportTable.getReportIndicators() )
240
for ( final DataElementCategoryOptionCombo categoryOptionCombo : reportTable.getReportCategoryOptionCombos() )
242
for ( final Period period : reportTable.getReportPeriods() )
244
for ( final OrganisationUnit unit : reportTable.getReportUnits() )
248
// -----------------------------------------------------
250
// -----------------------------------------------------
252
if ( reportTable.getIndexColumns().contains( ReportTable.INDICATOR_ID ) )
254
grid.addValue( String.valueOf( metaObject.getId() ) );
257
if ( reportTable.getIndexColumns().contains( ReportTable.DATAELEMENT_ID ) )
259
grid.addValue( String.valueOf( metaObject.getId() ) );
262
if ( reportTable.getIndexColumns().contains( ReportTable.DATASET_ID ) )
264
grid.addValue( String.valueOf( metaObject.getId() ) );
267
if ( reportTable.getIndexColumns().contains( ReportTable.CATEGORYCOMBO_ID ) )
269
grid.addValue( String.valueOf( categoryOptionCombo.getId() ) );
272
if ( reportTable.getIndexColumns().contains( ReportTable.PERIOD_ID ) )
274
grid.addValue( String.valueOf( period.getId() ) );
277
if ( reportTable.getIndexColumns().contains( ReportTable.ORGANISATIONUNIT_ID ) )
279
grid.addValue( String.valueOf( unit.getId() ) );
282
// -----------------------------------------------------
284
// -----------------------------------------------------
286
if ( reportTable.getIndexNameColumns().contains( ReportTable.INDICATOR_NAME ) )
288
grid.addValue( metaObject.getShortName() );
291
if ( reportTable.getIndexNameColumns().contains( ReportTable.DATAELEMENT_NAME ) )
293
grid.addValue( metaObject.getShortName() );
296
if ( reportTable.getIndexNameColumns().contains( ReportTable.DATASET_NAME ) )
298
grid.addValue( metaObject.getShortName() );
301
if ( reportTable.getIndexNameColumns().contains( ReportTable.CATEGORYCOMBO_NAME ) )
303
grid.addValue( categoryOptionCombo.getShortName() );
306
if ( reportTable.getIndexNameColumns().contains( ReportTable.PERIOD_NAME ) )
308
grid.addValue( getPeriodName( reportTable, period ) );
311
if ( reportTable.getIndexNameColumns().contains( ReportTable.ORGANISATIONUNIT_NAME ) )
313
grid.addValue( unit.getShortName() );
316
// -----------------------------------------------------
317
// Reporting month name
318
// -----------------------------------------------------
320
grid.addValue( reportTable.getReportingMonthName() );
322
// -----------------------------------------------------
324
// -----------------------------------------------------
326
map = reportTableManager.getAggregatedValueMap( reportTable, metaObject, categoryOptionCombo, period, unit );
328
for ( String identifier : reportTable.getCrossTabIdentifiers() )
330
grid.addValue( parseAndReplaceNull( map.get( identifier ) ) );
340
private static String parseAndReplaceNull( Double value )
342
return value != null ? String.valueOf( value ) : NULL_REPLACEMENT;
345
private String getPeriodName( ReportTable reportTable, Period period )
347
if ( period.getPeriodType().getName().equals( RelativePeriodType.NAME ) )
349
return period.getName();
353
return reportTable.getI18nFormat().formatPeriod( period );