← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 22253: Predictable order when using multiple order parameters, code style

 

------------------------------------------------------------
revno: 22253
committer: Markus Bekken <markus.bekken@xxxxxxxxx>
branch nick: dhis2
timestamp: Fri 2016-03-11 14:24:12 +0100
message:
  Predictable order when using multiple order parameters, code style
modified:
  dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/common/OrderParams.java
  dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/events/event/AbstractEventService.java
  dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/events/event/EventSearchParams.java
  dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/events/event/JdbcEventStore.java
  dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/event/EventController.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-dxf2/src/main/java/org/hisp/dhis/dxf2/common/OrderParams.java'
--- dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/common/OrderParams.java	2016-03-10 22:37:23 +0000
+++ dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/common/OrderParams.java	2016-03-11 13:24:12 +0000
@@ -29,13 +29,13 @@
  */
 
 import com.google.common.base.MoreObjects;
+
 import org.hisp.dhis.query.Order;
 import org.hisp.dhis.schema.Property;
 import org.hisp.dhis.schema.Schema;
-
 import java.util.ArrayList;
-import java.util.HashMap;
 import java.util.HashSet;
+import java.util.LinkedHashMap;
 import java.util.List;
 import java.util.Map;
 import java.util.Objects;
@@ -64,7 +64,7 @@
 
     public List<Order> getOrders( Schema schema )
     {
-        Map<String, Order> orders = new HashMap<>();
+        Map<String, Order> orders = new LinkedHashMap<String,Order>();
 
         for ( String o : order )
         {
@@ -79,7 +79,7 @@
             }
             else if ( split.length == 2 )
             {
-            	direction = split[1].toLowerCase();
+                direction = split[1].toLowerCase();
             }
 
             String propertyName = split[0];

=== modified file 'dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/events/event/AbstractEventService.java'
--- dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/events/event/AbstractEventService.java	2016-03-10 22:37:23 +0000
+++ dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/events/event/AbstractEventService.java	2016-03-11 13:24:12 +0000
@@ -30,8 +30,6 @@
 
 import com.fasterxml.jackson.core.type.TypeReference;
 import com.fasterxml.jackson.databind.ObjectMapper;
-import com.google.common.collect.Sets;
-
 import org.apache.commons.lang3.StringUtils;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;

=== modified file 'dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/events/event/EventSearchParams.java'
--- dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/events/event/EventSearchParams.java	2016-03-10 22:37:23 +0000
+++ dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/events/event/EventSearchParams.java	2016-03-11 13:24:12 +0000
@@ -307,7 +307,7 @@
     
     public List<Order> getOrders() 
     {
-    	return this.orders;
+        return this.orders;
     }
 
     public void setOrders( List<Order> orders )
@@ -324,5 +324,4 @@
     {
         this.categoryOptionCombo = categoryOptionCombo;
     }
-
 }

=== modified file 'dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/events/event/JdbcEventStore.java'
--- dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/events/event/JdbcEventStore.java	2016-03-10 22:37:23 +0000
+++ dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/events/event/JdbcEventStore.java	2016-03-11 13:24:12 +0000
@@ -30,7 +30,7 @@
 
 import com.fasterxml.jackson.core.type.TypeReference;
 import com.fasterxml.jackson.databind.ObjectMapper;
-
+import com.google.common.collect.ImmutableMap;
 import org.apache.commons.lang3.StringUtils;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
@@ -55,6 +55,7 @@
 import java.util.ArrayList;
 import java.util.HashSet;
 import java.util.List;
+import java.util.Map;
 import java.util.Set;
 
 import static org.hisp.dhis.common.IdentifiableObjectUtils.getIdentifiers;
