← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 10853: [mobile] modify save patient method

 

------------------------------------------------------------
revno: 10853
committer: Hong Em <em.hispvietnam@xxxxxxxxx>
branch nick: dhis2
timestamp: Thu 2013-05-16 10:53:39 +0700
message:
  [mobile] modify save patient method
added:
  dhis-2/dhis-services/dhis-service-mobile/src/main/java/org/hisp/dhis/mobile/service/PatientIdentifierGenerator.java
modified:
  dhis-2/dhis-services/dhis-service-mobile/src/main/java/org/hisp/dhis/api/mobile/ActivityReportingService.java
  dhis-2/dhis-services/dhis-service-mobile/src/main/java/org/hisp/dhis/mobile/service/ActivityReportingServiceImpl.java
  dhis-2/dhis-web/dhis-web-api-mobile/src/main/java/org/hisp/dhis/api/mobile/controller/MobileOrganisationUnitController.java


--
lp:dhis2
https://code.launchpad.net/~dhis2-devs-core/dhis2/trunk

Your team DHIS 2 developers is subscribed to branch lp:dhis2.
To unsubscribe from this branch go to https://code.launchpad.net/~dhis2-devs-core/dhis2/trunk/+edit-subscription
=== modified file 'dhis-2/dhis-services/dhis-service-mobile/src/main/java/org/hisp/dhis/api/mobile/ActivityReportingService.java'
--- dhis-2/dhis-services/dhis-service-mobile/src/main/java/org/hisp/dhis/api/mobile/ActivityReportingService.java	2013-05-15 03:23:45 +0000
+++ dhis-2/dhis-services/dhis-service-mobile/src/main/java/org/hisp/dhis/api/mobile/ActivityReportingService.java	2013-05-16 03:53:39 +0000
@@ -84,5 +84,7 @@
     public Program findProgram( String programInfo )
         throws NotAllowedException;
     public Patient findLatestPatient(int orgUnitId) throws NotAllowedException;
+    
+    public String savePatient(Patient patient, int orgUnitId) throws NotAllowedException;
 
 }

=== modified file 'dhis-2/dhis-services/dhis-service-mobile/src/main/java/org/hisp/dhis/mobile/service/ActivityReportingServiceImpl.java'
--- dhis-2/dhis-services/dhis-service-mobile/src/main/java/org/hisp/dhis/mobile/service/ActivityReportingServiceImpl.java	2013-05-15 03:23:45 +0000
+++ dhis-2/dhis-services/dhis-service-mobile/src/main/java/org/hisp/dhis/mobile/service/ActivityReportingServiceImpl.java	2013-05-16 03:53:39 +0000
@@ -89,6 +89,7 @@
 import org.hisp.dhis.system.util.DateUtils;
 import org.joda.time.DateTime;
 import org.joda.time.Period;
+import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Required;
 
 public class ActivityReportingServiceImpl
@@ -100,6 +101,8 @@
 
     private static final String ANONYMOUS_PROGRAM_UPLOADED = "anonymous_program_uploaded";
 
+    private static final String PATIENT_REGISTERED = "patient_registered";
+
     private ActivityComparator activityComparator = new ActivityComparator();
 
     // -------------------------------------------------------------------------
@@ -144,11 +147,24 @@
 
     private PatientIdentifierTypeService patientIdentifierTypeService;
 
+    @Autowired
+    private OrganisationUnitService organisationUnitService;
+
+    public PatientAttributeService getPatientAttributeService()
+    {
+        return patientAttributeService;
+    }
+
     public void setPatientIdentifierTypeService( PatientIdentifierTypeService patientIdentifierTypeService )
     {
         this.patientIdentifierTypeService = patientIdentifierTypeService;
     }
 
+    public PatientIdentifierTypeService getPatientIdentifierTypeService()
+    {
+        return patientIdentifierTypeService;
+    }
+
     private Collection<PatientIdentifier> patientIdentifiers;
 
     public Collection<PatientIdentifier> getPatientIdentifiers()
@@ -192,6 +208,18 @@
         this.patientAttributeService = patientAttributeService;
     }
 
+    private Integer patientId;
+
+    public Integer getPatientId()
+    {
+        return patientId;
+    }
+
+    public void setPatientId( Integer patientId )
+    {
+        this.patientId = patientId;
+    }
+
     // -------------------------------------------------------------------------
     // MobileDataSetService
     // -------------------------------------------------------------------------
