← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 22243: Added ordering of events

 

------------------------------------------------------------
revno: 22243
committer: Markus Bekken <markus.bekken@xxxxxxxxx>
branch nick: dhis2
timestamp: Thu 2016-03-10 23:37:23 +0100
message:
  Added ordering of events
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/EventService.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-02-03 07:40:27 +0000
+++ dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/common/OrderParams.java	2016-03-10 22:37:23 +0000
@@ -70,14 +70,21 @@
         {
             String[] split = o.split( ":" );
 
-            if ( split.length <= 1 )
+            //Using ascending as default direction. 
+            String direction = "asc";
+            
+            if ( split.length < 1 )
             {
                 continue;
             }
+            else if ( split.length == 2 )
+            {
+            	direction = split[1].toLowerCase();
+            }
 
             String propertyName = split[0];
             Property property = schema.getProperty( propertyName );
-            String direction = split[1].toLowerCase();
+            
 
             if ( orders.containsKey( propertyName ) || !schema.haveProperty( propertyName )
                 || !validProperty( property ) || !validDirection( direction ) )

=== 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-02-05 16:28:34 +0000
+++ 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
@@ -30,6 +30,8 @@
 
 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;
@@ -71,6 +73,7 @@
 import org.hisp.dhis.program.ProgramStageInstanceService;
 import org.hisp.dhis.program.ProgramStageService;
 import org.hisp.dhis.program.ProgramStatus;
+import org.hisp.dhis.query.Order;
 import org.hisp.dhis.scheduling.TaskId;
 import org.hisp.dhis.system.callable.IdentifiableObjectCallable;
 import org.hisp.dhis.system.notification.NotificationLevel;
@@ -505,7 +508,8 @@
     @Override
     public EventSearchParams getFromUrl( String program, String programStage, ProgramStatus programStatus, Boolean followUp, String orgUnit,
         OrganisationUnitSelectionMode orgUnitSelectionMode, String trackedEntityInstance, Date startDate, Date endDate,
-        EventStatus status, Date lastUpdated, DataElementCategoryOptionCombo attributeCoc, IdSchemes idSchemes, Integer page, Integer pageSize, boolean totalPages, boolean skipPaging, boolean includeAttributes )
+        EventStatus status, Date lastUpdated, DataElementCategoryOptionCombo attributeCoc, IdSchemes idSchemes, Integer page, Integer pageSize, boolean totalPages, boolean skipPaging,
+        List<Order> orders, boolean includeAttributes )
     {
         UserCredentials userCredentials = currentUserService.getCurrentUser().getUserCredentials();
 
@@ -570,7 +574,8 @@
         params.setTotalPages( totalPages );
         params.setSkipPaging( skipPaging );
         params.setIncludeAttributes( includeAttributes );
-
+        params.setOrders( orders );
+        
         return params;
     }
 

=== 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-01-04 02:27:49 +0000
+++ 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
@@ -29,6 +29,7 @@
  */
 
 import java.util.Date;
+import java.util.List;
 
 import org.hisp.dhis.common.OrganisationUnitSelectionMode;
 import org.hisp.dhis.dataelement.DataElementCategoryOptionCombo;
@@ -38,6 +39,7 @@
 import org.hisp.dhis.program.Program;
 import org.hisp.dhis.program.ProgramStage;
 import org.hisp.dhis.program.ProgramStatus;