@@ -69,6 +70,25 @@
 {
     private static final Log log = LogFactory.getLog( JdbcEventStore.class );
 
+    private static final Map<String, String> QUERY_PARAM_COL_MAP = ImmutableMap.<String, String>builder().
+        put( "event", "psi_uid" ).
+        put( "program", "p_uid" ).
+        put( "programStage", "ps_uid" ).
+        put( "enrollment", "pi_uid" ).
+        put( "enrollmentStatus", "pi_status" ).
+        put( "orgUnit", "ou_uid" ).
+        put( "orgUnitName", "ou_name" ).
+        put( "trackedEntityInstance", "tei_uid" ).
+        put( "eventDate", "psi_executiondate" ).
+        put( "followup", "pi_followup" ).
+        put( "status", "psi_status" ).
+        put( "dueDate", "psi_duedate" ).
+        put( "storedBy", "psi_storedby" ).
+        put( "created", "psi_created" ).
+        put( "lastUpdated", "psi_lastupdated" ).
+        put( "completedBy", "psi_completedby" ).
+        put( "completedDate", "psi_completeddate" ).build();
+    
     @Autowired
     private JdbcTemplate jdbcTemplate;
 
@@ -474,93 +494,27 @@
     
     private String getOrderQuery(List<Order> orders)
     {
-    	if( orders != null ) 
-        {	
-        	ArrayList<String> orderFields = new ArrayList<String>();
-        	
-        	for( Order order : orders )
-        	{
-        		boolean validForOrdering = false;
-        		
-        		//Simple name conversion:
-        		String orderText = "psi_" + order.getProperty().getName().toLowerCase();
-        		        		
-        		//Handling parameters that is not named with the simple convension, or not possible to sort on:
-        		switch( order.getProperty().getName() ) 
-        		{
-        		case "event":
-        			orderText = "psi_uid";
-        			validForOrdering = true;
-        			break;
-        		case "program":
-        			orderText = "p_uid";
-        			validForOrdering = true;
-        			break;
-        		case "programStage":
-        			orderText = "ps_uid";
-        			validForOrdering = true;
-        			break;
-        		case "enrollment":
-        			orderText = "pi_uid";
-        			validForOrdering = true;
-        			break;
-        		case "enrollmentStatus":
-        			orderText = "pi_status";
-        			validForOrdering = true;
-        			break;
-        		case "orgUnit":
-        			orderText = "ou_uid";
-        			validForOrdering = true;
-        			break;
-        		case "orgUnitName":
-        			orderText = "ou_name";
-        			validForOrdering = true;
-        			break;
-        		case "trackedEntityInstance":
-        			orderText = "tei_uid";
-        			validForOrdering = true;
-        			break;
-        		case "eventDate":
-        			orderText = "psi_executiondate";
-        			validForOrdering = true;
-        			break;
-        		case "followup":
-        			orderText = "pi_followup";
-        			validForOrdering = true;
-        			break;
-        		case "status":
-        		case "dueDate":
-        		case "storedBy":
-        		case "created":
-        		case "lastUpdated":
-        		case "completedBy":
-        		case "completedDate":
-        			validForOrdering = true;
-        			break;
-        		default:
-        			validForOrdering = false;
-        			break;
-        		}
-        		
-        		if( validForOrdering ) {
-	        		if( order.isAscending() )
-	        		{
-	        			orderText += " asc";
-	        		}
-	        		else
-	        		{
-	        			orderText += " desc";
-	        		}
-	        		
-	        		orderFields.add( orderText );
-        		}
-        	}
-        	
-        	if( !orderFields.isEmpty() ) {
-        		return "order by " + StringUtils.join(orderFields, ',') + " ";
-        	}
+        if( orders != null ) 
+        {    
+            ArrayList<String> orderFields = new ArrayList<String>();
+            
+            for( Order order : orders )
+            {                   
+                if( QUERY_PARAM_COL_MAP.containsKey( order.getProperty().getName() ) )
+                {
+                    String orderText = QUERY_PARAM_COL_MAP.get( order.getProperty().getName() );
+                    orderText += order.isAscending() ? " asc" : " desc";
+                    
+                    orderFields.add( orderText );
+                }
+            }
+            
+            if( !orderFields.isEmpty() ) 
+            {
+                return "order by " + StringUtils.join(orderFields, ',') + " ";
+            }
         }
-    	
+        
         return "order by psi_lastupdated desc ";
     }
 

=== modified file 'dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/event/EventController.java'
--- dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/event/EventController.java	2016-03-10 22:37:23 +0000
+++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/event/EventController.java	2016-03-11 13:24:12 +0000
@@ -103,6 +103,7 @@
 import java.io.InputStream;
 import java.io.OutputStream;
 import java.net.URI;
+import java.util.Arrays;
 import java.util.Date;
 import java.util.HashMap;
 import java.util.List;
@@ -211,7 +212,7 @@
     {
         WebOptions options = new WebOptions( parameters );
         List<String> fields = Lists.newArrayList( contextService.getParameterValues( "fields" ) );
-        	
+            
         if ( fields.isEmpty() )
         {
             fields.addAll( Preset.ALL.getFields() );
@@ -378,8 +379,8 @@
     {
         if( order != null && !StringUtils.isEmpty(order) ) 
         {
-        	OrderParams op = new OrderParams( Sets.newHashSet(order.split(",")) );
-        	return op.getOrders(getSchema());
+            OrderParams op = new OrderParams( Sets.newLinkedHashSet( Arrays.asList( order.split(",") ) ) );
+            return op.getOrders(getSchema());
         }
         return null;
     }