~mortenoh/+junk/dhis2-detailed-import-export

« back to all changes in this revision

Viewing changes to dhis-2/dhis-services/dhis-service-reporting/src/main/java/org/hisp/dhis/reporttable/DefaultReportTableCreator.java

  • Committer: larshelge at gmail
  • Date: 2009-03-03 16:46:36 UTC
  • Revision ID: larshelge@gmail.com-20090303164636-2sjlrquo7ib1gf7r
Initial check-in

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
package org.hisp.dhis.reporttable;
 
2
 
 
3
/*
 
4
 * Copyright (c) 2004-2007, University of Oslo
 
5
 * All rights reserved.
 
6
 *
 
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.
 
17
 *
 
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.
 
28
 */
 
29
 
 
30
import static org.hisp.dhis.system.util.ConversionUtils.getIdentifiers;
 
31
 
 
32
import java.util.List;
 
33
import java.util.Map;
 
34
 
 
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;
 
53
 
 
54
/**
 
55
 * @author Lars Helge Overland
 
56
 * @version $Id$
 
57
 */
 
58
public class DefaultReportTableCreator
 
59
    extends ReportTableInternalProcess
 
60
{
 
61
    private static final Log log = LogFactory.getLog( DefaultReportTableCreator.class );
 
62
    
 
63
    private static final String NULL_REPLACEMENT = "0.0";
 
64
    
 
65
    // ---------------------------------------------------------------------
 
66
    // Dependencies
 
67
    // ---------------------------------------------------------------------
 
68
 
 
69
    private ReportTableManager reportTableManager;
 
70
    
 
71
    public void setReportTableManager( ReportTableManager reportTableManager )
 
72
    {
 
73
        this.reportTableManager = reportTableManager;
 
74
    }
 
75
    
 
76
    private ReportTableService reportTableService;
 
77
 
 
78
    public void setReportTableService( ReportTableService reportTableService )
 
79
    {
 
80
        this.reportTableService = reportTableService;
 
81
    }
 
82
    
 
83
    private BatchHandlerFactory batchHandlerFactory;
 
84
    
 
85
    public void setBatchHandlerFactory( BatchHandlerFactory batchHandlerFactory )
 
86
    {
 
87
        this.batchHandlerFactory = batchHandlerFactory;
 
88
    }
 
89
    
 
90
    private DataMartService dataMartService;
 
91
    
 
92
    public void setDataMartService( DataMartService dataMartService )
 
93
    {
 
94
        this.dataMartService = dataMartService;
 
95
    }
 
96
    
 
97
    private DataMartStore dataMartStore;
 
98
 
 
99
    public void setDataMartStore( DataMartStore dataMartStore )
 
100
    {
 
101
        this.dataMartStore = dataMartStore;
 
102
    }
 
103
    
 
104
    private DataSetCompletenessExportService completenessExportService;
 
105
    
 
106
    public void setCompletenessExportService( DataSetCompletenessExportService completenessExportService )
 
107
    {
 
108
        this.completenessExportService = completenessExportService;
 
109
    }
 
110
 
 
111
    public void createReportTable( ReportTable reportTable, boolean doDataMart )
 
112
    {
 
113
        log.info( "Process started for report table: '" + reportTable.getName() + "'" );
 
114
        
 
115
        setMessage( "aggregating_data" );
 
116
        
 
117
        reportTableService.saveOrUpdateReportTable( reportTable );
 
118
        
 
119
        // ---------------------------------------------------------------------
 
120
        // Exporting relevant data to data mart
 
121
        // ---------------------------------------------------------------------
 
122
 
 
123
        if ( doDataMart )
 
124
        {
 
125
            String mode = reportTable.getMode();
 
126
            
 
127
            if ( mode.equals( ReportTable.MODE_DATAELEMENTS ) || mode.equals( ReportTable.MODE_INDICATORS ) )
 
128
            {
 
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() ) );
 
133
            }
 
134
            else if ( mode.equals( ReportTable.MODE_DATASETS ) )
 
135
            {
 
136
                completenessExportService.exportDataSetCompleteness( getIdentifiers( DataSet.class, reportTable.getDataSets() ),
 
137
                    getIdentifiers( Period.class, reportTable.getAllPeriods() ),
 
138
                    getIdentifiers( OrganisationUnit.class, reportTable.getUnits() ),
 
139
                    reportTable.getId() );
 
140
            }
 
141
        }
 
