1
package org.hisp.dhis.dashboard.aa.action;
4
* Copyright (c) 2004-2007, University of Oslo
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.
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.
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;
37
import javax.servlet.http.HttpServletRequest;
38
import javax.servlet.http.HttpSession;
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;
54
import com.opensymphony.webwork.ServletActionContext;
55
import com.opensymphony.xwork.Action;
56
import com.opensymphony.xwork.ActionContext;
58
public class GenerateAnnualAnalysisDataAction
63
private OrganisationUnitService organisationUnitService;
65
public void setOrganisationUnitService( OrganisationUnitService organisationUnitService )
67
this.organisationUnitService = organisationUnitService;
70
private PeriodStore periodStore;
72
public void setPeriodStore( PeriodStore periodStore )
74
this.periodStore = periodStore;
77
private IndicatorService indicatorService;
79
public void setIndicatorService( IndicatorService indicatorService )
81
this.indicatorService = indicatorService;
84
private DataElementService dataElementService;
86
public void setDataElementService( DataElementService dataElementService )
88
this.dataElementService = dataElementService;
91
private DashBoardService dashBoardService;
93
public void setDashBoardService( DashBoardService dashBoardService )
95
this.dashBoardService = dashBoardService;
98
private AggregationService aggregationService;
100
public void setAggregationService( AggregationService aggregationService )
102
this.aggregationService = aggregationService;
106
private OrganisationUnit selectedOrgUnit;
108
private DataElement selectedDataElement;
110
private Indicator selectedIndicator;
112
private PeriodType monthlyPeriodType;
114
private String[] series1;
116
public String[] getSeries1()
121
private String[] categories1;
123
public String[] getCategories1()
128
private String[] series2;
130
public String[] getSeries2()
135
private String[] categories2;
137
public String[] getCategories2()
142
String chartTitle = "Service : ";
144
public String getChartTitle()
151
public String getXAxis_Title()
158
public String getYAxis_Title()
163
List<String> numeratorDEList;
165
public List<String> getNumeratorDEList()
167
return numeratorDEList;
170
List<String> denominatorDEList;
172
public List<String> getDenominatorDEList()
174
return denominatorDEList;
179
public Double[][] getData1()
186
public Double[][] getData2()
191
List<List<Double>> dataList;
193
public List<List<Double>> getDataList()
198
List<String> xseriesList;
200
public List<String> getXseriesList()
205
List<String> yseriesList;
207
public List<String> getYseriesList()
212
/* Input Parameters */
213
private int availableDataElements;
215
private int availableIndicators;
217
private List<String> annualPeriodsListCB;
219
private List<String> monthlyPeriodsListCB;
223
private String riRadio;
225
public String getRiRadio()
230
public void setRiRadio( String riRadio )
232
this.riRadio = riRadio;
235
public void setAnnualPeriodsListCB( List<String> annualPeriodsListCB )
237
this.annualPeriodsListCB = annualPeriodsListCB;
240
public void setAvailableDataElements( int availableDataElements )
242
this.availableDataElements = availableDataElements;
245
public void setAvailableIndicators( int availableIndicators )
247
this.availableIndicators = availableIndicators;
250
public void setMonthlyPeriodsListCB( List<String> monthlyPeriodsListCB )
252
this.monthlyPeriodsListCB = monthlyPeriodsListCB;
255
public void setOuIDTB( int ouIDTB )
257
this.ouIDTB = ouIDTB;
260
public String execute()
263
dataList = new ArrayList<List<Double>>();
264
xseriesList = new ArrayList<String>();
265
yseriesList = new ArrayList<String>();
267
// OrgUnit Related Info
268
selectedOrgUnit = new OrganisationUnit();
269
selectedOrgUnit = organisationUnitService.getOrganisationUnit( ouIDTB );
270
chartTitle = "Facility : " + selectedOrgUnit.getShortName();
272
// Service Related Info
273
if ( riRadio.equals( "indicatorsRadio" ) )
275
selectedIndicator = new Indicator();
276
selectedIndicator = indicatorService.getIndicator( availableIndicators );
277
chartTitle += "\n Indicator : " + selectedIndicator.getName();
282
selectedDataElement = new DataElement();
283
selectedDataElement = dataElementService.getDataElement( availableDataElements );
284
if ( selectedDataElement.getAlternativeName() != null )
285
chartTitle += "\n DataElement : " + selectedDataElement.getAlternativeName();
287
chartTitle += "\n DataElement : " + selectedDataElement.getName();
290
// Period Related Info
291
monthlyPeriodType = dashBoardService.getPeriodTypeObject( "monthly" );
293
data1 = getServiceValuesByPeriod();
294
xAxis_Title = "Month";
295
yAxis_Title = "Facilty";
298
while ( count1 != categories1.length )
300
xseriesList.add( categories1[count1] );
304
ActionContext ctx = ActionContext.getContext();
305
HttpServletRequest req = (HttpServletRequest) ctx.get( ServletActionContext.HTTP_REQUEST );
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 );
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
327
public Double[][] getServiceValuesByPeriod()
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" };
336
Period p = new Period();
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() )
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 );
351
Iterator iterator2 = monthlyPeriodsListCB.iterator();
353
while ( iterator2.hasNext() )
355
int tempMonth = Integer.parseInt( (String) iterator2.next() );
356
p = dashBoardService.getPeriodByMonth( tempMonth, tempYear, monthlyPeriodType );
359
serviceValues[count1][count2] = 0.0;
360
System.out.println("PERIOD IS NULL for "+tempMonth+" : "+tempYear);
364
if ( riRadio.equals( "indicatorsRadio" ) )
366
serviceValues[count1][count2] = aggregationService.getAggregatedIndicatorValue(
367
selectedIndicator, p.getStartDate(), p.getEndDate(), selectedOrgUnit );
368
System.out.println("indicators Radio is Selected");
372
double aggDataValue = 0.0;
373
serviceValues[count1][count2] = 0.0;
374
DataElementCategoryCombo dataElementCategoryCombo = selectedDataElement.getCategoryCombo();
376
List<DataElementCategoryOptionCombo> optionCombos = new ArrayList<DataElementCategoryOptionCombo>(
377
dataElementCategoryCombo.getOptionCombos() );
379
Iterator<DataElementCategoryOptionCombo> optionComboIterator = optionCombos.iterator();
380
while ( optionComboIterator.hasNext() )
382
DataElementCategoryOptionCombo decoc = (DataElementCategoryOptionCombo) optionComboIterator.next();
384
aggDataValue = aggregationService.getAggregatedDataValue( selectedDataElement, decoc, p.getStartDate(), p.getEndDate(), selectedOrgUnit );
385
if(aggDataValue == -1) aggDataValue = 0.0;
386
serviceValues[count1][count2] += aggDataValue;
388
System.out.println("VALUE : "+serviceValues[count1][count2]);
390
serviceValues[count1][count2] = Math.round( serviceValues[count1][count2] * Math.pow( 10, 1 ) )
393
if ( serviceValues[count1][count2] == -1 )
395
serviceValues[count1][count2] = 0.0;
397
categories1[count2] = monthNames[tempMonth];
398
categories2[count2] = monthNames[tempMonth];
399
data2[count1][count2] = 0.0;
400
dataValues.add( serviceValues[count1][count2] );
402
}// Monthly PeriodList loop end
403
dataList.add( dataValues );
405
} // Annual PeriodList loop end
406
return serviceValues;
407
}// getServiceValues method end
409
public Period getPeriodByMonth( int month, int year, PeriodType periodType )
411
int monthDays[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
413
Calendar cal = Calendar.getInstance();
414
cal.set( year, month, 1, 0, 0, 0 );
415
Date firstDay = new Date( cal.getTimeInMillis() );
417
if ( periodType.getName().equalsIgnoreCase( "Monthly" ) )
419
cal.set( year, month, 1, 0, 0, 0 );
420
if ( year % 4 == 0 && month == 1)
422
cal.set( Calendar.DAY_OF_MONTH, monthDays[month] + 1 );
426
cal.set( Calendar.DAY_OF_MONTH, monthDays[month] );
429
else if ( periodType.getName().equalsIgnoreCase( "Yearly" ) )
431
cal.set( year, Calendar.DECEMBER, 31 );
434
Date lastDay = new Date( cal.getTimeInMillis() );
435
System.out.println( lastDay.toString() );
437
Period newPeriod = periodStore.getPeriod( firstDay, lastDay, periodType );
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
447
public PeriodType getPeriodTypeObject( String periodTypeName )
449
Collection<PeriodType> periodTypes = periodStore.getAllPeriodTypes();
450
PeriodType periodType = null;
451
Iterator<PeriodType> iter = periodTypes.iterator();
452
while ( iter.hasNext() )
454
PeriodType tempPeriodType = (PeriodType) iter.next();
455
if ( tempPeriodType.getName().trim().equalsIgnoreCase( periodTypeName ) )
457
periodType = tempPeriodType;
461
if ( periodType == null )