← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 16685: Use TrackedEntityInstanceService.getTrackedEntityInstances( TrackedEntityInstanceQueryParams ) in...

 

------------------------------------------------------------
revno: 16685
committer: sherylyn.marie
branch nick: dhis2
timestamp: Wed 2014-09-10 21:08:18 +0800
message:
  Use TrackedEntityInstanceService.getTrackedEntityInstances( TrackedEntityInstanceQueryParams ) in FindPatientInAdvanced()
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/api/mobile/NotAllowedException.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	2014-06-28 17:56:14 +0000
+++ dhis-2/dhis-services/dhis-service-mobile/src/main/java/org/hisp/dhis/api/mobile/ActivityReportingService.java	2014-09-10 13:08:18 +0000
@@ -57,7 +57,7 @@
     void saveActivityReport( OrganisationUnit unit, ActivityValue activityValue, Integer programStageSectionId )
         throws NotAllowedException;
 
-    Patient findPatient( int patientId )
+    Patient findPatient( String patientId )
         throws NotAllowedException;
 
     PatientList findPatients( String patientIds )

=== modified file 'dhis-2/dhis-services/dhis-service-mobile/src/main/java/org/hisp/dhis/api/mobile/NotAllowedException.java'
--- dhis-2/dhis-services/dhis-service-mobile/src/main/java/org/hisp/dhis/api/mobile/NotAllowedException.java	2014-05-20 15:39:34 +0000
+++ dhis-2/dhis-services/dhis-service-mobile/src/main/java/org/hisp/dhis/api/mobile/NotAllowedException.java	2014-09-10 13:08:18 +0000
@@ -37,6 +37,8 @@
 
     public static final NotAllowedException INVALID_PERIOD = new NotAllowedException( "INVALID_PERIOD" );
 
+    public static final NotAllowedException INVALID_FILTER = new NotAllowedException( "INVALID_FILTER");
+
     public static final NotAllowedException DATASET_LOCKED = new NotAllowedException( "DATASET_LOCKED" );
 
     public static final NotAllowedException NO_USER = new NotAllowedException( "NO_USER", "No user is logged in." );

=== 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	2014-09-05 15:57:19 +0000
+++ dhis-2/dhis-services/dhis-service-mobile/src/main/java/org/hisp/dhis/mobile/service/ActivityReportingServiceImpl.java	2014-09-10 13:08:18 +0000
@@ -63,13 +63,17 @@
 import org.hisp.dhis.chart.Chart;
 import org.hisp.dhis.chart.ChartService;
 import org.hisp.dhis.common.Grid;
+import org.hisp.dhis.common.GridHeader;
+import org.hisp.dhis.common.OrganisationUnitSelectionMode;
+import org.hisp.dhis.common.QueryFilter;
 import org.hisp.dhis.common.QueryItem;
+import org.hisp.dhis.common.QueryOperator;
 import org.hisp.dhis.dataelement.DataElement;
 import org.hisp.dhis.dataelement.DataElementService;
 import org.hisp.dhis.event.EventStatus;
-import org.hisp.dhis.interpretation.InterpretationService;
 import org.hisp.dhis.i18n.I18nFormat;
 import org.hisp.dhis.i18n.I18nManager;
+import org.hisp.dhis.interpretation.InterpretationService;
 import org.hisp.dhis.message.Message;
 import org.hisp.dhis.message.MessageConversation;
 import org.hisp.dhis.message.MessageService;
@@ -1490,10 +1494,17 @@
     }
 
     @Override
-    public org.hisp.dhis.api.mobile.model.LWUITmodel.Patient findPatient( int patientId )
+    public org.hisp.dhis.api.mobile.model.LWUITmodel.Patient findPatient( String patientId )
         throws NotAllowedException
     {
         TrackedEntityInstance patient = entityInstanceService.getTrackedEntityInstance( patientId );
+
+        // Temporary fix
+        if ( patient == null )
+        {
+            patient = entityInstanceService.getTrackedEntityInstance( Integer.parseInt( patientId ) );
+        }
+
         org.hisp.dhis.api.mobile.model.LWUITmodel.Patient patientMobile = getPatientModel( patient );
         return patientMobile;
     }