+import org.hisp.dhis.query.Order;
 import org.hisp.dhis.trackedentity.TrackedEntityInstance;
 
 /**
@@ -82,6 +84,8 @@
     private boolean totalPages;
 
     private boolean skipPaging;
+    
+    private List<Order> orders;
 
     private boolean includeAttributes;
 
@@ -300,7 +304,17 @@
     {
         this.includeAttributes = includeAttributes;
     }
+    
+    public List<Order> getOrders() 
+    {
+    	return this.orders;
+    }
 
+    public void setOrders( List<Order> orders )
+    {
+        this.orders = orders;
+    }
+    
     public DataElementCategoryOptionCombo getCategoryOptionCombo()
     {
         return categoryOptionCombo;

=== modified file 'dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/events/event/EventService.java'
--- dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/events/event/EventService.java	2016-01-04 02:27:49 +0000
+++ dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/events/event/EventService.java	2016-03-10 22:37:23 +0000
@@ -38,6 +38,7 @@
 import org.hisp.dhis.event.EventStatus;
 import org.hisp.dhis.program.ProgramStageInstance;
 import org.hisp.dhis.program.ProgramStatus;
+import org.hisp.dhis.query.Order;
 import org.hisp.dhis.scheduling.TaskId;
 
 import java.io.IOException;
@@ -60,7 +61,8 @@
 
     EventSearchParams getFromUrl( String program, String programStage, ProgramStatus programStatus, Boolean followUp, String orgUnit,
         OrganisationUnitSelectionMode orgUnitSelectionMode, String trackedEntityInstance, Date startDate, Date endDate,
-        EventStatus status, Date lastUpdated, DataElementCategoryOptionCombo attributeCoc, IdSchemes idSchemes, Integer page, Integer pageSize, boolean totalPages, boolean skipPaging, boolean includeAttributes );
+        EventStatus status, Date lastUpdated, DataElementCategoryOptionCombo attributeCoc, IdSchemes idSchemes, Integer page, 
+        Integer pageSize, boolean totalPages, boolean skipPaging, List<Order> orders, boolean includeAttributes );
 
     Event getEvent( String uid );
 

=== 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-01-15 13:45:46 +0000
+++ 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
@@ -30,6 +30,8 @@
 
 import com.fasterxml.jackson.core.type.TypeReference;
 import com.fasterxml.jackson.databind.ObjectMapper;
+
+import org.apache.commons.lang3.StringUtils;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 import org.hisp.dhis.common.IdSchemes;
@@ -42,6 +44,7 @@
 import org.hisp.dhis.organisationunit.OrganisationUnit;
 import org.hisp.dhis.program.ProgramStatus;
 import org.hisp.dhis.program.ProgramType;
+import org.hisp.dhis.query.Order;
 import org.hisp.dhis.system.util.DateUtils;
 import org.hisp.dhis.util.ObjectUtils;
 import org.springframework.beans.factory.annotation.Autowired;
@@ -337,7 +340,7 @@
 
         sql += ") as cm on event.psi_id=cm.psic_id ";
 
-        sql += "order by psi_uid desc ";
+        sql += getOrderQuery( params.getOrders() );
 
         return sql;
     }
@@ -468,6 +471,98 @@
 
         return sql;
     }
+    
+    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, ',') + " ";
+        	}
+        }
+    	
+        return "order by psi_lastupdated desc ";
+    }
 
     private String getAttributeValueQuery()
     {

=== 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-02-01 07:36:06 +0000
+++ 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
@@ -29,7 +29,9 @@
  */
 
 import com.google.common.collect.Lists;
+import com.google.common.collect.Sets;
 import com.google.common.io.ByteSource;
+
 import org.apache.commons.io.IOUtils;
 import org.hisp.dhis.common.IdSchemes;
 import org.hisp.dhis.common.OrganisationUnitSelectionMode;
@@ -38,6 +40,7 @@
 import org.hisp.dhis.dataelement.DataElementCategoryOptionCombo;
 import org.hisp.dhis.dataelement.DataElementService;
 import org.hisp.dhis.dxf2.common.ImportOptions;
+import org.hisp.dhis.dxf2.common.OrderParams;
 import org.hisp.dhis.dxf2.events.event.DataValue;
 import org.hisp.dhis.dxf2.events.event.Event;
 import org.hisp.dhis.dxf2.events.event.EventSearchParams;
@@ -51,6 +54,7 @@
 import org.hisp.dhis.dxf2.importsummary.ImportStatus;
 import org.hisp.dhis.dxf2.importsummary.ImportSummaries;
 import org.hisp.dhis.dxf2.importsummary.ImportSummary;
+import org.hisp.dhis.query.Order;
 import org.hisp.dhis.render.RenderService;
 import org.hisp.dhis.dxf2.utils.InputUtils;
 import org.hisp.dhis.dxf2.webmessage.WebMessage;
@@ -71,6 +75,8 @@
 import org.hisp.dhis.program.ProgramStatus;
 import org.hisp.dhis.scheduling.TaskCategory;
 import org.hisp.dhis.scheduling.TaskId;
+import org.hisp.dhis.schema.Schema;
+import org.hisp.dhis.schema.SchemaService;
 import org.hisp.dhis.system.scheduling.Scheduler;
 import org.hisp.dhis.user.CurrentUserService;
 import org.hisp.dhis.webapi.service.ContextService;
