2
* Copyright (c) 2004-2009, University of Oslo
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.
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.
27
package org.hisp.dhis.patient.action.patient;
29
import java.util.ArrayList;
30
import java.util.Collection;
31
import java.util.Date;
32
import java.util.HashSet;
35
import javax.servlet.http.HttpServletRequest;
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;
53
import com.opensymphony.xwork2.Action;
56
* @author Abyot Asalefew Gizaw
59
public class RemoveEnrollmentAction
62
public static final String PREFIX_ATTRIBUTE = "attr";
64
// -------------------------------------------------------------------------
66
// -------------------------------------------------------------------------
68
private PatientService patientService;
70
private ProgramInstanceService programInstanceService;
72
private ProgramAttributeService programAttributeService;
74
private ProgramAttributeOptionService programAttributeOptionService;
76
private ProgramAttributeValueService programAttributeValueService;
78
private I18nFormat format;
80
// -------------------------------------------------------------------------
82
// -------------------------------------------------------------------------
84
private Integer programInstanceId;
86
private Collection<Program> programs = new ArrayList<Program>();
88
// -------------------------------------------------------------------------
90
// -------------------------------------------------------------------------
92
public void setPatientService( PatientService patientService )
94
this.patientService = patientService;
97
public void setProgramInstanceService( ProgramInstanceService programInstanceService )
99
this.programInstanceService = programInstanceService;
102
public void setProgramAttributeService( ProgramAttributeService programAttributeService )
104
this.programAttributeService = programAttributeService;
107
public void setProgramAttributeOptionService( ProgramAttributeOptionService programAttributeOptionService )
109
this.programAttributeOptionService = programAttributeOptionService;
112
public void setProgramAttributeValueService( ProgramAttributeValueService programAttributeValueService )
114
this.programAttributeValueService = programAttributeValueService;
117
public void setFormat( I18nFormat format )
119
this.format = format;
122
public Collection<Program> getPrograms()
127
public void setProgramInstanceId( Integer programInstanceId )
129
this.programInstanceId = programInstanceId;
132
// -------------------------------------------------------------------------
133
// Action implementation
134
// -------------------------------------------------------------------------
136
public String execute()
140
ProgramInstance programInstance = programInstanceService.getProgramInstance( programInstanceId );
142
Patient patient = programInstance.getPatient();
144
Program program = programInstance.getProgram();
146
// ---------------------------------------------------------------------
147
// Update Information of programInstance
148
// ---------------------------------------------------------------------
150
programInstance.setEndDate( new Date() );
151
programInstance.setCompleted( true );
153
programInstanceService.updateProgramInstance( programInstance );
155
patient.getPrograms().remove( program );
156
patientService.updatePatient( patient );
158
// ---------------------------------------------------------------------
159
// Save Program Attributes
160
// ---------------------------------------------------------------------
162
HttpServletRequest request = ServletActionContext.getRequest();
164
Collection<ProgramAttribute> attributes = programAttributeService.getAllProgramAttributes();
166
Set<ProgramAttribute> programAttributes = new HashSet<ProgramAttribute>();
168
// ---------------------------------------------------------------------
169
// End-user inputs attribute value for DEAD-attribute
170
// ---------------------------------------------------------------------
172
boolean flag = false;
174
if ( attributes != null && attributes.size() > 0 )
176
programInstance.getAttributes().clear();
178
// Save other attributes
179
for ( ProgramAttribute attribute : attributes )
181
String value = request.getParameter( RemoveEnrollmentAction.PREFIX_ATTRIBUTE + attribute.getId() );
183
if ( StringUtils.isNotBlank( value ) )
185
programAttributes.add( attribute );
187
ProgramAttributeValue attributeValue = programAttributeValueService.getProgramAttributeValue(
188
programInstance, attribute );
190
if ( attributeValue == null )
192
attributeValue = new ProgramAttributeValue();
193
attributeValue.setProgramInstance( programInstance );
194
attributeValue.setProgramAttribute( attribute );
196
// DEAD program-attribute
197
if ( attribute.getName().equalsIgnoreCase( ProgramAttribute.DEAD_NAME )
198
&& attribute.getValueType().equalsIgnoreCase( ProgramAttribute.TYPE_BOOL ) )
200
attributeValue.setValue( value.trim() );
201
patient.setIsDead( Boolean.parseBoolean( value.trim() ) );
202
patientService.updatePatient( patient );
205
else if ( ProgramAttribute.TYPE_COMBO.equalsIgnoreCase( attribute.getValueType() ) )
207
ProgramAttributeOption option = programAttributeOptionService.get( NumberUtils.toInt(
209
if ( option != null )
211
attributeValue.setProgramAttributeOption( option );
212
attributeValue.setValue( option.getName() );
217
attributeValue.setValue( value.trim() );
220
// CLOSED-DATE program-attribute
221
if ( attribute.getName().equalsIgnoreCase( ProgramAttribute.CLOSED_DATE )
222
&& attribute.getValueType().equalsIgnoreCase( ProgramAttribute.TYPE_DATE ) && flag )
224
patient.setDeathDate( format.parseDate( value.trim() ) );
225
patientService.updatePatient( patient );
228
programAttributeValueService.saveProgramAttributeValue( attributeValue );
232
if ( ProgramAttribute.TYPE_COMBO.equalsIgnoreCase( attribute.getValueType() ) )
234
ProgramAttributeOption option = programAttributeOptionService.get( NumberUtils.toInt(
236
if ( option != null )
238
attributeValue.setProgramAttributeOption( option );
239
attributeValue.setValue( option.getName() );
244
attributeValue.setValue( value.trim() );
248
programAttributeValueService.updateProgramAttributeValue( attributeValue );
253
programInstance.setAttributes( programAttributes );
255
programInstanceService.updateProgramInstance( programInstance );