~dhis2-devs-core/dhis2/dhis2-patient-tz

« back to all changes in this revision

Viewing changes to local/tz/dhis-web-caseentry-tz/src/main/java/org/hisp/dhis/reports/util/ReportService.java

  • Committer: John Francis Mukulu
  • Date: 2011-08-09 06:36:18 UTC
  • mfrom: (4244.1.21 dhis2)
  • Revision ID: john.f.mukulu@gmail.com-20110809063618-wad1pcc9fd1mnc6k
[merge]

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
package org.hisp.dhis.reports.util;
2
 
 
3
 
import java.util.ArrayList;
4
 
import java.util.Calendar;
5
 
import java.util.Collections;
6
 
import java.util.Date;
7
 
import java.util.Iterator;
8
 
import java.util.List;
9
 
import java.util.regex.Matcher;
10
 
import java.util.regex.Pattern;
11
 
 
12
 
import org.hisp.dhis.aggregation.AggregationService;
13
 
import org.hisp.dhis.dataelement.DataElement;
14
 
import org.hisp.dhis.dataelement.DataElementCategoryOptionCombo;
15
 
import org.hisp.dhis.dataelement.DataElementCategoryService;
16
 
import org.hisp.dhis.dataelement.DataElementService;
17
 
import org.hisp.dhis.dataset.DataSet;
18
 
import org.hisp.dhis.dataset.DataSetService;
19
 
import org.hisp.dhis.datavalue.DataValue;
20
 
import org.hisp.dhis.datavalue.DataValueService;
21
 
import org.hisp.dhis.indicator.Indicator;
22
 
import org.hisp.dhis.indicator.IndicatorService;
23
 
import org.hisp.dhis.organisationunit.OrganisationUnit;
24
 
import org.hisp.dhis.period.Period;
25
 
import org.hisp.dhis.period.PeriodService;
26
 
import org.hisp.dhis.period.PeriodType;
27
 
import org.hisp.dhis.system.util.MathUtils;
28
 
import org.springframework.jdbc.core.JdbcTemplate;
29
 
import org.springframework.jdbc.support.rowset.SqlRowSet;
30
 
 
31
 
public class ReportService
32
 
