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

« back to all changes in this revision

Viewing changes to dhis-2/dhis-web/dhis-web-dataentry/src/main/java/org/hisp/dhis/de/action/FormAction.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.de.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 static org.hisp.dhis.options.SystemSettingManager.KEY_ZERO_VALUE_SAVE_MODE;
 
31
 
 
32
import java.util.ArrayList;
 
33
import java.util.Collection;
 
34
import java.util.HashMap;
 
35
import java.util.List;
 
36
import java.util.Map;
 
37
 
 
38
import org.hisp.dhis.dataelement.CalculatedDataElement;
 
39
import org.hisp.dhis.dataelement.DataElement;
 
40
import org.hisp.dhis.dataelement.DataElementCategoryOptionCombo;
 
41
import org.hisp.dhis.dataset.DataEntryForm;
 
42
import org.hisp.dhis.dataset.DataEntryFormService;
 
43
import org.hisp.dhis.dataset.DataSet;
 
44
import org.hisp.dhis.datavalue.DataValue;
 
45
import org.hisp.dhis.datavalue.DataValueService;
 
46
import org.hisp.dhis.de.comments.StandardCommentsManager;
 
47
import org.hisp.dhis.de.screen.DataEntryScreenManager;
 
48
import org.hisp.dhis.de.state.SelectedStateManager;
 
49
import org.hisp.dhis.i18n.I18n;
 
50
import org.hisp.dhis.minmax.MinMaxDataElement;
 
51
import org.hisp.dhis.minmax.MinMaxDataElementStore;
 
52
import org.hisp.dhis.options.SystemSettingManager;
 
53
import org.hisp.dhis.options.displayproperty.DisplayPropertyHandler;
 
54
import org.hisp.dhis.order.manager.DataElementOrderManager;
 
55
import org.hisp.dhis.organisationunit.OrganisationUnit;
 
56
import org.hisp.dhis.period.Period;
 
57
 
 
58
import com.opensymphony.xwork.Action;
 
59
 
 
60
/**
 
61
 * @author Torgeir Lorange Ostby
 
62
 * @version $Id: FormAction.java 6216 2008-11-06 18:06:42Z eivindwa $
 
63
 */
 
64
public class FormAction
 
65
    implements Action
 
