← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 12015: (Person-API) allow /api/persons parameters: orgUnit, gender

 

------------------------------------------------------------
revno: 12015
committer: Morten Olav Hansen <mortenoh@xxxxxxxxx>
branch nick: dhis2
timestamp: Tue 2013-09-10 14:18:56 +0200
message:
  (Person-API) allow /api/persons parameters: orgUnit, gender
modified:
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/patient/PatientService.java
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/patient/PatientStore.java
  dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/event/AbstractPersonService.java
  dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/event/DateOfBirthType.java
  dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/event/Gender.java
  dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/event/PersonService.java
  dhis-2/dhis-services/dhis-service-patient/src/main/java/org/hisp/dhis/patient/DefaultPatientService.java
  dhis-2/dhis-services/dhis-service-patient/src/main/java/org/hisp/dhis/patient/hibernate/HibernatePatientStore.java
  dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/event/PersonController.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-api/src/main/java/org/hisp/dhis/patient/PatientService.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/patient/PatientService.java	2013-09-06 09:34:21 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/patient/PatientService.java	2013-09-10 12:18:56 +0000
@@ -122,6 +122,30 @@
     Collection<Patient> getPatients( OrganisationUnit organisationUnit, Integer min, Integer max );
 
     /**
+     * Search Patient base on organization unit with result limited
+     *
+     * @param organisationUnit organisationUnit
+     * @return Patient List
+     */
+    Collection<Patient> getPatients( OrganisationUnit organisationUnit );
+
+    /**
+     * Search Patient base on organization unit with result limited
+     *
+     * @param organisationUnit organisationUnit
+     * @return Patient List
+     */
+    Collection<Patient> getPatients( OrganisationUnit organisationUnit, String gender, Integer min, Integer max );
+
+    /**
+     * Search Patient base on organization unit with result limited
+     *
+     * @param organisationUnit organisationUnit
+     * @return Patient List
+     */
+    Collection<Patient> getPatients( OrganisationUnit organisationUnit, String gender );
+
+    /**
      * Search Patient base on organization unit and sort the result by
      * PatientAttribute
      *
@@ -143,7 +167,7 @@
      * @param max
      * @return
      */