142
        
 
143
        // ---------------------------------------------------------------------
 
144
        // Creating report table
 
145
        // ---------------------------------------------------------------------
 
146
        
 
147
        setMessage( "creating_report_datasource" );
 
148
        
 
149
        reportTableManager.createReportTable( reportTable );
 
150
 
 
151
        // ---------------------------------------------------------------------
 
152
        // Updating existingt table name after deleting the database table
 
153
        // ---------------------------------------------------------------------
 
154
        
 
155
        reportTable.updateExistingTableName();
 
156
        
 
157
        reportTableService.saveOrUpdateReportTable( reportTable );
 
158
        
 
159
        log.info( "Created report table" );
 
160
 
 
161
        // ---------------------------------------------------------------------
 
162
        // Creating grid
 
163
        // ---------------------------------------------------------------------
 
164
 
 
165
        Grid grid = getGrid( reportTable );
 
166
        
 
167
        if ( reportTable.isRegression() )
 
168
        {
 
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
            // -----------------------------------------------------------------
 
174
 
 
175
            int numberOfColumns = reportTable.getCrossTabIdentifiers().size();
 
176
            int startColumnIndex = grid.getWidth() - numberOfColumns;
 
177
            
 
178
            addRegressionToGrid( grid, startColumnIndex, numberOfColumns );
 
179
        }
 
180
 
 
181
        // ---------------------------------------------------------------------
 
182
        // Populating report table from grid
 
183
        // ---------------------------------------------------------------------
 
184
 
 
185
        BatchHandler batchHandler = batchHandlerFactory.createBatchHandler( GenericBatchHandler.class );
 
186
 
 
187
        batchHandler.setTableName( reportTable.getTableName() );
 
188
        
 
189
        batchHandler.init();
 
190
        
 
191
        for ( List<String> row : grid.getRows() )
 
192
        {
 
193
            batchHandler.addObject( row );
 
194
        }
 
195
        
 
196
        batchHandler.flush();       
 
197
 
 
198
        setMessage( "process_done" );        
 
199
        
 
200
        log.info( "Populated report table: '" + reportTable.getTableName() + "'" );
 
201
    }
 
202
 
 
203
    public void removeReportTable( ReportTable reportTable )
 
204
    {
 
205
        reportTableManager.removeReportTable( reportTable );
 
206
    }
 
207
    
 
208
    @Override
 
209
    public void deleteRelativePeriods()
 
210
    {
 
211
        dataMartStore.deleteRelativePeriods();
 
212
        
 
213
        log.info( "Deleted relative periods" );
 
214
    }
 
215
 
 
216
    // -------------------------------------------------------------------------
 
217
    // Supportive methods
 
218
    // -------------------------------------------------------------------------
 
219
 
 
220
    private Grid addRegressionToGrid( Grid grid, int startColumnIndex, int numberOfColumns )
 
221
    {
 
222
        for ( int i = 0; i < numberOfColumns; i++ )
 
223
        {
 
224
            int columnIndex = i + startColumnIndex;
 
225
            
 
226
            grid.addRegressionColumn( columnIndex );
 
227
        }
 
228
        
 
229
        return grid;
 
230
    }
 
231
    
 
232
    private Grid getGrid( ReportTable reportTable )
 