{
33
 
 
34
 
    public static final String KEY_RAFOLDER = "reportfolder";
35
 
 
36
 
    public static final String NULL_REPLACEMENT = "0";
37
 
 
38
 
    // -------------------------------------------------------------------------
39
 
    // Dependencies
40
 
    // -------------------------------------------------------------------------
41
 
 
42
 
    private PeriodService periodService;
43
 
 
44
 
    public void setPeriodService( PeriodService periodService )
45
 
    {
46
 
        this.periodService = periodService;
47
 
    }
48
 
 
49
 
    /*
50
 
     * private DBConnection dbConnection;
51
 
     * 
52
 
     * public void setDbConnection( DBConnection dbConnection ) {
53
 
     * this.dbConnection = dbConnection; }
54
 
     */
55
 
 
56
 
    private JdbcTemplate jdbcTemplate;
57
 
 
58
 
    public void setJdbcTemplate( JdbcTemplate jdbcTemplate )
59
 
    {
60
 
        this.jdbcTemplate = jdbcTemplate;
61
 
    }
62
 
 
63
 
    private DataElementService dataElementService;
64
 
 
65
 
    public void setDataElementService( DataElementService dataElementService )
66
 
    {
67
 
        this.dataElementService = dataElementService;
68
 
    }
69
 
 
70
 
    private AggregationService aggregationService;
71
 
 
72
 
    public void setAggregationService( AggregationService aggregationService )
73
 
    {
74
 
        this.aggregationService = aggregationService;
75
 
    }
76
 
 
77
 
    private DataElementCategoryService dataElementCategoryService;
78
 
 
79
 
    public void setDataElementCategoryService( DataElementCategoryService dataElementCategoryService )
80
 
    {
81
 
        this.dataElementCategoryService = dataElementCategoryService;
82
 
    }
83
 
 
84
 
    private DataValueService dataValueService;
85
 
 
86
 
    public void setDataValueService( DataValueService dataValueService )
87
 
    {
88
 
        this.dataValueService = dataValueService;
89
 
    }
90
 
 
91
 
    private DataSetService dataSetService;
92
 
 
93
 
    public void setDataSetService( DataSetService dataSetService )
94
 
    {
95
 
        this.dataSetService = dataSetService;
96
 
    }
97
 
 
98
 
    private IndicatorService indicatorService;
99
 
 
100
 
    public void setIndicatorService( IndicatorService indicatorService )
101
 
    {
102
 
        this.indicatorService = indicatorService;
103
 
    }
104
 
 
105
 
 
106
 
 
107
 
    // -------------------------------------------------------------------------
108
 
    // Services
109
 
    // -------------------------------------------------------------------------
110
 
 
111
 
    public List<Period> getMonthlyPeriods( Date start, Date end )
112
 
    {
113
 
        List<Period> periodList = new ArrayList<Period>( periodService.getPeriodsBetweenDates( start, end ) );
114
 
        PeriodType monthlyPeriodType = PeriodType.getByNameIgnoreCase( "monthly" );
115
 
 
116
 
        List<Period> monthlyPeriodList = new ArrayList<Period>();
117
 
        for ( Period period : periodList )
118
 
        {
119
 
            if ( period.getPeriodType().getId() == monthlyPeriodType.getId() )
120
 
            {
121
 
                monthlyPeriodList.add( period );
122
 
            }
123
 
        }
124
 
        return monthlyPeriodList;
125
 
    }
126
 
 
127
 
    /*
128
 
     * Returns the Period Object of the given date For ex:- if the month is 3,
129
 
     * year is 2006 and periodType Object of type Monthly then it returns the
130
 
     * corresponding Period Object
131
 
     */
132
 
    public Period getPeriodByMonth( int month, int year, PeriodType periodType )
133
 
    {
134
 
        int monthDays[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
135
 
 
136
 
        Calendar cal = Calendar.getInstance();
137
 
        cal.set( year, month, 1, 0, 0, 0 );
138
 
        Date firstDay = new Date( cal.getTimeInMillis() );
139
 
 
140
 
        if ( periodType.getName().equals( "Monthly" ) )
141
 
        {
142
 
            cal.set( year, month, 1, 0, 0, 0 );
143
 
            if ( year % 4 == 0 )
144
 
            {
145
 
                cal.set( Calendar.DAY_OF_MONTH, monthDays[month] + 1 );
146
 
            }
147
 
            else
148
 
            {
149
 
                cal.set( Calendar.DAY_OF_MONTH, monthDays[month] );
150
 
            }
151
 
        }
152
 
        else if ( periodType.getName().equals( "Yearly" ) )
153
 
        {
154
 
            cal.set( year, Calendar.DECEMBER, 31 );
155
 
        }
156
 
        Date lastDay = new Date( cal.getTimeInMillis() );
157
 
        System.out.println( lastDay.toString() );
158
 
        Period newPeriod = new Period();
159
 
        newPeriod = periodService.getPeriod( firstDay, lastDay, periodType );
160
 
        return newPeriod;
161
 
    }
162
 
 
163
 
    /*
164
 
     * Returns the child tree of the selected Orgunit
165
 
     */
166
 
    public List<OrganisationUnit> getAllChildren( OrganisationUnit selecteOU )
167
 
    {
168
 
        List<OrganisationUnit> ouList = new ArrayList<OrganisationUnit>();
169
 
        Iterator it = selecteOU.getChildren().iterator();
170
 
        while ( it.hasNext() )
171
 
        {
172
 
            OrganisationUnit orgU = (OrganisationUnit) it.next();
173
 
            ouList.add( orgU );
174
 
        }
175
 
        return ouList;
176
 
    }
177
 
 
178
 
    public List<Integer> getLinelistingRecordNos( OrganisationUnit organisationUnit, Period period, String lltype )
179
 
    {
180
 
        List<Integer> recordNosList = new ArrayList<Integer>();
181
 
 
182
 
        // Connection con = dbConnection.openConnection();
183
 
 
184
 
        // Statement st = null;
185
 
 
186
 
        // ResultSet rs1 = null;
187
 
 
188
 
        String query = "";
189
 
 
190
 
        int dataElementid = 1020;
191
 
 
192
 
        /*
193
 
         * if( lltype.equalsIgnoreCase( "lllivebirth" ) ) dataElementid =
194
 
         * LLDataSets.LLB_CHILD_NAME; else if( lltype.equalsIgnoreCase(
195
 
         * "lllivebirth" ) ) dataElementid = LLDataSets.LLD_CHILD_NAME; else if(
196
 
         * lltype.equalsIgnoreCase( "lllivebirth" ) ) dataElementid =
197
 
         * LLDataSets.LLMD_MOTHER_NAME;
198
 
         */
199
 
 
200
 
        if ( lltype.equalsIgnoreCase( "lllivebirth-l4" ) || lltype.equalsIgnoreCase( "lllivebirth-l5" )
201
 
            || lltype.equalsIgnoreCase( "lllivebirth-l6" ) )
202
 
            dataElementid = 1020;
203
 
        else if ( lltype.equalsIgnoreCase( "lldeath-l4" ) || lltype.equalsIgnoreCase( "lldeath-l5" )
204
 
            || lltype.equalsIgnoreCase( "lldeath-l6" ) )
205
 
            dataElementid = 1027;
206
 
        else if ( lltype.equalsIgnoreCase( "llmaternaldeath-l4" ) || lltype.equalsIgnoreCase( "llmaternaldeath-l5" )
207
 
            || lltype.equalsIgnoreCase( "llmaternaldeath-l6" ) )
208
 
            dataElementid = 1032;
209
 
 
210
 
        try
211
 
        {
212
 
            // st = con.createStatement();
213
 
 
214
 
            query = "SELECT recordno FROM lldatavalue WHERE dataelementid = " + dataElementid + " AND periodid = "
215
 
                + period.getId() + " AND sourceid = " + organisationUnit.getId();
216
 
            // rs1 = st.executeQuery( query );
217
 
 
218
 
            SqlRowSet rs1 = jdbcTemplate.queryForRowSet( query );
219
 
 
220
 
            while ( rs1.next() )
221
 
            {
222
 
                recordNosList.add( rs1.getInt( 1 ) );
223
 
            }
224
 
 
225
 
            Collections.sort( recordNosList );
226
 
        }
227
 
        catch ( Exception e )
228
 
        {
229
 
            System.out.println( "SQL Exception : " + e.getMessage() );
230
 
            return null;
231
 
        }
232
 
        /*
233
 
         * finally { try { if(st != null) st.close(); if(rs1 != null)
234
 
         * rs1.close();
235
 
         * 
236
 
         * if(con != null) con.close(); } catch( Exception e ) {
237
 
         * System.out.println("SQL Exception : "+e.getMessage()); return null; }
238
 
         * }
239
 
         */// finally block end
240
 
        return recordNosList;
241
 
    }
242
 
 
243
 
    public String getRAFolderName()
244
 
    {
245
 
        // Connection con = dbConnection.openConnection();
246
 
 
247
 
        // Statement st = null;
248
 
 
249
 
        // ResultSet rs1 = null;
250
 
 
251
 
        String raFolderName = "ra_national";
252
 
 
253
 
        String query = "";
254
 
 
255
 
        try
256
 
        {
257
 
            // st = con.createStatement();
258
 
 
259
 
            /*
260
 
             * query = "SELECT mvalue FROM maintenancein WHERE mkey LIKE '" +
261
 
             * KEY_RAFOLDER + "'"; //rs1 = st.executeQuery( query );
262
 
             * 
263
 
             * SqlRowSet rs1 = jdbcTemplate.queryForRowSet( query );
264
 
             * 
265
 
             * if(rs1.next()) { raFolderName = rs1.getString( 1 ); }
266
 
             */
267
 
            //raFolderName = configurationService.getConfigurationByKey( Configuration_IN.KEY_REPORTFOLDER ).getValue();
268
 
        }
269
 
        catch ( Exception e )
270
 
        {
271
 
            System.out.println( "Exception : " + e.getMessage() );
272
 
            return null;
273
 
        }
274
 
        /*
275
 
         * finally { try { if(st != null) st.close(); if(con != null)
276
 
         * con.close(); } catch( Exception e ) {
277
 
         * System.out.println("SQL Exception : "+e.getMessage()); return null; }
278
 
         * }
279
 
         */// finally block end
280
 
        return raFolderName;
281
 
 
282
 
    }
283
 
 
284
 
    /*
285
 
     * Only report period type if element is in a data set?
286
 
     * 
287
 
     * Returns the PeriodType Object for selected DataElement, If no PeriodType
288
 
     * is found then by default returns Monthly Period type
289
 
     */
290
 
    public PeriodType getDataElementPeriodType( DataElement de )
291
 
    {
292
 
        for ( DataSet ds : dataSetService.getAllDataSets() )
293
 
        {
294
 
            if ( ds.getDataElements().contains( de ) )
295
 
            {
296
 
                return ds.getPeriodType();
297
 
            }
298
 
        }
299
 
 
300
 
        return null;
301
 
 
302
 
    }
303
 
 
304
 
    private String getResultDataValue( String formula, Date startDate, Date endDate, OrganisationUnit organisationUnit )
305
 
    {
306
 
        try
307
 
        {
308
 
            // System.out.println( "expression : " + formula + " ***** " +
309
 
            // String.valueOf( startDate ) + " **** "
310
 
            // + String.valueOf( endDate ) );
311
 
 
312
 
            int deFlag1 = 0;
313
 
            int deFlag2 = 0;
314
 
            Pattern pattern = Pattern.compile( "(\\[\\d+\\.\\d+\\])" );
315
 
 
316
 
            Matcher matcher = pattern.matcher( formula );
317
 
            StringBuffer buffer = new StringBuffer();
318
 
 
319
 
            String resultValue = "";
320
 
 
321
 
            while ( matcher.find() )
322
 
            {
323
 
                String replaceString = matcher.group();
324
 
 
325
 
                replaceString = replaceString.replaceAll( "[\\[\\]]", "" );
326
 
                String optionComboIdStr = replaceString.substring( replaceString.indexOf( '.' ) + 1,
327
 
                    replaceString.length() );
328
 
 
329
 
                replaceString = replaceString.substring( 0, replaceString.indexOf( '.' ) );
330
 
 
331
 
                int dataElementId = Integer.parseInt( replaceString );
332
 
                int optionComboId = Integer.parseInt( optionComboIdStr );
333
 
 
334
 
                DataElement dataElement = dataElementService.getDataElement( dataElementId );
335
 
                DataElementCategoryOptionCombo optionCombo = dataElementCategoryService
336
 
                    .getDataElementCategoryOptionCombo( optionComboId );
337
 
 
338
 
                if ( dataElement == null || optionCombo == null )
339
 
                {
340
 
                    replaceString = "";
341
 
                    matcher.appendReplacement( buffer, replaceString );
342
 
                    continue;
343
 
                }
344
 
                if ( dataElement.getType().equalsIgnoreCase( "int" ) )
345
 
                {
346
 
                    Double aggregatedValue = aggregationService.getAggregatedDataValue( dataElement, optionCombo,
347
 
                        startDate, endDate, organisationUnit );
348
 
                    if ( aggregatedValue == null )
349
 
                    {
350
 
                        replaceString = NULL_REPLACEMENT;
351
 
                    }
352
 
                    else
353
 
                    {
354
 
                        replaceString = String.valueOf( aggregatedValue );
355
 
 
356
 
                        deFlag2 = 1;
357
 
                    }
358
 
 
359
 
                }
360
 
                else
361
 
                {
362
 
                    deFlag1 = 1;
363
 
                    PeriodType dePeriodType = getDataElementPeriodType( dataElement );
364
 
                    List<Period> periodList = new ArrayList<Period>( periodService.getIntersectingPeriodsByPeriodType(
365
 
                        dePeriodType, startDate, endDate ) );
366
 
                    Period tempPeriod = new Period();
367
 
                    if ( periodList == null || periodList.isEmpty() )
368
 
                    {
369
 
                        replaceString = "";
370
 
                        matcher.appendReplacement( buffer, replaceString );
371
 
                        continue;
372
 
                    }
373
 
                    else
374
 
                    {
375
 
                        tempPeriod = (Period) periodList.get( 0 );
376
 
                    }
377
 
 
378
 
                    DataValue dataValue = dataValueService.getDataValue( organisationUnit, dataElement, tempPeriod,
379
 
                        optionCombo );
380
 
 
381
 
                    if ( dataValue != null )
382
 
                    {
383
 
                        // Works for both text and boolean data types
384
 
 
385
 
                        replaceString = dataValue.getValue();
386
 
                    }
387
 
 
388
 
                    else
389
 
                        replaceString = "";
390
 
 
391
 
                    if ( replaceString == null )
392
 
                        replaceString = "";
393
 
                }
394
 
                matcher.appendReplacement( buffer, replaceString );
395
 
 
396
 
                resultValue = replaceString;
397
 
            }
398
 
 
399
 
            matcher.appendTail( buffer );
400
 
 
401
 
            if ( deFlag1 == 0 )
402
 
            {
403
 
 
404
 
                double d = 0.0;
405
 
                try
406
 
                {
407
 
                    d = MathUtils.calculateExpression( buffer.toString() );
408
 
                }
409
 
                catch ( Exception e )
410
 
                {
411
 
                    d = 0.0;
412
 
                    resultValue = "";
413
 
                }
414
 
                if ( d == -1 )
415
 
                {
416
 
                    d = 0.0;
417
 
                    resultValue = "";
418
 
                }
419
 
                else
420
 
                {
421
 
 
422
 
                    // This is to display financial data as it is like 2.1476838
423
 
                    resultValue = "" + d;
424
 
 
425
 
                    // These lines are to display financial data that do not
426
 
                    // have decimals
427
 
                    d = d * 10;
428
 
 
429
 
                    if ( d % 10 == 0 )
430
 
                    {
431
 
                        resultValue = "" + (int) d / 10;
432
 
                    }
433
 
 
434
 
                    d = d / 10;
435
 
 
436
 
                    // These line are to display non financial data that do not
437
 
                    // require decimals
438
 
                    // if ( !(reportModelTB.equalsIgnoreCase( "STATIC-FINANCIAL"
439
 
                    // )) )
440
 
                    resultValue = "" + (int) d;
441
 
 
442
 
                    // if ( resultValue.equalsIgnoreCase( "0" ) )
443
 
                    // {
444
 
                    // resultValue = "";
445
 
                    // }
446
 
                }
447
 
 
448
 
            }
449
 
            else
450
 
            {
451
 
                resultValue = buffer.toString();
452
 
            }
453
 
 
454
 
            if ( resultValue.equalsIgnoreCase( "" ) )
455
 
                resultValue = " ";
456
 
 
457
 
            return resultValue;
458
 
        }
459
 
        catch ( NumberFormatException ex )
460
 
        {
461
 
            throw new RuntimeException( "Illegal DataElement id", ex );
462
 
        }
463
 
    }
464
 
 
465
 
    private String getIndividualResultDataValue( String formula, Date startDate, Date endDate,
466
 
        OrganisationUnit organisationUnit )
467
 
    {
468
 
        try
469
 
        {
470
 
            int deFlag1 = 0;
471
 
            int deFlag2 = 0;
472
 
            Pattern pattern = Pattern.compile( "(\\[\\d+\\.\\d+\\])" );
473
 
 
474
 
            Matcher matcher = pattern.matcher( formula );
475
 
            StringBuffer buffer = new StringBuffer();
476
 
 
477
 
            String resultValue = "";
478
 
            boolean valueDoesNotExist = true;
479
 
 
480
 
            while ( matcher.find() )
481
 
            {
482
 
 
483
 
                String replaceString = matcher.group();
484
 
 
485
 
                replaceString = replaceString.replaceAll( "[\\[\\]]", "" );
486
 
                String optionComboIdStr = replaceString.substring( replaceString.indexOf( '.' ) + 1,
487
 
                    replaceString.length() );
488
 
 
489
 
                replaceString = replaceString.substring( 0, replaceString.indexOf( '.' ) );
490
 
 
491
 
                int dataElementId = Integer.parseInt( replaceString );
492
 
                int optionComboId = Integer.parseInt( optionComboIdStr );
493
 
 
494
 
                DataElement dataElement = dataElementService.getDataElement( dataElementId );
495
 
                DataElementCategoryOptionCombo optionCombo = dataElementCategoryService
496
 
                    .getDataElementCategoryOptionCombo( optionComboId );
497
 
 
498
 
                if ( dataElement == null || optionCombo == null )
499
 
                {
500
 
                    replaceString = "";
501
 
                    matcher.appendReplacement( buffer, replaceString );
502
 
                    continue;
503
 
                }
504
 
                if ( dataElement.getType().equalsIgnoreCase( "int" ) )
505
 
                {
506
 
 
507
 
                    PeriodType dePeriodType = getDataElementPeriodType( dataElement );
508
 
                    List<Period> periodList = new ArrayList<Period>( periodService.getIntersectingPeriodsByPeriodType(
509
 
                        dePeriodType, startDate, endDate ) );
510
 
 
511
 
                    if ( periodList == null || periodList.isEmpty() )
512
 
                    {
513
 
                        replaceString = "";
514
 
                        matcher.appendReplacement( buffer, replaceString );
515
 
                        continue;
516
 
                    }
517
 
                    else
518
 
                    {
519
 
 
520
 
                        double aggregatedValue = 0.0;
521
 
                        for ( Period tempPeriod : periodList )
522
 
                        {
523
 
                            DataValue dataValue = dataValueService.getDataValue( organisationUnit, dataElement,
524
 
                                tempPeriod, optionCombo );
525
 
 
526
 
                            if ( dataValue != null )
527
 
                            {
528
 
                                aggregatedValue += Double.parseDouble( dataValue.getValue() );
529
 
 
530
 
                                valueDoesNotExist = false;
531
 
                            }
532
 
                        }
533
 
 
534
 
                        replaceString = String.valueOf( aggregatedValue );
535
 
 
536
 
                        deFlag2 = 1;
537
 
                    }
538
 
 
539
 
                }
540
 
                else
541
 
                {
542
 
                    deFlag1 = 1;
543
 
                    PeriodType dePeriodType = getDataElementPeriodType( dataElement );
544
 
                    List<Period> periodList = new ArrayList<Period>( periodService.getIntersectingPeriodsByPeriodType(
545
 
                        dePeriodType, startDate, endDate ) );
546
 
                    Period tempPeriod = new Period();
547
 
                    if ( periodList == null || periodList.isEmpty() )
548
 
                    {
549
 
                        replaceString = "";
550
 
                        matcher.appendReplacement( buffer, replaceString );
551
 
                        continue;
552
 
                    }
553
 
                    else
554
 
                    {
555
 
                        tempPeriod = (Period) periodList.get( 0 );
556
 
                    }
557
 
 
558
 
                    DataValue dataValue = dataValueService.getDataValue( organisationUnit, dataElement, tempPeriod,
559
 
                        optionCombo );
560
 
 
561
 
                    if ( dataValue != null )
562
 
                    {
563
 
                        // Works for both text and boolean data types
564
 
 
565
 
                        replaceString = dataValue.getValue();
566
 
                        valueDoesNotExist = false;
567
 
                    }
568
 
 
569
 
                    else
570
 
                        replaceString = "";
571
 
 
572
 
                    if ( replaceString == null )
573
 
                        replaceString = "";
574
 
                }
575
 
                matcher.appendReplacement( buffer, replaceString );
576
 
 
577
 
                resultValue = replaceString;
578
 
            }
579
 
 
580
 
            matcher.appendTail( buffer );
581
 
 
582
 
            if ( deFlag1 == 0 )
583
 
            {
584
 
                double d = 0.0;
585
 
                try
586
 
                {
587
 
                    d = MathUtils.calculateExpression( buffer.toString() );
588
 
                }
589
 
                catch ( Exception e )
590
 
                {
591
 
                    d = 0.0;
592
 
 
593
 
                    resultValue = "";
594
 
                }
595
 
                if ( d == -1 )
596
 
                {
597
 
                    d = 0.0;
598
 
 
599
 
                    resultValue = "";
600
 
                }
601
 
                else
602
 
                {
603
 
                    // This is to display financial data as it is like 2.1476838
604
 
                    resultValue = "" + d;
605
 
 
606
 
                    // These lines are to display financial data that do not
607
 
                    // have decimals
608
 
                    d = d * 10;
609
 
 
610
 
                    if ( d % 10 == 0 )
611
 
                    {
612
 
                        resultValue = "" + (int) d / 10;
613
 
                    }
614
 
 
615
 
                    d = d / 10;
616
 
 
617
 
                    // These line are to display non financial data that do not
618
 
                    // require decimals
619
 
                    // if ( !(reportModelTB.equalsIgnoreCase( "STATIC-FINANCIAL"
620
 
                    // )) )
621
 
                    resultValue = "" + (int) d;
622
 
 
623
 
                    // if ( resultValue.equalsIgnoreCase( "0" ) )
624
 
                    // {
625
 
                    // resultValue = "";
626
 
                    // }
627
 
                }
628
 
            }
629
 
            else
630
 
            {
631
 
                resultValue = buffer.toString();
632
 
            }
633
 
 
634
 
            if ( valueDoesNotExist )
635
 
                resultValue = " ";
636
 
 
637
 
            if ( resultValue.equalsIgnoreCase( "" ) )
638
 
                resultValue = " ";
639
 
 
640
 
            return resultValue;
641
 
        }
642
 
        catch ( NumberFormatException ex )
643
 
        {
644
 
            throw new RuntimeException( "Illegal DataElement id", ex );
645
 
        }
646
 
    }
647
 
 
648
 
    private String getBooleanDataValue( String formula, Date startDate, Date endDate, OrganisationUnit organisationUnit )
649
 
    {
650
 
        try
651
 
        {
652
 
            int deFlag1 = 0;
653
 
            int deFlag2 = 0;
654
 
            Pattern pattern = Pattern.compile( "(\\[\\d+\\.\\d+\\])" );
655
 
 
656
 
            Matcher matcher = pattern.matcher( formula );
657
 
            StringBuffer buffer = new StringBuffer();
658
 
 
659
 
            while ( matcher.find() )
660
 
            {
661
 
                String replaceString = matcher.group();
662
 
 
663
 
                replaceString = replaceString.replaceAll( "[\\[\\]]", "" );
664
 
                String optionComboIdStr = replaceString.substring( replaceString.indexOf( '.' ) + 1,
665
 
                    replaceString.length() );
666
 
 
667
 
                replaceString = replaceString.substring( 0, replaceString.indexOf( '.' ) );
668
 
 
669
 
                int dataElementId = Integer.parseInt( replaceString );
670
 
                int optionComboId = Integer.parseInt( optionComboIdStr );
671
 
 
672
 
                DataElement dataElement = dataElementService.getDataElement( dataElementId );
673
 
                DataElementCategoryOptionCombo optionCombo = dataElementCategoryService
674
 
                    .getDataElementCategoryOptionCombo( optionComboId );
675
 
 
676
 
                if ( dataElement == null || optionCombo == null )
677
 
                {
678
 
                    replaceString = "";
679
 
                    matcher.appendReplacement( buffer, replaceString );
680
 
                    continue;
681
 
                }
682
 
 
683
 
                if ( dataElement.getType().equalsIgnoreCase( "bool" ) )
684
 
                {
685
 
                    deFlag1 = 1;
686
 
                    PeriodType dePeriodType = getDataElementPeriodType( dataElement );
687
 
                    List<Period> periodList = new ArrayList<Period>( periodService.getIntersectingPeriodsByPeriodType(
688
 
                        dePeriodType, startDate, endDate ) );
689
 
                    Period tempPeriod = new Period();
690
 
                    if ( periodList == null || periodList.isEmpty() )
691
 
                    {
692
 
                        replaceString = "";
693
 
                        matcher.appendReplacement( buffer, replaceString );
694
 
                        continue;
695
 
                    }
696
 
                    else
697
 
                    {
698
 
                        tempPeriod = (Period) periodList.get( 0 );
699
 
                    }
700
 
 
701
 
                    DataValue dataValue = dataValueService.getDataValue( organisationUnit, dataElement, tempPeriod,
702
 
                        optionCombo );
703
 
 
704
 
                    if ( dataValue != null )
705
 
                    {
706
 
                        // Works for both text and boolean data types
707
 
 
708
 
                        if ( dataValue.getValue().equalsIgnoreCase( "true" ) )
709
 
                        {
710
 
                            replaceString = "Yes";
711
 
                        }
712
 
                        else if ( dataValue.getValue().equalsIgnoreCase( "false" ) )
713
 
                        {
714
 
                            replaceString = "No";
715
 
                        }
716
 
                        else
717
 
                        {
718
 
                            replaceString = dataValue.getValue();
719
 
                        }
720
 
                    }
721
 
 
722
 
                    else
723
 
                    {
724
 
                        replaceString = "";
725
 
                    }
726
 
 
727
 
                }
728
 
                else
729
 
                {
730
 
                    Double aggregatedValue = aggregationService.getAggregatedDataValue( dataElement, optionCombo,
731
 
                        startDate, endDate, organisationUnit );
732
 
                    if ( aggregatedValue == null )
733
 
                    {
734
 
                        replaceString = NULL_REPLACEMENT;
735
 
                    }
736
 
                    else
737
 
                    {
738
 
                        replaceString = String.valueOf( aggregatedValue );
739
 
 
740
 
                        deFlag2 = 1;
741
 
                    }
742
 
                }
743
 
                matcher.appendReplacement( buffer, replaceString );
744
 
            }
745
 
 
746
 
            matcher.appendTail( buffer );
747
 
 
748
 
            String resultValue = "";
749
 
            if ( deFlag1 == 0 )
750
 
            {
751
 
                double d = 0.0;
752
 
                try
753
 
                {
754
 
                    d = MathUtils.calculateExpression( buffer.toString() );
755
 
                }
756
 
                catch ( Exception e )
757
 
                {
758
 
                    d = 0.0;
759
 
                }
760
 
                if ( d == -1 )
761
 
                {
762
 
                    d = 0.0;
763
 
                }
764
 
                else
765
 
                {
766
 
                    d = Math.round( d * Math.pow( 10, 1 ) ) / Math.pow( 10, 1 );
767
 
                    resultValue = "" + (int) d;
768
 
                }
769
 
 
770
 
                if ( deFlag2 == 0 )
771
 
                {
772
 
                    resultValue = " ";
773
 
                }
774
 
            }
775
 
            else
776
 
            {
777
 
                resultValue = buffer.toString();
778
 
            }
779
 
            return resultValue;
780
 
        }
781
 
        catch ( NumberFormatException ex )
782
 
        {
783
 
            throw new RuntimeException( "Illegal DataElement id", ex );
784
 
        }
785
 
    }
786
 
 
787
 
    private String getResultIndicatorValue( String formula, Date startDate, Date endDate,
788
 
        OrganisationUnit organisationUnit )
789
 
    {
790
 
        try
791
 
        {
792
 
 
793
 
            int deFlag1 = 0;
794
 
            int deFlag2 = 0;
795
 
            Pattern pattern = Pattern.compile( "(\\[\\d+\\.\\d+\\])" );
796
 
 
797
 
            Matcher matcher = pattern.matcher( formula );
798
 
            StringBuffer buffer = new StringBuffer();
799
 
 
800
 
            while ( matcher.find() )
801
 
            {
802
 
                String replaceString = matcher.group();
803
 
 
804
 
                replaceString = replaceString.replaceAll( "[\\[\\]]", "" );
805
 
 
806
 
                replaceString = replaceString.substring( 0, replaceString.indexOf( '.' ) );
807
 
 
808
 
                int indicatorId = Integer.parseInt( replaceString );
809
 
 
810
 
                Indicator indicator = indicatorService.getIndicator( indicatorId );
811
 
 
812
 
                if ( indicator == null )
813
 
                {
814
 
                    replaceString = "";
815
 
                    matcher.appendReplacement( buffer, replaceString );
816
 
                    continue;
817
 
 
818
 
                }
819
 
 
820
 
                Double aggregatedValue = aggregationService.getAggregatedIndicatorValue( indicator, startDate, endDate,
821
 
                    organisationUnit );
822
 
 
823
 
                if ( aggregatedValue == null )
824
 
                {
825
 
                    replaceString = NULL_REPLACEMENT;
826
 
                }
827
 
                else
828
 
                {
829
 
                    replaceString = String.valueOf( aggregatedValue );
830
 
                    deFlag2 = 1;
831
 
                }
832
 
                matcher.appendReplacement( buffer, replaceString );
833
 
            }
834
 
 
835
 
            matcher.appendTail( buffer );
836
 
 
837
 
            String resultValue = "";
838
 
            if ( deFlag1 == 0 )
839
 
            {
840
 
                double d = 0.0;
841
 
                try
842
 
                {
843
 
                    d = MathUtils.calculateExpression( buffer.toString() );
844
 
                }
845
 
                catch ( Exception e )
846
 
                {
847
 
                    d = 0.0;
848
 
                }
849
 
                if ( d == -1 )
850
 
                    d = 0.0;
851
 
                else
852
 
                {
853
 
                    d = Math.round( d * Math.pow( 10, 1 ) ) / Math.pow( 10, 1 );
854
 
                    resultValue = "" + d;
855
 
                }
856
 
 
857
 
                if ( deFlag2 == 0 )
858
 
                {
859
 
                    resultValue = " ";
860
 
                }
861
 
            }
862
 
            else
863
 
            {
864
 
                resultValue = buffer.toString();
865
 
            }
866
 
            return resultValue;
867
 
        }
868
 
        catch ( NumberFormatException ex )
869
 
        {
870
 
            throw new RuntimeException( "Illegal DataElement id", ex );
871
 
        }
872
 
    }
873
 
 
874
 
    private String getIndividualResultIndicatorValue( String formula, Date startDate, Date endDate,
875
 
        OrganisationUnit organisationUnit )
876
 
    {
877
 
        try
878
 
        {
879
 
 
880
 
            int deFlag1 = 0;
881
 
            int deFlag2 = 0;
882
 
            Pattern pattern = Pattern.compile( "(\\[\\d+\\.\\d+\\])" );
883
 
 
884
 
            Matcher matcher = pattern.matcher( formula );
885
 
            StringBuffer buffer = new StringBuffer();
886
 
 
887
 
            while ( matcher.find() )
888
 
            {
889
 
                String replaceString = matcher.group();
890
 
 
891
 
                replaceString = replaceString.replaceAll( "[\\[\\]]", "" );
892
 
 
893
 
                replaceString = replaceString.substring( 0, replaceString.indexOf( '.' ) );
894
 
 
895
 
                int indicatorId = Integer.parseInt( replaceString );
896
 
 
897
 
                Indicator indicator = indicatorService.getIndicator( indicatorId );
898
 
 
899
 
                if ( indicator == null )
900
 
                {
901
 
                    replaceString = "";
902
 
                    matcher.appendReplacement( buffer, replaceString );
903
 
                    continue;
904
 
 
905
 
                }
906
 
 
907
 
                String numeratorExp = indicator.getNumerator();
908
 
                String denominatorExp = indicator.getDenominator();
909
 
                int indicatorFactor = indicator.getIndicatorType().getFactor();
910
 
                String numeratorVal = getIndividualResultDataValue( numeratorExp, startDate, endDate, organisationUnit );
911
 
                String denominatorVal = getIndividualResultDataValue( denominatorExp, startDate, endDate,
912
 
                    organisationUnit );
913
 
 
914
 
                double numeratorValue;
915
 
                try
916
 
                {
917
 
                    numeratorValue = Double.parseDouble( numeratorVal );
918
 
                }
919
 
                catch ( Exception e )
920
 
                {
921
 
                    System.out.println( "Exception while getting Numerator : " + numeratorExp + " for Indicaotr "
922
 
                        + indicator.getName() );
923
 
                    numeratorValue = 0.0;
924
 
                }
925
 
 
926
 
                double denominatorValue;
927
 
                try
928
 
                {
929
 
                    denominatorValue = Double.parseDouble( denominatorVal );
930
 
                }
931
 
                catch ( Exception e )
932
 
                {
933
 
                    System.out.println( "Exception while getting Deniminator : " + denominatorExp + " for Indicaotr "
934
 
                        + indicator.getName() );
935
 
                    denominatorValue = 1.0;
936
 
                }
937
 
 
938
 
                double aggregatedValue;
939
 
                try
940
 
                {
941
 
                    aggregatedValue = (numeratorValue / denominatorValue) * indicatorFactor;
942
 
                }
943
 
                catch ( Exception e )
944
 
                {
945
 
                    System.out.println( "Exception while calculating Indicator value for Indicaotr "
946
 
                        + indicator.getName() );
947
 
                    aggregatedValue = 0.0;
948
 
                }
949
 
 
950
 
                replaceString = String.valueOf( aggregatedValue );
951
 
                deFlag2 = 1;
952
 
 
953
 
                matcher.appendReplacement( buffer, replaceString );
954
 
            }
955
 
 
956
 
            matcher.appendTail( buffer );
957
 
 
958
 
            String resultValue = "";
959
 
            if ( deFlag1 == 0 )
960
 
            {
961
 
                double d = 0.0;
962
 
                try
963
 
                {
964
 
                    d = MathUtils.calculateExpression( buffer.toString() );
965
 
                }
966
 
                catch ( Exception e )
967
 
                {
968
 
                    d = 0.0;
969
 
                }
970
 
                if ( d == -1 )
971
 
                    d = 0.0;
972
 
                else
973
 
                {
974
 
                    d = Math.round( d * Math.pow( 10, 1 ) ) / Math.pow( 10, 1 );
975
 
                    resultValue = "" + d;
976
 
                }
977
 
 
978
 
                if ( deFlag2 == 0 )
979
 
                {
980
 
                    resultValue = " ";
981
 
                }
982
 
            }
983
 
            else
984
 
            {
985
 
                resultValue = buffer.toString();
986
 
            }
987
 
            return resultValue;
988
 
        }
989
 
        catch ( NumberFormatException ex )
990
 
        {
991
 
            throw new RuntimeException( "Illegal DataElement id", ex );
992
 
        }
993
 
    }
994
 
 
995
 
}