~dhis2-devs-core/dhis2/dhis2-patient-tz

« back to all changes in this revision

Viewing changes to local/tz/dhis-web-maintenance-patient-tz/src/main/java/org/hisp/dhis/patient/action/patient/RemoveEnrollmentAction.java

  • Committer: John Francis Mukulu
  • Date: 2011-08-09 06:36:18 UTC
  • mfrom: (4244.1.21 dhis2)
  • Revision ID: john.f.mukulu@gmail.com-20110809063618-wad1pcc9fd1mnc6k
[merge]

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * Copyright (c) 2004-2009, University of Oslo
3
 
 * All rights reserved.
4
 
 *
5
 
 * Redistribution and use in source and binary forms, with or without
6
 
 * modification, are permitted provided that the following conditions are met:
7
 
 * * Redistributions of source code must retain the above copyright notice, this
8
 
 *   list of conditions and the following disclaimer.
9
 
 * * Redistributions in binary form must reproduce the above copyright notice,
10
 
 *   this list of conditions and the following disclaimer in the documentation
11
 
 *   and/or other materials provided with the distribution.
12
 
 * * Neither the name of the HISP project nor the names of its contributors may
13
 
 *   be used to endorse or promote products derived from this software without
14
 
 *   specific prior written permission.
15
 
 *
16
 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17
 
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18
 
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19
 
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
20
 
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21
 
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22
 
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
23
 
 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24
 
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25
 
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26
 
 */
27
 
package org.hisp.dhis.patient.action.patient;
28
 
 
29
 
import java.util.ArrayList;
30
 
import java.util.Collection;
31
 
import java.util.Date;
32
 
import java.util.HashSet;
33
 
import java.util.Set;
34
 
 
35
 
import javax.servlet.http.HttpServletRequest;
36
 
 
37
 
import org.apache.commons.lang.StringUtils;
38
 
import org.apache.commons.lang.math.NumberUtils;
39
 
import org.apache.struts2.ServletActionContext;
40
 
import org.hisp.dhis.i18n.I18nFormat;
41
 
import org.hisp.dhis.patient.Patient;
42
 
import org.hisp.dhis.patient.PatientService;
43
 
import org.hisp.dhis.program.Program;
44
 
import org.hisp.dhis.program.ProgramAttribute;
45
 
import org.hisp.dhis.program.ProgramAttributeOption;
46
 
import org.hisp.dhis.program.ProgramAttributeOptionService;
47
 
import org.hisp.dhis.program.ProgramAttributeService;
48
 
import org.hisp.dhis.program.ProgramInstance;
49
 
import org.hisp.dhis.program.ProgramInstanceService;
50
 
import org.hisp.dhis.programattributevalue.ProgramAttributeValue;
51
 
import org.hisp.dhis.programattributevalue.ProgramAttributeValueService;
52
 
 
53
 
import com.opensymphony.xwork2.Action;
54
 
 
55
 
/**
56
 
 * @author Abyot Asalefew Gizaw
57
 
 * @version $Id$
58
 
 */
59
 
public class RemoveEnrollmentAction
60
 
    implements Action
61
 
