~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/aa/action/GenerateAnnualAnalysisDataAction.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.aa.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.Calendar;
 
32
import java.util.Collection;
 
33
import java.util.Date;
 
34
import java.util.Iterator;
 
35
import java.util.List;
 
36
 
 
37
import javax.servlet.http.HttpServletRequest;
 
38
import javax.servlet.http.HttpSession;
 
39
 
 
40
import org.hisp.dhis.aggregation.AggregationService;
 
41
import org.hisp.dhis.dashboard.util.DashBoardService;
 
42
import org.hisp.dhis.dataelement.DataElement;
 
43
import org.hisp.dhis.dataelement.DataElementCategoryCombo;
 
44
import org.hisp.dhis.dataelement.DataElementCategoryOptionCombo;
 
45
import org.hisp.dhis.dataelement.DataElementService;
 
46
import org.hisp.dhis.indicator.Indicator;
 
47
import org.hisp.dhis.indicator.IndicatorService;
 
48
import org.hisp.dhis.organisationunit.OrganisationUnit;
 
49
import org.hisp.dhis.organisationunit.OrganisationUnitService;
 
50
import org.hisp.dhis.period.Period;
 
51
import org.hisp.dhis.period.PeriodStore;
 
52
import org.hisp.dhis.period.PeriodType;
 
53
 
 
54
import com.opensymphony.webwork.ServletActionContext;
 
55
import com.opensymphony.xwork.Action;
 
56
import com.opensymphony.xwork.ActionContext;
 
57
 
 
58
public class GenerateAnnualAnalysisDataAction
 
59
    implements Action
 
