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

« back to all changes in this revision

Viewing changes to dhis-2/dhis-web/dhis-web-reporting/src/main/java/org/hisp/dhis/reporting/dataset/action/GenerateCustomDataSetReportAction.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.reporting.dataset.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.Collection;
 
31
import java.util.Map;
 
32
import java.util.TreeMap;
 
33
 
 
34
import org.hisp.dhis.dataelement.DataElement;
 
35
import org.hisp.dhis.dataelement.DataElementCategoryCombo;
 
36
import org.hisp.dhis.dataelement.DataElementCategoryOptionCombo;
 
37
import org.hisp.dhis.datamart.DataMartStore;
 
38
import org.hisp.dhis.dataset.DataEntryForm;
 
39
import org.hisp.dhis.dataset.DataEntryFormService;
 
40
import org.hisp.dhis.dataset.DataSet;
 
41
import org.hisp.dhis.datavalue.DataValue;
 
42
import org.hisp.dhis.datavalue.DataValueService;
 
43
import org.hisp.dhis.order.manager.DataElementOrderManager;
 
44
import org.hisp.dhis.organisationunit.OrganisationUnit;
 
45
import org.hisp.dhis.period.Period;
 
46
import org.hisp.dhis.reporting.dataset.generators.CustomDataSetReportGenerator;
 
47
import org.hisp.dhis.reporting.dataset.state.SelectedStateManager;
 
48
import org.hisp.dhis.reporting.dataset.utils.NumberUtils;
 
49
 
 
50
/**
 
51
 * @author Abyot Asalefew Gizaw
 
52
 * @version $Id$
 
53
 */
 
54
public class GenerateCustomDataSetReportAction
 
55
    extends AbstractAction
 