66
{
 
67
    // -------------------------------------------------------------------------
 
68
    // Dependencies
 
69
    // -------------------------------------------------------------------------
 
70
        
 
71
    private SystemSettingManager systemSettingManager;
 
72
 
 
73
    public void setSystemSettingManager( SystemSettingManager systemSettingManager )
 
74
    {
 
75
        this.systemSettingManager = systemSettingManager;
 
76
    }
 
77
 
 
78
    private DataElementOrderManager dataElementOrderManager;
 
79
 
 
80
    public void setDataElementOrderManager( DataElementOrderManager dataElementOrderManager )
 
81
    {
 
82
        this.dataElementOrderManager = dataElementOrderManager;
 
83
    }
 
84
 
 
85
    private DataValueService dataValueService;
 
86
 
 
87
    public void setDataValueService( DataValueService dataValueService )
 
88
    {
 
89
        this.dataValueService = dataValueService;
 
90
    }      
 
91
    
 
92
    private DataEntryFormService dataEntryFormService;
 
93
 
 
94
    public void setDataEntryFormService( DataEntryFormService dataEntryFormService )
 
95
    {
 
96
        this.dataEntryFormService = dataEntryFormService;
 
97
    }
 
98
    
 
99
    private StandardCommentsManager standardCommentsManager;
 
100
 
 
101
    public void setStandardCommentsManager( StandardCommentsManager standardCommentsManager )
 
102
    {
 
103
        this.standardCommentsManager = standardCommentsManager;
 
104
    }
 
105
 
 
106
    private MinMaxDataElementStore minMaxDataElementStore;
 
107
 
 
108
    public void setMinMaxDataElementStore( MinMaxDataElementStore minMaxDataElementStore )
 
109
    {
 
110
        this.minMaxDataElementStore = minMaxDataElementStore;
 
111
    }
 
112
 
 
113
    private SelectedStateManager selectedStateManager;
 
114
 
 
115
    public void setSelectedStateManager( SelectedStateManager selectedStateManager )
 
116
    {
 
117
        this.selectedStateManager = selectedStateManager;
 
118
    }
 
119
    
 
120
    private DataEntryScreenManager dataEntryScreenManager;
 
121
 
 
122
    public void setDataEntryScreenManager( DataEntryScreenManager dataEntryScreenManager )
 
123
    {
 
124
        this.dataEntryScreenManager = dataEntryScreenManager;
 
125
    }
 
126
 
 
127
    private I18n i18n;
 
128
 
 
129
    public void setI18n( I18n i18n )
 
130
    {
 
131
        this.i18n = i18n;
 
132
    } 
 
133
    
 
134
    // -------------------------------------------------------------------------
 
135
    // DisplayPropertyHandler
 
136
    // -------------------------------------------------------------------------
 
137
 
 
138
    private DisplayPropertyHandler displayPropertyHandler;
 
139
 
 
140
    public void setDisplayPropertyHandler( DisplayPropertyHandler displayPropertyHandler )
 
141
    {
 
142
        this.displayPropertyHandler = displayPropertyHandler;
 
143
    }
 
144
 
 
145
    // -------------------------------------------------------------------------
 
146
    // Output
 
147
    // -------------------------------------------------------------------------
 
148
    
 
149
    private List<DataElement> orderedDataElements = new ArrayList<DataElement>();
 
150
 
 
151
    public List<DataElement> getOrderedDataElements()
 
152
    {
 
153
        return orderedDataElements;
 
154
    }
 
155
 
 
156
    private Map<Integer, DataValue> dataValueMap;
 
157
 
 
158
    public Map<Integer, DataValue> getDataValueMap()
 
159
    {
 
160
        return dataValueMap;
 
161
    }
 
162
 
 
163
    private Map<CalculatedDataElement,Integer> calculatedValueMap;
 
164
    
 
165
    public Map<CalculatedDataElement,Integer> getCalculatedValueMap()
 
166
    {
 
167
        return calculatedValueMap;
 
168
    }
 
169
    
 
170
    private List<String> standardComments;
 
171
 
 
172
    public List<String> getStandardComments()
 
173
    {
 
174
        return standardComments;
 
175
    }
 
176
 
 
177
    private Map<String, String> dataElementTypeMap;
 
178
 
 
179
    public Map<String, String> getDataElementTypeMap()
 
180
    {
 
181
        return dataElementTypeMap;
 
182
    }
 
183
 
 
184
    private Map<Integer, MinMaxDataElement> minMaxMap;
 
185
 
 
186
    public Map<Integer, MinMaxDataElement> getMinMaxMap()
 
187
    {
 
188
        return minMaxMap;
 
189
    }
 
190
 
 
191
    private Integer integer = 0;
 
192
 
 
193
    public Integer getInteger()
 
194
    {
 
195
        return integer;
 
196
    }
 
197
    
 
198
    private Boolean cdeFormExists;
 
199
    
 
200
    private DataEntryForm dataEntryForm;
 
201
 
 
202
    public DataEntryForm getDataEntryForm()
 
203
    {
 
204
        return this.dataEntryForm;
 
205
    }
 
206
 
 
207
    private String customDataEntryFormCode;
 
208
 
 
209
    public String getCustomDataEntryFormCode()
 
210
    {
 
211
        return this.customDataEntryFormCode;
 
212
    }
 
213
    
 
214
    private Boolean zeroValueSaveMode;
 
215
 
 
216
    public Boolean getZeroValueSaveMode()
 
217
    {
 
218
        return zeroValueSaveMode;
 
219
    }
 
220
    
 
221
    // -------------------------------------------------------------------------
 
222
    // Input/output
 
223
    // -------------------------------------------------------------------------
 
224
 
 
225
    private Integer selectedDataSetId;
 
226
 
 
227
    public void setSelectedDataSetId( Integer selectedDataSetId )
 
228
    {
 
229
        this.selectedDataSetId = selectedDataSetId;
 
230
    }
 
231
 
 
232
    public Integer getSelectedDataSetId()
 
233
    {
 
234
        return selectedDataSetId;
 
235
    }
 
236
 
 
237
    private Integer selectedPeriodIndex;
 
238
 
 
239
    public void setSelectedPeriodIndex( Integer selectedPeriodIndex )
 
240
    {
 
241
        this.selectedPeriodIndex = selectedPeriodIndex;
 
242
    }
 
243
 
 
244
    public Integer getSelectedPeriodIndex()
 
245
    {
 
246
        return selectedPeriodIndex;
 
247
    }
 
248
    
 
249
    private String disabled = " ";
 
250
    
 
251
    // -------------------------------------------------------------------------
 
252
    // Action implementation
 
253
    // -------------------------------------------------------------------------
 
254
 
 
255
    public String execute()
 
256
        throws Exception
 
257
    {           
 
258
        zeroValueSaveMode = (Boolean) systemSettingManager.getSystemSetting( KEY_ZERO_VALUE_SAVE_MODE, false );         
 
259
        
 
260
        OrganisationUnit organisationUnit = selectedStateManager.getSelectedOrganisationUnit();
 
261
 
 
262
        DataSet dataSet = selectedStateManager.getSelectedDataSet();
 
263
 
 
264
        Period period = selectedStateManager.getSelectedPeriod();   
 
265
 
 
266
        if ( dataSet.getLockedPeriods().contains( period ) )
 
267
        {
 
268
            disabled = "disabled";                        
 
269
        }
 
270
 
 
271
        Collection<DataElement> dataElements = dataSet.getDataElements();        
 
272
 
 
273
        if ( dataElements.size() == 0 )
 
274
        {
 
275
            return SUCCESS;
 
276
        }  
 
277
        
 
278
        Collection<DataElementCategoryOptionCombo> defaultOptionCombo = dataElements.iterator().next().getCategoryCombo().getOptionCombos();        
 
279
        
 
280
        // ---------------------------------------------------------------------
 
281
        // Get the min/max values
 
282
        // ---------------------------------------------------------------------
 
283
 
 
284
        Collection<MinMaxDataElement> minMaxDataElements = minMaxDataElementStore.getMinMaxDataElements(
 
285
            organisationUnit, dataElements );
 
286
        
 
287
        minMaxMap = new HashMap<Integer, MinMaxDataElement>( minMaxDataElements.size() );
 
288
 
 
289
        for ( MinMaxDataElement minMaxDataElement : minMaxDataElements )
 
290
        {
 
291
            minMaxMap.put( minMaxDataElement.getDataElement().getId(), minMaxDataElement );
 
292
        }      
 
293
 
 
294
        // ---------------------------------------------------------------------
 
295
        // Get the DataValues and create a map
 
296
        // ---------------------------------------------------------------------
 
297
 
 
298
        Collection<DataValue> dataValues = dataValueService.getDataValues( organisationUnit, period, dataElements, defaultOptionCombo );
 
299
        
 
300
        dataValueMap = new HashMap<Integer, DataValue>( dataValues.size() );
 
301
 
 
302
        for ( DataValue dataValue : dataValues )
 
303
        {
 
304
            dataValueMap.put( dataValue.getDataElement().getId(), dataValue );
 
305
        }
 
306
 
 
307
        // ---------------------------------------------------------------------
 
308
        // Prepare values for unsaved CalculatedDataElements
 
309
        // ---------------------------------------------------------------------
 
310
        
 
311
        calculatedValueMap = dataEntryScreenManager.populateValuesForCalculatedDataElements( organisationUnit, dataSet, period );
 
312
        
 
313
        // ---------------------------------------------------------------------
 
314
        // Make the standard comments available
 
315
        // ---------------------------------------------------------------------
 
316
 
 
317
        standardComments = standardCommentsManager.getStandardComments();
 
318
 
 
319
        // ---------------------------------------------------------------------
 
320
        // Make the DataElement types available
 
321
        // ---------------------------------------------------------------------
 
322
 
 
323
        dataElementTypeMap = new HashMap<String, String>();
 
324
        dataElementTypeMap.put( DataElement.TYPE_BOOL, i18n.getString( "yes_no" ) );
 
325
        dataElementTypeMap.put( DataElement.TYPE_INT, i18n.getString( "number" ) );
 
326
        dataElementTypeMap.put( DataElement.TYPE_STRING, i18n.getString( "text" ) );
 
327
 
 
328
        // ---------------------------------------------------------------------
 
329
        // Get the custom data entry form (if any)
 
330
        // ---------------------------------------------------------------------
 
331
        
 
332
        dataEntryForm = dataEntryFormService.getDataEntryFormByDataSet( dataSet );
 
333
        cdeFormExists = ( dataEntryForm != null );
 
334
        
 
335
        if ( cdeFormExists )
 
336
        {               
 
337
                customDataEntryFormCode = dataEntryScreenManager.populateCustomDataEntryScreen( dataEntryForm.getHtmlCode(), dataValues, calculatedValueMap, minMaxMap, disabled, i18n );
 
338
        }            
 
339
 
 
340
        // ---------------------------------------------------------------------
 
341
        // Working on the display of dataelements
 
342
        // ---------------------------------------------------------------------
 
343
 
 
344
        orderedDataElements = dataElementOrderManager.getOrderedDataElements( dataSet );
 
345
 
 
346
        displayPropertyHandler.handle( orderedDataElements );
 
347
        
 
348
        return SUCCESS;
 
349
    }
 
350
 
 
351
}