60
{
 
61
 
 
62
    /* Dependencies */
 
63
    private OrganisationUnitService organisationUnitService;
 
64
 
 
65
    public void setOrganisationUnitService( OrganisationUnitService organisationUnitService )
 
66
    {
 
67
        this.organisationUnitService = organisationUnitService;
 
68
    }
 
69
 
 
70
    private PeriodStore periodStore;
 
71
 
 
72
    public void setPeriodStore( PeriodStore periodStore )
 
73
    {
 
74
        this.periodStore = periodStore;
 
75
    }
 
76
 
 
77
    private IndicatorService indicatorService;
 
78
 
 
79
    public void setIndicatorService( IndicatorService indicatorService )
 
80
    {
 
81
        this.indicatorService = indicatorService;
 
82
    }
 
83
 
 
84
    private DataElementService dataElementService;
 
85
 
 
86
    public void setDataElementService( DataElementService dataElementService )
 
87
    {
 
88
        this.dataElementService = dataElementService;
 
89
    }
 
90
 
 
91
    private DashBoardService dashBoardService;
 
92
 
 
93
    public void setDashBoardService( DashBoardService dashBoardService )
 
94
    {
 
95
        this.dashBoardService = dashBoardService;
 
96
    }
 
97
 
 
98
    private AggregationService aggregationService;
 
99
 
 
100
    public void setAggregationService( AggregationService aggregationService )
 
101
    {
 
102
        this.aggregationService = aggregationService;
 
103
    }
 
104
 
 
105
    /* Parameters */
 
106
    private OrganisationUnit selectedOrgUnit;
 
107
 
 
108
    private DataElement selectedDataElement;
 
109
 
 
110
    private Indicator selectedIndicator;
 
111
 
 
112
    private PeriodType monthlyPeriodType;
 
113
 
 
114
    private String[] series1;
 
115
 
 
116
    public String[] getSeries1()
 
117
    {
 
118
        return series1;
 
119
    }
 
120
 
 
121
    private String[] categories1;
 
122
 
 
123
    public String[] getCategories1()
 
124
    {
 
125
        return categories1;
 
126
    }
 
127
 
 
128
    private String[] series2;
 
129
 
 
130
    public String[] getSeries2()
 
131
    {
 
132
        return series2;
 
133
    }
 
134
 
 
135
    private String[] categories2;
 
136
 
 
137
    public String[] getCategories2()
 
138
    {
 
139
        return categories2;
 
140
    }
 
141
 
 
142
    String chartTitle = "Service : ";
 
143
 
 
144
    public String getChartTitle()
 
145
    {
 
146
        return chartTitle;
 
147
    }
 
148
 
 
149
    String xAxis_Title;
 
150
 
 
151
    public String getXAxis_Title()
 
152
    {
 
153
        return xAxis_Title;
 
154
    }
 
155
 
 
156
    String yAxis_Title;
 
157
 
 
158
    public String getYAxis_Title()
 
159
    {
 
160
        return yAxis_Title;
 
161
    }
 
162
 
 
163
    List<String> numeratorDEList;
 
164
 
 
165
    public List<String> getNumeratorDEList()
 
166
    {
 
167
        return numeratorDEList;
 
168
    }
 
169
 
 
170
    List<String> denominatorDEList;
 
171
 
 
172
    public List<String> getDenominatorDEList()
 
173
    {
 
174
        return denominatorDEList;
 
175
    }
 
176
 
 
177
    Double data1[][];
 
178
 
 
179
    public Double[][] getData1()
 
180
    {
 
181
        return data1;
 
182
    }
 
183
 
 
184
    Double data2[][];
 
185
 
 
186
    public Double[][] getData2()
 
187
    {
 
188
        return data2;
 
189
    }
 
190
 
 
191
    List<List<Double>> dataList;
 
192
 
 
193
    public List<List<Double>> getDataList()
 
194
    {
 
195
        return dataList;
 
196
    }
 
197
 
 
198
    List<String> xseriesList;
 
199
 
 
200
    public List<String> getXseriesList()
 
201
    {
 
202
        return xseriesList;
 
203
    }
 
204
 
 
205
    List<String> yseriesList;
 
206
 
 
207
    public List<String> getYseriesList()
 
208
    {
 
209
        return yseriesList;
 
210
    }
 
211
 
 
212
    /* Input Parameters */
 
213
    private int availableDataElements;
 
214
 
 
215
    private int availableIndicators;
 
216
 
 
217
    private List<String> annualPeriodsListCB;
 
218
 
 
219
    private List<String> monthlyPeriodsListCB;
 
220
 
 
221
    private int ouIDTB;
 
222
 
 
223
    private String riRadio;
 
224
 
 
225
    public String getRiRadio()
 
226
    {
 
227
        return riRadio;
 
228
    }
 
229
 
 
230
    public void setRiRadio( String riRadio )
 
231
    {
 
232
        this.riRadio = riRadio;
 
233
    }
 
234
 
 
235
    public void setAnnualPeriodsListCB( List<String> annualPeriodsListCB )
 
236
    {
 
237
        this.annualPeriodsListCB = annualPeriodsListCB;
 
238
    }
 
239
 
 
240
    public void setAvailableDataElements( int availableDataElements )
 
241
    {
 
242
        this.availableDataElements = availableDataElements;
 
243
    }
 
244
 
 
245
    public void setAvailableIndicators( int availableIndicators )
 
246
    {
 
247
        this.availableIndicators = availableIndicators;
 
248
    }
 
249
 
 
250
    public void setMonthlyPeriodsListCB( List<String> monthlyPeriodsListCB )
 
251
    {
 
252
        this.monthlyPeriodsListCB = monthlyPeriodsListCB;
 
253
    }
 
254
 
 
255
    public void setOuIDTB( int ouIDTB )
 
256
    {
 
257
        this.ouIDTB = ouIDTB;
 
258
    }
 
259
 
 
260
    public String execute()
 
261
        throws Exception
 
262
    {
 
263
        dataList = new ArrayList<List<Double>>();
 
264
        xseriesList = new ArrayList<String>();
 
265
        yseriesList = new ArrayList<String>();
 
266
 
 
267
        // OrgUnit Related Info
 
268
        selectedOrgUnit = new OrganisationUnit();
 
269
        selectedOrgUnit = organisationUnitService.getOrganisationUnit( ouIDTB );
 
270
        chartTitle = "Facility : " + selectedOrgUnit.getShortName();
 
271
 
 
272
        // Service Related Info
 
273
        if ( riRadio.equals( "indicatorsRadio" ) )
 
274
        {
 
275
            selectedIndicator = new Indicator();
 
276
            selectedIndicator = indicatorService.getIndicator( availableIndicators );
 
277
            chartTitle += "\n Indicator : " + selectedIndicator.getName();
 
278
 
 
279
        }
 
280
        else
 
281
        {
 
282
            selectedDataElement = new DataElement();
 
283
            selectedDataElement = dataElementService.getDataElement( availableDataElements );
 
284
            if ( selectedDataElement.getAlternativeName() != null )
 
285
                chartTitle += "\n DataElement : " + selectedDataElement.getAlternativeName();
 
286
            else
 
287
                chartTitle += "\n DataElement : " + selectedDataElement.getName();
 
288
        }
 
289
 
 
290
        // Period Related Info
 
291
        monthlyPeriodType = dashBoardService.getPeriodTypeObject( "monthly" );
 
292
 
 
293
        data1 = getServiceValuesByPeriod();
 
294
        xAxis_Title = "Month";
 
295
        yAxis_Title = "Facilty";
 
296
 
 
297
        int count1 = 0;
 
298
        while ( count1 != categories1.length )
 
299
        {
 
300
            xseriesList.add( categories1[count1] );
 
301
            count1++;
 
302
        }
 
303
 
 
304
        ActionContext ctx = ActionContext.getContext();
 
305
        HttpServletRequest req = (HttpServletRequest) ctx.get( ServletActionContext.HTTP_REQUEST );
 
306
 
 
307
        HttpSession session = req.getSession();
 
308
        session.setAttribute( "data1", data1 );
 
309
        session.setAttribute( "data2", data2 );
 
310
        session.setAttribute( "series1", series1 );
 
311
        session.setAttribute( "categories1", categories1 );
 
312
        session.setAttribute( "series2", series2 );
 
313
        session.setAttribute( "categories2", categories2 );
 
314
        session.setAttribute( "chartTitle", chartTitle );
 
315
        session.setAttribute( "xAxisTitle", xAxis_Title );
 
316
        session.setAttribute( "yAxisTitle", yAxis_Title );
 
317
 
 
318
        return SUCCESS;
 
319
    }// execute end
 
320
 
 
321
    /*
 
322
     * Returns the values for selected years by month wise for ex:- the months
 
323
     * are jan,feb,mar and the years are 2006 and 2007 then it returns the
 
324
     * values for 2006 - jan, feb, mar and 2007 - jan, feb, mar for the selected
 
325
     * orgunit and selected service
 
326
     */
 
327
    public Double[][] getServiceValuesByPeriod()
 
328
    {
 
329
        Double[][] serviceValues = new Double[annualPeriodsListCB.size()][monthlyPeriodsListCB.size()];
 
330
        data2 = new Double[annualPeriodsListCB.size()][monthlyPeriodsListCB.size()];
 
331
        String[] monthNames = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
 
332
 
 
333
        int count1 = 0;
 
334
        int count2 = 0;
 
335
 
 
336
        Period p = new Period();
 
337
 
 
338
        series1 = new String[annualPeriodsListCB.size()];
 
339
        series2 = new String[annualPeriodsListCB.size()];
 
340
        categories1 = new String[monthlyPeriodsListCB.size()];
 
341
        categories2 = new String[monthlyPeriodsListCB.size()];
 
342
        Iterator iterator1 = annualPeriodsListCB.iterator();
 
343
        while ( iterator1.hasNext() )
 
344
        {
 
345
            List<Double> dataValues = new ArrayList<Double>();
 
346
            int tempYear = Integer.parseInt( (String) iterator1.next() );
 
347
            series1[count1] = "" + tempYear;
 
348
            series2[count1] = " ";
 
349
            yseriesList.add( "" + tempYear );
 
350
 
 
351
            Iterator iterator2 = monthlyPeriodsListCB.iterator();
 
352
            count2 = 0;
 
353
            while ( iterator2.hasNext() )
 
354
            {
 
355
                int tempMonth = Integer.parseInt( (String) iterator2.next() );
 
356
                p = dashBoardService.getPeriodByMonth( tempMonth, tempYear, monthlyPeriodType );
 
357
                if ( p == null )
 
358
                {
 
359
                    serviceValues[count1][count2] = 0.0;
 
360
                    System.out.println("PERIOD IS NULL for "+tempMonth+" : "+tempYear);
 
361
                }
 
362
                else
 
363
                {
 
364
                    if ( riRadio.equals( "indicatorsRadio" ) )
 
365
                    {
 
366
                        serviceValues[count1][count2] = aggregationService.getAggregatedIndicatorValue(
 
367
                            selectedIndicator, p.getStartDate(), p.getEndDate(), selectedOrgUnit );
 
368
                        System.out.println("indicators Radio is Selected");
 
369
                    }
 
370
                    else
 
371
                    {
 
372
                        double aggDataValue = 0.0;
 
373
                        serviceValues[count1][count2] = 0.0;
 
374
                        DataElementCategoryCombo dataElementCategoryCombo = selectedDataElement.getCategoryCombo();
 
375
 
 
376
                        List<DataElementCategoryOptionCombo> optionCombos = new ArrayList<DataElementCategoryOptionCombo>(
 
377
                            dataElementCategoryCombo.getOptionCombos() );
 
378
 
 
379
                        Iterator<DataElementCategoryOptionCombo> optionComboIterator = optionCombos.iterator();
 
380
                        while ( optionComboIterator.hasNext() )
 
381
                        {                        
 
382
                            DataElementCategoryOptionCombo decoc = (DataElementCategoryOptionCombo) optionComboIterator.next();
 
383
 
 
384
                            aggDataValue = aggregationService.getAggregatedDataValue( selectedDataElement, decoc, p.getStartDate(), p.getEndDate(), selectedOrgUnit );
 
385
                            if(aggDataValue == -1) aggDataValue = 0.0;
 
386
                            serviceValues[count1][count2] += aggDataValue;
 
387
                        } 
 
388
                        System.out.println("VALUE : "+serviceValues[count1][count2]);
 
389
                    }
 
390
                    serviceValues[count1][count2] = Math.round( serviceValues[count1][count2] * Math.pow( 10, 1 ) )
 
391
                        / Math.pow( 10, 1 );
 
392
                }
 
393
                if ( serviceValues[count1][count2] == -1 )
 
394
                {
 
395
                    serviceValues[count1][count2] = 0.0;
 
396
                }
 
397
                categories1[count2] = monthNames[tempMonth];
 
398
                categories2[count2] = monthNames[tempMonth];
 
399
                data2[count1][count2] = 0.0;
 
400
                dataValues.add( serviceValues[count1][count2] );
 
401
                count2++;
 
402
            }// Monthly PeriodList loop end
 
403
            dataList.add( dataValues );
 
404
            count1++;
 
405
        } // Annual PeriodList loop end
 
406
        return serviceValues;
 
407
    }// getServiceValues method end
 
408
 
 
409
    public Period getPeriodByMonth( int month, int year, PeriodType periodType )
 
410
    {
 
411
        int monthDays[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
 
412
 
 
413
        Calendar cal = Calendar.getInstance();
 
414
        cal.set( year, month, 1, 0, 0, 0 );
 
415
        Date firstDay = new Date( cal.getTimeInMillis() );
 
416
 
 
417
        if ( periodType.getName().equalsIgnoreCase( "Monthly" ) )
 
418
        {
 
419
            cal.set( year, month, 1, 0, 0, 0 );
 
420
            if ( year % 4 == 0 && month == 1)
 
421
            {
 
422
                cal.set( Calendar.DAY_OF_MONTH, monthDays[month] + 1 );
 
423
            }
 
424
            else
 
425
            {
 
426
                cal.set( Calendar.DAY_OF_MONTH, monthDays[month] );
 
427
            }
 
428
        }
 
429
        else if ( periodType.getName().equalsIgnoreCase( "Yearly" ) )
 
430
        {
 
431
            cal.set( year, Calendar.DECEMBER, 31 );
 
432
        }
 
433
 
 
434
        Date lastDay = new Date( cal.getTimeInMillis() );
 
435
        System.out.println( lastDay.toString() );
 
436
 
 
437
        Period newPeriod = periodStore.getPeriod( firstDay, lastDay, periodType );
 
438
 
 
439
        return newPeriod;
 
440
    }
 
441
 
 
442
    /*
 
443
     * Returns the PeriodType Object based on the Period Type Name For ex:- if
 
444
     * we pass name as Monthly then it returns the PeriodType Object for Monthly
 
445
     * PeriodType If there is no such PeriodType returns null
 
446
     */
 
447
    public PeriodType getPeriodTypeObject( String periodTypeName )
 
448
    {
 
449
        Collection<PeriodType> periodTypes = periodStore.getAllPeriodTypes();
 
450
        PeriodType periodType = null;
 
451
        Iterator<PeriodType> iter = periodTypes.iterator();
 
452
        while ( iter.hasNext() )
 
453
        {
 
454
            PeriodType tempPeriodType = (PeriodType) iter.next();
 
455
            if ( tempPeriodType.getName().trim().equalsIgnoreCase( periodTypeName ) )
 
456
            {
 
457
                periodType = tempPeriodType;
 
458
                break;
 
459
            }
 
460
        }
 
461
        if ( periodType == null )
 
462
        {            
 
463
            return null;
 
464
        }
 
465
        
 
466
        return periodType;
 
467
    }
 
468
 
 
469
}// class end