@@ -1641,13 +1669,121 @@
     }
 
     @Override
-    public org.hisp.dhis.api.mobile.model.LWUITmodel.Patient findLatestPatient(int orgUnitId)
+    public org.hisp.dhis.api.mobile.model.LWUITmodel.Patient findLatestPatient( int orgUnitId )
         throws NotAllowedException
     {
 
-        Patient patient = (Patient) this.patientService.getLatestPatient(orgUnitId);
+        Patient patient = (Patient) this.patientService.getLatestPatient( orgUnitId );
 
         org.hisp.dhis.api.mobile.model.LWUITmodel.Patient patientMobile = getPatientModel( patient );
         return patientMobile;
     }
+
+    @Override
+    public String savePatient( org.hisp.dhis.api.mobile.model.LWUITmodel.Patient patient, int orgUnitId )
+        throws NotAllowedException
+    {
+        org.hisp.dhis.patient.Patient patientWeb = new org.hisp.dhis.patient.Patient();
+
+        int startIndex = patient.getFirstName().indexOf( ' ' );
+        int endIndex = patient.getFirstName().lastIndexOf( ' ' );
+
+        String firstName = patient.getFirstName().toString();
+        String middleName = "";
+        String lastName = "";
+
+        if ( patient.getFirstName().indexOf( ' ' ) != -1 )
+        {
+            firstName = patient.getFirstName().substring( 0, startIndex );
+            if ( startIndex == endIndex )
+            {
+                middleName = "";
+                lastName = patient.getFirstName().substring( startIndex + 1, patient.getFirstName().length() );
+            }
+            else
+            {
+                middleName = patient.getFirstName().substring( startIndex + 1, endIndex );
+                lastName = patient.getFirstName().substring( endIndex + 1, patient.getFirstName().length() );
+            }
+        }
+
+        patientWeb.setFirstName( firstName );
+        patientWeb.setMiddleName( middleName );
+        patientWeb.setLastName( lastName );
+        patientWeb.setGender( patient.getGender() );
+        patientWeb.setDobType( patient.getDobType() );
+        patientWeb.setPhoneNumber( patient.getPhoneNumber() );
+        patientWeb.setBirthDate( patient.getBirthDate() );
+        patientWeb.setOrganisationUnit( organisationUnitService.getOrganisationUnit( orgUnitId ) );
+        patientWeb.setRegistrationDate( new Date() );
+
+        Set<org.hisp.dhis.patient.PatientIdentifier> patientIdentifierSet = new HashSet<org.hisp.dhis.patient.PatientIdentifier>();
+        Set<org.hisp.dhis.patient.PatientAttribute> patientAttributeSet = new HashSet<org.hisp.dhis.patient.PatientAttribute>();
+        List<PatientAttributeValue> patientAttributeValues = new ArrayList<PatientAttributeValue>();
+
+        Collection<org.hisp.dhis.api.mobile.model.PatientIdentifier> identifiers = patient.getIdentifiers();
+
+        Collection<PatientIdentifierType> identifierTypes = patientIdentifierTypeService.getAllPatientIdentifierTypes();
+
+        Collection<org.hisp.dhis.api.mobile.model.PatientAttribute> patientAttributesMobile = patient
+            .getPatientAttValues();
+
+        if ( identifierTypes.size() > 0 )
+        {
+            for ( org.hisp.dhis.api.mobile.model.PatientIdentifier identifier : identifiers )
+            {
+                PatientIdentifierType patientIdentifierType = patientIdentifierTypeService
+                    .getPatientIdentifierType( identifier.getIdentifierType() );
+
+                org.hisp.dhis.patient.PatientIdentifier patientIdentifier = new org.hisp.dhis.patient.PatientIdentifier();
+
+                patientIdentifier.setIdentifierType( patientIdentifierType );
+                patientIdentifier.setPatient( patientWeb );
+                patientIdentifier.setIdentifier( identifier.getIdentifier() );
+                patientIdentifierSet.add( patientIdentifier );
+            }
+        }
+        // --------------------------------------------------------------------------------
+        // Generate system id with this format :
+        // (BirthDate)(Gender)(XXXXXX)(checkdigit)
+        // PatientIdentifierType will be null
+        // --------------------------------------------------------------------------------
+        if ( identifierTypes.size() == 0 )
+        {
+            String identifier = PatientIdentifierGenerator.getNewIdentifier( patient.getBirthDate(),
+                patient.getGender() );
+
+            org.hisp.dhis.patient.PatientIdentifier systemGenerateIdentifier = new org.hisp.dhis.patient.PatientIdentifier();
+            systemGenerateIdentifier.setIdentifier( identifier );
+            systemGenerateIdentifier.setIdentifierType( null );
+            systemGenerateIdentifier.setPatient( patientWeb );
+            patientIdentifierSet.add( systemGenerateIdentifier );
+        }
+
+        for ( org.hisp.dhis.api.mobile.model.PatientAttribute paAtt : patientAttributesMobile )
+        {
+
+            org.hisp.dhis.patient.PatientAttribute patientAttribute = patientAttributeService
+                .getPatientAttributeByName( paAtt.getName() );
+
+            patientAttributeSet.add( patientAttribute );
+
+            PatientAttributeValue patientAttributeValue = new PatientAttributeValue();
+
+            patientAttributeValue.setPatient( patientWeb );
+            patientAttributeValue.setPatientAttribute( patientAttribute );
+            patientAttributeValue.setValue( paAtt.getValue() );
+            patientAttributeValues.add( patientAttributeValue );
+
+        }
+
+        patientWeb.setIdentifiers( patientIdentifierSet );
+        patientWeb.setAttributes( patientAttributeSet );
+
+        patientId = patientService.createPatient( patientWeb, null, null, patientAttributeValues );
+        
+
+        return PATIENT_REGISTERED;
+
+    }
 }