-    Collection<Patient> getPatients( OrganisationUnit organisationUnit, String searchText, Integer min, Integer max );
+    Collection<Patient> getPatientsLikeName( OrganisationUnit organisationUnit, String name, Integer min, Integer max );
 
     /**
      * Search Patient base on PatientIdentifierType or Attribute or Patient's

=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/patient/PatientStore.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/patient/PatientStore.java	2013-09-06 09:34:21 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/patient/PatientStore.java	2013-09-10 12:18:56 +0000
@@ -56,6 +56,8 @@
 
     Collection<Patient> getByOrgUnit( OrganisationUnit organisationUnit, Integer min, Integer max );
 
+    Collection<Patient> getByOrgUnitAndGender( OrganisationUnit organisationUnit, String gender, Integer min, Integer max );
+
     Collection<Patient> getByOrgUnitProgram( OrganisationUnit organisationUnit, Program program, Integer min,
         Integer max );
 

=== modified file 'dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/event/AbstractPersonService.java'
--- dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/event/AbstractPersonService.java	2013-09-10 11:41:45 +0000
+++ dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/event/AbstractPersonService.java	2013-09-10 12:18:56 +0000
@@ -30,6 +30,7 @@
 
 import org.hisp.dhis.i18n.I18nFormat;
 import org.hisp.dhis.i18n.I18nManager;
+import org.hisp.dhis.organisationunit.OrganisationUnit;
 import org.hisp.dhis.patient.Patient;
 import org.hisp.dhis.patient.PatientService;
 import org.springframework.beans.factory.annotation.Autowired;
@@ -80,6 +81,36 @@
     }
 
     @Override
+    public Persons getPersons( OrganisationUnit organisationUnit )
+    {
+        Persons persons = new Persons();
+
+        List<Patient> patients = new ArrayList<Patient>( patientService.getPatients( organisationUnit ) );
+
+        for ( Patient patient : patients )
+        {
+            persons.getPersons().add( getPerson( patient ) );
+        }
+
+        return persons;
+    }
+
+    @Override
+    public Persons getPersons( OrganisationUnit organisationUnit, Gender gender )
+    {
+        Persons persons = new Persons();
+
+        List<Patient> patients = new ArrayList<Patient>( patientService.getPatients( organisationUnit, gender.getValue() ) );
+
+        for ( Patient patient : patients )
+        {
+            persons.getPersons().add( getPerson( patient ) );
+        }
+
+        return persons;
+    }
+
+    @Override
     public Persons getPersons( Collection<Patient> patients )
     {
         Persons persons = new Persons();
@@ -136,5 +167,15 @@
     @Override
     public void deletePerson( Person person )
     {
+        Patient patient = patientService.getPatient( person.getPerson() );
+
+        if ( patient != null )
+        {
+            patientService.deletePatient( patient );
+        }
+        else
+        {
+            throw new IllegalArgumentException();
+        }
     }
 }

=== modified file 'dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/event/DateOfBirthType.java'
--- dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/event/DateOfBirthType.java	2013-09-10 10:46:59 +0000
+++ dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/event/DateOfBirthType.java	2013-09-10 12:18:56 +0000
@@ -37,23 +37,23 @@
     DECLARED( "D" ),
     APPROXIMATE( "A" );
 
-    private final String type;
+    private final String value;
 
-    private DateOfBirthType( String type )
+    private DateOfBirthType( String value )
     {
-        this.type = type;
+        this.value = value;
     }
 
-    public String getType()
+    public String getValue()
     {
-        return type;
+        return value;
     }
 
     public static DateOfBirthType fromString( String text )
     {
         for ( DateOfBirthType dateOfBirthType : DateOfBirthType.values() )
         {
-            if ( text.equals( dateOfBirthType.getType() ) )
+            if ( text.equals( dateOfBirthType.getValue() ) )
             {
                 return dateOfBirthType;
             }

=== modified file 'dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/event/Gender.java'
--- dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/event/Gender.java	2013-09-10 10:46:59 +0000
+++ dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/event/Gender.java	2013-09-10 12:18:56 +0000
@@ -39,23 +39,23 @@
 {
     MALE( "M" ), FEMALE( "F" ), TRANSGENDER( "T" );
 
-    private String type;
+    private final String value;
 
-    private Gender( String type )
+    private Gender( String value )
     {
-        this.type = type;
+        this.value = value;
     }
 
-    public String getType()
+    public String getValue()
     {
-        return type;
+        return value;
     }
 
     public static Gender fromString( String text )
     {
         for ( Gender gender : Gender.values() )
         {
-            if ( text.equals( gender.getType() ) )
+            if ( text.equals( gender.getValue() ) )
             {
                 return gender;
             }

=== modified file 'dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/event/PersonService.java'
--- dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/event/PersonService.java	2013-09-06 09:34:21 +0000
+++ dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/event/PersonService.java	2013-09-10 12:18:56 +0000
@@ -28,6 +28,7 @@
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
+import org.hisp.dhis.organisationunit.OrganisationUnit;
 import org.hisp.dhis.patient.Patient;
 
 import java.util.Collection;
@@ -39,6 +40,10 @@
 {
     Persons getPersons();
 
+    Persons getPersons( OrganisationUnit organisationUnit );
+
+    Persons getPersons( OrganisationUnit organisationUnit, Gender gender );
+
     Persons getPersons( Collection<Patient> patients );
 
     Person getPerson( String uid );

=== modified file 'dhis-2/dhis-services/dhis-service-patient/src/main/java/org/hisp/dhis/patient/DefaultPatientService.java'
--- dhis-2/dhis-services/dhis-service-patient/src/main/java/org/hisp/dhis/patient/DefaultPatientService.java	2013-09-08 08:36:32 +0000
+++ dhis-2/dhis-services/dhis-service-patient/src/main/java/org/hisp/dhis/patient/DefaultPatientService.java	2013-09-10 12:18:56 +0000
@@ -292,6 +292,24 @@
     }
 
     @Override
+    public Collection<Patient> getPatients( OrganisationUnit organisationUnit )
+    {
+        return patientStore.getByOrgUnit( organisationUnit, 0, Integer.MAX_VALUE );
+    }
+
+    @Override
+    public Collection<Patient> getPatients( OrganisationUnit organisationUnit, String gender, Integer min, Integer max )
+    {
+        return patientStore.getByOrgUnitAndGender( organisationUnit, gender, min, max );
+    }
+
+    @Override
+    public Collection<Patient> getPatients( OrganisationUnit organisationUnit, String gender )
+    {
+        return patientStore.getByOrgUnitAndGender( organisationUnit, gender, 0, Integer.MAX_VALUE );
+    }
+
+    @Override
     public Collection<Patient> getPatients( OrganisationUnit organisationUnit, PatientAttribute patientAttribute,
         Integer min, Integer max )
     {
@@ -308,12 +326,12 @@
     }
 
     @Override
-    public Collection<Patient> getPatients( OrganisationUnit organisationUnit, String searchText, Integer min,
+    public Collection<Patient> getPatientsLikeName( OrganisationUnit organisationUnit, String name, Integer min,
         Integer max )
     {
         Collection<Patient> patients = new ArrayList<Patient>();
 
-        Collection<Patient> allPatients = getPatientsByNames( searchText, min, max );
+        Collection<Patient> allPatients = getPatientsByNames( name, min, max );
 
         if ( allPatients.retainAll( getPatients( organisationUnit, min, max ) ) )
         {

=== modified file 'dhis-2/dhis-services/dhis-service-patient/src/main/java/org/hisp/dhis/patient/hibernate/HibernatePatientStore.java'
--- dhis-2/dhis-services/dhis-service-patient/src/main/java/org/hisp/dhis/patient/hibernate/HibernatePatientStore.java	2013-09-10 11:29:23 +0000
+++ dhis-2/dhis-services/dhis-service-patient/src/main/java/org/hisp/dhis/patient/hibernate/HibernatePatientStore.java	2013-09-10 12:18:56 +0000
@@ -190,6 +190,24 @@
 
     @Override
     @SuppressWarnings( "unchecked" )
+    public Collection<Patient> getByOrgUnitAndGender( OrganisationUnit organisationUnit, String gender, Integer min, Integer max )
+    {
+        String hql = "select p from Patient p where p.organisationUnit = :organisationUnit and p.gender = :gender order by p.id DESC";
+
+        Query query = getQuery( hql );
+        query.setEntity( "organisationUnit", organisationUnit );
+        query.setString( "gender", gender );
+
+        if ( min != null && max != null )
+        {
+            query.setFirstResult( min ).setMaxResults( max );
+        }
+
+        return query.list();
+    }
+
+    @Override
+    @SuppressWarnings( "unchecked" )
     public Collection<Patient> getByOrgUnitProgram( OrganisationUnit organisationUnit, Program program, Integer min,
         Integer max )
     {

=== modified file 'dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/event/PersonController.java'
--- dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/event/PersonController.java	2013-09-10 10:46:59 +0000
+++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/event/PersonController.java	2013-09-10 12:18:56 +0000
@@ -29,9 +29,12 @@
  */
 
 import org.hisp.dhis.api.controller.WebOptions;