{
62
 
    public static final String PREFIX_ATTRIBUTE = "attr";
63
 
 
64
 
    // -------------------------------------------------------------------------
65
 
    // Dependencies
66
 
    // -------------------------------------------------------------------------
67
 
 
68
 
    private PatientService patientService;
69
 
 
70
 
    private ProgramInstanceService programInstanceService;
71
 
 
72
 
    private ProgramAttributeService programAttributeService;
73
 
 
74
 
    private ProgramAttributeOptionService programAttributeOptionService;
75
 
 
76
 
    private ProgramAttributeValueService programAttributeValueService;
77
 
 
78
 
    private I18nFormat format;
79
 
 
80
 
    // -------------------------------------------------------------------------
81
 
    // Input/Output
82
 
    // -------------------------------------------------------------------------
83
 
 
84
 
    private Integer programInstanceId;
85
 
 
86
 
    private Collection<Program> programs = new ArrayList<Program>();
87
 
 
88
 
    // -------------------------------------------------------------------------
89
 
    // Getters && Setters
90
 
    // -------------------------------------------------------------------------
91
 
 
92
 
    public void setPatientService( PatientService patientService )
93
 
    {
94
 
        this.patientService = patientService;
95
 
    }
96
 
 
97
 
    public void setProgramInstanceService( ProgramInstanceService programInstanceService )
98
 
    {
99
 
        this.programInstanceService = programInstanceService;
100
 
    }
101
 
 
102
 
    public void setProgramAttributeService( ProgramAttributeService programAttributeService )
103
 
    {
104
 
        this.programAttributeService = programAttributeService;
105
 
    }
106
 
 
107
 
    public void setProgramAttributeOptionService( ProgramAttributeOptionService programAttributeOptionService )
108
 
    {
109
 
        this.programAttributeOptionService = programAttributeOptionService;
110
 
    }
111
 
 
112
 
    public void setProgramAttributeValueService( ProgramAttributeValueService programAttributeValueService )
113
 
    {
114
 
        this.programAttributeValueService = programAttributeValueService;
115
 
    }
116
 
 
117
 
    public void setFormat( I18nFormat format )
118
 
    {
119
 
        this.format = format;
120
 
    }
121
 
 
122
 
    public Collection<Program> getPrograms()
123
 
    {
124
 
        return programs;
125
 
    }
126
 
 
127
 
    public void setProgramInstanceId( Integer programInstanceId )
128
 
    {
129
 
        this.programInstanceId = programInstanceId;
130
 
    }
131
 
 
132
 
    // -------------------------------------------------------------------------
133
 
    // Action implementation
134
 
    // -------------------------------------------------------------------------
135
 
 
136
 
    public String execute()
137
 
        throws Exception
138
 
    {
139
 
 
140
 
        ProgramInstance programInstance = programInstanceService.getProgramInstance( programInstanceId );
141
 
 
142
 
        Patient patient = programInstance.getPatient();
143
 
 
144
 
        Program program = programInstance.getProgram();
145
 
 
146
 
        // ---------------------------------------------------------------------
147
 
        // Update Information of programInstance
148
 
        // ---------------------------------------------------------------------
149
 
 
150
 
        programInstance.setEndDate( new Date() );
151
 
        programInstance.setCompleted( true );
152
 
 
153
 
        programInstanceService.updateProgramInstance( programInstance );
154
 
 
155
 
        patient.getPrograms().remove( program );
156
 
        patientService.updatePatient( patient );
157
 
 
158
 
        // ---------------------------------------------------------------------
159
 
        // Save Program Attributes
160
 
        // ---------------------------------------------------------------------
161
 
 
162
 
        HttpServletRequest request = ServletActionContext.getRequest();
163
 
 
164
 
        Collection<ProgramAttribute> attributes = programAttributeService.getAllProgramAttributes();
165
 
 
166
 
        Set<ProgramAttribute> programAttributes = new HashSet<ProgramAttribute>();
167
 
 
168
 
        // ---------------------------------------------------------------------
169
 
        // End-user inputs attribute value for DEAD-attribute
170
 
        // ---------------------------------------------------------------------
171
 
        
172
 
        boolean flag = false;
173
 
 
174
 
        if ( attributes != null && attributes.size() > 0 )
175
 
        {
176
 
            programInstance.getAttributes().clear();
177
 
 
178
 
            // Save other attributes
179
 
            for ( ProgramAttribute attribute : attributes )
180
 
            {
181
 
                String value = request.getParameter( RemoveEnrollmentAction.PREFIX_ATTRIBUTE + attribute.getId() );
182
 
 
183
 
                if ( StringUtils.isNotBlank( value ) )
184
 
                {
185
 
                    programAttributes.add( attribute );
186
 
 
187
 
                    ProgramAttributeValue attributeValue = programAttributeValueService.getProgramAttributeValue(
188
 
                        programInstance, attribute );
189
 
 
190
 
                    if ( attributeValue == null )
191
 
                    {
192
 
                        attributeValue = new ProgramAttributeValue();
193
 
                        attributeValue.setProgramInstance( programInstance );
194
 
                        attributeValue.setProgramAttribute( attribute );
195
 
 
196
 
                        // DEAD program-attribute
197
 
                        if ( attribute.getName().equalsIgnoreCase( ProgramAttribute.DEAD_NAME )
198
 
                            && attribute.getValueType().equalsIgnoreCase( ProgramAttribute.TYPE_BOOL ) )
199
 
                        {
200
 
                            attributeValue.setValue( value.trim() );
201
 
                            patient.setIsDead( Boolean.parseBoolean( value.trim() ) );
202
 
                            patientService.updatePatient( patient );
203
 
                            flag = true;
204
 
                        }
205
 
                        else if ( ProgramAttribute.TYPE_COMBO.equalsIgnoreCase( attribute.getValueType() ) )
206
 
                        {
207
 
                            ProgramAttributeOption option = programAttributeOptionService.get( NumberUtils.toInt(
208
 
                                value, 0 ) );
209
 
                            if ( option != null )
210
 
                            {
211
 
                                attributeValue.setProgramAttributeOption( option );
212
 
                                attributeValue.setValue( option.getName() );
213
 
                            }
214
 
                        }
215
 
                        else
216
 
                        {
217
 
                            attributeValue.setValue( value.trim() );
218
 
                        }
219
 
 
220
 
                        // CLOSED-DATE program-attribute
221
 
                        if ( attribute.getName().equalsIgnoreCase( ProgramAttribute.CLOSED_DATE )
222
 
                            && attribute.getValueType().equalsIgnoreCase( ProgramAttribute.TYPE_DATE ) && flag )
223
 
                        {
224
 
                            patient.setDeathDate( format.parseDate( value.trim() ) );
225
 
                            patientService.updatePatient( patient );
226
 
                        }
227
 
 
228
 
                        programAttributeValueService.saveProgramAttributeValue( attributeValue );
229
 
                    }
230
 
                    else
231
 
                    {
232
 
                        if ( ProgramAttribute.TYPE_COMBO.equalsIgnoreCase( attribute.getValueType() ) )
233
 
                        {
234
 
                            ProgramAttributeOption option = programAttributeOptionService.get( NumberUtils.toInt(
235
 
                                value, 0 ) );
236
 
                            if ( option != null )
237
 
                            {
238
 
                                attributeValue.setProgramAttributeOption( option );
239
 
                                attributeValue.setValue( option.getName() );
240
 
                            }
241
 
                        }
242
 
                        else
243
 
                        {
244
 
                            attributeValue.setValue( value.trim() );
245
 
                        }
246
 
                    }
247
 
 
248
 
                    programAttributeValueService.updateProgramAttributeValue( attributeValue );
249
 
                }
250
 
            }
251
 
        }
252
 
 
253
 
        programInstance.setAttributes( programAttributes );
254
 
 
255
 
        programInstanceService.updateProgramInstance( programInstance );
256
 
 
257
 
        return SUCCESS;
258
 
    }
259
 
}