@@ -1514,102 +1525,128 @@
         return patientlist;
     }
 
+    /**
+     * keyword is on format of
+     * {attribute-id1}:{operator1}:{filter-value1};{attribute
+     * -id2}:{operator2}:{filter-value2}
+     */
     @Override
     public String findPatientInAdvanced( String keyword, int orgUnitId, int programId )
         throws NotAllowedException
     {
-        Set<TrackedEntityInstance> patients = new HashSet<>();
-
-        Collection<TrackedEntityAttribute> attributes = attributeService.getAllTrackedEntityAttributes();
-
-        for ( TrackedEntityAttribute displayAttribute : attributes )
-        {
-            Collection<TrackedEntityInstance> resultPatients = null; // attValueService.getTrackedEntityInstance( displayAttribute, keyword );
-                
-            //TODO re-implement using DefaultTrackedEntityInstanceService.getTrackedEntityInstances( TrackedEntityInstanceQueryParams )
-                
-            // Search in specific OrgUnit
-            if ( orgUnitId != 0 )
-            {
-                for ( TrackedEntityInstance patient : resultPatients )
-                {
-                    if ( patient.getOrganisationUnit().getId() == orgUnitId )
-                    {
-                        patients.add( patient );
-                    }
-                }
-            }
-            // Search in all OrgUnit
+        TrackedEntityInstanceQueryParams param = new TrackedEntityInstanceQueryParams();
+        List<TrackedEntityAttribute> displayAttributes = new ArrayList<TrackedEntityAttribute>(
+            attributeService.getTrackedEntityAttributesDisplayInList() );
+
+        for ( TrackedEntityAttribute trackedEntityAttribute : displayAttributes )
+        {
+            QueryItem queryItem = new QueryItem( trackedEntityAttribute );
+            param.addAttribute( queryItem );
+        }
+
+        param.setProgram( programService.getProgram( programId ) );
+
+        if ( orgUnitId != 0 )
+        {
+            param.addOrganisationUnit( organisationUnitService.getOrganisationUnit( orgUnitId ) );
+            param.setOrganisationUnitMode( OrganisationUnitSelectionMode.SELECTED );
+        }
+        else
+        {
+            param.setOrganisationUnitMode( OrganisationUnitSelectionMode.ALL );
+        }
+
+        String[] items = keyword.split( ";" );
+
+        if ( items == null )
+        {
+            items = new String[1];
+            items[0] = keyword;
+        }
+
+        for ( int i = 0; i < items.length; i++ )
+        {
+            String[] split = keyword.split( ":" );
+            if ( split == null || (split.length != 3 && split.length != 2) )
+            {
+                throw NotAllowedException.INVALID_FILTER;
+            }
+
+            if ( split.length == 2 )
+            {
+                QueryOperator operator = QueryOperator.fromString( split[0] );
+                param.setQuery( new QueryFilter( operator, split[1] ) );
+            }
             else
             {
-                patients.addAll( resultPatients );
+                TrackedEntityAttribute at = attributeService.getTrackedEntityAttributeByName( split[0] );
+                QueryItem queryItem = new QueryItem( at, at.isNumericType() );
+                QueryOperator operator = QueryOperator.fromString( split[1] );
+                queryItem.getFilters().add( new QueryFilter( operator, split[2] ) );
+                param.getFilters().add( queryItem );
             }
         }
 
-        if ( patients.size() == 0 )
+        Grid trackedEntityInstanceGrid = entityInstanceService.getTrackedEntityInstances( param );
+        List<List<Object>> listOfTrackedEntityInstance = trackedEntityInstanceGrid.getRows();
+
+        if ( listOfTrackedEntityInstance.size() == 0 )
         {
             throw NotAllowedException.NO_BENEFICIARY_FOUND;
         }
 
-        Set<TrackedEntity> trackedentities = new HashSet<>();
-        for ( TrackedEntityInstance patient : patients )
-        {
-            if ( patient.getTrackedEntity() != null )
-            {
-                trackedentities.add( patient.getTrackedEntity() );
-            }
-        }
-
-        String resultSet = "";
-
-        Collection<TrackedEntityAttribute> displayAttributes = attributeService
-            .getTrackedEntityAttributesDisplayInList();
-        for ( TrackedEntity trackedentity : trackedentities )
-        {
-            resultSet += trackedentity.getDisplayName() + "$";
-            for ( TrackedEntityInstance patient : patients )
-            {
-                if ( patient.getTrackedEntity() != null && patient.getTrackedEntity().getId() == trackedentity.getId() )
-                {
-                    resultSet += patient.getId() + "/";
-                    String attText = "";
-                    for ( TrackedEntityAttribute displayAttribute : displayAttributes )
-                    {
-                        TrackedEntityAttributeValue value = attValueService.getTrackedEntityAttributeValue( patient,
-                            displayAttribute );
-                        if ( value != null )
-                        {
-                            attText += value.getValue() + " ";
-                        }
-                    }
-                    attText = attText.trim();
-                    resultSet += attText + "$";
-                }
-            }
-        }
-
-        // get tracked entity with no tracked entity name
-        resultSet += "Others$";
-        for ( TrackedEntityInstance patient : patients )
-        {
-            if ( patient.getTrackedEntity() == null )
-            {
-                resultSet += patient.getId() + "/";
-                String attText = "";
-                for ( TrackedEntityAttribute displayAttribute : displayAttributes )
-                {
-                    TrackedEntityAttributeValue value = attValueService.getTrackedEntityAttributeValue( patient,
-                        displayAttribute );
-                    if ( value != null )
-                    {
-                        attText += value.getValue() + " ";
-                    }
-                }
-                attText = attText.trim();
-                resultSet += attText + "$";
-            }
-        }
-        return resultSet;
+        /**
+         * Grid columns: 0 = instance 1 = created 2 = lastupdated 3 = ou 4 = te
+         * 5 onwards = attributes
+         */
+        int instanceIndex = 0;
+        int teIndex = 4;
+        List<Integer> attributesIndex = new ArrayList<Integer>();
+        List<GridHeader> headers = trackedEntityInstanceGrid.getHeaders();
+        int index = 0;
+        for ( GridHeader header : headers )
+        {
+            if ( header.getName().equals( "instance" ) )
+            {
+                instanceIndex = index;
+            }
+            else if ( header.getName().equals( "te" ) )
+            {
+                teIndex = index;
+            }
+            else if ( !header.getName().equals( "created" ) && !header.getName().equals( "lastupdated" )
+                && !header.getName().equals( "ou" ) )
+            {
+                attributesIndex.add( new Integer( index ) );
+            }
+            index++;
+        }
+
+        String instanceInfo = "";
+        String trackedEntityName = "";
+        for ( List<Object> row : listOfTrackedEntityInstance )
+        {
+            TrackedEntity te = trackedEntityService.getTrackedEntity( (String) row.get( teIndex ) );
+            if ( !trackedEntityName.equals( te.getDisplayName() ) )
+            {
+                trackedEntityName = te.getDisplayName();
+                instanceInfo += te.getDisplayName() + "$";
+            }
+
+            instanceInfo += (String) row.get( instanceIndex ) + "/";
+
+            String attText = "";
+            for ( Integer attIndex : attributesIndex )
+            {
+                if ( row.get( attIndex.intValue() ) != null )
+                {
+                    attText += (String) row.get( attIndex.intValue() ) + " ";
+                }
+            }
+            instanceInfo += attText.trim() + "$";
+        }
+
+        return instanceInfo;
     }
 
     @Override

=== 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	2014-08-15 07:40:20 +0000
+++ dhis-2/dhis-web/dhis-web-api-mobile/src/main/java/org/hisp/dhis/api/mobile/controller/MobileOrganisationUnitController.java	2014-09-10 13:08:18 +0000
@@ -366,7 +366,7 @@
     String patientId )
         throws NotAllowedException
     {
-        return activityReportingService.findPatient( Integer.parseInt( patientId ) );
+        return activityReportingService.findPatient( patientId );
     }
 
     @RequestMapping( method = RequestMethod.GET, value = "{clientVersion}/LWUIT/orgUnits/{id}/findPatients" )