@@ -92,6 +98,7 @@
 
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
+
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
@@ -156,7 +163,22 @@
 
     @Autowired
     private ContextService contextService;
-
+    
+    @Autowired
+    private SchemaService schemaService;
+    
+    private Schema schema;
+
+    protected Schema getSchema()
+    {
+        if ( schema == null )
+        {
+            schema = schemaService.getDynamicSchema( Event.class );
+        }
+
+        return schema;
+    }
+        
     // -------------------------------------------------------------------------
     // READ
     // -------------------------------------------------------------------------
@@ -182,18 +204,19 @@
         @RequestParam( required = false ) Integer pageSize,
         @RequestParam( required = false ) boolean totalPages,
         @RequestParam( required = false ) boolean skipPaging,
+        @RequestParam( required = false ) String order,
         @RequestParam( required = false ) String attachment,
         @RequestParam Map<String, String> parameters, IdSchemes idSchemes, Model model, HttpServletResponse response, HttpServletRequest request )
         throws WebMessageException
     {
         WebOptions options = new WebOptions( parameters );
         List<String> fields = Lists.newArrayList( contextService.getParameterValues( "fields" ) );
-
+        	
         if ( fields.isEmpty() )
         {
             fields.addAll( Preset.ALL.getFields() );
         }
-
+        
         DataElementCategoryOptionCombo attributeOptionCombo = inputUtils.getAttributeOptionCombo( attributeCc, attributeCos );
 
         if ( attributeOptionCombo == null )
@@ -202,7 +225,7 @@
         }
 
         EventSearchParams params = eventService.getFromUrl( program, programStage, programStatus, followUp, orgUnit, ouMode,
-            trackedEntityInstance, startDate, endDate, status, lastUpdated, attributeOptionCombo, idSchemes, page, pageSize, totalPages, skipPaging, false );
+            trackedEntityInstance, startDate, endDate, status, lastUpdated, attributeOptionCombo, idSchemes, page, pageSize, totalPages, skipPaging, getOrderParams(order), false );
 
         Events events = eventService.getEvents( params );
 
@@ -256,6 +279,7 @@
         @RequestParam( required = false ) Integer pageSize,
         @RequestParam( required = false ) boolean totalPages,
         @RequestParam( required = false ) boolean skipPaging,
+        @RequestParam( required = false ) String order,
         @RequestParam( required = false ) String attachment,
         @RequestParam( required = false, defaultValue = "false" ) boolean skipHeader,
         IdSchemes idSchemes, HttpServletResponse response, HttpServletRequest request ) throws IOException, WebMessageException
@@ -268,7 +292,7 @@
         }
 
         EventSearchParams params = eventService.getFromUrl( program, programStage, programStatus, followUp, orgUnit, ouMode,
-            trackedEntityInstance, startDate, endDate, status, lastUpdated, attributeOptionCombo, idSchemes, page, pageSize, totalPages, skipPaging, false );
+            trackedEntityInstance, startDate, endDate, status, lastUpdated, attributeOptionCombo, idSchemes, page, pageSize, totalPages, skipPaging, getOrderParams(order), false );
 
         Events events = eventService.getEvents( params );
 
@@ -304,6 +328,7 @@
         @RequestParam( required = false ) String attributeCos,
         @RequestParam( required = false ) boolean totalPages,
         @RequestParam( required = false ) boolean skipPaging,
+        @RequestParam( required = false ) String order,
         @RequestParam Map<String, String> parameters, Model model )
         throws WebMessageException
 
@@ -318,7 +343,7 @@
         }
 
         EventSearchParams params = eventService.getFromUrl( program, null, programStatus, null,
-            orgUnit, ouMode, null, startDate, endDate, eventStatus, null, attributeOptionCombo, null, null, null, totalPages, skipPaging, true );
+            orgUnit, ouMode, null, startDate, endDate, eventStatus, null, attributeOptionCombo, null, null, null, totalPages, skipPaging, getOrderParams(order), true );
 
         EventRows eventRows = eventRowService.getEventRows( params );
 
@@ -348,6 +373,16 @@
 
         return "event";
     }
+    
+    private List<Order> getOrderParams(String order)
+    {
+        if( order != null && !StringUtils.isEmpty(order) ) 
+        {
+        	OrderParams op = new OrderParams( Sets.newHashSet(order.split(",")) );
+        	return op.getOrders(getSchema());
+        }
+        return null;
+    }
 
     private Map<Object, Object> getMetaData( Program program )
     {