56
{
 
57
    // -----------------------------------------------------------------------
 
58
    // Dependencies
 
59
    // -----------------------------------------------------------------------    
 
60
        
 
61
    private DataElementOrderManager dataElementOrderManager;
 
62
 
 
63
    public void setDataElementOrderManager( DataElementOrderManager dataElementOrderManager )
 
64
    {
 
65
        this.dataElementOrderManager = dataElementOrderManager;
 
66
    }   
 
67
    
 
68
    private DataEntryFormService dataEntryFormService;
 
69
 
 
70
    public void setDataEntryFormService( DataEntryFormService dataEntryFormService )
 
71
    {
 
72
        this.dataEntryFormService = dataEntryFormService;
 
73
    }    
 
74
    
 
75
    private SelectedStateManager selectedStateManager;
 
76
 
 
77
    public void setSelectedStateManager( SelectedStateManager selectedStateManager )
 
78
    {
 
79
        this.selectedStateManager = selectedStateManager;
 
80
    }
 
81
    
 
82
    private DataMartStore dataMartStore;
 
83
    
 
84
    public void setDataMartStore( DataMartStore dataMartStore )
 
85
    {
 
86
        this.dataMartStore = dataMartStore;
 
87
    }
 
88
    
 
89
    private DataValueService dataValueService;
 
90
    
 
91
    public void setDataValueService( DataValueService dataValueService)
 
92
    {
 
93
        this.dataValueService = dataValueService;
 
94
    }   
 
95
    
 
96
    // -------------------------------------------------------------------------
 
97
    // Parameters
 
98
    // -------------------------------------------------------------------------      
 
99
    
 
100
    private String customDataEntryFormCode;
 
101
 
 
102
    public String getCustomDataEntryFormCode()
 
103
    {
 
104
        return this.customDataEntryFormCode;
 
105
    }
 
106
    
 
107
    private String reportingUnit;
 
108
    
 
109
    public String getReportingUnit()
 
110
    {
 
111
        return this.reportingUnit;
 
112
    }
 
113
    
 
114
    private String reportingPeriod;
 
115
    
 
116
    public String getReportingPeriod()
 
117
    {
 
118
        return this.reportingPeriod;
 
119
    }  
 
120
   
 
121
    private String selectedUnitOnly;
 
122
    
 
123
    public void setSelectedUnitOnly( String selectedUnitOnly )
 
124
    {
 
125
        this.selectedUnitOnly = selectedUnitOnly;
 
126
    }
 
127
    
 
128
    public String getSelectedUnitOnly()
 
129
    {
 
130
        return selectedUnitOnly;
 
131
    }
 
132
    
 
133
    // -----------------------------------------------------------------------
 
134
    // Action implementation
 
135
    // -----------------------------------------------------------------------
 
136
    
 
137
    public String execute()
 
138
        throws Exception
 
139
    {        
 
140
        OrganisationUnit orgUnit = selectedStateManager.getSelectedOrganisationUnit();    
 
141
        
 
142
        DataSet dataSet = selectedStateManager.getSelectedDataSet();
 
143
 
 
144
        Period period = selectedStateManager.getSelectedPeriod();       
 
145
        
 
146
        if ( orgUnit != null && dataSet != null && period != null )
 
147
        {
 
148
            Collection<DataElement> dataElements = dataElementOrderManager.getOrderedDataElements( dataSet );
 
149
            
 
150
            //CollectionUtils.filter( dataElements, new AggregateableDataElementPredicate() );
 
151
            
 
152
            Map<String, String> aggregatedDataValueMap = new TreeMap<String,String>();            
 
153
            
 
154
            for ( DataElement dataElement : dataElements )
 
155
            {
 
156
                DataElementCategoryCombo catCombo = dataElement.getCategoryCombo();                                        
 
157
                
 
158
                for ( DataElementCategoryOptionCombo optionCombo : catCombo.getOptionCombos() )
 
159
                {
 
160
                    String value;  
 
161
                    DataValue dataValue;
 
162
                    
 
163
                    if ( dataElement.getType().equals( DataElement.TYPE_INT ) )
 
164
                    {
 
165
                        double aggregatedValue;
 
166
                        
 
167
                        if ( selectedUnitOnly != null )
 
168
                        {                                       
 
169
                            dataValue = dataValueService.getDataValue(orgUnit, dataElement, period, optionCombo);                                       
 
170
                            value = ( dataValue != null ) ? dataValue.getValue() : "";
 
171
                        }
 
172
                        else
 
173
                        {                                       
 
174
                            aggregatedValue = dataMartStore.getAggregatedValue( dataElement, optionCombo, period, orgUnit );                                    
 
175
                            value = ( aggregatedValue != DataMartStore.NO_VALUES_REGISTERED ) ? NumberUtils.formatDataValue( aggregatedValue ) : "";                                                                    
 
176
                        }                 
 
177
                                 
 
178
                        aggregatedDataValueMap.put(dataElement.getId() + ":" + optionCombo.getId(), value );
 
179
                    }
 
180
                    else
 
181
                    {                           
 
182
                        if ( selectedUnitOnly != null )
 
183
                        {                                       
 
184
                            dataValue = dataValueService.getDataValue(orgUnit, dataElement, period, optionCombo);                                       
 
185
                            value = ( dataValue != null ) ? dataValue.getValue() : "";                              
 
186
                        }
 
187
                        else
 
188
                        {
 
189
                                value = " ";
 
190
                        } 
 
191
                        
 
192
                        aggregatedDataValueMap.put(dataElement.getId() + ":" + optionCombo.getId(), value );
 
193
                    }
 
194
                }
 
195
            }           
 
196
            
 
197
            // -----------------------------------------------------------------
 
198
            // Get the custom data entry form if any
 
199
            // -----------------------------------------------------------------
 
200
 
 
201
            DataEntryForm dataEntryForm = dataEntryFormService.getDataEntryFormByDataSet( dataSet );
 
202
            
 
203
            customDataEntryFormCode = CustomDataSetReportGenerator.prepareReportContent( dataEntryForm.getHtmlCode(), aggregatedDataValueMap );
 
204
            
 
205
            reportingUnit = orgUnit.getName();
 
206
            
 
207
            reportingPeriod = format.formatPeriod( period );
 
208
           
 
209
            return SUCCESS;             
 
210
        }
 
211
        
 
212
        return ERROR;
 
213
    }
 
214
}