~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/relationship/AddRelationshipPatientAction.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
 
/* Copyright (c) 2004-2009, University of Oslo
2
 
 * All rights reserved.
3
 
 *
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.
14
 
 *
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.
25
 
 */
26
 
 
27
 
 package org.hisp.dhis.patient.action.relationship;
28
 
 
29
 
import java.util.Collection;
30
 
 
31
 
import javax.servlet.http.HttpServletRequest;
32
 
 
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;
56
 
 
57
 
import com.opensymphony.xwork2.Action;
58
 
 
59
 
public class AddRelationshipPatientAction
60
 
    implements Action
61
 
{
62
 
    public static final String PREFIX_ATTRIBUTE = "attr";
63
 
 
64
 
    public static final String PREFIX_IDENTIFIER = "iden";
65
 
 
66
 
    // -------------------------------------------------------------------------
67
 
    // Dependencies
68
 
    // -------------------------------------------------------------------------
69
 
 
70
 
    private I18nFormat format;
71
 
 
72
 
    private PatientService patientService;
73
 
 
74
 
    private PatientIdentifierService patientIdentifierService;
75
 
 
76
 
    private PatientIdentifierTypeService patientIdentifierTypeService;
77
 
 
78
 
    private SelectedStateManager selectedStateManager;
79
 
 
80
 
    private PatientAttributeService patientAttributeService;
81
 
 
82
 
    private PatientAttributeValueService patientAttributeValueService;
83
 
 
84
 
    private PatientAttributeOptionService patientAttributeOptionService;
85
 
 
86
 
    private RelationshipService relationshipService;
87
 
 
88
 
    private RelationshipTypeService relationshipTypeService;
89
 
 
90
 
    // -------------------------------------------------------------------------
91
 
    // Input
92
 
    // -------------------------------------------------------------------------
93
 
 
94
 
    private String fullName;
95
 
 
96
 
    private String birthDate;
97
 
 
98
 
    private char ageType;
99
 
 
100
 
    private Integer age;
101
 
 
102
 
    private Character dobType;
103
 
 
104
 
    private String gender;
105
 
 
106
 
    private String bloodGroup;
107
 
 
108
 
    private String registrationDate;
109
 
 
110
 
    private boolean underAge;
111
 
 
112
 
    private Integer relationshipId;
113
 
 
114
 
    private Integer relationshipTypeId;
115
 
 
116
 
    // -------------------------------------------------------------------------
117
 
    // Output
118
 
    // -------------------------------------------------------------------------
119
 
 
120
 
    private Patient patient;
121
 
 
122
 
    public Patient getPatient()
123
 
    {
124
 
        return patient;
125
 
    }
126
 
 
127
 
    // -------------------------------------------------------------------------
128
 
    // Action implementation
129
 
    // -------------------------------------------------------------------------
130
 
 
131
 
    public String execute()
132
 
    {
133
 
        OrganisationUnit organisationUnit = selectedStateManager.getSelectedOrganisationUnit();
134
 
 
135
 
        patient = new Patient();
136
 
 
137
 
        // ---------------------------------------------------------------------
138
 
        // Set FirstName, MiddleName, LastName by FullName
139
 
        // ---------------------------------------------------------------------
140
 
 
141
 
        fullName = fullName.trim();
142
 
 
143
 
        int startIndex = fullName.indexOf( ' ' );
144
 
        int endIndex = fullName.lastIndexOf( ' ' );
145
 
 
146
 
        String firstName = fullName.toString();
147
 
        String middleName = "";
148
 
        String lastName = "";
149
 
 
150
 
        if ( fullName.indexOf( ' ' ) != -1 )
151
 
        {
152
 
            firstName = fullName.substring( 0, startIndex );
153
 
            if ( startIndex == endIndex )
154
 
            {
155
 
                middleName = "";
156
 
                lastName = fullName.substring( startIndex + 1, fullName.length() );
157
 
            }
158
 
            else
159
 
            {
160
 
                middleName = fullName.substring( startIndex + 1, endIndex );
161
 
                lastName = fullName.substring( endIndex + 1, fullName.length() );
162
 
            }
163
 
        }
164
 
 
165
 
        patient.setFirstName( firstName );
166
 
        patient.setMiddleName( middleName );
167
 
        patient.setLastName( lastName );
168
 
 
169
 
        // ---------------------------------------------------------------------
170
 
        // Set Other information for patient
171
 
        // ---------------------------------------------------------------------
172
 
 
173
 
        patient.setGender( gender );
174
 
        patient.setIsDead( false );
175
 
        patient.setBloodGroup( bloodGroup );
176
 
        patient.setUnderAge( underAge );
177
 
        patient.setOrganisationUnit( organisationUnit );
178
 
 
179
 
        if ( dobType == Patient.DOB_TYPE_VERIFIED || dobType == Patient.DOB_TYPE_DECLARED )
180
 
        {
181
 
            birthDate = birthDate.trim();
182
 
            patient.setBirthDate( format.parseDate( birthDate ) );
183
 
        }
184
 
        else
185
 
        {
186
 
            patient.setBirthDateFromAge( age.intValue(), ageType );
187
 
        }
188
 
 
189
 
        patient.setDobType( dobType );
190
 
 
191
 
        patient.setRegistrationDate( format.parseDate( registrationDate ) );
192
 
 
193
 
        // ---------------------------------------------------------------------
194
 
        // Generate system id with this format :
195
 
        // (BirthDate)(Gender)(XXXXXX)(checkdigit)
196
 
        // PatientIdentifierType will be null
197
 
        // ---------------------------------------------------------------------
198
 
 
199
 
        String identifier = PatientIdentifierGenerator.getNewIdentifier( patient.getBirthDate(), patient.getGender() );
200
 
 
201
 
        PatientIdentifier systemGenerateIdentifier = patientIdentifierService.get( null, identifier );
202
 
        while ( systemGenerateIdentifier != null )
203
 
        {
204
 
            identifier = PatientIdentifierGenerator.getNewIdentifier( patient.getBirthDate(), patient.getGender() );
205
 
            systemGenerateIdentifier = patientIdentifierService.get( null, identifier );
206
 
        }
207
 
 
208
 
        systemGenerateIdentifier = new PatientIdentifier();
209
 
        systemGenerateIdentifier.setIdentifier( identifier );
210
 
        systemGenerateIdentifier.setPatient( patient );
211
 
 
212
 
        patient.getIdentifiers().add( systemGenerateIdentifier );
213
 
 
214
 
        selectedStateManager.clearListAll();
215
 
        selectedStateManager.clearSearchingAttributeId();
216
 
        selectedStateManager.clearSortingAttributeId();
217
 
        selectedStateManager.setSearchText( systemGenerateIdentifier.getIdentifier() );
218
 
 
219
 
        // ---------------------------------------------------------------------
220
 
        // Save Patient Identifiers
221
 
        // ---------------------------------------------------------------------
222
 
 
223
 
        HttpServletRequest request = ServletActionContext.getRequest();
224
 
 
225
 
        String value = null;
226
 
 
227
 
        Collection<PatientIdentifierType> identifierTypes = patientIdentifierTypeService.getAllPatientIdentifierTypes();
228
 
 
229
 
        PatientIdentifier pIdentifier = null;
230
 
 
231
 
        if ( identifierTypes != null && identifierTypes.size() > 0 )
232
 
        {
233
 
            for ( PatientIdentifierType identifierType : identifierTypes )
234
 
            {
235
 
                value = request.getParameter( PREFIX_IDENTIFIER + identifierType.getId() );
236
 
 
237
 
                if ( StringUtils.isNotBlank( value ) )
238
 
                {
239
 
                    pIdentifier = new PatientIdentifier();
240
 
                    pIdentifier.setIdentifierType( identifierType );
241
 
                    pIdentifier.setPatient( patient );
242
 
                    pIdentifier.setIdentifier( value.trim() );
243
 
                    patient.getIdentifiers().add( pIdentifier );
244
 
                }
245
 
            }
246
 
        }
247
 
 
248
 
        patientService.savePatient( patient );
249
 
 
250
 
        if ( relationshipId != null && relationshipTypeId != null )
251
 
        {
252
 
            Patient relationship = patientService.getPatient( relationshipId );
253
 
            if ( relationship != null )
254
 
            {
255
 
                if ( underAge )
256
 
                    patient.setRepresentative( relationship );
257
 
 
258
 
                Relationship rel = new Relationship();
259
 
                rel.setPatientA( relationship );
260
 
                rel.setPatientB( patient );
261
 
 
262
 
                if ( relationshipTypeId != null )
263
 
                {
264
 
                    RelationshipType relType = relationshipTypeService.getRelationshipType( relationshipTypeId );
265
 
                    if ( relType != null )
266
 
                    {
267
 
                        rel.setRelationshipType( relType );
268
 
                        relationshipService.saveRelationship( rel );
269
 
                    }
270
 
                }
271
 
            }
272
 
        }
273
 
 
274
 
        // -----------------------------------------------------------------------------
275
 
        // Save Patient Attributes
276
 
        // -----------------------------------------------------------------------------
277
 
 
278
 
        Collection<PatientAttribute> attributes = patientAttributeService.getAllPatientAttributes();
279
 
 
280
 
        PatientAttributeValue attributeValue = null;
281
 
 
282
 
        if ( attributes != null && attributes.size() > 0 )
283
 
        {
284
 
            for ( PatientAttribute attribute : attributes )
285
 
            {
286
 
                value = request.getParameter( PREFIX_ATTRIBUTE + attribute.getId() );
287
 
                if ( StringUtils.isNotBlank( value ) )
288
 
                {
289
 
                    if ( !patient.getAttributes().contains( attribute ) )
290
 
                    {
291
 
                        patient.getAttributes().add( attribute );
292
 
                    }
293
 
 
294
 
                    attributeValue = new PatientAttributeValue();
295
 
                    attributeValue.setPatient( patient );
296
 
                    attributeValue.setPatientAttribute( attribute );
297
 
 
298
 
                    if ( PatientAttribute.TYPE_COMBO.equalsIgnoreCase( attribute.getValueType() ) )
299
 
                    {
300
 
                        PatientAttributeOption option = patientAttributeOptionService
301
 
                            .get( NumberUtils.toInt( value, 0 ) );
302
 
                        if ( option != null )
303
 
                        {
304
 
                            attributeValue.setPatientAttributeOption( option );
305
 
                            attributeValue.setValue( option.getName() );
306
 
                        }
307
 
                        else
308
 
                        {
309
 
                            // Someone deleted this option ...
310
 
                        }
311
 
                    }
312
 
                    else
313
 
                    {
314
 
                        attributeValue.setValue( value.trim() );
315
 
                    }
316
 
                    patientAttributeValueService.savePatientAttributeValue( attributeValue );
317
 
                }
318
 
            }
319
 
        }
320
 
 
321
 
        return SUCCESS;
322
 
    }
323
 
 
324
 
    // -----------------------------------------------------------------------------
325
 
    // Getter/Setter
326
 
    // -----------------------------------------------------------------------------
327
 
 
328
 
    public void setPatientIdentifierTypeService( PatientIdentifierTypeService patientIdentifierTypeService )
329
 
    {
330
 
        this.patientIdentifierTypeService = patientIdentifierTypeService;
331
 
    }
332
 
 
333
 
    public void setBirthDate( String birthDate )
334
 
    {
335
 
        this.birthDate = birthDate;
336
 
    }
337
 
 
338
 
    public void setFormat( I18nFormat format )
339
 
    {
340
 
        this.format = format;
341
 
    }
342
 
 
343
 
    public void setPatientService( PatientService patientService )
344
 
    {
345
 
        this.patientService = patientService;
346
 
    }
347
 
 
348
 
    public void setPatientIdentifierService( PatientIdentifierService patientIdentifierService )
349
 
    {
350
 
        this.patientIdentifierService = patientIdentifierService;
351
 
    }
352
 
 
353
 
    public void setSelectedStateManager( SelectedStateManager selectedStateManager )
354
 
    {
355
 
        this.selectedStateManager = selectedStateManager;
356
 
    }
357
 
 
358
 
    public void setPatientAttributeService( PatientAttributeService patientAttributeService )
359
 
    {
360
 
        this.patientAttributeService = patientAttributeService;
361
 
    }
362
 
 
363
 
    public void setPatientAttributeValueService( PatientAttributeValueService patientAttributeValueService )
364
 
    {
365
 
        this.patientAttributeValueService = patientAttributeValueService;
366
 
    }
367
 
 
368
 
    public void setFullName( String fullName )
369
 
    {
370
 
        this.fullName = fullName;
371
 
    }
372
 
 
373
 
    public void setAge( Integer age )
374
 
    {
375
 
        this.age = age;
376
 
    }
377
 
 
378
 
    public void setGender( String gender )
379
 
    {
380
 
        this.gender = gender;
381
 
    }
382
 
 
383
 
    public void setBloodGroup( String bloodGroup )
384
 
    {
385
 
        this.bloodGroup = bloodGroup;
386
 
    }
387
 
 
388
 
    public void setPatientAttributeOptionService( PatientAttributeOptionService patientAttributeOptionService )
389
 
    {
390
 
        this.patientAttributeOptionService = patientAttributeOptionService;
391
 
    }
392
 
 
393
 
    public void setRelationshipTypeId( Integer relationshipTypeId )
394
 
    {
395
 
        this.relationshipTypeId = relationshipTypeId;
396
 
    }
397
 
 
398
 
    public void setRelationshipService( RelationshipService relationshipService )
399
 
    {
400
 
        this.relationshipService = relationshipService;
401
 
    }
402
 
 
403
 
    public void setRelationshipTypeService( RelationshipTypeService relationshipTypeService )
404
 
    {
405
 
        this.relationshipTypeService = relationshipTypeService;
406
 
    }
407
 
 
408
 
    public void setUnderAge( boolean underAge )
409
 
    {
410
 
        this.underAge = underAge;
411
 
    }
412
 
 
413
 
    public void setRelationshipId( Integer relationshipId )
414
 
    {
415
 
        this.relationshipId = relationshipId;
416
 
    }
417
 
 
418
 
    public void setDobType( Character dobType )
419
 
    {
420
 
        this.dobType = dobType;
421
 
    }
422
 
 
423
 
    public void setAgeType( char ageType )
424
 
    {
425
 
        this.ageType = ageType;
426
 
    }
427
 
 
428
 
    public void setRegistrationDate( String registrationDate )
429
 
    {
430
 
        this.registrationDate = registrationDate;
431
 
    }
432
 
}