=== added file 'dhis-2/dhis-services/dhis-service-mobile/src/main/java/org/hisp/dhis/mobile/service/PatientIdentifierGenerator.java'
--- dhis-2/dhis-services/dhis-service-mobile/src/main/java/org/hisp/dhis/mobile/service/PatientIdentifierGenerator.java	1970-01-01 00:00:00 +0000
+++ dhis-2/dhis-services/dhis-service-mobile/src/main/java/org/hisp/dhis/mobile/service/PatientIdentifierGenerator.java	2013-05-16 03:53:39 +0000
@@ -0,0 +1,124 @@
+/*
+ * Copyright (c) 2004-2009, University of Oslo
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ * * Redistributions of source code must retain the above copyright notice, this
+ *   list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above copyright notice,
+ *   this list of conditions and the following disclaimer in the documentation
+ *   and/or other materials provided with the distribution.
+ * * Neither the name of the HISP project nor the names of its contributors may
+ *   be used to endorse or promote products derived from this software without
+ *   specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
+ * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
+ * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+package org.hisp.dhis.mobile.service;
+
+import java.text.DecimalFormat;
+import java.text.SimpleDateFormat;
+import java.util.Date;
+import java.util.Random;
+
+/**
+ * @author Viet
+ */
+public class PatientIdentifierGenerator
+{
+    /**
+     * Creates a new Patient Identifier: (BirthDate)(Gender)(XXXXXX)(checkdigit) <br>
+     * <strong>BirthDate</strong> = YYYYMMDD <br>
+     * <strong>Gender</strong> = Male : 1 | Female : 0<br>
+     * <strong>XXXXXX</strong> = random digits e.g. from 0 - 999999 <br>
+     * <strong>checkdigit</strong>= using the Lunh Algorithm
+     * 
+     * @param birthDate
+     * @param gender
+     * @return
+     */
+    public static String getNewIdentifier( Date birthDate, String gender )
+    {
+        String noCheck = formatDate( birthDate ) + formatGender( gender )
+            + getFixLengthOfNumber( new Random().nextInt( 100000 ), 6 );
+        return noCheck + getCheckdigit( noCheck );
+    }
+
+    /**
+     * Using the Luhn Algorithm to generate check digits
+     * 
+     * @param idWithoutCheckdigit
+     * @return idWithCheckdigit
+     */
+    private static int getCheckdigit( String idWithoutCheckdigit )
+    {
+        idWithoutCheckdigit = idWithoutCheckdigit.trim().toUpperCase();
+        int sum = 0;
+        for ( int i = 0; i < idWithoutCheckdigit.length(); i++ )
+        {
+            char ch = idWithoutCheckdigit.charAt( idWithoutCheckdigit.length() - i - 1 );
+
+            int digit = ch - 48;
+            int weight;
+            if ( i % 2 == 0 )
+            {
+                weight = (2 * digit) - (digit / 5) * 9;
+            }
+            else
+            {
+                weight = digit;
+            }
+            sum += weight;
+        }
+        sum = Math.abs( sum ) + 10;
+        return (10 - (sum % 10)) % 10;
+    }
+
+    public static boolean isValidCC( String num )
+    {
+        final int[][] sumTable = { { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }, { 0, 2, 4, 6, 8, 1, 3, 5, 7, 9 } };
+        int sum = 0, flip = 0;
+
+        for ( int i = num.length() - 1; i >= 0; i--, flip++ )
+            sum += sumTable[flip & 0x1][num.charAt( i ) - '0'];
+        return sum % 10 == 0;
+    }
+
+    private static String getFixLengthOfNumber( long number, int length )
+    {
+        int i = 0;
+        String pattern = "";
+        if ( length == 0 )
+            pattern = "000000";
+        while ( i < length && length > 0 )
+        {
+            pattern += "0";
+            i++;
+        }
+        DecimalFormat df = new DecimalFormat( pattern );
+        return df.format( number );
+    }
+
+    private static String formatDate( Date birthDate )
+    {
+        SimpleDateFormat formater = new SimpleDateFormat( "yyyyMMdd" );
+        String bd = formater.format( birthDate );
+        return bd;
+    }
+
+    private static String formatGender( String gender )
+    {
+        return gender.equalsIgnoreCase( "f" ) ? "0" : "1";
+    }
+}