+import org.hisp.dhis.common.IdentifiableObjectManager;
+import org.hisp.dhis.dxf2.event.Gender;
 import org.hisp.dhis.dxf2.event.Person;
 import org.hisp.dhis.dxf2.event.PersonService;
 import org.hisp.dhis.dxf2.event.Persons;
+import org.hisp.dhis.organisationunit.OrganisationUnit;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Controller;
 import org.springframework.ui.Model;
@@ -55,11 +58,33 @@
     @Autowired
     private PersonService personService;
 
-    @RequestMapping(value = "", method = RequestMethod.GET)
-    public String getPersons( @RequestParam Map<String, String> parameters, Model model, HttpServletRequest request ) throws Exception
+    @Autowired
+    private IdentifiableObjectManager manager;
+
+    @RequestMapping( value = "", method = RequestMethod.GET )
+    public String getPersons(
+        @RequestParam( required = false ) String orgUnit,
+        @RequestParam( required = false ) Gender gender,
+        @RequestParam Map<String, String> parameters, Model model, HttpServletRequest request ) throws Exception
     {
         WebOptions options = new WebOptions( parameters );
-        Persons persons = personService.getPersons();
+        Persons persons;
+
+        // it will be required in the future to have at least orgUnit, but for now, we allow no parameters
+        if ( gender == null && orgUnit == null )
+        {
+            persons = personService.getPersons();
+        }
+        else if ( gender != null )
+        {
+            OrganisationUnit organisationUnit = manager.get( OrganisationUnit.class, orgUnit );
+            persons = personService.getPersons( organisationUnit, gender );
+        }
+        else
+        {
+            OrganisationUnit organisationUnit = manager.get( OrganisationUnit.class, orgUnit );
+            persons = personService.getPersons( organisationUnit );
+        }
 
         model.addAttribute( "model", persons );
         model.addAttribute( "viewClass", options.getViewClass( "basic" ) );