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

« back to all changes in this revision

Viewing changes to local/in/dhis-web-dashboard/src/main/java/org/hisp/dhis/dashboard/ga/action/charts/GenerateChartDataAction.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.dashboard.ga.action.charts;
 
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.File;
 
31
import java.sql.Connection;
 
32
import java.sql.PreparedStatement;
 
33
import java.sql.ResultSet;
 
34
import java.util.ArrayList;
 
35
import java.util.Collection;
 
36
import java.util.Collections;
 
37
import java.util.Comparator;
 
38
import java.util.HashMap;
 
39
import java.util.Iterator;
 
40
import java.util.List;
 
41
import java.util.Map;
 
42
 
 
43
import javax.servlet.http.HttpServletRequest;
 
44
import javax.servlet.http.HttpSession;
 
45
import javax.xml.parsers.DocumentBuilder;
 
46
import javax.xml.parsers.DocumentBuilderFactory;
 
47
 
 
48
import org.hisp.dhis.aggregation.AggregationService;
 
49
import org.hisp.dhis.jdbc.StatementManager;
 
50
import org.hisp.dhis.dashboard.util.DBConnection;
 
51
import org.hisp.dhis.dashboard.util.DashBoardService;
 
52
import org.hisp.dhis.dashboard.util.SurveyData;
 
53
import org.hisp.dhis.dataelement.DataElement;
 
54
import org.hisp.dhis.dataelement.DataElementCategoryCombo;
 
55
import org.hisp.dhis.dataelement.DataElementCategoryOptionCombo;
 
56
import org.hisp.dhis.dataelement.DataElementCategoryOptionComboService;
 
57
import org.hisp.dhis.dataelement.DataElementGroup;
 
58
import org.hisp.dhis.dataelement.DataElementService;
 
59
import org.hisp.dhis.indicator.Indicator;
 
60
import org.hisp.dhis.indicator.IndicatorService;
 
61
import org.hisp.dhis.options.displayproperty.DisplayPropertyHandler;
 
62
import org.hisp.dhis.organisationunit.OrganisationUnit;
 
63
import org.hisp.dhis.organisationunit.OrganisationUnitGroup;
 
64
import org.hisp.dhis.organisationunit.OrganisationUnitGroupService;
 
65
import org.hisp.dhis.organisationunit.OrganisationUnitService;
 
66
import org.hisp.dhis.organisationunit.comparator.OrganisationUnitShortNameComparator;
 
67
import org.hisp.dhis.period.Period;
 
68
import org.hisp.dhis.period.PeriodStore;
 
69
import org.hisp.dhis.period.PeriodType;
 
70
import org.hisp.dhis.target.Target;
 
71
import org.hisp.dhis.target.TargetService;
 
72
import org.w3c.dom.Document;
 
73
import org.w3c.dom.Element;
 
74
import org.w3c.dom.NodeList;
 
75
import org.xml.sax.SAXException;
 
76
import org.xml.sax.SAXParseException;
 
77
 
 
78
import com.opensymphony.webwork.ServletActionContext;
 
79
import com.opensymphony.xwork.Action;
 
80
import com.opensymphony.xwork.ActionContext;
 
81
 
 
82
public class GenerateChartDataAction
 
83
    implements Action
 
