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

« back to all changes in this revision

Viewing changes to dhis-2/dhis-api/src/main/java/org/hisp/dhis/reporttable/ReportTable.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 java.io.Serializable;
 
31
import java.util.ArrayList;
 
32
import java.util.List;
 
33
import java.util.regex.Matcher;
 
34
import java.util.regex.Pattern;
 
35
 
 
36
import org.hisp.dhis.common.MetaObject;
 
37
import org.hisp.dhis.dataelement.DataElement;
 
38
import org.hisp.dhis.dataelement.DataElementCategoryOptionCombo;
 
39
import org.hisp.dhis.dataset.DataSet;
 
40
import org.hisp.dhis.i18n.I18nFormat;
 
41
import org.hisp.dhis.indicator.Indicator;
 
42
import org.hisp.dhis.organisationunit.OrganisationUnit;
 
43
import org.hisp.dhis.period.Period;
 
44
 
 
45
/**
 
46
 * @author Lars Helge Overland
 
47
 * @version $Id$
 
48
 */
 
49
public class ReportTable
 
50
    implements Serializable
 
51
{
 
52
    public static final String DATAELEMENT_ID = "dataelementid";
 
53
    public static final String DATAELEMENT_NAME = "dataelementname";
 
54
    public static final String CATEGORYCOMBO_ID = "categoryoptioncomboid";
 
55
    public static final String CATEGORYCOMBO_NAME = "categoryoptioncomboname";
 
56
    public static final String INDICATOR_ID = "indicatorid";
 
57
    public static final String INDICATOR_NAME = "indicatorname";
 
58
    public static final String DATASET_ID = "datasetid";
 
59
    public static final String DATASET_NAME = "datasetname";
 
60
    public static final String PERIOD_ID = "periodid";
 
61
    public static final String PERIOD_NAME = "periodname";
 
62
    public static final String ORGANISATIONUNIT_ID = "organisationunitid";
 
63
    public static final String ORGANISATIONUNIT_NAME = "organisationunitname";
 
64
    
 
65
    public static final String SEPARATOR = "_";
 
66
    
 
67
    public static final String MODE_DATAELEMENTS = "dataelements";
 
68
    public static final String MODE_INDICATORS = "indicators";
 
69
    public static final String MODE_DATASETS = "datasets";
 
70
    
 
71
    public static final String REGRESSION_COLUMN_PREFIX = "regression_";
 
72
    
 
73
    private static final String EMPTY_REPLACEMENT = "_";    
 
74
    private static final String TABLE_PREFIX = "_report_";
 
75
    private static final String REGEX_NUMERIC = "([0-9]*)";
 
76
 
 
77
    // -------------------------------------------------------------------------
 
78
    // Persisted properties
 
79
    // -------------------------------------------------------------------------
 
80
 
 
81
    private int id;
 
82
    
 
83
    private String name;
 
84
 
 
85
    private String tableName;
 
86
    
 
87
    private String existingTableName;
 
88
    
 
89
    private String mode;
 
90
    
 
91
    private Boolean regression;
 
92
 
 
93
    private List<DataElement> dataElements = new ArrayList<DataElement>();
 
94
    
 
95
    private List<DataElementCategoryOptionCombo> categoryOptionCombos = new ArrayList<DataElementCategoryOptionCombo>();
 
96
    
 
97
    private List<Indicator> indicators = new ArrayList<Indicator>();
 
98
    
 
99
    private List<DataSet> dataSets = new ArrayList<DataSet>();
 
100
    
 
101
    private List<Period> periods = new ArrayList<Period>();
 
102
    
 
103
    private List<OrganisationUnit> units = new ArrayList<OrganisationUnit>();
 
104
 
 
105
    private Boolean doIndicators;
 
106
    
 
107
    private Boolean doCategoryOptionCombos;
 
108
    
 
109
    private Boolean doPeriods;
 
110
    
 
111
    private Boolean doUnits;
 
112
 
 
113
    private RelativePeriods relatives;
 
114
 
 
115
    private ReportParams reportParams;
 
116
    
 
117
    // -------------------------------------------------------------------------
 
118
    // Transient properties
 
119
    // -------------------------------------------------------------------------
 
120
    
 
121
    /**
 
122
     * Periods relative to the reporting month.
 
123
     */
 
124
    private List<Period> relativePeriods = new ArrayList<Period>();
 
125
    
 
126
    /**
 
127
     * Static periods and relative periods.
 
128
     */
 
129
    private List<Period> allPeriods = new ArrayList<Period>();
 
130
    
 
131
    /**
 
132
     * Indicators that will be crosstabulated on the columns axis. Indicators 
 
133
     * comprises dataelements, indicators, datasets.
 
134
     */
 
135
    private List<MetaObject> crossTabIndicators = new ArrayList<MetaObject>();
 
136
 
 
137
    /**
 
138
     * CategoryCombos that will be crosstabulated on the columns axis. Optional dimension.
 
139
     */
 
140
    private List<DataElementCategoryOptionCombo> crossTabCategoryOptionCombos = new ArrayList<DataElementCategoryOptionCombo>();
 
141
    
 
142
    /**
 
143
     * Periods that will be crosstabulated on the columns axis. Mandatory dimension.
 
144
     */
 
145
    private List<Period> crossTabPeriods = new ArrayList<Period>();
 
146
    
 
147
    /**
 
148
     * OrganisationUnits that will be crosstabulated on the columns axis. Mandatory dimension.
 
149
     */
 
150
    private List<OrganisationUnit> crossTabUnits = new ArrayList<OrganisationUnit>();
 
151
    
 
152
    /**
 
153
     * Indicators that will be present on the rows axis.
 
154
     */
 
155
    private List<MetaObject> reportIndicators = new ArrayList<MetaObject>();
 
156
 
 
157
    /**
 
158
     * CategoryOptionCombos that will be present on the rows axis. Optional dimension.
 
159
     */
 
160
    private List<DataElementCategoryOptionCombo> reportCategoryOptionCombos = new ArrayList<DataElementCategoryOptionCombo>();
 
161
    
 
162
    /**
 
163
     * Periods that will be present on the rows axis. Mandatory dimension.
 
164
     */
 
165
    private List<Period> reportPeriods = new ArrayList<Period>();
 
166
    
 
167
    /**
 
168
     * OrganisationUnits that will be present on the rows axis. Mandatory dimension.
 
169
     */
 
170
    private List<OrganisationUnit> reportUnits = new ArrayList<OrganisationUnit>();
 
171
    
 
172
    /**
 
173
     * Names of the columns used to query the datavalue table and as index columns
 
174
     * in the report table.
 
175
     */
 
176
    private List<String> indexColumns = new ArrayList<String>();
 
177
    
 
178
    /**
 
179
     * Names of the columns holding entry names used to query the datavalue table.
 
180
     */
 
181
    private List<String> indexNameColumns = new ArrayList<String>();
 
182
    
 
183
    /**
 
184
     * Names of the columns which should be retrieved from the datavalue table.
 
185
     */
 
186
    private List<String> selectColumns = new ArrayList<String>();
 
187
    
 
188
    /**
 
189
     * Generated names for crosstabulated columns in the report table.
 
190
     */
 
191
    private List<String> crossTabColumns = new ArrayList<String>();
 
192
    
 
193
    /**
 
194
     * Generated unique identifiers used to retrieve the corresponding value from the datavalue table. 
 
195
     */
 
196
    private List<String> crossTabIdentifiers = new ArrayList<String>();
 
197
    
 
198
    /**
 
199
     * The I18nFormat used for internationalization of ie. periods.
 
200
     */
 
201
    private I18nFormat i18nFormat;
 
202
    
 
203
    /**
 
204
     * The name of the reporting month.
 
205
     */
 
206
    private String reportingMonthName;
 
207
    
 
208
    // -------------------------------------------------------------------------
 
209
    // Constructors
 
210
    // -------------------------------------------------------------------------
 
211
 
 
212
    /**
 
213
     * Constructor for persistence purposes.
 
214
     */
 
215
    public ReportTable()
 
216
    {   
 
217
    }
 
218
    
 
219
    /**
 
220
     * Constructor for testing purposes.
 
221
     * 
 
222
     * @param name the name.
 
223
     * @param tableName the table name.
 
224
     */
 
225
    public ReportTable( String name, String tableName )
 
226
    {
 
227
        this.name = name;
 
228
        this.tableName = tableName;
 
229
    }
 
230
 
 
231
    /**
 
232
     * Default constructor.
 
233
     * 
 
234
     * @param name the name.
 
235
     * @param mode the mode.
 
236
     * @param dataElements the data elements.
 
237
     * @param indicators the indicators.
 
238
     * @param dataSets the datasets.
 
239
     * @param categoryOptionCombos the category option combos.
 
240
     * @param periods the periods. These periods cannot have the name property set.
 
241
     * @param relativePeriods the relative periods. These periods must have the name property set. Not persisted.
 
242
     * @param units the organisation units.
 
243
     * @param doIndicators indicating whether indicators should be crosstabulated.
 
244
     * @param doCategoryOptionCombos indicating whether category option combos should be crosstabulated.
 
245
     * @param doPeriods indicating whether periods should be crosstabulated.
 
246
     * @param doUnits indicating whether organisation units should be crosstabulated.
 
247
     * @param relatives the relative periods.
 
248
     * @param i18nFormat the i18n format. Not persisted.
 
249
     * @param reportingMonthName the reporting month name. Not persisted.
 
250
     */
 
251
    public ReportTable( String name,
 
252
        String mode,
 
253
        boolean regression,
 
254
        List<DataElement> dataElements,
 
255
        List<Indicator> indicators,
 
256
        List<DataSet> dataSets,
 
257
        List<DataElementCategoryOptionCombo> categoryOptionCombos,
 
258
        List<Period> periods,
 
259
        List<Period> relativePeriods,
 
260
        List<OrganisationUnit> units,
 
261
        boolean doIndicators,
 
262
        boolean doCategoryOptionCombos,
 
263
        boolean doPeriods,
 
264
        boolean doUnits,
 
265
        RelativePeriods relatives,
 
266
        ReportParams reportParams,
 
267
        I18nFormat i18nFormat,
 
268
        String reportingMonthName )
 
269
    {
 
270
        this.name = name;
 
271
        this.tableName = generateTableName( name );
 
272
        this.existingTableName = generateTableName( name );
 
273
        this.mode = mode;
 
274
        this.regression = regression;
 
275
        this.dataElements = dataElements;
 
276
        this.indicators = indicators;
 
277
        this.dataSets = dataSets;
 
278
        this.categoryOptionCombos = categoryOptionCombos;
 
279
        this.periods = periods;
 
280
        this.relativePeriods = relativePeriods;
 
281
        this.units = units;
 
282
        this.doIndicators = doIndicators;
 
283
        this.doCategoryOptionCombos = doCategoryOptionCombos;
 
284
        this.doPeriods = doPeriods;
 
285
        this.doUnits = doUnits;
 
286
        this.relatives = relatives;
 
287
        this.reportParams = reportParams;
 
288
        this.i18nFormat = i18nFormat;
 
289
        this.reportingMonthName = reportingMonthName;
 
290
        
 
291
        init();
 
292
    }
 
293
 
 
294
    // -------------------------------------------------------------------------
 
295
    // Init
 
296
    // -------------------------------------------------------------------------
 
297
 
 
298
    public void init()
 
299
    {
 
300
        if ( nonEmptyLists( dataElements, indicators, dataSets ) > 1 )
 
301
        {
 
302
            throw new IllegalArgumentException( "ReportTable cannot contain more than one out of dataelements, indicators, and datasets" );
 
303
        }
 
304
        
 
305
        if ( listIsNonEmpty( categoryOptionCombos ) && ( mode != null && !mode.equalsIgnoreCase( MODE_DATAELEMENTS ) ) )
 
306
        {
 
307
            throw new IllegalArgumentException( "ReportTable cannot contain category option combos when not in dataelement mode" );
 
308
        }
 
309
        
 
310
        // ---------------------------------------------------------------------
 
311
        // Init tableName and allPeriods
 
312
        // ---------------------------------------------------------------------
 
313
 
 
314
        this.tableName = generateTableName( name );
 
315
        
 
316
        allPeriods.addAll( periods );
 
317
        allPeriods.addAll( relativePeriods );
 
318
        
 
319
        // ---------------------------------------------------------------------
 
320
        // Init indexColumns and selectColumns
 
321
        // ---------------------------------------------------------------------
 
322
 
 
323
        if ( isDoIndicators() )
 
324
        {
 
325
            crossTabIndicators = new ArrayList<MetaObject>();
 
326
            crossTabIndicators.addAll( indicators );
 
327
            crossTabIndicators.addAll( dataElements );
 
328
            crossTabIndicators.addAll( dataSets );
 
329
            reportIndicators.add( null );
 
330
            selectColumns.add( getIdentifier( mode ) );
 
331
        }
 
332
        else
 
333
        {
 
334
            crossTabIndicators.add( null );
 
335
            reportIndicators = new ArrayList<MetaObject>();
 
336
            reportIndicators.addAll( indicators );
 
337
            reportIndicators.addAll( dataElements );
 
338
            reportIndicators.addAll( dataSets );
 
339
            indexColumns.add( getIdentifier( mode ) );
 
340
            indexNameColumns.add( getName( mode ) );
 
341
        }
 
342
        
 
343
        if ( isDoCategoryOptionCombos() )
 
344
        {
 
345
            reportCategoryOptionCombos.add( null );
 
346
            
 
347
            if ( listIsNonEmpty( categoryOptionCombos ) ) // Optional dimension
 
348
            {
 
349
                crossTabCategoryOptionCombos = new ArrayList<DataElementCategoryOptionCombo>( categoryOptionCombos );
 
350
                selectColumns.add( CATEGORYCOMBO_ID );
 
351
            }
 
352
            else
 
353
            {
 
354
                crossTabCategoryOptionCombos.add( null );
 
355
            }
 
356
        }
 
357
        else
 
358
        {
 
359
            crossTabCategoryOptionCombos.add( null );
 
360
            
 
361
            if ( listIsNonEmpty( categoryOptionCombos ) ) // Optional dimension
 
362
            {
 
363
                reportCategoryOptionCombos = new ArrayList<DataElementCategoryOptionCombo>( categoryOptionCombos );
 
364
                indexColumns.add( CATEGORYCOMBO_ID );
 
365
                indexNameColumns.add( CATEGORYCOMBO_NAME );
 
366
            }
 
367
            else
 
368
            {
 
369
                reportCategoryOptionCombos.add( null );
 
370
            }
 
371
        }
 
372
        
 
373
        if ( isDoPeriods() )
 
374
        {
 
375
            crossTabPeriods = new ArrayList<Period>( periods );
 
376
            crossTabPeriods.addAll( relativePeriods );
 
377
            reportPeriods.add( null );
 
378
            selectColumns.add( PERIOD_ID );
 
379
        }
 
380
        else
 
381
        {
 
382
            crossTabPeriods.add( null );
 
383
            reportPeriods = new ArrayList<Period>( periods );
 
384
            reportPeriods.addAll( relativePeriods );
 
385
            indexColumns.add( PERIOD_ID );
 
386
            indexNameColumns.add( PERIOD_NAME );
 
387
        }
 
388
        
 
389
        if ( isDoUnits() )
 
390
        {
 
391
            crossTabUnits = new ArrayList<OrganisationUnit>( units );
 
392
            reportUnits.add( null );
 
393
            selectColumns.add( ORGANISATIONUNIT_ID );            
 
394
        }
 
395
        else
 
396
        {
 
397
            crossTabUnits.add( null );
 
398
            reportUnits = new ArrayList<OrganisationUnit>( units );
 
399
            indexColumns.add( ORGANISATIONUNIT_ID );
 
400
            indexNameColumns.add( ORGANISATIONUNIT_NAME );
 
401
        }
 
402
 
 
403
        // ---------------------------------------------------------------------
 
404
        // Init crossTabColumns and crossTabIdentifiers
 
405
        // ---------------------------------------------------------------------
 
406
 
 
407
        for ( MetaObject indicator : crossTabIndicators )
 
408
        {
 
409
            for ( DataElementCategoryOptionCombo categoryOptionCombo : crossTabCategoryOptionCombos )
 
410
            {
 
411
                for ( Period period : crossTabPeriods )
 
412
                {
 
413
                    for ( OrganisationUnit unit : crossTabUnits )
 
414
                    {
 
415
                        String columnName = getColumnName( indicator, categoryOptionCombo, period, unit );
 
416
                        String columnIdentifier = getColumnIdentifier( indicator, categoryOptionCombo, period, unit );
 
417
                        
 
418
                        crossTabColumns.add( columnName );
 
419
                        crossTabIdentifiers.add( columnIdentifier );
 
420
                    }
 
421
                }
 
422
            }
 
423
        }
 
424
    }
 
425
 
 
426
    // -------------------------------------------------------------------------
 
427
    // Public methods
 
428
    // -------------------------------------------------------------------------
 
429
 
 
430
    public boolean isRegression()
 
431
    {
 
432
        return regression != null && regression;
 
433
    }
 
434
    
 
435
    public void updateExistingTableName()
 
436
    {
 
437
        this.existingTableName = generateTableName( name );
 
438
    }
 
439
    
 
440
    public boolean hasCategoryOptionCombos()
 
441
    {
 
442
        return categoryOptionCombos != null && categoryOptionCombos.size() > 0;
 
443
    }
 
444
    
 
445
    public boolean isDoIndicators()
 
446
    {
 
447
        return doIndicators != null && doIndicators;
 
448
    }
 
449
    
 
450
    public boolean isDoCategoryOptionCombos()
 
451
    {
 
452
        return doCategoryOptionCombos != null && doCategoryOptionCombos;
 
453
    }
 
454
    
 
455
    public boolean isDoPeriods()
 
456
    {
 
457
        return doPeriods != null && doPeriods;
 
458
    }
 
459
    
 
460
    public boolean isDoUnits()
 
461
    {
 
462
        return doUnits != null && doUnits;
 
463
    }
 
464
    
 
465
    // -------------------------------------------------------------------------
 
466
    // Supportive methods
 
467
    // -------------------------------------------------------------------------
 
468
 
 
469
    private String generateTableName( String name )
 
470
    {
 
471
        return TABLE_PREFIX + databaseEncode( name );
 
472
    }
 
473
 
 
474
    private String getIdentifier( String mode )
 
475
    {
 
476
        if ( mode == null || mode.equals( MODE_INDICATORS ) )
 
477
        {
 
478
            return INDICATOR_ID;
 
479
        }
 
480
        else if ( mode.equals( MODE_DATAELEMENTS ) )
 
481
        {
 
482
            return DATAELEMENT_ID;
 
483
        }
 
484
        else if ( mode.equals( MODE_DATASETS ) )
 
485
        {
 
486
            return DATASET_ID;
 
487
        }
 
488
        
 
489
        return null;
 
490
    }
 
491
    
 
492
    private String getName( String mode )
 
493
    {
 
494
        if ( mode == null || mode.equals( MODE_INDICATORS ) )
 
495
        {
 
496
            return INDICATOR_NAME;
 
497
        }
 
498
        else if ( mode.equals( MODE_DATAELEMENTS ) )
 
499
        {
 
500
            return DATAELEMENT_NAME;
 
501
        }
 
502
        else if ( mode.equals( MODE_DATASETS ) )
 
503
        {
 
504
            return DATASET_NAME;
 
505
        }
 
506
        
 
507
        return null;
 
508
    }
 
509
    
 
510
    private int nonEmptyLists( List<?>... lists )
 
511
    {
 
512
        int nonEmpty = 0;
 
513
        
 
514
        for ( List<?> list : lists )
 
515
        {
 
516
            if ( list != null && list.size() > 0 )
 
517
            {
 
518
                ++nonEmpty;
 
519
            }
 
520
        }
 
521
        
 
522
        return nonEmpty;
 
523
    }
 
524
    
 
525
    private boolean listIsNonEmpty( List<?> list )
 
526
    {
 
527
        return list != null && list.size() > 0;
 
528
    }
 
529
    
 
530
    private String getColumnName( MetaObject metaObject, DataElementCategoryOptionCombo categoryOptionCombo, Period period, OrganisationUnit unit )
 
531
    {
 
532
        StringBuffer buffer = new StringBuffer();
 
533
        
 
534
        if ( metaObject != null )
 
535
        {
 
536
            buffer.append( databaseEncode( metaObject.getShortName() ) + SEPARATOR );
 
537
        }
 
538
        if ( categoryOptionCombo != null )
 
539
        {
 
540
            buffer.append( databaseEncode( categoryOptionCombo.getShortName() ) + SEPARATOR );
 
541
        }
 
542
        if ( period != null )
 
543
        {
 
544
            String periodName = period.getName() != null ? period.getName() : i18nFormat.formatPeriod( period );
 
545
            
 
546
            buffer.append( databaseEncode( periodName ) + SEPARATOR );
 
547
        }
 
548
        if ( unit != null )
 
549
        {
 
550
            buffer.append( databaseEncode( unit.getShortName() ) + SEPARATOR );
 
551
        }
 
552
 
 
553
        // ---------------------------------------------------------------------
 
554
        // Columns cannot start with numeric character
 
555
        // ---------------------------------------------------------------------
 
556
 
 
557
        if ( buffer.length() > 0 && buffer.substring( 0, 1 ).matches( REGEX_NUMERIC ) )
 
558
        {
 
559
            buffer.insert( 0, SEPARATOR );
 
560
        }        
 
561
        
 
562
        return buffer.length() > 0 ? buffer.substring( 0, buffer.lastIndexOf( SEPARATOR ) ) : buffer.toString();
 
563
    }
 
564
    
 
565
    private String getColumnIdentifier( MetaObject metaObject, DataElementCategoryOptionCombo categoryOptionCombo, Period period, OrganisationUnit unit )
 
566
    {
 
567
        StringBuffer buffer = new StringBuffer();
 
568
 
 
569
        if ( metaObject != null )
 
570
        {
 
571
            buffer.append( metaObject.getId() + SEPARATOR );
 
572
        }
 
573
        if ( categoryOptionCombo != null )
 
574
        {
 
575
            buffer.append( categoryOptionCombo.getId() + SEPARATOR );
 
576
        }
 
577
        if ( period != null )
 
578
        {
 
579
            buffer.append( period.getId() + SEPARATOR );
 
580
        }
 
581
        if ( unit != null )
 
582
        {
 
583
            buffer.append( unit.getId() + SEPARATOR );
 
584
        }
 
585
 
 
586
        return buffer.length() > 0 ? buffer.substring( 0, buffer.lastIndexOf( SEPARATOR ) ) : buffer.toString();
 
587
    }
 
588
    
 
589
    private String databaseEncode( String string )
 
590
    {
 
591
        if ( string != null )
 
592
        {
 
593
            string = string.toLowerCase();
 
594
            
 
595
            string = string.replaceAll( " ", EMPTY_REPLACEMENT );
 
596
            string = string.replaceAll( "<", EMPTY_REPLACEMENT + "lt" + EMPTY_REPLACEMENT );
 
597
            string = string.replaceAll( ">", EMPTY_REPLACEMENT + "gt" + EMPTY_REPLACEMENT );
 
598
            
 
599
            StringBuffer buffer = new StringBuffer();
 
600
            
 
601
            Pattern pattern = Pattern.compile( "[a-zA-Z0-9_]" );
 
602
            
 
603
            Matcher matcher = pattern.matcher( string );
 
604
            
 
605
            while ( matcher.find() )
 
606
            {
 
607
                buffer.append( matcher.group() );
 
608
            }
 
609
            
 
610
            string = buffer.toString();
 
611
            
 
612
            string = string.replaceAll( EMPTY_REPLACEMENT + "+", EMPTY_REPLACEMENT );
 
613
            
 
614
            if ( string.length() > 255 )
 
615
            {
 
616
                string = string.substring( 0, 255 );
 
617
            }
 
618
        }
 
619
        
 
620
        return string;
 
621
    }
 
622
    
 
623
    // -------------------------------------------------------------------------
 
624
    // Equals and hashCode
 
625
    // -------------------------------------------------------------------------
 
626
 
 
627
    @Override
 
628
    public int hashCode()
 
629
    {
 
630
        final int PRIME = 31;
 
631
        
 
632
        int result = 1;
 
633
        
 
634
        result = PRIME * result + ( ( name == null ) ? 0 : name.hashCode() );
 
635
        
 
636
        return result;
 
637
    }
 
638
 
 
639
    @Override
 
640
    public boolean equals( Object object )
 
641
    {
 
642
        if ( this == object )
 
643
        {
 
644
            return true;
 
645
        }
 
646
        
 
647
        if ( object == null )
 
648
        {
 
649
            return false;
 
650
        }
 
651
        
 
652
        if ( getClass() != object.getClass() )
 
653
        {
 
654
            return false;
 
655
        }
 
656
        
 
657
        final ReportTable other = (ReportTable) object;
 
658
        
 
659
        return name.equals( other.getName() );
 
660
    }
 
661
    
 
662
    // -------------------------------------------------------------------------
 
663
    // Get- and set-methods for persisted properties
 
664
    // -------------------------------------------------------------------------
 
665
 
 
666
    public int getId()
 
667
    {
 
668
        return id;
 
669
    }
 
670
 
 
671
    public void setId( int id )
 
672
    {
 
673
        this.id = id;
 
674
    }
 
675
    
 
676
    public String getName()
 
677
    {
 
678
        return name;
 
679
    }
 
680
    
 
681
    public void setName( String name )
 
682
    {
 
683
        this.name = name;
 
684
    }
 
685
 
 
686
    public String getTableName()
 
687
    {
 
688
        return tableName;
 
689
    }
 
690
    
 
691
    public void setTableName( String tableName )
 
692
    {
 
693
        this.tableName = tableName;
 
694
    }
 
695
    
 
696
    public String getExistingTableName()
 
697
    {
 
698
        return existingTableName;
 
699
    }
 
700
 
 
701
    public void setExistingTableName( String existingTableName )
 
702
    {
 
703
        this.existingTableName = existingTableName;
 
704
    }
 
705
 
 
706
    public String getMode()
 
707
    {
 
708
        return mode;
 
709
    }
 
710
 
 
711
    public void setMode( String mode )
 
712
    {
 
713
        this.mode = mode;
 
714
    }
 
715
 
 
716
    public Boolean getRegression()
 
717
    {
 
718
        return regression;
 
719
    }
 
720
 
 
721
    public void setRegression( Boolean regression )
 
722
    {
 
723
        this.regression = regression;
 
724
    }
 
725
    
 
726
    public List<DataElement> getDataElements()
 
727
    {
 
728
        return dataElements;
 
729
    }
 
730
 
 
731
    public void setDataElements( List<DataElement> dataElements )
 
732
    {
 
733
        this.dataElements = dataElements;
 
734
    }
 
735
 
 
736
    public List<DataElementCategoryOptionCombo> getCategoryOptionCombos()
 
737
    {
 
738
        return categoryOptionCombos;
 
739
    }
 
740
 
 
741
    public void setCategoryOptionCombos( List<DataElementCategoryOptionCombo> categoryOptionCombos )
 
742
    {
 
743
        this.categoryOptionCombos = categoryOptionCombos;
 
744
    }
 
745
    
 
746
    public List<Indicator> getIndicators()
 
747
    {
 
748
        return indicators;
 
749
    }
 
750
 
 
751
    public void setIndicators( List<Indicator> indicators )
 
752
    {
 
753
        this.indicators = indicators;
 
754
    }
 
755
 
 
756
    public List<Period> getPeriods()
 
757
    {
 
758
        return periods;
 
759
    }
 
760
 
 
761
    public List<DataSet> getDataSets()
 
762
    {
 
763
        return dataSets;
 
764
    }
 
765
 
 
766
    public void setDataSets( List<DataSet> dataSets )
 
767
    {
 
768
        this.dataSets = dataSets;
 
769
    }
 
770
 
 
771
    public void setPeriods( List<Period> periods )
 
772
    {
 
773
        this.periods = periods;
 
774
    }
 
775
 
 
776
    public List<OrganisationUnit> getUnits()
 
777
    {
 
778
        return units;
 
779
    }
 
780
 
 
781
    public void setUnits( List<OrganisationUnit> units )
 
782
    {
 
783
        this.units = units;
 
784
    }
 
785
 
 
786
    public Boolean getDoIndicators()
 
787
    {
 
788
        return doIndicators;
 
789
    }
 
790
 
 
791
    public void setDoIndicators( Boolean doIndicators )
 
792
    {
 
793
        this.doIndicators = doIndicators;
 
794
    }
 
795
 
 
796
    public Boolean getDoCategoryOptionCombos()
 
797
    {
 
798
        return doCategoryOptionCombos;
 
799
    }
 
800
 
 
801
    public void setDoCategoryOptionCombos( Boolean doCategoryOptionCombos )
 
802
    {
 
803
        this.doCategoryOptionCombos = doCategoryOptionCombos;
 
804
    }
 
805
 
 
806
    public Boolean getDoPeriods()
 
807
    {
 
808
        return doPeriods;
 
809
    }
 
810
 
 
811
    public void setDoPeriods( Boolean doPeriods )
 
812
    {
 
813
        this.doPeriods = doPeriods;
 
814
    }
 
815
 
 
816
    public Boolean getDoUnits()
 
817
    {
 
818
        return doUnits;
 
819
    }
 
820
 
 
821
    public void setDoUnits( Boolean doUnits )
 
822
    {
 
823
        this.doUnits = doUnits;
 
824
    }
 
825
    
 
826
    public RelativePeriods getRelatives()
 
827
    {
 
828
        return relatives;
 
829
    }
 
830
 
 
831
    public void setRelatives( RelativePeriods relatives )
 
832
    {
 
833
        this.relatives = relatives;
 
834
    }
 
835
 
 
836
    public ReportParams getReportParams()
 
837
    {
 
838
        return reportParams;
 
839
    }
 
840
 
 
841
    public void setReportParams( ReportParams reportParams )
 
842
    {
 
843
        this.reportParams = reportParams;
 
844
    }
 
845
 
 
846
    // -------------------------------------------------------------------------
 
847
    // Get- and set-methods for transient properties
 
848
    // -------------------------------------------------------------------------
 
849
 
 
850
    public List<Period> getRelativePeriods()
 
851
    {
 
852
        return relativePeriods;
 
853
    }
 
854
 
 
855
    public void setRelativePeriods( List<Period> relativePeriods )
 
856
    {
 
857
        this.relativePeriods = relativePeriods;
 
858
    }
 
859
 
 
860
    public List<Period> getAllPeriods()
 
861
    {
 
862
        return allPeriods;
 
863
    }
 
864
 
 
865
    public I18nFormat getI18nFormat()
 
866
    {
 
867
        return i18nFormat;
 
868
    }
 
869
 
 
870
    public void setI18nFormat( I18nFormat format )
 
871
    {
 
872
        i18nFormat = format;
 
873
    }
 
874
 
 
875
    public List<MetaObject> getReportIndicators()
 
876
    {
 
877
        return reportIndicators;
 
878
    }
 
879
 
 
880
    public List<DataElementCategoryOptionCombo> getReportCategoryOptionCombos()
 
881
    {
 
882
        return reportCategoryOptionCombos;
 
883
    }
 
884
 
 
885
    public List<Period> getReportPeriods()
 
886
    {
 
887
        return reportPeriods;
 
888
    }
 
889
 
 
890
    public List<OrganisationUnit> getReportUnits()
 
891
    {
 
892
        return reportUnits;
 
893
    }
 
894
    
 
895
    public List<String> getIndexColumns()
 
896
    {
 
897
        return indexColumns;
 
898
    }
 
899
 
 
900
    public List<String> getIndexNameColumns()
 
901
    {
 
902
        return indexNameColumns;
 
903
    }
 
904
 
 
905
    public List<String> getSelectColumns()
 
906
    {
 
907
        return selectColumns;
 
908
    }
 
909
 
 
910
    public List<String> getCrossTabColumns()
 
911
    {
 
912
        return crossTabColumns;
 
913
    }
 
914
 
 
915
    public List<String> getCrossTabIdentifiers()
 
916
    {
 
917
        return crossTabIdentifiers;
 
918
    }
 
919
 
 
920
    public String getReportingMonthName()
 
921
    {
 
922
        return reportingMonthName;
 
923
    }
 
924
 
 
925
    public void setReportingMonthName( String reportingMonthName )
 
926
    {
 
927
        this.reportingMonthName = reportingMonthName;
 
928
    }
 
929
}