~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/dm/action/DashBoardMatrixResultAction.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.dm.action;
 
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.util.ArrayList;
 
31
import java.util.Collection;
 
32
import java.util.Collections;
 
33
import java.util.Date;
 
34
import java.util.HashMap;
 
35
import java.util.Iterator;
 
36
import java.util.List;
 
37
import java.util.Map;
 
38
 
 
39
import org.hisp.dhis.aggregation.AggregationService;
 
40
import org.hisp.dhis.indicator.Indicator;
 
41
import org.hisp.dhis.indicator.IndicatorGroup;
 
42
import org.hisp.dhis.indicator.IndicatorService;
 
43
import org.hisp.dhis.organisationunit.OrganisationUnit;
 
44
import org.hisp.dhis.organisationunit.OrganisationUnitGroup;
 
45
import org.hisp.dhis.organisationunit.OrganisationUnitGroupService;
 
46
import org.hisp.dhis.organisationunit.OrganisationUnitGroupSet;
 
47
import org.hisp.dhis.organisationunit.OrganisationUnitService;
 
48
import org.hisp.dhis.organisationunit.comparator.OrganisationUnitShortNameComparator;
 
49
import org.hisp.dhis.period.Period;
 
50
import org.hisp.dhis.period.PeriodStore;
 
51
import org.hisp.dhis.period.PeriodType;
 
52
import org.hisp.dhis.source.Source;
 
53
import org.hisp.dhis.source.SourceStore;
 
54
import org.hisp.dhis.target.Target;
 
55
import org.hisp.dhis.target.TargetService;
 
56
 
 
57
import com.opensymphony.xwork.Action;
 
58
 
 
59
public class DashBoardMatrixResultAction
 
60
    implements Action
 