84
{
 
85
 
 
86
    /* Dependencies */
 
87
 
 
88
    private TargetService targetService;
 
89
 
 
90
    public void setTargetService( TargetService targetService )
 
91
    {
 
92
        this.targetService = targetService;
 
93
    }
 
94
 
 
95
    private DataElementCategoryOptionComboService dataElementCategoryOptionComboService;
 
96
 
 
97
    public void setDataElementCategoryOptionComboService(
 
98
        DataElementCategoryOptionComboService dataElementCategoryOptionComboService )
 
99
    {
 
100
        this.dataElementCategoryOptionComboService = dataElementCategoryOptionComboService;
 
101
    }
 
102
 
 
103
    private OrganisationUnitService organisationUnitService;
 
104
 
 
105
    public void setOrganisationUnitService( OrganisationUnitService organisationUnitService )
 
106
    {
 
107
        this.organisationUnitService = organisationUnitService;
 
108
    }
 
109
 
 
110
    private PeriodStore periodStore;
 
111
 
 
112
    public void setPeriodStore( PeriodStore periodStore )
 
113
    {
 
114
        this.periodStore = periodStore;
 
115
    }
 
116
 
 
117
    private IndicatorService indicatorService;
 
118
 
 
119
    public void setIndicatorService( IndicatorService indicatorService )
 
120
    {
 
121
        this.indicatorService = indicatorService;
 
122
    }
 
123
 
 
124
    private DataElementService dataElementService;
 
125
 
 
126
    public void setDataElementService( DataElementService dataElementService )
 
127
    {
 
128
        this.dataElementService = dataElementService;
 
129
    }
 
130
 
 
131
    private DashBoardService dashBoardService;
 
132
 
 
133
    public void setDashBoardService( DashBoardService dashBoardService )
 
134
    {
 
135
        this.dashBoardService = dashBoardService;
 
136
    }
 
137
 
 
138
    private AggregationService aggregationService;
 
139
 
 
140
    public void setAggregationService( AggregationService aggregationService )
 
141
    {
 
142
        this.aggregationService = aggregationService;
 
143
    }
 
144
 
 
145
    private StatementManager statementManager;
 
146
 
 
147
    public void setStatementManager( StatementManager statementManager )
 
148
    {
 
149
        this.statementManager = statementManager;
 
150
    }
 
151
 
 
152
    private OrganisationUnitGroupService organisationUnitGroupService;
 
153
 
 
154
    public void setOrganisationUnitGroupService( OrganisationUnitGroupService organisationUnitGroupService )
 
155
    {
 
156
        this.organisationUnitGroupService = organisationUnitGroupService;
 
157
    }
 
158
 
 
159
    // -------------------------------------------------------------------------
 
160
    // Comparator
 
161
    // -------------------------------------------------------------------------
 
162
 
 
163
    private Comparator<DataElement> dataElementComparator;
 
164
 
 
165
    public void setDataElementComparator( Comparator<DataElement> dataElementComparator )
 
166
    {
 
167
        this.dataElementComparator = dataElementComparator;
 
168
    }
 
169
 
 
170
    private Comparator<Indicator> indicatorComparator;
 
171
 
 
172
    public void setIndicatorComparator( Comparator<Indicator> indicatorComparator )
 
173
    {
 
174
        this.indicatorComparator = indicatorComparator;
 
175
    }
 
176
 
 
177
    // -------------------------------------------------------------------------
 
178
    // DisplayPropertyHandler
 
179
    // -------------------------------------------------------------------------
 
180
 
 
181
    private DisplayPropertyHandler displayPropertyHandler;
 
182
 
 
183
    public void setDisplayPropertyHandler( DisplayPropertyHandler displayPropertyHandler )
 
184
    {
 
185
        this.displayPropertyHandler = displayPropertyHandler;
 
186
    }
 
187
 
 
188
    // --------------------------------------------------------------------------
 
189
    // Parameters
 
190
    // --------------------------------------------------------------------------
 
191
    private HttpSession session;
 
192
    
 
193
    public HttpSession getSession()
 
194
    {
 
195
        return session;
 
196
    }
 
197
 
 
198
    
 
199
    private Period startPeriod;
 
200
 
 
201
    private Period endPeriod;
 
202
 
 
203
    private OrganisationUnit selectedOrgUnit;
 
204
 
 
205
    private OrganisationUnitGroup selectedOrgUnitGroup;
 
206
 
 
207
    private List<Period> selectedPeriodList;
 
208
 
 
209
    private Map<String, Double> targetValues;
 
210
 
 
211
    private List<Double> targetList;
 
212
 
 
213
    public List<Double> getTargetList()
 
214
    {
 
215
        return targetList;
 
216
    }
 
217
 
 
218
    private List<DataElementCategoryOptionCombo> selectedOptionComboList;
 
219
 
 
220
    private List<Object> selectedServiceList;
 
221
 
 
222
    public List<Object> getSelectedServiceList()
 
223
    {
 
224
        return selectedServiceList;
 
225
    }
 
226
 
 
227
    private String[] series1;
 
228
 
 
229
    public String[] getSeries1()
 
230
    {
 
231
        return series1;
 
232
    }
 
233
 
 
234
    private String[] categories1;
 
235
 
 
236
    public String[] getCategories1()
 
237
    {
 
238
        return categories1;
 
239
    }
 
240
 
 
241
    private String[] series2;
 
242
 
 
243
    public String[] getSeries2()
 
244
    {
 
245
        return series2;
 
246
    }
 
247
 
 
248
    private String[] categories2;
 
249
 
 
250
    public String[] getCategories2()
 
251
    {
 
252
        return categories2;
 
253
    }
 
254
 
 
255
    String chartTitle = "Service : ";
 
256
 
 
257
    public String getChartTitle()
 
258
    {
 
259
        return chartTitle;
 
260
    }
 
261
 
 
262
    String xAxis_Title;
 
263
 
 
264
    public String getXAxis_Title()
 
265
    {
 
266
        return xAxis_Title;
 
267
    }
 
268
 
 
269
    String yAxis_Title;
 
270
 
 
271
    public String getYAxis_Title()
 
272
    {
 
273
        return yAxis_Title;
 
274
    }
 
275
 
 
276
    List<String> numeratorDEList;
 
277
 
 
278
    public List<String> getNumeratorDEList()
 
279
    {
 
280
        return numeratorDEList;
 
281
    }
 
282
 
 
283
    List<String> denominatorDEList;
 
284
 
 
285
    public List<String> getDenominatorDEList()
 
286
    {
 
287
        return denominatorDEList;
 
288
    }
 
289
 
 
290
    Double data1[][];
 
291
 
 
292
    public Double[][] getData1()
 
293
    {
 
294
        return data1;
 
295
    }
 
296
 
 
297
    Double data2[][];
 
298
 
 
299
    public Double[][] getData2()
 
300
    {
 
301
        return data2;
 
302
    }
 
303
 
 
304
    List<List<String>> dataList;
 
305
 
 
306
    public List<List<String>> getDataList()
 
307
    {
 
308
        return dataList;
 
309
    }
 
310
 
 
311
    List<List<String>> numList;
 
312
    List<List<String>> denList;
 
313
    
 
314
    
 
315
    
 
316
    List<String> xseriesList;
 
317
 
 
318
    public List<String> getXseriesList()
 
319
    {
 
320
        return xseriesList;
 
321
    }
 
322
 
 
323
    List<String> yseriesList;
 
324
 
 
325
    public List<String> getYseriesList()
 
326
    {
 
327
        return yseriesList;
 
328
    }
 
329
 
 
330
    Map<Indicator, List<Target>> indicatorTargetList;
 
331
 
 
332
    public Map<Indicator, List<Target>> getIndicatorTargetList()
 
333
    {
 
334
        return indicatorTargetList;
 
335
    }
 
336
 
 
337
    Map<Indicator, List<SurveyData>> indicatorSurveyList;
 
338
    
 
339
    
 
340
    /* Input Parameters */
 
341
    private String ougSetCB;
 
342
 
 
343
    public void setOugSetCB( String ougSetCB )
 
344
    {
 
345
        this.ougSetCB = ougSetCB;
 
346
    }
 
347
 
 
348
    private String deSelection;
 
349
 
 
350
    public void setDeSelection( String deSelection )
 
351
    {
 
352
        this.deSelection = deSelection;
 
353
    }
 
354
 
 
355
    private List<String> selectedDataElements;
 
356
 
 
357
    public void setSelectedDataElements( List<String> selectedDataElements )
 
358
    {
 
359
        this.selectedDataElements = selectedDataElements;
 
360
    }
 
361
 
 
362
    private List<String> selectedIndicators;
 
363
 
 
364
    public void setSelectedIndicators( List<String> selectedIndicators )
 
365
    {
 
366
        this.selectedIndicators = selectedIndicators;
 
367
    }
 
368
 
 
369
    private int sDateLB;
 
370
 
 
371
    public void setSDateLB( int dateLB )
 
372
    {
 
373
        sDateLB = dateLB;
 
374
    }
 
375
 
 
376
    private int eDateLB;
 
377
 
 
378
    public void setEDateLB( int dateLB )
 
379
    {
 
380
        eDateLB = dateLB;
 
381
    }
 
382
 
 
383
    private String riRadio;
 
384
 
 
385
    public void setRiRadio( String riRadio )
 
386
    {
 
387
        this.riRadio = riRadio;
 
388
    }
 
389
 
 
390
    public String getRiRadio()
 
391
    {
 
392
        return riRadio;
 
393
    }
 
394
 
 
395
    private String categoryLB;
 
396
 
 
397
    public String getCategoryLB()
 
398
    {
 
399
        return categoryLB;
 
400
    }
 
401
 
 
402
    public void setCategoryLB( String categoryLB )
 
403
    {
 
404
        this.categoryLB = categoryLB;
 
405
    }
 
406
 
 
407
    private String selectedButton;
 
408
 
 
409
    public String getSelectedButton()
 
410
    {
 
411
        return selectedButton;
 
412
    }
 
413
 
 
414
    public void setSelectedButton( String selectedButton )
 
415
    {
 
416
        this.selectedButton = selectedButton;
 
417
    }
 
418
 
 
419
    private List<String> orgUnitListCB;
 
420
 
 
421
    public void setOrgUnitListCB( List<String> orgUnitListCB )
 
422
    {
 
423
        this.orgUnitListCB = orgUnitListCB;
 
424
    }
 
425
 
 
426
    private String facilityLB;
 
427
 
 
428
    public void setFacilityLB( String facilityLB )
 
429
    {
 
430
        this.facilityLB = facilityLB;
 
431
    }
 
432
 
 
433
    public List<List<String>> getNumList()
 
434
    {
 
435
        return numList;
 
436
    }
 
437
 
 
438
    public List<List<String>> getDenList()
 
439
    {
 
440
        return denList;
 
441
    }
 
442
 
 
443
    public Map<Indicator, List<SurveyData>> getIndicatorSurveyList()
 
444
    {
 
445
        return indicatorSurveyList;
 
446
    }
 
447
 
 
448
    public String execute()
 
449
        throws Exception
 
450
    {
 
451
        statementManager.initialise();
 
452
 
 
453
        indicatorTargetList = new HashMap<Indicator, List<Target>>();
 
454
        indicatorSurveyList = new HashMap<Indicator, List<SurveyData>>();
 
455
        dataList = new ArrayList<List<String>>();
 
456
        numList = new ArrayList<List<String>>();
 
457
        denList = new ArrayList<List<String>>();
 
458
        targetList = new ArrayList<Double>();
 
459
        selectedOptionComboList = new ArrayList<DataElementCategoryOptionCombo>();
 
460
 
 
461
        // OrgUnit Related Info
 
462
        if ( ougSetCB == null )
 
463
        {
 
464
            selectedOrgUnit = new OrganisationUnit();
 
465
            selectedOrgUnit = organisationUnitService.getOrganisationUnit( Integer.parseInt( orgUnitListCB.get( 0 ) ) );
 
466
        }
 
467
        else
 
468
        {
 
469
            selectedOrgUnitGroup = new OrganisationUnitGroup();
 
470
            selectedOrgUnitGroup = organisationUnitGroupService.getOrganisationUnitGroup( Integer
 
471
                .parseInt( orgUnitListCB.get( 0 ) ) );
 
472
        }
 
473
 
 
474
        // Service Related Info
 
475
        int count1 = 0;
 
476
        numeratorDEList = new ArrayList<String>();
 
477
        denominatorDEList = new ArrayList<String>();
 
478
        // selectedServiceList = new ArrayList<Object>();
 
479
        xseriesList = new ArrayList<String>();
 
480
        yseriesList = new ArrayList<String>();
 
481
 
 
482
        List<Indicator> li1 = new ArrayList<Indicator>();
 
483
 
 
484
        if ( riRadio.equals( "indicatorsRadio" ) )
 
485
        {
 
486
            Iterator indicatorIterator = selectedIndicators.iterator();
 
487
            while ( indicatorIterator.hasNext() )
 
488
            {
 
489
                int serviceID = Integer.parseInt( (String) indicatorIterator.next() );
 
490
                Indicator indicator = indicatorService.getIndicator( serviceID );
 
491
                // selectedServiceList.add( indicator );
 
492
                li1.add( indicator );
 
493
                chartTitle += indicator.getName() + ", ";
 
494
 
 
495
                // numeratorDEList.add( getIndicatorDataElements(
 
496
                // indicator.getNumerator() ) );
 
497
                // denominatorDEList.add( getIndicatorDataElements(
 
498
                // indicator.getDenominator() ) );
 
499
                numeratorDEList.add( indicator.getNumeratorDescription() );
 
500
                denominatorDEList.add( indicator.getDenominatorDescription() );
 
501
                count1++;
 
502
            } // while loop end
 
503
            Collections.sort( li1, indicatorComparator );
 
504
            selectedServiceList = new ArrayList<Object>( li1 );
 
505
        }
 
506
        else
 
507
        {
 
508
            List<DataElement> li2 = new ArrayList<DataElement>();
 
509
            if ( deSelection == null )
 
510
            {
 
511
                System.out.println( "deOptionValue is null" );
 
512
                return null;
 
513
            }
 
514
            else
 
515
            {
 
516
                System.out.println( "deOptionValue : " + deSelection );
 
517
            }
 
518
            if ( deSelection.equalsIgnoreCase( "optioncombo" ) )
 
519
            {
 
520
                Iterator deIterator = selectedDataElements.iterator();
 
521
                while ( deIterator.hasNext() )
 
522
                {
 
523
                    String serviceId = (String) deIterator.next();
 
524
                    String partsOfServiceId[] = serviceId.split( ":" );
 
525
                    int dataElementId = Integer.parseInt( partsOfServiceId[0] );
 
526
                    DataElement dataElement = dataElementService.getDataElement( dataElementId );
 
527
                    // selectedServiceList.add( dataElement );
 
528
                    li2.add( dataElement );
 
529
                    int optionComboId = Integer.parseInt( partsOfServiceId[1] );
 
530
                    DataElementCategoryOptionCombo decoc = dataElementCategoryOptionComboService
 
531
                        .getDataElementCategoryOptionCombo( optionComboId );
 
532
                    selectedOptionComboList.add( decoc );
 
533
                    chartTitle += dataElement.getName() + " : "
 
534
                        + dataElementCategoryOptionComboService.getOptionNames( decoc ) + ", ";
 
535
                }
 
536
            }
 
537
            else
 
538
            {
 
539
                Iterator deIterator = selectedDataElements.iterator();
 
540
                while ( deIterator.hasNext() )
 
541
                {
 
542
                    int serviceID = Integer.parseInt( (String) deIterator.next() );
 
543
                    DataElement dataElement = dataElementService.getDataElement( serviceID );
 
544
                    // selectedServiceList.add( dataElement );
 
545
                    li2.add( dataElement );
 
546
                    chartTitle += dataElement.getName() + ", ";
 
547
                }
 
548
            }
 
549
            Collections.sort( li2, dataElementComparator );
 
550
            selectedServiceList = new ArrayList<Object>( li2 );
 
551
        }
 
552
 
 
553
        // TargetValues
 
554
        targetValues = new HashMap<String, Double>();
 
555
        /*
 
556
         * if(riRadio.equals( "indicatorsRadio" )) { getTargetValues(); }
 
557
         */
 
558
 
 
559
        /* Hack to Intialize Indicator Targets to Zeros */
 
560
        if ( riRadio.equals( "indicatorsRadio" ) )
 
561
        {
 
562
            Iterator serviceIterator = selectedServiceList.iterator();
 
563
            while ( serviceIterator.hasNext() )
 
564
            {
 
565
                Indicator indicator = (Indicator) serviceIterator.next();
 
566
                targetValues.put( "" + indicator.getId(), new Double( 0.0 ) );
 
567
                targetList.add( targetValues.get( "" + indicator.getId() ) );
 
568
            }
 
569
        }
 
570
 
 
571
        // Period Related Info
 
572
        startPeriod = periodStore.getPeriod( sDateLB );
 
573
        endPeriod = periodStore.getPeriod( eDateLB );
 
574
 
 
575
        int monthlyPeriodTypeId = 0;
 
576
        Collection periodTypes = periodStore.getAllPeriodTypes();
 
577
        PeriodType monthlyPeriodType = null;
 
578
        Iterator iter = periodTypes.iterator();
 
579
        while ( iter.hasNext() )
 
580
        {
 
581
            PeriodType periodType = (PeriodType) iter.next();
 
582
            if ( periodType.getName().toLowerCase().trim().equals( "monthly" ) )
 
583
            {
 
584
                monthlyPeriodType = periodType;
 
585
                break;
 
586
            }
 
587
        }
 
588
        if ( monthlyPeriodType != null )
 
589
        {
 
590
            // System.out.println( "Monthly Period id : " +
 
591
            // monthlyPeriodType.getId() );
 
592
            monthlyPeriodTypeId = monthlyPeriodType.getId();
 
593
        }
 
594
        /*
 
595
         * else { System.out.println( "Monthly Period Type is NULL" ); }
 
596
         */
 
597
 
 
598
        selectedPeriodList = dashBoardService.getMonthlyPeriods( startPeriod.getStartDate(), endPeriod.getEndDate() );
 
599
 
 
600
        if ( categoryLB.equals( "facility" ) && facilityLB.equals( "random" ) )
 
601
        {
 
602
            chartTitle += "\n OrganisationUnit : --- \nPeriod : " + startPeriod.getStartDate() + " To "
 
603
                + endPeriod.getEndDate();
 
604
        }
 
605
        else
 
606
        {
 
607
            if ( ougSetCB == null )
 
608
            {
 
609
                chartTitle += "\n OrganisationUnit : " + selectedOrgUnit.getShortName() + "\nPeriod : "
 
610
                    + startPeriod.getStartDate() + " To " + endPeriod.getEndDate();
 
611
            }
 
612
            else
 
613
            {
 
614
                chartTitle += "\n OrganisationUnit : --- \nPeriod : "
 
615
                    + startPeriod.getStartDate() + " To " + endPeriod.getEndDate();
 
616
            }
 
617
        }
 
618
 
 
619
        if ( categoryLB.equals( "period" ) )
 
620
        {
 
621
            data1 = getServiceValuesByPeriod();
 
622
            xAxis_Title = "Time Line";
 
623
        }
 
624
        else
 
625
        {
 
626
            data1 = getServiceValuesByFacility();
 
627
            xAxis_Title = "Facilities";
 
628
        }
 
629
 
 
630
        if ( riRadio.equals( "indicatorsRadio" ) )
 
631
        {
 
632
            yAxis_Title = "Percentage/Rate";
 
633
        }
 
634
        else
 
635
        {
 
636
            yAxis_Title = "Value";
 
637
        }
 
638
 
 
639
        count1 = 0;
 
640
        while ( count1 != categories1.length )
 
641
        {
 
642
            xseriesList.add( categories1[count1] );
 
643
            count1++;
 
644
        }
 
645
        
 
646
        // if(selectedButton.equals("ViewSummary")) return "ViewSummary";
 
647
 
 
648
        ActionContext ctx = ActionContext.getContext();
 
649
        HttpServletRequest req = (HttpServletRequest) ctx.get( ServletActionContext.HTTP_REQUEST );
 
650
 
 
651
        session = req.getSession();
 
652
        session.setAttribute( "data1", data1 );
 
653
        session.setAttribute( "data2", data2 );
 
654
        session.setAttribute( "series1", series1 );
 
655
        session.setAttribute( "categories1", categories1 );
 
656
        session.setAttribute( "series2", series2 );
 
657
        session.setAttribute( "categories2", categories2 );
 
658
        session.setAttribute( "chartTitle", chartTitle );
 
659
        session.setAttribute( "xAxisTitle", xAxis_Title );
 
660
        session.setAttribute( "yAxisTitle", yAxis_Title );
 
661
 
 
662
        statementManager.destroy();
 
663
 
 
664
        return SUCCESS;
 
665
    }// execute end
 
666
 
 
667
    /*
 
668
     * Returns the values for selected services by period wise for ex:- the
 
669
     * periods are jan-2006,feb-2006,mar-2006 and the services are service1 and
 
670
     * service2 then it returns the values for service1 -
 
671
     * jan2006,feb2006,mar2006 and service2 - jan2006,feb2006,mar2006 for the
 
672
     * selected orgunit
 
673
     */
 
674
    public Double[][] getServiceValuesByPeriod()
 
675
    {
 
676
        Double[][] serviceValues = new Double[selectedServiceList.size()][selectedPeriodList.size()];
 
677
        data2 = new Double[selectedServiceList.size()][selectedPeriodList.size()];
 
678
 
 
679
        int countForServiceList = 0;
 
680
        int countForPeriodList = 0;
 
681
        int targetFlag = 0;
 
682
        Indicator ind = new Indicator();
 
683
        DataElement dElement = new DataElement();
 
684
        DataElementCategoryOptionCombo decoc = new DataElementCategoryOptionCombo();
 
685
        Period p = new Period();
 
686
 
 
687
        /*
 
688
         * if ( selectedServiceList == null ) System.out.println( "Service List
 
689
         * is empty" ); if ( selectedPeriodList == null ) System.out.println(
 
690
         * "Period List is Empty" );
 
691
         */
 
692
        series1 = new String[selectedServiceList.size()];
 
693
        series2 = new String[selectedServiceList.size()];
 
694
        categories1 = new String[selectedPeriodList.size()];
 
695
        categories2 = new String[selectedPeriodList.size()];
 
696
        Iterator serviceListIterator = selectedServiceList.iterator();
 
697
        while ( serviceListIterator.hasNext() )
 
698
        {
 
699
            List<String> numValues = new ArrayList<String>();
 
700
            List<String> denValues = new ArrayList<String>();
 
701
            List<String> dataValues = new ArrayList<String>();
 
702
            List<Target> targetList = new ArrayList<Target>();
 
703
            List<SurveyData> surveyList = new ArrayList<SurveyData>();
 
704
 
 
705
            if ( riRadio.equals( "indicatorsRadio" ) )
 
706
            {
 
707
                ind = (Indicator) serviceListIterator.next();
 
708
                // System.out.println( ind.getName() );
 
709
                series1[countForServiceList] = ind.getName();
 
710
                series2[countForServiceList] = ind.getName() + "(Target)";
 
711
                yseriesList.add( ind.getName() );
 
712
 
 
713
                List<Period> tempPeriodList = new ArrayList<Period>( periodStore.getIntersectingPeriods( startPeriod
 
714
                    .getStartDate(), endPeriod.getEndDate() ) );
 
715
                Iterator tempPeriodListIterator = tempPeriodList.iterator();
 
716
                while ( tempPeriodListIterator.hasNext() )
 
717
                {
 
718
                    Period tempPeriod = (Period) tempPeriodListIterator.next();
 
719
                    if ( ougSetCB == null )
 
720
                    {
 
721
                        List<Target> tempTarget = new ArrayList<Target>( targetService.getTargets( ind, tempPeriod,
 
722
                            selectedOrgUnit ) );
 
723
                        List<SurveyData> tempSurvey = new ArrayList<SurveyData>( getSurveyList(ind, selectedOrgUnit));
 
724
                        surveyList = new ArrayList<SurveyData>();
 
725
                        if ( tempTarget != null )
 
726
                            targetList.addAll( tempTarget );
 
727
                        if(tempSurvey !=null && tempSurvey.size() > 0)
 
728
                            surveyList.addAll( tempSurvey );
 
729
                    }
 
730
                    else
 
731
                    {
 
732
                        List<Target> tempTarget = new ArrayList<Target>( targetService.getTargets( ind, tempPeriod,
 
733
                            selectedOrgUnitGroup.getMembers().iterator().next() ) );
 
734
                        if ( tempTarget != null )
 
735
                            targetList.addAll( tempTarget );
 
736
                        List<SurveyData> tempSurvey = new ArrayList<SurveyData>( getSurveyList(ind, selectedOrgUnitGroup.getMembers().iterator().next()));
 
737
                        surveyList = new ArrayList<SurveyData>();
 
738
                        if(tempSurvey !=null && tempSurvey.size() > 0)
 
739
                            surveyList.addAll( tempSurvey );                        
 
740
                    }
 
741
 
 
742
                }
 
743
                indicatorTargetList.put( ind, targetList );
 
744
                indicatorSurveyList.put( ind, surveyList );
 
745
            }
 
746
            else
 
747
            {
 
748
                dElement = (DataElement) serviceListIterator.next();
 
749
                if ( deSelection.equalsIgnoreCase( "optioncombo" ) )
 
750
                {
 
751
                    decoc = selectedOptionComboList.get( countForServiceList );
 
752
                    /*if ( dElement.getAlternativeName() != null )
 
753
                    {
 
754
                        series1[countForServiceList] = dElement.getAlternativeName() + " : "
 
755
                            + dataElementCategoryOptionComboService.getOptionNames( decoc );
 
756
                        series2[countForServiceList] = dElement.getAlternativeName() + " : "
 
757
                            + dataElementCategoryOptionComboService.getOptionNames( decoc ) + " (Target)";
 
758
                    }
 
759
                    else*/
 
760
                    {
 
761
                        series1[countForServiceList] = dElement.getName() + " : "
 
762
                            + dataElementCategoryOptionComboService.getOptionNames( decoc );
 
763
                        series2[countForServiceList] = dElement.getName() + " : "
 
764
                            + dataElementCategoryOptionComboService.getOptionNames( decoc ) + " (Target)";
 
765
                    }
 
766
                    yseriesList.add( dElement.getName() + " : "
 
767
                        + dataElementCategoryOptionComboService.getOptionNames( decoc ) );
 
768
                }
 
769
                else
 
770
                {
 
771
                    /*
 
772
                    if ( dElement.getAlternativeName() != null )
 
773
                    {
 
774
                        series1[countForServiceList] = dElement.getAlternativeName();
 
775
                        series2[countForServiceList] = dElement.getAlternativeName() + " (Target)";
 
776
                    }
 
777
                    else*/
 
778
                    {
 
779
                        series1[countForServiceList] = dElement.getName();
 
780
                        series2[countForServiceList] = dElement.getName() + " (Target)";
 
781
                    }
 
782
                    yseriesList.add( dElement.getName() );
 
783
                }
 
784
            }
 
785
            Iterator periodListIterator = selectedPeriodList.iterator();
 
786
            countForPeriodList = 0;
 
787
            while ( periodListIterator.hasNext() )
 
788
            {
 
789
                double numVal = 0.0;
 
790
                double denVal = 0.0;
 
791
 
 
792
                p = (Period) periodListIterator.next();
 
793
                /*
 
794
                 * if ( p == null ) System.out.println( "In Period While Loop :
 
795
                 * but it is Empty" ); else System.out.println( "In Period While
 
796
                 * Loop : but it is not Empty" );
 
797
                 */
 
798
                if ( riRadio.equals( "indicatorsRadio" ) )
 
799
                {
 
800
 
 
801
                    if ( ougSetCB == null )
 
802
                    {
 
803
                        serviceValues[countForServiceList][countForPeriodList] = aggregationService
 
804
                            .getAggregatedIndicatorValue( ind, p.getStartDate(), p.getEndDate(), selectedOrgUnit );
 
805
                        numVal = aggregationService.getAggregatedNumeratorValue( ind, p.getStartDate(), p.getEndDate(), selectedOrgUnit );
 
806
                        denVal = aggregationService.getAggregatedDenominatorValue( ind, p.getStartDate(), p.getEndDate(), selectedOrgUnit );
 
807
                    }
 
808
                    else
 
809
                    {
 
810
                        double aggValue = 0.0;
 
811
                        List<OrganisationUnit> orgUnits = new ArrayList<OrganisationUnit>( selectedOrgUnitGroup
 
812
                            .getMembers() );
 
813
                        Iterator<OrganisationUnit> orgUnitsIterator = orgUnits.iterator();
 
814
                        while ( orgUnitsIterator.hasNext() )
 
815
                        {
 
816
                            OrganisationUnit ou = (OrganisationUnit) orgUnitsIterator.next();
 
817
                            double tempd = aggregationService.getAggregatedIndicatorValue( ind, p.getStartDate(), p
 
818
                                .getEndDate(), ou );
 
819
                            double tempnum = aggregationService.getAggregatedNumeratorValue( ind, p.getStartDate(), p
 
820
                                .getEndDate(), ou );
 
821
                            double tempden = aggregationService.getAggregatedDenominatorValue( ind, p.getStartDate(), p
 
822
                                .getEndDate(), ou );
 
823
                            if(tempd == -1) tempd = 0.0;
 
824
                            if(tempnum == -1) tempnum = 0.0;
 
825
                            if(tempden == -1) tempden = 0.0;
 
826
                            
 
827
                            aggValue += tempd;
 
828
                            numVal += tempnum;
 
829
                            denVal += tempden;
 
830
                        }
 
831
                        serviceValues[countForServiceList][countForPeriodList] = aggValue / orgUnits.size();
 
832
                        numVal /= orgUnits.size();
 
833
                        denVal /= orgUnits.size();
 
834
                    }
 
835
                }
 
836
                else
 
837
                {
 
838
                    if ( deSelection.equalsIgnoreCase( "optioncombo" ) )
 
839
                    {
 
840
                        if ( ougSetCB == null )
 
841
                        {
 
842
                            serviceValues[countForServiceList][countForPeriodList] = aggregationService
 
843
                                .getAggregatedDataValue( dElement, decoc, p.getStartDate(), p.getEndDate(),
 
844
                                    selectedOrgUnit );
 
845
                        }
 
846
                        else
 
847
                        {
 
848
                            double aggValue = 0.0;
 
849
                            List<OrganisationUnit> orgUnits = new ArrayList<OrganisationUnit>( selectedOrgUnitGroup
 
850
                                .getMembers() );
 
851
                            Iterator<OrganisationUnit> orgUnitsIterator = orgUnits.iterator();
 
852
                            while ( orgUnitsIterator.hasNext() )
 
853
                            {
 
854
                                OrganisationUnit ou = (OrganisationUnit) orgUnitsIterator.next();
 
855
                                double tempd = aggregationService.getAggregatedDataValue( dElement, decoc, p
 
856
                                    .getStartDate(), p.getEndDate(), ou );
 
857
                                if ( tempd == -1 )
 
858
                                    tempd = 0.0;
 
859
                                aggValue += tempd;
 
860
                            }
 
861
                            serviceValues[countForServiceList][countForPeriodList] = aggValue;
 
862
                        }
 
863
                    }
 
864
                    else
 
865
                    {
 
866
                        double aggDataValue = 0.0;
 
867
                        serviceValues[countForServiceList][countForPeriodList] = 0.0;
 
868
                        DataElementCategoryCombo dataElementCategoryCombo = dElement.getCategoryCombo();
 
869
 
 
870
                        List<DataElementCategoryOptionCombo> optionCombos = new ArrayList<DataElementCategoryOptionCombo>(
 
871
                            dataElementCategoryCombo.getOptionCombos() );
 
872
 
 
873
                        Iterator<DataElementCategoryOptionCombo> optionComboIterator = optionCombos.iterator();
 
874
                        while ( optionComboIterator.hasNext() )
 
875
                        {
 
876
                            DataElementCategoryOptionCombo decoc1 = (DataElementCategoryOptionCombo) optionComboIterator
 
877
                                .next();
 
878
 
 
879
                            if ( ougSetCB == null )
 
880
                            {
 
881
                                aggDataValue = aggregationService.getAggregatedDataValue( dElement, decoc1, p
 
882
                                    .getStartDate(), p.getEndDate(), selectedOrgUnit );
 
883
                            }
 
884
                            else
 
885
                            {
 
886
                                List<OrganisationUnit> orgUnits = new ArrayList<OrganisationUnit>( selectedOrgUnitGroup
 
887
                                    .getMembers() );
 
888
                                Iterator<OrganisationUnit> orgUnitsIterator = orgUnits.iterator();
 
889
                                while ( orgUnitsIterator.hasNext() )
 
890
                                {
 
891
                                    OrganisationUnit ou = (OrganisationUnit) orgUnitsIterator.next();
 
892
                                    double tempd = aggregationService.getAggregatedDataValue( dElement, decoc1, p
 
893
                                        .getStartDate(), p.getEndDate(), ou );
 
894
                                    if ( tempd == -1 )
 
895
                                        tempd = 0.0;
 
896
                                    aggDataValue += tempd;
 
897
                                }
 
898
                            }
 
899
                            if ( aggDataValue == -1 )
 
900
                                aggDataValue = 0.0;
 
901
                            serviceValues[countForServiceList][countForPeriodList] += aggDataValue;
 
902
                        }
 
903
                    }
 
904
                }
 
905
                serviceValues[countForServiceList][countForPeriodList] = Math
 
906
                    .round( serviceValues[countForServiceList][countForPeriodList] * Math.pow( 10, 2 ) )
 
907
                    / Math.pow( 10, 2 );
 
908
                numVal = Math.round( numVal * Math.pow( 10, 2 ) ) / Math.pow( 10, 2 );
 
909
                denVal = Math.round( denVal * Math.pow( 10, 2 ) ) / Math.pow( 10, 2 );
 
910
                
 
911
                if ( serviceValues[countForServiceList][countForPeriodList] == -1 )
 
912
                    serviceValues[countForServiceList][countForPeriodList] = 0.0;
 
913
                categories1[countForPeriodList] = p.getStartDate().toString();
 
914
                categories2[countForPeriodList] = p.getStartDate().toString();
 
915
 
 
916
                if ( riRadio.equals( "indicatorsRadio" ) )
 
917
                    data2[countForServiceList][countForPeriodList] = (targetValues.get( "" + ind.getId() ))
 
918
                        .doubleValue();
 
919
                else
 
920
                    data2[countForServiceList][countForPeriodList] = 0.0;
 
921
 
 
922
                if ( riRadio.equals( "dataElementsRadio" ) )
 
923
                {
 
924
                    if ( dElement.getType().equalsIgnoreCase( "int" ) )
 
925
                        dataValues.add( "" + serviceValues[countForServiceList][countForPeriodList] );
 
926
                    else
 
927
                        dataValues.add( " " );
 
928
                }
 
929
                else
 
930
                {
 
931
                    dataValues.add( "" + serviceValues[countForServiceList][countForPeriodList] );
 
932
                    numValues.add(""+ numVal);
 
933
                    denValues.add(""+ denVal);
 
934
                }
 
935
 
 
936
                countForPeriodList++;
 
937
            }// periodList loop end
 
938
            dataList.add( dataValues );
 
939
            numList.add( numValues );
 
940
            denList.add( denValues );
 
941
            countForServiceList++;
 
942
        } // serviceList loop end
 
943
        return serviceValues;
 
944
    }// getServiceValues method end
 
945
 
 
946
    /*
 
947
     * Returns the period aggregated values for the children of selected orgunit
 
948
     * and list of selected services for ex:- PeriodList is
 
949
     * jan2006,feb2006,mar2006 ServiceList is service1 and service2 then it
 
950
     * returns the period aggregated values for service1 - child1, child2,
 
951
     * child3 service2 - child2, child2, child3 for the selected orgunit
 
952
     */
 
953
    public Double[][] getServiceValuesByFacility()
 
954
    {
 
955
        
 
956
        int countForServiceList = 0;
 
957
        int countForChildOrgUnitList = 0;
 
958
        // int noOfPeriods = selectedPeriodList.size();
 
959
        int noOfPeriods = 1;
 
960
 
 
961
        Indicator ind = new Indicator();
 
962
        DataElement dElement = new DataElement();
 
963
        DataElementCategoryOptionCombo decoc = new DataElementCategoryOptionCombo();
 
964
 
 
965
        List<Object> childOrgUnitList = new ArrayList<Object>();
 
966
        if ( facilityLB.equals( "children" ) )
 
967
        {
 
968
            if ( ougSetCB == null )
 
969
            {
 
970
                childOrgUnitList = new ArrayList<Object>( dashBoardService.getAllChildren( selectedOrgUnit ) );                
 
971
            }
 
972
            else
 
973
            {
 
974
                //childOrgUnitList = new ArrayList<Object>( selectedOrgUnitGroup.getMembers() );
 
975
                childOrgUnitList = new ArrayList<Object>();
 
976
                Iterator orgUnitGroupIte = orgUnitListCB.iterator();
 
977
                while ( orgUnitGroupIte.hasNext() )
 
978
                {
 
979
                    OrganisationUnitGroup oug = organisationUnitGroupService.getOrganisationUnitGroup( Integer
 
980
                        .parseInt( (String) orgUnitGroupIte.next() ) );
 
981
                    List<OrganisationUnit> tempOUList = new ArrayList<OrganisationUnit>(oug.getMembers());
 
982
                    Collections.sort( tempOUList, new OrganisationUnitShortNameComparator() );
 
983
                    childOrgUnitList.addAll( tempOUList );
 
984
                }
 
985
            }
 
986
        }
 
987
        else
 
988
        {
 
989
            if ( ougSetCB == null )
 
990
            {
 
991
                Iterator orgUnitIterator = orgUnitListCB.iterator();
 
992
                while ( orgUnitIterator.hasNext() )
 
993
                {
 
994
                    OrganisationUnit o = organisationUnitService.getOrganisationUnit( Integer
 
995
                        .parseInt( (String) orgUnitIterator.next() ) );
 
996
                    childOrgUnitList.add( o );
 
997
                }
 
998
            }
 
999
            else
 
1000
            {
 
1001
                Iterator orgUnitGroupIte = orgUnitListCB.iterator();
 
1002
                while ( orgUnitGroupIte.hasNext() )
 
1003
                {
 
1004
                    OrganisationUnitGroup oug = organisationUnitGroupService.getOrganisationUnitGroup( Integer
 
1005
                        .parseInt( (String) orgUnitGroupIte.next() ) );
 
1006
                    childOrgUnitList.add( oug );
 
1007
                }
 
1008
            }
 
1009
        }
 
1010
        Iterator serviceListIterator = selectedServiceList.iterator();
 
1011
        Double[][] serviceValues = new Double[selectedServiceList.size()][childOrgUnitList.size()];
 
1012
 
 
1013
        data2 = new Double[selectedServiceList.size()][childOrgUnitList.size()];
 
1014
        series1 = new String[selectedServiceList.size()];
 
1015
        series2 = new String[selectedServiceList.size()];
 
1016
        categories1 = new String[childOrgUnitList.size()];
 
1017
        categories2 = new String[childOrgUnitList.size()];
 
1018
        while ( serviceListIterator.hasNext() )
 
1019
        {
 
1020
            int noOfChildren = 1;
 
1021
            
 
1022
            List<String> numValues = new ArrayList<String>();
 
1023
            List<String> denValues = new ArrayList<String>();
 
1024
            List<String> dataValues = new ArrayList<String>();
 
1025
            List<Target> targetList = new ArrayList<Target>();
 
1026
            List<SurveyData> surveyList = new ArrayList<SurveyData>();
 
1027
            if ( riRadio.equals( "indicatorsRadio" ) )
 
1028
            {
 
1029
                ind = (Indicator) serviceListIterator.next();
 
1030
                // System.out.println( ind.getName() );
 
1031
                series1[countForServiceList] = ind.getName();
 
1032
                series2[countForServiceList] = ind.getName() + "(Target)";
 
1033
                yseriesList.add( ind.getName() );
 
1034
 
 
1035
                List<Period> tempPeriodList = new ArrayList<Period>( periodStore.getIntersectingPeriods( startPeriod
 
1036
                    .getStartDate(), endPeriod.getEndDate() ) );
 
1037
                Iterator tempPeriodListIterator = tempPeriodList.iterator();
 
1038
                while ( tempPeriodListIterator.hasNext() )
 
1039
                {
 
1040
                    Period tempPeriod = (Period) tempPeriodListIterator.next();
 
1041
                    if ( ougSetCB == null )
 
1042
                    {
 
1043
                        if ( facilityLB.equals( "children" ) )
 
1044
                        {
 
1045
                            List<Target> tempTarget = new ArrayList<Target>( targetService.getTargets( ind, tempPeriod,
 
1046
                                selectedOrgUnit ) );
 
1047
                            if ( tempTarget != null )
 
1048
                                targetList.addAll( tempTarget );
 
1049
                            List<SurveyData> tempSurvey = new ArrayList<SurveyData>( getSurveyList(ind, selectedOrgUnit));
 
1050
                            surveyList = new ArrayList<SurveyData>();
 
1051
                            if(tempSurvey !=null && tempSurvey.size() > 0)
 
1052
                                surveyList.addAll( tempSurvey );
 
1053
                        }
 
1054
                        else
 
1055
                        {
 
1056
                            List<Target> tempTarget = new ArrayList<Target>( targetService.getTargets( ind, tempPeriod,
 
1057
                                (OrganisationUnit) childOrgUnitList.iterator().next() ) );
 
1058
                            if ( tempTarget != null )
 
1059
                                targetList.addAll( tempTarget );
 
1060
                            List<SurveyData> tempSurvey = new ArrayList<SurveyData>( getSurveyList(ind, (OrganisationUnit) childOrgUnitList.iterator().next()));
 
1061
                            surveyList = new ArrayList<SurveyData>();
 
1062
                            if(tempSurvey !=null && tempSurvey.size() > 0)
 
1063
                                surveyList.addAll( tempSurvey );
 
1064
                        }
 
1065
                    }
 
1066
                    else
 
1067
                    {
 
1068
                        if ( facilityLB.equals( "children" ) )
 
1069
                        {
 
1070
                            List<Target> tempTarget = new ArrayList<Target>( targetService.getTargets( ind, tempPeriod,
 
1071
                                selectedOrgUnitGroup.getMembers().iterator().next() ) );
 
1072
                            if ( tempTarget != null )
 
1073
                                targetList.addAll( tempTarget );
 
1074
                            
 
1075
                            List<SurveyData> tempSurvey = new ArrayList<SurveyData>( getSurveyList(ind, selectedOrgUnitGroup.getMembers().iterator().next()));
 
1076
                            surveyList = new ArrayList<SurveyData>();
 
1077
                            if(tempSurvey !=null && tempSurvey.size() > 0)
 
1078
                                surveyList.addAll( tempSurvey );
 
1079
 
 
1080
                        }
 
1081
                        else
 
1082
                        {
 
1083
                            OrganisationUnitGroup oug = (OrganisationUnitGroup) childOrgUnitList.iterator().next();
 
1084
                            List<Target> tempTarget = new ArrayList<Target>( targetService.getTargets( ind, tempPeriod,
 
1085
                                oug.getMembers().iterator().next() ) );
 
1086
                            if ( tempTarget != null )
 
1087
                                targetList.addAll( tempTarget );
 
1088
                            
 
1089
                            List<SurveyData> tempSurvey = new ArrayList<SurveyData>( getSurveyList(ind, oug.getMembers().iterator().next()));
 
1090
                            surveyList = new ArrayList<SurveyData>();
 
1091
                            if(tempSurvey !=null && tempSurvey.size() > 0)
 
1092
                                surveyList.addAll( tempSurvey );
 
1093
                            
 
1094
                        }
 
1095
 
 
1096
                    }
 
1097
                }
 
1098
                indicatorTargetList.put( ind, targetList );
 
1099
                indicatorSurveyList.put( ind, surveyList );
 
1100
 
 
1101
            }
 
1102
            else
 
1103
            {                
 
1104
                
 
1105
                dElement = (DataElement) serviceListIterator.next();
 
1106
                                                
 
1107
                if ( deSelection.equalsIgnoreCase( "optioncombo" ) )
 
1108
                {
 
1109
                    decoc = selectedOptionComboList.get( countForServiceList );
 
1110
                    /*if ( dElement.getAlternativeName() != null )
 
1111
                    {
 
1112
                        series1[countForServiceList] = dElement.getAlternativeName() + " : "
 
1113
                            + dataElementCategoryOptionComboService.getOptionNames( decoc );
 
1114
                        series2[countForServiceList] = dElement.getAlternativeName() + " : "
 
1115
                            + dataElementCategoryOptionComboService.getOptionNames( decoc ) + " (Target)";
 
1116
                    }
 
1117
                    else*/
 
1118
                    {
 
1119
                        series1[countForServiceList] = dElement.getName() + " : "
 
1120
                            + dataElementCategoryOptionComboService.getOptionNames( decoc );
 
1121
                        series2[countForServiceList] = dElement.getName() + " : "
 
1122
                            + dataElementCategoryOptionComboService.getOptionNames( decoc ) + " (Target)";
 
1123
                    }
 
1124
                    yseriesList.add( dElement.getName() + " : "
 
1125
                        + dataElementCategoryOptionComboService.getOptionNames( decoc ) );
 
1126
                }
 
1127
                else
 
1128
                {
 
1129
                    /*if ( dElement.getAlternativeName() != null )
 
1130
                    {
 
1131
                        series1[countForServiceList] = dElement.getAlternativeName();
 
1132
                        series2[countForServiceList] = dElement.getAlternativeName() + "(Target)";
 
1133
                    }
 
1134
                    else*/
 
1135
                    {
 
1136
                        series1[countForServiceList] = dElement.getName();
 
1137
                        series2[countForServiceList] = dElement.getName() + "(Target)";
 
1138
                    }
 
1139
                    yseriesList.add( dElement.getName() );
 
1140
                }
 
1141
            }
 
1142
 
 
1143
            double numVal = 0.0;
 
1144
            double denVal = 0.0;
 
1145
            Iterator childOrgUnitListIterator = childOrgUnitList.iterator();
 
1146
            countForChildOrgUnitList = 0;
 
1147
            while ( childOrgUnitListIterator.hasNext() )
 
1148
            {
 
1149
                OrganisationUnit childOrgUnit = new OrganisationUnit();
 
1150
                OrganisationUnitGroup childOrgUnitGroup = new OrganisationUnitGroup();
 
1151
 
 
1152
                if ( ougSetCB == null || facilityLB.equals( "children" ) )
 
1153
                {
 
1154
                    childOrgUnit = (OrganisationUnit) childOrgUnitListIterator.next();
 
1155
                }
 
1156
                else
 
1157
                {
 
1158
                    childOrgUnitGroup = (OrganisationUnitGroup) childOrgUnitListIterator.next();
 
1159
                }
 
1160
 
 
1161
                if ( riRadio.equals( "indicatorsRadio" ) )
 
1162
                {
 
1163
                    /*
 
1164
                     * if ( ind.getIndicatorType().getFactor() == 1200 ) { //
 
1165
                     * TODO } else noOfPeriods = 1;
 
1166
                     */
 
1167
                    if ( ougSetCB == null || facilityLB.equals( "children" ) )
 
1168
                    {
 
1169
                        serviceValues[countForServiceList][countForChildOrgUnitList] = aggregationService
 
1170
                            .getAggregatedIndicatorValue( ind, startPeriod.getStartDate(), endPeriod.getEndDate(),
 
1171
                                childOrgUnit )
 
1172
                            / noOfPeriods;
 
1173
                        numVal = aggregationService.getAggregatedNumeratorValue( ind, startPeriod.getStartDate(), endPeriod.getEndDate(),
 
1174
                            childOrgUnit )/ noOfPeriods;
 
1175
                        denVal = aggregationService.getAggregatedDenominatorValue( ind, startPeriod.getStartDate(), endPeriod.getEndDate(),
 
1176
                            childOrgUnit )/ noOfPeriods;
 
1177
                        
 
1178
                    }
 
1179
                    else
 
1180
                    {
 
1181
                        double aggValue = 0.0;
 
1182
                        List<OrganisationUnit> orgUnits = new ArrayList<OrganisationUnit>( childOrgUnitGroup
 
1183
                            .getMembers() );
 
1184
                        Iterator<OrganisationUnit> orgUnitsIterator = orgUnits.iterator();
 
1185
                        while ( orgUnitsIterator.hasNext() )
 
1186
                        {
 
1187
                            OrganisationUnit ou = (OrganisationUnit) orgUnitsIterator.next();
 
1188
                            double tempd = aggregationService.getAggregatedIndicatorValue( ind, startPeriod
 
1189
                                .getStartDate(), endPeriod.getEndDate(), ou );
 
1190
                            double tempnum = aggregationService.getAggregatedNumeratorValue( ind, startPeriod
 
1191
                                .getStartDate(), endPeriod.getEndDate(), ou );
 
1192
                            double tempden = aggregationService.getAggregatedDenominatorValue( ind, startPeriod
 
1193
                                .getStartDate(), endPeriod.getEndDate(), ou );
 
1194
                            if ( tempd == -1 ) tempd = 0.0;
 
1195
                            if( tempnum == -1 ) tempnum = 0.0;
 
1196
                            if( tempden == -1 ) tempden = 0.0;
 
1197
                            aggValue += tempd;
 
1198
                            numVal += numVal;
 
1199
                            denVal += denVal;
 
1200
                        }
 
1201
                        serviceValues[countForServiceList][countForChildOrgUnitList] = aggValue / noOfPeriods;
 
1202
                        serviceValues[countForServiceList][countForChildOrgUnitList] /= orgUnits.size();
 
1203
                        numVal /= noOfPeriods;
 
1204
                        numVal /= orgUnits.size();
 
1205
                        denVal /= noOfPeriods;
 
1206
                        denVal /= orgUnits.size();
 
1207
                    }
 
1208
                }
 
1209
                else
 
1210
                {
 
1211
 
 
1212
                    DataElementGroup deg = dataElementService.getDataElementGroupByName( "Annual State Baseline data (%)" );                
 
1213
                    if(deg != null && deg.getMembers().contains( dElement ))
 
1214
                    {
 
1215
                        if ( facilityLB.equals( "children" ) )
 
1216
                        {
 
1217
                            noOfChildren = 1;
 
1218
                        }
 
1219
                        else
 
1220
                        {                        
 
1221
                            if ( ougSetCB == null )
 
1222
                            {
 
1223
                                noOfChildren = dashBoardService.getAllChildren( (OrganisationUnit) childOrgUnit ).size();
 
1224
                            }
 
1225
                            else
 
1226
                            {
 
1227
                                noOfChildren = childOrgUnitGroup.getMembers().size();
 
1228
                            }                        
 
1229
                        }
 
1230
                    }
 
1231
 
 
1232
                    if ( deSelection.equalsIgnoreCase( "optioncombo" ) )
 
1233
                    {
 
1234
                        if ( ougSetCB == null || facilityLB.equals( "children" ) )
 
1235
                        {
 
1236
                            serviceValues[countForServiceList][countForChildOrgUnitList] = aggregationService
 
1237
                                .getAggregatedDataValue( dElement, decoc, startPeriod.getStartDate(), endPeriod
 
1238
                                    .getEndDate(), childOrgUnit );
 
1239
                        }
 
1240
                        else
 
1241
                        {
 
1242
                            double aggValue = 0.0;
 
1243
                            List<OrganisationUnit> orgUnits = new ArrayList<OrganisationUnit>( childOrgUnitGroup
 
1244
                                .getMembers() );
 
1245
                            Iterator<OrganisationUnit> orgUnitsIterator = orgUnits.iterator();
 
1246
                            while ( orgUnitsIterator.hasNext() )
 
1247
                            {
 
1248
                                OrganisationUnit ou = (OrganisationUnit) orgUnitsIterator.next();
 
1249
                                double tempd = aggregationService.getAggregatedDataValue( dElement, decoc, startPeriod
 
1250
                                    .getStartDate(), endPeriod.getEndDate(), ou );
 
1251
                                if ( tempd == -1 )
 
1252
                                    tempd = 0.0;
 
1253
                                aggValue += tempd;
 
1254
                            }
 
1255
                            serviceValues[countForServiceList][countForChildOrgUnitList] = aggValue;
 
1256
                        }
 
1257
                    }
 
1258
                    else
 
1259
                    {
 
1260
                        double aggDataValue = 0.0;
 
1261
                        serviceValues[countForServiceList][countForChildOrgUnitList] = 0.0;
 
1262
 
 
1263
                        DataElementCategoryCombo dataElementCategoryCombo = dElement.getCategoryCombo();
 
1264
 
 
1265
                        List<DataElementCategoryOptionCombo> optionCombos = new ArrayList<DataElementCategoryOptionCombo>(
 
1266
                            dataElementCategoryCombo.getOptionCombos() );
 
1267
 
 
1268
                        Iterator<DataElementCategoryOptionCombo> optionComboIterator = optionCombos.iterator();
 
1269
                        while ( optionComboIterator.hasNext() )
 
1270
                        {
 
1271
                            DataElementCategoryOptionCombo decoc1 = (DataElementCategoryOptionCombo) optionComboIterator
 
1272
                                .next();
 
1273
                            if ( ougSetCB == null || facilityLB.equals( "children" ) )
 
1274
                            {
 
1275
                                aggDataValue = aggregationService.getAggregatedDataValue( dElement, decoc1, startPeriod
 
1276
                                    .getStartDate(), endPeriod.getEndDate(), childOrgUnit );
 
1277
                            }
 
1278
                            else
 
1279
                            {
 
1280
                                List<OrganisationUnit> orgUnits = new ArrayList<OrganisationUnit>( childOrgUnitGroup
 
1281
                                    .getMembers() );
 
1282
                                Iterator<OrganisationUnit> orgUnitsIterator = orgUnits.iterator();
 
1283
                                while ( orgUnitsIterator.hasNext() )
 
1284
                                {
 
1285
                                    OrganisationUnit ou = (OrganisationUnit) orgUnitsIterator.next();
 
1286
                                    double tempd = aggregationService.getAggregatedDataValue( dElement, decoc1,
 
1287
                                        startPeriod.getStartDate(), endPeriod.getEndDate(), ou );
 
1288
                                    if ( tempd == -1 )
 
1289
                                        tempd = 0.0;
 
1290
                                    aggDataValue += tempd;
 
1291
                                }
 
1292
                            }
 
1293
                            if ( aggDataValue == -1 )
 
1294
                                aggDataValue = 0.0;
 
1295
                            serviceValues[countForServiceList][countForChildOrgUnitList] += aggDataValue;
 
1296
                        }
 
1297
                    }
 
1298
                }
 
1299
                serviceValues[countForServiceList][countForChildOrgUnitList] /= noOfChildren;
 
1300
                serviceValues[countForServiceList][countForChildOrgUnitList] = Math
 
1301
                    .round( serviceValues[countForServiceList][countForChildOrgUnitList] * Math.pow( 10, 2 ) )
 
1302
                    / Math.pow( 10, 2 );
 
1303
            
 
1304
                numVal = Math.round( numVal * Math.pow( 10, 2 ) ) / Math.pow( 10, 2 );
 
1305
                denVal = Math.round( denVal * Math.pow( 10, 2 ) ) / Math.pow( 10, 2 );
 
1306
            
 
1307
                if ( serviceValues[countForServiceList][countForChildOrgUnitList] == -1 )
 
1308
                    serviceValues[countForServiceList][countForChildOrgUnitList] = 0.0;
 
1309
                if ( ougSetCB == null || facilityLB.equals( "children" ) )
 
1310
                {
 
1311
                    categories1[countForChildOrgUnitList] = childOrgUnit.getShortName();
 
1312
                    categories2[countForChildOrgUnitList] = childOrgUnit.getShortName();
 
1313
                }
 
1314
                else
 
1315
                {
 
1316
                    categories1[countForChildOrgUnitList] = childOrgUnitGroup.getName();
 
1317
                    categories2[countForChildOrgUnitList] = childOrgUnitGroup.getName();
 
1318
                }
 
1319
                if ( riRadio.equals( "indicatorsRadio" ) )
 
1320
                    data2[countForServiceList][countForChildOrgUnitList] = (targetValues.get( "" + ind.getId() ))
 
1321
                        .doubleValue();
 
1322
                else
 
1323
                    data2[countForServiceList][countForChildOrgUnitList] = 0.0;
 
1324
 
 
1325
                if ( riRadio.equals( "dataElementsRadio" ) )
 
1326
                {
 
1327
                    if ( dElement.getType().equalsIgnoreCase( "int" ) )
 
1328
                        dataValues.add( "" + serviceValues[countForServiceList][countForChildOrgUnitList] );
 
1329
                    else
 
1330
                        dataValues.add( " " );
 
1331
                }
 
1332
                else
 
1333
                {
 
1334
                    dataValues.add( "" + serviceValues[countForServiceList][countForChildOrgUnitList] );
 
1335
                    numValues.add( ""+numVal );
 
1336
                    denValues.add( ""+denVal );
 
1337
                }
 
1338
                countForChildOrgUnitList++;
 
1339
            }// childOrgUnitList loop end
 
1340
            dataList.add( dataValues );
 
1341
            numList.add( numValues );
 
1342
            denList.add( denValues );
 
1343
            countForServiceList++;
 
1344
        } // serviceList loop end
 
1345
        return serviceValues;
 
1346
    }// getServiceValuesByFacility end
 
1347
 
 
1348
    /*
 
1349
     * Returns a String which contains all the DataElement Names of Numerator or
 
1350
     * Denominator
 
1351
     */
 
1352
    public String getIndicatorDataElements( String tempSD )
 
1353
    {
 
1354
        char[] tempCD = tempSD.toCharArray();
 
1355
        String deNames = "";
 
1356
        String temp1 = "";
 
1357
        int flag = 0;
 
1358
        try
 
1359
        {
 
1360
            for ( int i = 0; i < tempCD.length; i++ )
 
1361
            {
 
1362
                if ( tempCD[i] == '[' )
 
1363
                {
 
1364
                    flag = 1;
 
1365
                    temp1 = "";
 
1366
                }
 
1367
                else if ( tempCD[i] == ']' )
 
1368
                {
 
1369
                    flag = 2;
 
1370
                    int itemp = Integer.parseInt( temp1 );
 
1371
                    DataElement de = dataElementService.getDataElement( itemp );
 
1372
                    /*if ( de.getAlternativeName() != null )
 
1373
                    {
 
1374
                        deNames += de.getAlternativeName() + ",<br>";
 
1375
                    }
 
1376
                    else*/
 
1377
                    {
 
1378
                        deNames += de.getName() + ",<br>";
 
1379
                    }
 
1380
 
 
1381
                }
 
1382
                else if ( flag == 1 )
 
1383
                    temp1 += tempCD[i];
 
1384
            }// for end
 
1385
        } // try block end
 
1386
        catch ( Exception e )
 
1387
        {
 
1388
            return null;
 
1389
        }
 
1390
 
 
1391
        return deNames;
 
1392
    }// end function getIndicatorDataElements
 
1393
 
 
1394
    /*
 
1395
     * Assigns the Indicator Target values in the list.
 
1396
     */
 
1397
    public void getTargetValues()
 
1398
    {
 
1399
        Connection con = null;
 
1400
        PreparedStatement pst = null;
 
1401
        ResultSet rs = null;
 
1402
 
 
1403
        try
 
1404
        {
 
1405
            DBConnection dbc = new DBConnection();
 
1406
            con = dbc.openConnection();
 
1407
 
 
1408
            String query = "SELECT target FROM indicator WHERE indicatorid = ?";
 
1409
            pst = con.prepareStatement( query );
 
1410
            Iterator serviceIterator = selectedServiceList.iterator();
 
1411
            while ( serviceIterator.hasNext() )
 
1412
            {
 
1413
                Indicator indicator = (Indicator) serviceIterator.next();
 
1414
                pst.setInt( 1, indicator.getId() );
 
1415
                rs = pst.executeQuery();
 
1416
                if ( rs.next() )
 
1417
                {
 
1418
                    targetValues.put( "" + indicator.getId(), new Double( rs.getDouble( 1 ) ) );
 
1419
                }
 
1420
                else
 
1421
                {
 
1422
                    targetValues.put( "" + indicator.getId(), new Double( 0.0 ) );
 
1423
                }
 
1424
                targetList.add( targetValues.get( "" + indicator.getId() ) );
 
1425
                // System.out.println( " Target Value For Indiactor " +
 
1426
                // indicator.getName() + " is : "
 
1427
                // + targetValues.get( "" + indicator.getId() ) );
 
1428
            }// while loop end
 
1429
        } // try block end
 
1430
        catch ( Exception e )
 
1431
        {
 
1432
            System.out.println( "Exception while getting Target Values : " + e.getMessage() );
 
1433
        }
 
1434
        finally
 
1435
        {
 
1436
            try
 
1437
            {
 
1438
                if ( rs != null )
 
1439
                    rs.close();
 
1440
                if ( pst != null )
 
1441
                    pst.close();
 
1442
                if ( con != null )
 
1443
                    con.close();
 
1444
            }
 
1445
            catch ( Exception e )
 
1446
            {
 
1447
                System.out.println( "Exception while closing connection : " + e.getMessage() );
 
1448
            }
 
1449
        }// finally block end
 
1450
 
 
1451
    } // getTargetValues end
 
1452
 
 
1453
    
 
1454
    private List<SurveyData> getSurveyList(Indicator ind, OrganisationUnit selectedOrgUnit)
 
1455
    {
 
1456
        List<SurveyData> resultList = new ArrayList<SurveyData>();
 
1457
        
 
1458
        String path = System.getProperty( "user.home" ) + File.separator + "dhis" + File.separator + "db"
 
1459
        + File.separator + "surveyDataMapping.xml";
 
1460
        try
 
1461
        {
 
1462
            String newpath = System.getenv( "USER_HOME" );
 
1463
            if ( newpath != null )
 
1464
            {
 
1465
                path = newpath + File.separator + "dhis" + File.separator + "db" + File.separator + "surveyDataMapping.xml";
 
1466
            }
 
1467
        }
 
1468
        catch ( NullPointerException npe )
 
1469
        {
 
1470
            // do nothing, but we might be using this somewhere without
 
1471
            // USER_HOME set, which will throw a NPE
 
1472
        }
 
1473
 
 
1474
        try
 
1475
        {
 
1476
            DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
 
1477
            DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
 
1478
            Document doc = docBuilder.parse( new File( path ) );
 
1479
            if ( doc == null )
 
1480
            {
 
1481
                // System.out.println( "There is no DECodes related XML file in
 
1482
                // the user home" );
 
1483
                return null;
 
1484
            }
 
1485
 
 
1486
            NodeList listOfSurveys = doc.getElementsByTagName( "survey" );
 
1487
            int totalSurveys = listOfSurveys.getLength();
 
1488
 
 
1489
            for ( int s = 0; s < totalSurveys; s++ )
 
1490
            {
 
1491
                Element surveyElement = (Element) listOfSurveys.item( s );
 
1492
                int indId = Integer.parseInt(surveyElement.getAttribute( "indicatorid" ));
 
1493
                int ouId = Integer.parseInt(surveyElement.getAttribute( "orgunitid" ));
 
1494
                String dlhs = surveyElement.getAttribute( "dlhs" );
 
1495
                String nfhs = surveyElement.getAttribute( "nfhs" );
 
1496
                if(indId == ind.getId() && ouId==selectedOrgUnit.getId())
 
1497
                {
 
1498
                    if(dlhs.equalsIgnoreCase( "na" ))
 
1499
                    {
 
1500
                    
 
1501
                    }
 
1502
                    else
 
1503
                    {
 
1504
                        SurveyData sd = new SurveyData("DLHS", dlhs);
 
1505
                        resultList.add( sd );
 
1506
                    }
 
1507
                    if(nfhs.equalsIgnoreCase( "na" ))
 
1508
                    {
 
1509
                    
 
1510
                    }
 
1511
                    else
 
1512
                    {
 
1513
                        SurveyData sd = new SurveyData("NFHS", nfhs);
 
1514
                        resultList.add( sd );
 
1515
                    }                    
 
1516
                }            
 
1517
            }// end of for loop with s var
 
1518
        }// try block end
 
1519
        catch ( SAXParseException err )
 
1520
        {
 
1521
            System.out.println( "** Parsing error" + ", line " + err.getLineNumber() + ", uri " + err.getSystemId() );
 
1522
            System.out.println( " " + err.getMessage() );
 
1523
        }
 
1524
        catch ( SAXException e )
 
1525
        {
 
1526
            Exception x = e.getException();
 
1527
            ((x == null) ? e : x).printStackTrace();
 
1528
        }
 
1529
        catch ( Throwable t )
 
1530
        {
 
1531
            t.printStackTrace();
 
1532
        }
 
1533
        
 
1534
        return resultList;
 
1535
    }
 
1536
    
 
1537
 
 
1538
 
 
1539
}// class end