1
/* Copyright (c) 2004-2009, University of Oslo
4
* Redistribution and use in source and binary forms, with or without
5
* modification, are permitted provided that the following conditions are met:
6
* * Redistributions of source code must retain the above copyright notice, this
7
* list of conditions and the following disclaimer.
8
* * Redistributions in binary form must reproduce the above copyright notice,
9
* this list of conditions and the following disclaimer in the documentation
10
* and/or other materials provided with the distribution.
11
* * Neither the name of the HISP project nor the names of its contributors may
12
* be used to endorse or promote products derived from this software without
13
* specific prior written permission.
15
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
16
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
19
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
22
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
package org.hisp.dhis.patient.action.relationship;
29
import java.util.Collection;
31
import javax.servlet.http.HttpServletRequest;
33
import org.apache.commons.lang.StringUtils;
34
import org.apache.commons.lang.math.NumberUtils;
35
import org.apache.struts2.ServletActionContext;
36
import org.hisp.dhis.i18n.I18nFormat;
37
import org.hisp.dhis.organisationunit.OrganisationUnit;
38
import org.hisp.dhis.patient.Patient;
39
import org.hisp.dhis.patient.PatientAttribute;
40
import org.hisp.dhis.patient.PatientAttributeOption;
41
import org.hisp.dhis.patient.PatientAttributeOptionService;
42
import org.hisp.dhis.patient.PatientAttributeService;
43
import org.hisp.dhis.patient.PatientIdentifier;
44
import org.hisp.dhis.patient.PatientIdentifierService;
45
import org.hisp.dhis.patient.PatientIdentifierType;
46
import org.hisp.dhis.patient.PatientIdentifierTypeService;
47
import org.hisp.dhis.patient.PatientService;
48
import org.hisp.dhis.patient.idgen.PatientIdentifierGenerator;
49
import org.hisp.dhis.patient.state.SelectedStateManager;
50
import org.hisp.dhis.patientattributevalue.PatientAttributeValue;
51
import org.hisp.dhis.patientattributevalue.PatientAttributeValueService;
52
import org.hisp.dhis.relationship.Relationship;
53
import org.hisp.dhis.relationship.RelationshipService;
54
import org.hisp.dhis.relationship.RelationshipType;
55
import org.hisp.dhis.relationship.RelationshipTypeService;
57
import com.opensymphony.xwork2.Action;
59
public class AddRelationshipPatientAction
62
public static final String PREFIX_ATTRIBUTE = "attr";
64
public static final String PREFIX_IDENTIFIER = "iden";
66
// -------------------------------------------------------------------------
68
// -------------------------------------------------------------------------
70
private I18nFormat format;
72
private PatientService patientService;
74
private PatientIdentifierService patientIdentifierService;
76
private PatientIdentifierTypeService patientIdentifierTypeService;
78
private SelectedStateManager selectedStateManager;
80
private PatientAttributeService patientAttributeService;
82
private PatientAttributeValueService patientAttributeValueService;
84
private PatientAttributeOptionService patientAttributeOptionService;
86
private RelationshipService relationshipService;
88
private RelationshipTypeService relationshipTypeService;
90
// -------------------------------------------------------------------------
92
// -------------------------------------------------------------------------
94
private String fullName;
96
private String birthDate;
102
private Character dobType;
104
private String gender;
106
private String bloodGroup;
108
private String registrationDate;
110
private boolean underAge;
112
private Integer relationshipId;
114
private Integer relationshipTypeId;
116
// -------------------------------------------------------------------------
118
// -------------------------------------------------------------------------
120
private Patient patient;
122
public Patient getPatient()
127
// -------------------------------------------------------------------------
128
// Action implementation
129
// -------------------------------------------------------------------------
131
public String execute()
133
OrganisationUnit organisationUnit = selectedStateManager.getSelectedOrganisationUnit();
135
patient = new Patient();
137
// ---------------------------------------------------------------------
138
// Set FirstName, MiddleName, LastName by FullName
139
// ---------------------------------------------------------------------
141
fullName = fullName.trim();
143
int startIndex = fullName.indexOf( ' ' );
144
int endIndex = fullName.lastIndexOf( ' ' );
146
String firstName = fullName.toString();
147
String middleName = "";
148
String lastName = "";
150
if ( fullName.indexOf( ' ' ) != -1 )
152
firstName = fullName.substring( 0, startIndex );
153
if ( startIndex == endIndex )
156
lastName = fullName.substring( startIndex + 1, fullName.length() );
160
middleName = fullName.substring( startIndex + 1, endIndex );
161
lastName = fullName.substring( endIndex + 1, fullName.length() );
165
patient.setFirstName( firstName );
166
patient.setMiddleName( middleName );
167
patient.setLastName( lastName );
169
// ---------------------------------------------------------------------
170
// Set Other information for patient
171
// ---------------------------------------------------------------------
173
patient.setGender( gender );
174
patient.setIsDead( false );
175
patient.setBloodGroup( bloodGroup );
176
patient.setUnderAge( underAge );
177
patient.setOrganisationUnit( organisationUnit );
179
if ( dobType == Patient.DOB_TYPE_VERIFIED || dobType == Patient.DOB_TYPE_DECLARED )
181
birthDate = birthDate.trim();
182
patient.setBirthDate( format.parseDate( birthDate ) );
186
patient.setBirthDateFromAge( age.intValue(), ageType );
189
patient.setDobType( dobType );
191
patient.setRegistrationDate( format.parseDate( registrationDate ) );
193
// ---------------------------------------------------------------------
194
// Generate system id with this format :
195
// (BirthDate)(Gender)(XXXXXX)(checkdigit)
196
// PatientIdentifierType will be null
197
// ---------------------------------------------------------------------
199
String identifier = PatientIdentifierGenerator.getNewIdentifier( patient.getBirthDate(), patient.getGender() );
201
PatientIdentifier systemGenerateIdentifier = patientIdentifierService.get( null, identifier );
202
while ( systemGenerateIdentifier != null )
204
identifier = PatientIdentifierGenerator.getNewIdentifier( patient.getBirthDate(), patient.getGender() );
205
systemGenerateIdentifier = patientIdentifierService.get( null, identifier );
208
systemGenerateIdentifier = new PatientIdentifier();
209
systemGenerateIdentifier.setIdentifier( identifier );
210
systemGenerateIdentifier.setPatient( patient );
212
patient.getIdentifiers().add( systemGenerateIdentifier );
214
selectedStateManager.clearListAll();
215
selectedStateManager.clearSearchingAttributeId();
216
selectedStateManager.clearSortingAttributeId();
217
selectedStateManager.setSearchText( systemGenerateIdentifier.getIdentifier() );
219
// ---------------------------------------------------------------------
220
// Save Patient Identifiers
221
// ---------------------------------------------------------------------
223
HttpServletRequest request = ServletActionContext.getRequest();
227
Collection<PatientIdentifierType> identifierTypes = patientIdentifierTypeService.getAllPatientIdentifierTypes();
229
PatientIdentifier pIdentifier = null;
231
if ( identifierTypes != null && identifierTypes.size() > 0 )
233
for ( PatientIdentifierType identifierType : identifierTypes )
235
value = request.getParameter( PREFIX_IDENTIFIER + identifierType.getId() );
237
if ( StringUtils.isNotBlank( value ) )
239
pIdentifier = new PatientIdentifier();
240
pIdentifier.setIdentifierType( identifierType );
241
pIdentifier.setPatient( patient );
242
pIdentifier.setIdentifier( value.trim() );
243
patient.getIdentifiers().add( pIdentifier );
248
patientService.savePatient( patient );
250
if ( relationshipId != null && relationshipTypeId != null )
252
Patient relationship = patientService.getPatient( relationshipId );
253
if ( relationship != null )
256
patient.setRepresentative( relationship );
258
Relationship rel = new Relationship();
259
rel.setPatientA( relationship );
260
rel.setPatientB( patient );
262
if ( relationshipTypeId != null )
264
RelationshipType relType = relationshipTypeService.getRelationshipType( relationshipTypeId );
265
if ( relType != null )
267
rel.setRelationshipType( relType );
268
relationshipService.saveRelationship( rel );
274
// -----------------------------------------------------------------------------
275
// Save Patient Attributes
276
// -----------------------------------------------------------------------------
278
Collection<PatientAttribute> attributes = patientAttributeService.getAllPatientAttributes();
280
PatientAttributeValue attributeValue = null;
282
if ( attributes != null && attributes.size() > 0 )
284
for ( PatientAttribute attribute : attributes )
286
value = request.getParameter( PREFIX_ATTRIBUTE + attribute.getId() );
287
if ( StringUtils.isNotBlank( value ) )
289
if ( !patient.getAttributes().contains( attribute ) )
291
patient.getAttributes().add( attribute );
294
attributeValue = new PatientAttributeValue();
295
attributeValue.setPatient( patient );
296
attributeValue.setPatientAttribute( attribute );
298
if ( PatientAttribute.TYPE_COMBO.equalsIgnoreCase( attribute.getValueType() ) )
300
PatientAttributeOption option = patientAttributeOptionService
301
.get( NumberUtils.toInt( value, 0 ) );
302
if ( option != null )
304
attributeValue.setPatientAttributeOption( option );
305
attributeValue.setValue( option.getName() );
309
// Someone deleted this option ...
314
attributeValue.setValue( value.trim() );
316
patientAttributeValueService.savePatientAttributeValue( attributeValue );
324
// -----------------------------------------------------------------------------
326
// -----------------------------------------------------------------------------
328
public void setPatientIdentifierTypeService( PatientIdentifierTypeService patientIdentifierTypeService )
330
this.patientIdentifierTypeService = patientIdentifierTypeService;
333
public void setBirthDate( String birthDate )
335
this.birthDate = birthDate;
338
public void setFormat( I18nFormat format )
340
this.format = format;
343
public void setPatientService( PatientService patientService )
345
this.patientService = patientService;
348
public void setPatientIdentifierService( PatientIdentifierService patientIdentifierService )
350
this.patientIdentifierService = patientIdentifierService;
353
public void setSelectedStateManager( SelectedStateManager selectedStateManager )
355
this.selectedStateManager = selectedStateManager;
358
public void setPatientAttributeService( PatientAttributeService patientAttributeService )
360
this.patientAttributeService = patientAttributeService;
363
public void setPatientAttributeValueService( PatientAttributeValueService patientAttributeValueService )
365
this.patientAttributeValueService = patientAttributeValueService;
368
public void setFullName( String fullName )
370
this.fullName = fullName;
373
public void setAge( Integer age )
378
public void setGender( String gender )
380
this.gender = gender;
383
public void setBloodGroup( String bloodGroup )
385
this.bloodGroup = bloodGroup;
388
public void setPatientAttributeOptionService( PatientAttributeOptionService patientAttributeOptionService )
390
this.patientAttributeOptionService = patientAttributeOptionService;
393
public void setRelationshipTypeId( Integer relationshipTypeId )
395
this.relationshipTypeId = relationshipTypeId;
398
public void setRelationshipService( RelationshipService relationshipService )
400
this.relationshipService = relationshipService;
403
public void setRelationshipTypeService( RelationshipTypeService relationshipTypeService )
405
this.relationshipTypeService = relationshipTypeService;
408
public void setUnderAge( boolean underAge )
410
this.underAge = underAge;
413
public void setRelationshipId( Integer relationshipId )
415
this.relationshipId = relationshipId;
418
public void setDobType( Character dobType )
420
this.dobType = dobType;
423
public void setAgeType( char ageType )
425
this.ageType = ageType;
428
public void setRegistrationDate( String registrationDate )
430
this.registrationDate = registrationDate;