=== modified file 'dhis-2/dhis-web/dhis-web-api-mobile/src/main/java/org/hisp/dhis/api/mobile/controller/MobileOrganisationUnitController.java'
--- dhis-2/dhis-web/dhis-web-api-mobile/src/main/java/org/hisp/dhis/api/mobile/controller/MobileOrganisationUnitController.java	2013-05-15 03:23:45 +0000
+++ dhis-2/dhis-web/dhis-web-api-mobile/src/main/java/org/hisp/dhis/api/mobile/controller/MobileOrganisationUnitController.java	2013-05-16 03:53:39 +0000
@@ -3,10 +3,8 @@
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Date;
-import java.util.HashSet;
 import java.util.List;
 import java.util.Locale;
-import java.util.Set;
 
 import org.hisp.dhis.api.mobile.ActivityReportingService;
 import org.hisp.dhis.api.mobile.FacilityReportingService;
@@ -20,7 +18,6 @@
 import org.hisp.dhis.api.mobile.model.DataStreamSerializable;
 import org.hisp.dhis.api.mobile.model.MobileModel;
 import org.hisp.dhis.api.mobile.model.ModelList;
-import org.hisp.dhis.api.mobile.model.PatientIdentifier;
 import org.hisp.dhis.api.mobile.model.SMSCode;
 import org.hisp.dhis.api.mobile.model.SMSCommand;
 import org.hisp.dhis.api.mobile.model.LWUITmodel.Patient;
@@ -31,13 +28,6 @@
 import org.hisp.dhis.i18n.I18nService;
 import org.hisp.dhis.organisationunit.OrganisationUnit;
 import org.hisp.dhis.organisationunit.OrganisationUnitService;
-import org.hisp.dhis.patient.PatientAttribute;
-import org.hisp.dhis.patient.PatientAttributeService;
-import org.hisp.dhis.patient.PatientIdentifierService;
-import org.hisp.dhis.patient.PatientIdentifierType;
-import org.hisp.dhis.patient.PatientIdentifierTypeService;
-import org.hisp.dhis.patient.PatientService;
-import org.hisp.dhis.patientattributevalue.PatientAttributeValue;
 import org.hisp.dhis.smscommand.SMSCommandService;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Controller;
@@ -57,8 +47,6 @@
 
     private static final String DATASET_REPORT_UPLOADED = "dataset_report_uploaded";
 
-    private static final String PATIENT_REGISTERED = "patient_registered";
-
     @Autowired
     private ActivityReportingService activityReportingService;
 
@@ -75,24 +63,8 @@
     private I18nService i18nService;
 
     @Autowired
-    private PatientService patientService;
-
-    @Autowired
     private SMSCommandService smsCommandService;
 