61
{
 
62
 
 
63
    // -------------------------------------------------------------------------
 
64
    // Dependencies
 
65
    // -------------------------------------------------------------------------
 
66
 
 
67
    private OrganisationUnitService organisationUnitService;
 
68
 
 
69
    public void setOrganisationUnitService( OrganisationUnitService organisationUnitService )
 
70
    {
 
71
        this.organisationUnitService = organisationUnitService;
 
72
    }
 
73
 
 
74
    private OrganisationUnitGroupService organisationUnitGroupService;
 
75
 
 
76
    public void setOrganisationUnitGroupService( OrganisationUnitGroupService organisationUnitGroupService )
 
77
    {
 
78
        this.organisationUnitGroupService = organisationUnitGroupService;
 
79
    }
 
80
 
 
81
    private PeriodStore periodStore;
 
82
 
 
83
    public void setPeriodStore( PeriodStore periodStore )
 
84
    {
 
85
        this.periodStore = periodStore;
 
86
    }
 
87
 
 
88
    private IndicatorService indicatorService;
 
89
 
 
90
    public void setIndicatorService( IndicatorService indicatorService )
 
91
    {
 
92
        this.indicatorService = indicatorService;
 
93
    }
 
94
 
 
95
    private TargetService targetService;
 
96
 
 
97
    public void setTargetService( TargetService targetService )
 
98
    {
 
99
        this.targetService = targetService;
 
100
    }
 
101
 
 
102
    private AggregationService aggregationService;
 
103
 
 
104
    public void setAggregationService( AggregationService aggregationService )
 
105
    {
 
106
        this.aggregationService = aggregationService;
 
107
    }
 
108
 
 
109
    private SourceStore sourceStore;
 
110
 
 
111
    public void setSourceStore( SourceStore sourceStore )
 
112
    {
 
113
        this.sourceStore = sourceStore;
 
114
    }
 
115
 
 
116
    // -------------------------------------------------------------------
 
117
    // Input Parameters
 
118
    // -------------------------------------------------------------------
 
119
 
 
120
    private int sDateLB;
 
121
 
 
122
    public void setSDateLB( int dateLB )
 
123
    {
 
124
        sDateLB = dateLB;
 
125
    }
 
126
 
 
127
    private String ougSetCB;
 
128
 
 
129
    public void setOugSetCB( String ougSetCB )
 
130
    {
 
131
        this.ougSetCB = ougSetCB;
 
132
    }
 
133
 
 
134
    private List<String> orgUnitListCB;
 
135
 
 
136
    public void setOrgUnitListCB( List<String> orgUnitListCB )
 
137
    {
 
138
        this.orgUnitListCB = orgUnitListCB;
 
139
    }
 
140
 
 
141
    private int eDateLB;
 
142
 
 
143
    public void setEDateLB( int dateLB )
 
144
    {
 
145
        eDateLB = dateLB;
 
146
    }
 
147
 
 
148
    private String ouNameTB;
 
149
 
 
150
    public String getOuNameTB()
 
151
    {
 
152
        return ouNameTB;
 
153
    }
 
154
 
 
155
    private Integer ouLevel;
 
156
 
 
157
    public Integer getOuLevel()
 
158
    {
 
159
        return ouLevel;
 
160
    }
 
161
 
 
162
    private String selectedGroup;
 
163
 
 
164
    public String getSelectedGroup()
 
165
    {
 
166
        return selectedGroup;
 
167
    }
 
168
 
 
169
    public void setSelectedGroup( String selectedGroup )
 
170
    {
 
171
        this.selectedGroup = selectedGroup;
 
172
    }
 
173
 
 
174
    // -----------------------------------------------------------------
 
175
    // Output parameters
 
176
    // -----------------------------------------------------------------
 
177
 
 
178
    private List<String> selectedIndicators;
 
179
 
 
180
    public List<String> getSelectedIndicators()
 
181
    {
 
182
        return selectedIndicators;
 
183
    }
 
184
 
 
185
    public void setSelectedIndicators( List<String> selectedIndicators )
 
186
    {
 
187
        this.selectedIndicators = selectedIndicators;
 
188
    }
 
189
 
 
190
    private List<String> indicatorNames;
 
191
 
 
192
    public List<String> getIndicatorNames()
 
193
    {
 
194
        return indicatorNames;
 
195
    }
 
196
 
 
197
    private Indicator indicator;
 
198
 
 
199
    public Indicator getIndicator()
 
200
    {
 
201
        return indicator;
 
202
    }
 
203
 
 
204
    private List<Indicator> indicators;
 
205
 
 
206
    public List<Indicator> getIndicators()
 
207
    {
 
208
        return indicators;
 
209
    }
 
210
 
 
211
    private String indicatorName;
 
212
 
 
213
    public String getIndicatorName()
 
214
    {
 
215
        return indicatorName;
 
216
    }
 
217
 
 
218
    private ArrayList<OrganisationUnit> orgUnitList;
 
219
 
 
220
    public ArrayList<OrganisationUnit> getOrgUnitList()
 
221
    {
 
222
        return orgUnitList;
 
223
    }
 
224
 
 
225
    private OrganisationUnitGroup organisationUnitGroup;
 
226
 
 
227
    public void setOrganisationUnitGroup( OrganisationUnitGroup organisationUnitGroup )
 
228
    {
 
229
        this.organisationUnitGroup = organisationUnitGroup;
 
230
    }
 
231
 
 
232
    private OrganisationUnit organisationUnit;
 
233
 
 
234
    public OrganisationUnit getOrganisationUnit()
 
235
    {
 
236
        return organisationUnit;
 
237
    }
 
238
 
 
239
    private Collection<OrganisationUnit> corporations;
 
240
 
 
241
    public Collection<OrganisationUnit> getCorporations()
 
242
    {
 
243
        return corporations;
 
244
    }
 
245
 
 
246
    private Source source;
 
247
 
 
248
    public Source getSource()
 
249
    {
 
250
        return source;
 
251
    }
 
252
 
 
253
    private Period period;
 
254
 
 
255
    public Period getPeriod()
 
256
    {
 
257
        return period;
 
258
    }
 
259
 
 
260
    private PeriodType periodType;
 
261
 
 
262
    public PeriodType getPeriodType()
 
263
    {
 
264
        return periodType;
 
265
    }
 
266
 
 
267
    private Period startPeriod;
 
268
 
 
269
    public Period getStartPeriod()
 
270
    {
 
271
 
 
272
        return startPeriod;
 
273
    }
 
274
 
 
275
    private Period endPeriod;
 
276
 
 
277
    public Period getEndPeriod()
 
278
    {
 
279
        return endPeriod;
 
280
    }
 
281
 
 
282
    private Target target;
 
283
 
 
284
    public Target getTarget()
 
285
    {
 
286
        return target;
 
287
    }
 
288
 
 
289
    private List<Target> targets;
 
290
 
 
291
    public List<Target> getTargets()
 
292
    {
 
293
        return targets;
 
294
    }
 
295
 
 
296
    private String minMaxOrgUnit;
 
297
 
 
298
    public String getMinMaxOrganisationUnit()
 
299
    {
 
300
        return minMaxOrgUnit;
 
301
    }
 
302
 
 
303
    private List<Double> collectedIndicatorValues;
 
304
 
 
305
    public List<Double> getCollectedIndicatorValues()
 
306
    {
 
307
        return collectedIndicatorValues;
 
308
    }
 
309
 
 
310
    private Double indicatorValueResult;
 
311
 
 
312
    public Double getIndicatorValueResult()
 
313
    {
 
314
        return indicatorValueResult;
 
315
    }
 
316
 
 
317
    private String minMaxArray[][];
 
318
 
 
319
    public String[][] getMinMaxArray()
 
320
    {
 
321
        return minMaxArray;
 
322
    }
 
323
 
 
324
    private Double indicatorValue;
 
325
 
 
326
    public Double getIndicatorValue()
 
327
    {
 
328
        return indicatorValue;
 
329
    }
 
330
 
 
331
    private List<String> minValues;
 
332
 
 
333
    public List<String> getMinValues()
 
334
    {
 
335
        return minValues;
 
336
    }
 
337
 
 
338
    private List<String> maxValues;
 
339
 
 
340
    public List<String> getMaxValues()
 
341
    {
 
342
        return maxValues;
 
343
    }
 
344
 
 
345
    private List<String> achievementRates;
 
346
 
 
347
    public List<String> getAchievementRates()
 
348
    {
 
349
        return achievementRates;
 
350
    }
 
351
 
 
352
    private List<Double> targetValues;
 
353
 
 
354
    public List<Double> getTargetValues()
 
355
    {
 
356
        return targetValues;
 
357
    }
 
358
 
 
359
    private String achievementRate;
 
360
 
 
361
    public String getAchievementRate()
 
362
    {
 
363
        return achievementRate;
 
364
    }
 
365
 
 
366
    private Double targetValue;
 
367
 
 
368
    public Double getTargetValue()
 
369
    {
 
370
        return targetValue;
 
371
    }
 
372
 
 
373
    private String minValue;
 
374
 
 
375
    public String getMinValue()
 
376
    {
 
377
        return minValue;
 
378
 
 
379
    }
 
380
 
 
381
    private String maxValue;
 
382
 
 
383
    public String getMaxValue()
 
384
    {
 
385
        return maxValue;
 
386
    }
 
387
 
 
388
    private String value;
 
389
 
 
390
    public String getValue()
 
391
    {
 
392
        return value;
 
393
    }
 
394
 
 
395
    private String minOrgUnit;
 
396
 
 
397
    public String getMinOrgUnit()
 
398
    {
 
399
        return minOrgUnit;
 
400
    }
 
401
 
 
402
    private String maxOrgUnit;
 
403
 
 
404
    public String getMaxOrgUnit()
 
405
    {
 
406
        return maxOrgUnit;
 
407
    }
 
408
 
 
409
    private String message;
 
410
 
 
411
    public String getMessage()
 
412
    {
 
413
        return message;
 
414
    }
 
415
 
 
416
    private Collection<OrganisationUnitGroup> orgUnitGroups;
 
417
 
 
418
    public Collection<OrganisationUnitGroup> getOrgUnitGroups()
 
419
    {
 
420
        return orgUnitGroups;
 
421
    }
 
422
 
 
423
    private Integer counter;
 
424
 
 
425
    private Integer ougSetControl;
 
426
 
 
427
    public Integer getOugSetControl()
 
428
    {
 
429
        return ougSetControl;
 
430
    }
 
431
 
 
432
    // ------------------------------------------------------------------
 
433
    // Action implementation
 
434
    // ------------------------------------------------------------------
 
435
 
 
436
    @SuppressWarnings( { "unchecked", "static-access" } )
 
437
    public String execute()
 
438
        throws Exception
 
439
    {
 
440
 
 
441
        organisationUnit = new OrganisationUnit();
 
442
 
 
443
        organisationUnitGroup = new OrganisationUnitGroup();
 
444
 
 
445
        orgUnitList = new ArrayList<OrganisationUnit>();
 
446
 
 
447
        orgUnitGroups = new ArrayList<OrganisationUnitGroup>();
 
448
 
 
449
        counter = 0;
 
450
 
 
451
        ougSetControl = 0;
 
452
 
 
453
        if ( ougSetCB == null )
 
454
        {
 
455
            ougSetControl = 0;
 
456
 
 
457
            organisationUnit = organisationUnitService.getOrganisationUnit( Integer.parseInt( orgUnitListCB.get( 0 ) ) );
 
458
 
 
459
            ouLevel = organisationUnitService.getLevelOfOrganisationUnit( organisationUnit );
 
460
            
 
461
            Collection orgList = new ArrayList<OrganisationUnit>();
 
462
 
 
463
            orgList = organisationUnit.getChildren();
 
464
 
 
465
            organisationUnitGroup = new OrganisationUnitGroup();
 
466
 
 
467
            if ( orgList == null || orgList.isEmpty() || (orgList.size() == 0) )
 
468
            {
 
469
                message = "Please select an Organisation Unit with Children";
 
470
 
 
471
                return INPUT;
 
472
            }
 
473
 
 
474
            else
 
475
            {
 
476
                if ( selectedGroup == null || selectedGroup.equals( "" ) )
 
477
                {
 
478
                    organisationUnitGroup = organisationUnitGroupService
 
479
                        .getOrganisationUnitGroupByName( "Corporations" );
 
480
 
 
481
                    corporations = new ArrayList<OrganisationUnit>();
 
482
 
 
483
                    corporations = organisationUnitGroup.getMembers();
 
484
 
 
485
                    Iterator corpIterator = corporations.iterator();
 
486
 
 
487
                    while ( corpIterator.hasNext() )
 
488
                    {
 
489
                        OrganisationUnit currentCorp = (OrganisationUnit) corpIterator.next();
 
490
 
 
491
                        if ( orgList.contains( currentCorp ) )
 
492
                        {
 
493
                            orgList.remove( currentCorp );
 
494
                        }
 
495
                    }
 
496
 
 
497
                    orgUnitList.addAll( orgList );
 
498
                }
 
499
 
 
500
                else
 
501
                {
 
502
                    if ( selectedGroup.equalsIgnoreCase( "corporations" ) && (ouLevel == 1) )
 
503
                    {
 
504
                        organisationUnitGroup = organisationUnitGroupService
 
505
                            .getOrganisationUnitGroupByName( "Corporations" );
 
506
 
 
507
                        corporations = new ArrayList<OrganisationUnit>();
 
508
 
 
509
                        corporations = organisationUnitGroup.getMembers();
 
510
 
 
511
                        orgUnitList.addAll( corporations );
 
512
 
 
513
                    }
 
514
 
 
515
                    else if ( selectedGroup.equalsIgnoreCase( "corporations" ) && (ouLevel != 1) )
 
516
                    {
 
517
                        List<OrganisationUnit> rootOrgUnits = new ArrayList<OrganisationUnit>( organisationUnitService
 
518
                            .getRootOrganisationUnits() );
 
519
 
 
520
                        OrganisationUnit topOrgUnit = new OrganisationUnit();
 
521
 
 
522
                        topOrgUnit = rootOrgUnits.get( 0 );
 
523
 
 
524
                        message = "Please select " + topOrgUnit.getShortName() + " or select another group";
 
525
 
 
526
                        return INPUT;
 
527
 
 
528
                    }
 
529
 
 
530
                    else
 
531
                    {
 
532
                        organisationUnitGroup = organisationUnitGroupService
 
533
                            .getOrganisationUnitGroupByName( "Corporations" );
 
534
 
 
535
                        corporations = new ArrayList<OrganisationUnit>();
 
536
 
 
537
                        corporations = organisationUnitGroup.getMembers();
 
538
 
 
539
                        Iterator corpIterator = corporations.iterator();
 
540
 
 
541
                        while ( corpIterator.hasNext() )
 
542
                        {
 
543
                            OrganisationUnit currentCorp = (OrganisationUnit) corpIterator.next();
 
544
 
 
545
                            if ( orgList.contains( currentCorp ) )
 
546
                            {
 
547
                                orgList.remove( currentCorp );
 
548
                            }
 
549
                        }
 
550
 
 
551
                        orgUnitList.addAll( orgList );
 
552
                    }
 
553
 
 
554
                }
 
555
 
 
556
            }
 
557
 
 
558
            ouLevel = organisationUnitService.getLevelOfOrganisationUnit( organisationUnit );
 
559
 
 
560
            Collections.sort( orgUnitList, new OrganisationUnitShortNameComparator() );
 
561
 
 
562
        }
 
563
 
 
564
        else
 
565
        {
 
566
            ougSetControl = 1;
 
567
 
 
568
            Iterator cbIterator = orgUnitListCB.iterator();
 
569
 
 
570
            while ( cbIterator.hasNext() )
 
571
            {
 
572
                String cb = (String) cbIterator.next();
 
573
 
 
574
                OrganisationUnitGroup og = new OrganisationUnitGroup();
 
575
 
 
576
                og = organisationUnitGroupService.getOrganisationUnitGroup( Integer.parseInt( cb ) );
 
577
 
 
578
                orgUnitGroups.add( og );
 
579
 
 
580
            }
 
581
 
 
582
        }
 
583
 
 
584
        indicatorName = "";
 
585
 
 
586
        indicatorNames = new ArrayList<String>();
 
587
 
 
588
        minValues = new ArrayList<String>();
 
589
 
 
590
        maxValues = new ArrayList<String>();
 
591
        
 
592
        targetValues = new ArrayList<Double>();
 
593
        
 
594
        targetValue = 0.0;
 
595
 
 
596
        collectedIndicatorValues = new ArrayList<Double>();
 
597
 
 
598
        startPeriod = periodStore.getPeriod( sDateLB );
 
599
 
 
600
        endPeriod = periodStore.getPeriod( eDateLB );
 
601
 
 
602
        indicators = new ArrayList<Indicator>();
 
603
 
 
604
        Iterator indiIterator = selectedIndicators.iterator();
 
605
 
 
606
        while ( indiIterator.hasNext() )
 
607
        {
 
608
            String str = (String) indiIterator.next();
 
609
 
 
610
            Indicator ind = new Indicator();
 
611
 
 
612
            ind = indicatorService.getIndicator( Integer.parseInt( str ) );
 
613
 
 
614
            indicators.add( ind );
 
615
        }
 
616
 
 
617
        Date startDate = startPeriod.getStartDate();
 
618
 
 
619
        Date endDate = endPeriod.getEndDate();
 
620
 
 
621
        int i;
 
622
 
 
623
        if ( ougSetCB == null )
 
624
        {
 
625
 
 
626
            Iterator indicatorsIterator = selectedIndicators.iterator();
 
627
            
 
628
            targetValue = 0.0;
 
629
 
 
630
            indicatorName = "";
 
631
 
 
632
            indicatorNames = new ArrayList<String>();
 
633
 
 
634
            minValues = new ArrayList<String>();
 
635
 
 
636
            maxValues = new ArrayList<String>();
 
637
 
 
638
            targetValues = new ArrayList<Double>();
 
639
 
 
640
            collectedIndicatorValues = new ArrayList<Double>();
 
641
 
 
642
            while ( indicatorsIterator.hasNext() )
 
643
            {
 
644
 
 
645
                i = 0;
 
646
 
 
647
                indicator = indicatorService.getIndicator( Integer.parseInt( (String) indicatorsIterator.next() ) );
 
648
 
 
649
                indicatorNames.add( indicator.getName() );
 
650
 
 
651
                period = periodStore.getPeriod( startDate, endDate, PeriodType.getPeriodTypeByName( "monthly" ) );
 
652
 
 
653
                targets = new ArrayList<Target>();
 
654
 
 
655
                List<Period> tempPeriodList = new ArrayList<Period>( periodStore
 
656
                    .getIntersectingPeriods( startDate, endDate ) );
 
657
                Iterator tempPeriodListIterator = tempPeriodList.iterator();
 
658
                while ( tempPeriodListIterator.hasNext() )
 
659
                {
 
660
                    Period tempPeriod = (Period) tempPeriodListIterator.next();
 
661
                    List<Target> tempTarget = new ArrayList<Target>( targetService.getTargets( indicator, tempPeriod,
 
662
                        organisationUnit ) );
 
663
                    if ( tempTarget != null )
 
664
                        targets.addAll( tempTarget );
 
665
                }
 
666
                
 
667
                if (targets != null && targets.size() > 0)
 
668
                {
 
669
                    targetValue = targets.get( 0 ).getValue();
 
670
                }
 
671
 
 
672
                else
 
673
                {
 
674
                    targetValue = 0.0;
 
675
                }
 
676
 
 
677
                targetValue = Math.round( targetValue * Math.pow( 10, 0 ) ) / Math.pow( 10, 0 );
 
678
 
 
679
                if ( targetValue == -1 )
 
680
                {
 
681
                    targetValue = 0.0;
 
682
                }
 
683
 
 
684
                targetValues.add(targetValue);
 
685
                
 
686
                if ( organisationUnit != null )
 
687
                {
 
688
                    indicatorValue = aggregationService.getAggregatedIndicatorValue( indicator, startDate, endDate,
 
689
                        organisationUnit );
 
690
 
 
691
                }
 
692
 
 
693
                else
 
694
                {
 
695
                    indicatorValue = aggregationService.getAggregatedIndicatorValue( indicator, startDate, endDate,
 
696
                        organisationUnit );
 
697
 
 
698
                }
 
699
 
 
700
                indicatorValue = Math.round( indicatorValue * Math.pow( 10, 0 ) ) / Math.pow( 10, 0 );
 
701
 
 
702
                if ( indicatorValue == -1 )
 
703
                {
 
704
                    indicatorValue = 0.0;
 
705
                }
 
706
 
 
707
                collectedIndicatorValues.add( indicatorValue );
 
708
 
 
709
                Iterator organisationUnitListIterator = orgUnitList.iterator();
 
710
 
 
711
                minMaxArray = new String[orgUnitList.size()][2];
 
712
 
 
713
                while ( organisationUnitListIterator.hasNext() )
 
714
                {
 
715
 
 
716
                    OrganisationUnit o = (OrganisationUnit) organisationUnitListIterator.next();
 
717
 
 
718
                    indicatorValueResult = aggregationService.getAggregatedIndicatorValue( indicator, startDate,
 
719
                        endDate, o );
 
720
 
 
721
                    indicatorValueResult = Math.round( indicatorValueResult * Math.pow( 10, 0 ) ) / Math.pow( 10, 0 );
 
722
 
 
723
                    if ( indicatorValueResult == -1 )
 
724
                    {
 
725
                        indicatorValueResult = 0.0;
 
726
                    }
 
727
 
 
728
                    minMaxOrgUnit = o.getShortName();
 
729
 
 
730
                    minMaxArray[i][0] = String.valueOf( 0 );
 
731
 
 
732
                    minMaxArray[i][1] = String.valueOf( 0 );
 
733
 
 
734
                    minMaxArray[i][0] = String.valueOf( indicatorValueResult );
 
735
 
 
736
                    minMaxArray[i][1] = minMaxOrgUnit;
 
737
 
 
738
                    i++;
 
739
 
 
740
                }
 
741
 
 
742
                minMaxSort( minMaxArray );
 
743
 
 
744
                StringBuffer minValue = new StringBuffer( minMaxArray[0][0] + " " );
 
745
 
 
746
                StringBuffer maxValue = new StringBuffer( minMaxArray[orgUnitList.size() - 1][0] + " " );
 
747
 
 
748
                int count = 4;
 
749
 
 
750
                if (selectedGroup != null && selectedGroup.equalsIgnoreCase( "corporations" ))
 
751
                {
 
752
                    count = 3;
 
753
                }
 
754
                
 
755
                else
 
756
                {
 
757
                    count = 4;
 
758
                }
 
759
 
 
760
                for ( int q = 0; q < minMaxArray.length; q++ )
 
761
                {
 
762
                    String currentValue = minMaxArray[q][0];
 
763
 
 
764
                    String currentOrgUnit = minMaxArray[q][1];
 
765
 
 
766
                    if ( currentValue.equalsIgnoreCase( minMaxArray[0][0] ) )
 
767
                    {
 
768
                        if ( q == 0 || q % count == 0 )
 
769
                        {
 
770
                            minValue.append( "<br>" );
 
771
                        }
 
772
 
 
773
                        if ( q == minMaxArray.length - 1 )
 
774
                        {
 
775
                            minValue.append( currentOrgUnit );
 
776
                        }
 
777
 
 
778
                        else
 
779
                        {
 
780
                            minValue.append( currentOrgUnit ).append( ", " );
 
781
                        }
 
782
 
 
783
                    }
 
784
 
 
785
                    if ( currentValue.equalsIgnoreCase( minMaxArray[orgUnitList.size() - 1][0] ) )
 
786
                    {
 
787
                        if ( q == 0 || q % count == 0 )
 
788
                        {
 
789
                            maxValue.append( "<br>" );
 
790
                        }
 
791
 
 
792
                        if ( q == minMaxArray.length - 1 )
 
793
                        {
 
794
                            maxValue.append( currentOrgUnit );
 
795
                        }
 
796
                        else
 
797
                        {
 
798
                            maxValue.append( currentOrgUnit ).append( ", " );
 
799
                        }
 
800
                    }
 
801
 
 
802
                }
 
803
 
 
804
                minValues.add( minValue.toString() );
 
805
 
 
806
                maxValues.add( maxValue.toString() );
 
807
 
 
808
                counter++;
 
809
 
 
810
            }
 
811
 
 
812
        }
 
813
 
 
814
        else
 
815
        {
 
816
            OrganisationUnitGroup oGroup = new OrganisationUnitGroup();
 
817
 
 
818
            Iterator orgUnitGroupIterator = orgUnitGroups.iterator();
 
819
 
 
820
            minMaxArray = new String[orgUnitList.size()][2];
 
821
 
 
822
            while ( orgUnitGroupIterator.hasNext() )
 
823
            {
 
824
                oGroup = (OrganisationUnitGroup) orgUnitGroupIterator.next();
 
825
 
 
826
                Iterator indicatorsIterator = selectedIndicators.iterator();
 
827
 
 
828
                while ( indicatorsIterator.hasNext() )
 
829
                {
 
830
 
 
831
                    i = 0;
 
832
 
 
833
                    Double indicatorSum = 0.0;
 
834
 
 
835
                    indicator = indicatorService.getIndicator( Integer.parseInt( (String) indicatorsIterator.next() ) );
 
836
 
 
837
                    indicatorNames.add( indicator.getName() );
 
838
 
 
839
                    period = periodStore.getPeriod( startDate, endDate, PeriodType.getPeriodTypeByName( "monthly" ) );
 
840
 
 
841
                    /*targets = new ArrayList<Target>();
 
842
 
 
843
                    List<Period> tempPeriodList = new ArrayList<Period>( periodStore
 
844
                        .getIntersectingPeriods( startDate, endDate ) );
 
845
                    Iterator tempPeriodListIterator = tempPeriodList.iterator();
 
846
                    while ( tempPeriodListIterator.hasNext() )
 
847
                    {
 
848
                        Period tempPeriod = (Period) tempPeriodListIterator.next();
 
849
                        List<Target> tempTarget = new ArrayList<Target>( targetService.getTargets( indicator, tempPeriod,
 
850
                            organisationUnit ) );
 
851
                        if ( tempTarget != null )
 
852
                            targets.addAll( tempTarget );
 
853
                    }
 
854
                    
 
855
                    if (targets == null)
 
856
                    {
 
857
                        targetValue = 0.0;
 
858
                    }
 
859
                    
 
860
                    else
 
861
                    {
 
862
                        target = targets.get(0);
 
863
                        
 
864
                        targetValue = target.getValue();
 
865
                    }
 
866
 
 
867
                    targetValue = Math.round( targetValue * Math.pow( 10, 0 ) ) / Math.pow( 10, 0 );
 
868
 
 
869
                    if ( targetValue == -1 )
 
870
                    {
 
871
                        targetValue = 0.0;
 
872
                    }
 
873
 
 
874
                    targetValues.add(targetValue);*/
 
875
                    
 
876
                    orgUnitList = new ArrayList<OrganisationUnit>();
 
877
 
 
878
                    orgUnitList.addAll( oGroup.getMembers() );
 
879
 
 
880
                    Iterator organisationUnitListIterator = orgUnitList.iterator();
 
881
 
 
882
                    minMaxArray = new String[orgUnitList.size()][2];
 
883
 
 
884
                    while ( organisationUnitListIterator.hasNext() )
 
885
                    {
 
886
 
 
887
                        OrganisationUnit o = (OrganisationUnit) organisationUnitListIterator.next();
 
888
 
 
889
                        indicatorValueResult = aggregationService.getAggregatedIndicatorValue( indicator, startDate,
 
890
                            endDate, o );
 
891
 
 
892
                        indicatorValueResult = Math.round( indicatorValueResult * Math.pow( 10, 0 ) )
 
893
                            / Math.pow( 10, 0 );
 
894
 
 
895
                        if ( indicatorValueResult == -1 )
 
896
                        {
 
897
                            indicatorValueResult = 0.0;
 
898
                        }
 
899
 
 
900
                        minMaxOrgUnit = o.getShortName();
 
901
 
 
902
                        minMaxArray[i][0] = String.valueOf( 0 );
 
903
 
 
904
                        minMaxArray[i][1] = String.valueOf( 0 );
 
905
 
 
906
                        minMaxArray[i][0] = String.valueOf( indicatorValueResult );
 
907
 
 
908
                        minMaxArray[i][1] = minMaxOrgUnit;
 
909
 
 
910
                        i++;
 
911
 
 
912
                        indicatorSum += indicatorValueResult;
 
913
 
 
914
                    }
 
915
 
 
916
                    indicatorValue = indicatorSum / orgUnitList.size();
 
917
 
 
918
                    indicatorValue = Math.round( indicatorValue * Math.pow( 10, 0 ) ) / Math.pow( 10, 0 );
 
919
 
 
920
                    collectedIndicatorValues.add( indicatorValue );
 
921
 
 
922
                    minMaxSort( minMaxArray );
 
923
 
 
924
                    String minValue = minMaxArray[0][0] + " ";
 
925
 
 
926
                    String maxValue = minMaxArray[orgUnitList.size() - 1][0] + " ";
 
927
 
 
928
                    int count = 4;
 
929
 
 
930
                    for ( int q = 0; q < minMaxArray.length; q++ )
 
931
                    {
 
932
                        String currentValue = minMaxArray[q][0];
 
933
 
 
934
                        String currentOrgUnit = minMaxArray[q][1];
 
935
 
 
936
                        if ( currentValue.equalsIgnoreCase( minMaxArray[0][0] ) )
 
937
                        {
 
938
                            if ( q == 0 || q % count == 0 )
 
939
                            {
 
940
                                minValue += "<br>";
 
941
                            }
 
942
 
 
943
                            if ( q == minMaxArray.length - 1 )
 
944
                            {
 
945
                                minValue += currentOrgUnit;
 
946
                            }
 
947
 
 
948
                            else
 
949
                            {
 
950
                                minValue += currentOrgUnit + ", ";
 
951
                            }
 
952
 
 
953
                        }
 
954
 
 
955
                        if ( currentValue.equalsIgnoreCase( minMaxArray[orgUnitList.size() - 1][0] ) )
 
956
                        {
 
957
                            if ( q == 0 || q % count == 0 )
 
958
                            {
 
959
                                maxValue += "<br>";
 
960
                            }
 
961
 
 
962
                            if ( q == minMaxArray.length - 1 )
 
963
                            {
 
964
                                maxValue += currentOrgUnit;
 
965
                            }
 
966
                            else
 
967
                            {
 
968
                                maxValue += currentOrgUnit + ", ";
 
969
                            }
 
970
                        }
 
971
 
 
972
                    }
 
973
 
 
974
                    minValues.add( minValue );
 
975
 
 
976
                    maxValues.add( maxValue );
 
977
 
 
978
                    counter++;
 
979
 
 
980
                }
 
981
 
 
982
            }
 
983
 
 
984
        }
 
985
 
 
986
        return SUCCESS;
 
987
    }
 
988
 
 
989
    public String[][] minMaxSort( String[][] minMaxArray )
 
990
    {
 
991
        int n = minMaxArray.length;
 
992
 
 
993
        boolean doMore = true;
 
994
 
 
995
        Double currentValue;
 
996
 
 
997
        Double nextValue;
 
998
 
 
999
        String currentOrgUnit;
 
1000
 
 
1001
        String nextOrgUnit;
 
1002
 
 
1003
        while ( doMore )
 
1004
        {
 
1005
            n--;
 
1006
 
 
1007
            doMore = false;
 
1008
 
 
1009
            for ( int m = 0; m < n; m++ )
 
1010
            {
 
1011
                if ( m <= n - 1 )
 
1012
                {
 
1013
                    currentValue = Double.valueOf( minMaxArray[m][0] );
 
1014
 
 
1015
                    nextValue = Double.valueOf( minMaxArray[m + 1][0] );
 
1016
 
 
1017
                    currentOrgUnit = minMaxArray[m][1];
 
1018
 
 
1019
                    nextOrgUnit = minMaxArray[m + 1][1];
 
1020
                }
 
1021
 
 
1022
                else
 
1023
                {
 
1024
                    currentValue = Double.valueOf( minMaxArray[m][0] );
 
1025
 
 
1026
                    nextValue = currentValue;
 
1027
 
 
1028
                    currentOrgUnit = minMaxArray[m][1];
 
1029
 
 
1030
                    nextOrgUnit = currentOrgUnit;
 
1031
 
 
1032
                }
 
1033
                if ( currentValue > nextValue )
 
1034
                {
 
1035
                    String tempValue = minMaxArray[m][0];
 
1036
 
 
1037
                    String tempOrgUnit = minMaxArray[m][1];
 
1038
 
 
1039
                    minMaxArray[m][0] = minMaxArray[m + 1][0];
 
1040
 
 
1041
                    minMaxArray[m + 1][0] = tempValue;
 
1042
 
 
1043
                    minMaxArray[m][1] = minMaxArray[m + 1][1];
 
1044
 
 
1045
                    minMaxArray[m + 1][1] = tempOrgUnit;
 
1046
 
 
1047
                    doMore = true;
 
1048
                }
 
1049
 
 
1050
                if ( currentValue == nextValue )
 
1051
                {
 
1052
                    if ( currentOrgUnit.compareTo( nextOrgUnit ) < 0 )
 
1053
                    {
 
1054
                        String tempValue = minMaxArray[m][0];
 
1055
 
 
1056
                        String tempOrgUnit = minMaxArray[m][1];
 
1057
 
 
1058
                        minMaxArray[m][0] = minMaxArray[m + 1][0];
 
1059
 
 
1060
                        minMaxArray[m + 1][0] = tempValue;
 
1061
 
 
1062
                        minMaxArray[m][1] = minMaxArray[m + 1][1];
 
1063
 
 
1064
                        minMaxArray[m + 1][1] = tempOrgUnit;
 
1065
 
 
1066
                        doMore = true;
 
1067
 
 
1068
                    }
 
1069
                }
 
1070
 
 
1071
            }
 
1072
        }
 
1073
 
 
1074
        return minMaxArray;
 
1075
    }
 
1076
 
 
1077
}