233
    {
 
234
        Grid grid = new Grid();
 
235
        
 
236
        Map<String, Double> map = null;
 
237
 
 
238
        for ( final MetaObject metaObject : reportTable.getReportIndicators() )
 
239
        {
 
240
            for ( final DataElementCategoryOptionCombo categoryOptionCombo : reportTable.getReportCategoryOptionCombos() )
 
241
            {
 
242
                for ( final Period period : reportTable.getReportPeriods() )
 
243
                {
 
244
                    for ( final OrganisationUnit unit : reportTable.getReportUnits() )
 
245
                    {
 
246
                        grid.nextRow();
 
247
                        
 
248
                        // -----------------------------------------------------
 
249
                        // Identifier
 
250
                        // -----------------------------------------------------
 
251
 
 
252
                        if ( reportTable.getIndexColumns().contains( ReportTable.INDICATOR_ID ) )
 
253
                        {
 
254
                            grid.addValue( String.valueOf( metaObject.getId() ) );
 
255
                        }
 
256
                        
 
257
                        if ( reportTable.getIndexColumns().contains( ReportTable.DATAELEMENT_ID ) )
 
258
                        {
 
259
                            grid.addValue( String.valueOf( metaObject.getId() ) );
 
260
                        }
 
261
                        
 
262
                        if ( reportTable.getIndexColumns().contains( ReportTable.DATASET_ID ) )
 
263
                        {
 
264
                            grid.addValue( String.valueOf( metaObject.getId() ) );
 
265
                        }
 
266
                        
 
267
                        if ( reportTable.getIndexColumns().contains( ReportTable.CATEGORYCOMBO_ID ) )
 
268
                        {
 
269
                            grid.addValue( String.valueOf( categoryOptionCombo.getId() ) );
 
270
                        }
 
271
                        
 
272
                        if ( reportTable.getIndexColumns().contains( ReportTable.PERIOD_ID ) )
 
273
                        {
 
274
                            grid.addValue( String.valueOf( period.getId() ) );
 
275
                        }
 
276
                        
 
277
                        if ( reportTable.getIndexColumns().contains( ReportTable.ORGANISATIONUNIT_ID ) )
 
278
                        {
 
279
                            grid.addValue( String.valueOf( unit.getId() ) );
 
280
                        }
 
281
 
 
282
                        // -----------------------------------------------------
 
283
                        // Name
 
284
                        // -----------------------------------------------------
 
285
    
 
286
                        if ( reportTable.getIndexNameColumns().contains( ReportTable.INDICATOR_NAME ) )
 
287
                        {
 
288
                            grid.addValue( metaObject.getShortName() );
 
289
                        }
 
290
                        
 
291
                        if ( reportTable.getIndexNameColumns().contains( ReportTable.DATAELEMENT_NAME ) )
 
292
                        {
 
293
                            grid.addValue( metaObject.getShortName() );
 
294
                        }
 
295
 
 
296
                        if ( reportTable.getIndexNameColumns().contains( ReportTable.DATASET_NAME ) )
 
297
                        {
 
298
                            grid.addValue( metaObject.getShortName() );
 
299
                        }
 
300
                        
 
301
                        if ( reportTable.getIndexNameColumns().contains( ReportTable.CATEGORYCOMBO_NAME ) )
 
302
                        {
 
303
                            grid.addValue( categoryOptionCombo.getShortName() );
 
304
                        }
 
305
                        
 
306
                        if ( reportTable.getIndexNameColumns().contains( ReportTable.PERIOD_NAME ) )
 
307
                        {
 
308
                            grid.addValue( getPeriodName( reportTable, period ) );
 
309
                        }
 
310
                        
 
311
                        if ( reportTable.getIndexNameColumns().contains( ReportTable.ORGANISATIONUNIT_NAME ) )
 
312
                        {
 
313
                            grid.addValue( unit.getShortName() );
 
314
                        }
 
315
 
 
316
                        // -----------------------------------------------------
 
317
                        // Reporting month name
 
318
                        // -----------------------------------------------------
 
319
 
 
320
                        grid.addValue( reportTable.getReportingMonthName() );
 
321
    
 
322
                        // -----------------------------------------------------
 
323
                        // Values
 
324
                        // -----------------------------------------------------
 
325
 
 
326
                        map = reportTableManager.getAggregatedValueMap( reportTable, metaObject, categoryOptionCombo, period, unit );
 
327
                        
 
328
                        for ( String identifier : reportTable.getCrossTabIdentifiers() )
 
329
                        {
 
330
                            grid.addValue( parseAndReplaceNull( map.get( identifier ) ) );
 
331
                        }
 
332
                    }
 
333
                }
 
334
            }
 
335
        }
 
336
        
 
337
        return grid;
 
338
    }
 
339
    
 
340
    private static String parseAndReplaceNull( Double value )
 
341
    {
 
342
        return value != null ? String.valueOf( value ) : NULL_REPLACEMENT;
 
343
    }
 
344
    
 
345
    private String getPeriodName( ReportTable reportTable, Period period )
 
346
    {
 
347
        if ( period.getPeriodType().getName().equals( RelativePeriodType.NAME ) )
 
348
        {
 
349
            return period.getName();
 
350
        }
 
351
        else
 
352
        {
 
353
            return reportTable.getI18nFormat().formatPeriod( period );
 
354
        }
 
355
    }
 
356
}