-    @Autowired
-    private PatientIdentifierTypeService patientIdentifierTypeService;
-
-    @Autowired
-    private PatientAttributeService patientAttributeService;
-
-    private PatientIdentifierService patientIdentifierService;
-
-    public void setPatientIdentifierService( PatientIdentifierService patientIdentifierService )
-    {
-        this.patientIdentifierService = patientIdentifierService;
-    }
-
     private Integer patientId;
 
     public Integer getPatientId()
@@ -465,84 +437,10 @@
     public String savePatient( @PathVariable
     int id, @RequestBody
     Patient patient )
+        throws NotAllowedException
     {
 
-        org.hisp.dhis.patient.Patient patientWeb = new org.hisp.dhis.patient.Patient();
-
-        int startIndex = patient.getFirstName().indexOf( ' ' );
-        int endIndex = patient.getFirstName().lastIndexOf( ' ' );
-
-        String firstName = patient.getFirstName().toString();
-        String middleName = "";
-        String lastName = "";
-
-        if ( patient.getFirstName().indexOf( ' ' ) != -1 )
-        {
-            firstName = patient.getFirstName().substring( 0, startIndex );
-            if ( startIndex == endIndex )
-            {
-                middleName = "";
-                lastName = patient.getFirstName().substring( startIndex + 1, patient.getFirstName().length() );
-            }
-            else
-            {
-                middleName = patient.getFirstName().substring( startIndex + 1, endIndex );
-                lastName = patient.getFirstName().substring( endIndex + 1, patient.getFirstName().length() );
-            }
-        }
-
-        patientWeb.setFirstName( firstName );
-        patientWeb.setMiddleName( middleName );
-        patientWeb.setLastName( lastName );
-        patientWeb.setGender( patient.getGender() );
-        patientWeb.setDobType( patient.getDobType() );
-        patientWeb.setPhoneNumber( patient.getPhoneNumber() );
-        patientWeb.setBirthDate( patient.getBirthDate() );
-        patientWeb.setOrganisationUnit( organisationUnitService.getOrganisationUnit( id ) );
-        patientWeb.setRegistrationDate( new Date() );
-
-        Set<org.hisp.dhis.patient.PatientIdentifier> patientIdentifierSet = new HashSet<org.hisp.dhis.patient.PatientIdentifier>();
-        Set<PatientAttribute> patientAttributeSet = new HashSet<PatientAttribute>();
-        List<PatientAttributeValue> patientAttributeValues = new ArrayList<PatientAttributeValue>();
-
-        Collection<PatientIdentifier> identifiers = patient.getIdentifiers();
-
-        Collection<org.hisp.dhis.api.mobile.model.PatientAttribute> patientAttributesMobile = patient
-            .getPatientAttValues();
-        for ( PatientIdentifier identifier : identifiers )
-        {
-            PatientIdentifierType patientIdentifierType = patientIdentifierTypeService
-                .getPatientIdentifierType( identifier.getIdentifierType() );
-
-            org.hisp.dhis.patient.PatientIdentifier patientIdentifier = new org.hisp.dhis.patient.PatientIdentifier();
-            patientIdentifier.setIdentifierType( patientIdentifierType );
-            patientIdentifier.setPatient( patientWeb );
-            patientIdentifier.setIdentifier( identifier.getIdentifier() );
-            patientIdentifierSet.add( patientIdentifier );
-        }
-
-        for ( org.hisp.dhis.api.mobile.model.PatientAttribute paAtt : patientAttributesMobile )
-        {
-
-            PatientAttribute patientAttribute = patientAttributeService.getPatientAttributeByName( paAtt.getName() );
-
-            patientAttributeSet.add( patientAttribute );
-
-            PatientAttributeValue patientAttributeValue = new PatientAttributeValue();
-
-            patientAttributeValue.setPatient( patientWeb );
-            patientAttributeValue.setPatientAttribute( patientAttribute );
-            patientAttributeValue.setValue( paAtt.getValue() );
-            patientAttributeValues.add( patientAttributeValue );
-
-        }
-
-        patientWeb.setIdentifiers( patientIdentifierSet );
-        patientWeb.setAttributes( patientAttributeSet );
-
-        patientId = patientService.createPatient( patientWeb, null, null, patientAttributeValues );
-
-        return PATIENT_REGISTERED;
+        return activityReportingService.savePatient( patient, id );
 
     }