dhis2-devs team mailing list archive
-
dhis2-devs team
-
Mailing list archive
-
Message #24075
[Branch ~dhis2-devs-core/dhis2/trunk] Rev 11750: Analytics events, added sorting
------------------------------------------------------------
revno: 11750
committer: Lars Helge Øverland <larshelge@xxxxxxxxx>
branch nick: dhis2
timestamp: Thu 2013-08-22 18:03:12 +0200
message:
Analytics events, added sorting
modified:
dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/event/EventAnalyticsService.java
dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/event/EventQueryParams.java
dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/event/data/DefaultEventAnalyticsService.java
dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/event/data/JdbcEventAnalyticsManager.java
dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/EventAnalyticsController.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-analytics/src/main/java/org/hisp/dhis/analytics/event/EventAnalyticsService.java'
--- dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/event/EventAnalyticsService.java 2013-08-22 15:05:17 +0000
+++ dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/event/EventAnalyticsService.java 2013-08-22 16:03:12 +0000
@@ -38,5 +38,6 @@
{
Grid getEvents( EventQueryParams params );
- EventQueryParams getFromUrl( String program, String stage, String startDate, String endDate, String ou, Set<String> item, Integer page, Integer pageSize );
+ EventQueryParams getFromUrl( String program, String stage, String startDate, String endDate, String ou,
+ Set<String> item, Set<String> asc, Set<String> desc, Integer page, Integer pageSize );
}
=== modified file 'dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/event/EventQueryParams.java'
--- dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/event/EventQueryParams.java 2013-08-22 15:05:17 +0000
+++ dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/event/EventQueryParams.java 2013-08-22 16:03:12 +0000
@@ -50,6 +50,10 @@
private List<QueryItem> items = new ArrayList<QueryItem>();
+ private List<String> asc = new ArrayList<String>();
+
+ private List<String> desc = new ArrayList<String>();
+
private List<OrganisationUnit> organisationUnits = new ArrayList<OrganisationUnit>();
private String tableName;
@@ -73,6 +77,8 @@
this.startDate = params.getStartDate();
this.endDate = params.getEndDate();
this.items = new ArrayList<QueryItem>( params.getItems() );
+ this.asc = new ArrayList<String>( params.getAsc() );
+ this.desc = new ArrayList<String>( params.getDesc() );
this.organisationUnits = new ArrayList<OrganisationUnit>( params.getOrganisationUnits() );
this.tableName = params.getTableName();
this.page = params.getPage();
@@ -88,14 +94,29 @@
return organisationUnits != null && !organisationUnits.isEmpty();
}
+ public boolean isSorting()
+ {
+ return ( asc != null && !asc.isEmpty() ) || ( desc != null && !desc.isEmpty() );
+ }
+
+ public boolean isPaging()
+ {
+ return page != null || pageSize != null;
+ }
+
+ public int getPageWithDefault()
+ {
+ return page != null && page > 0 ? page : 1;
+ }
+
public int getPageSizeWithDefault()
{
- return pageSize != null && page > 0 ? pageSize : 50;
+ return pageSize != null && pageSize >= 0 ? pageSize : 50;
}
-
+
public int getOffset()
{
- return page != null && page > 0 ? ( ( page - 1 ) * getPageSizeWithDefault() ) : 0;
+ return ( getPageWithDefault() - 1 ) * getPageSizeWithDefault();
}
// -------------------------------------------------------------------------
@@ -152,6 +173,26 @@
this.items = items;
}
+ public List<String> getAsc()
+ {
+ return asc;
+ }
+
+ public void setAsc( List<String> asc )
+ {
+ this.asc = asc;
+ }
+
+ public List<String> getDesc()
+ {
+ return desc;
+ }
+
+ public void setDesc( List<String> desc )
+ {
+ this.desc = desc;
+ }
+
public List<OrganisationUnit> getOrganisationUnits()
{
return organisationUnits;
=== modified file 'dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/event/data/DefaultEventAnalyticsService.java'
--- dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/event/data/DefaultEventAnalyticsService.java 2013-08-22 15:05:17 +0000
+++ dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/event/data/DefaultEventAnalyticsService.java 2013-08-22 16:03:12 +0000
@@ -62,7 +62,12 @@
*/
public class DefaultEventAnalyticsService
implements EventAnalyticsService
-{
+{
+ private static final String ITEM_EVENT = "psi";
+ private static final String ITEM_PROGRAM_STAGE = "ps";
+ private static final String ITEM_EXECUTION_DATE = "executiondate";
+ private static final String ITEM_ORG_UNIT = "ou";
+
@Autowired
private ProgramService programService;
@@ -96,10 +101,10 @@
{
Grid grid = new ListGrid();
- grid.addHeader( new GridHeader( "Event", "psi" ) );
- grid.addHeader( new GridHeader( "Program stage", "ps" ) );
- grid.addHeader( new GridHeader( "Execution date", "executiondate" ) );
- grid.addHeader( new GridHeader( "Organisation unit", "ou" ) );
+ grid.addHeader( new GridHeader( "Event", ITEM_EVENT ) );
+ grid.addHeader( new GridHeader( "Program stage", ITEM_PROGRAM_STAGE ) );
+ grid.addHeader( new GridHeader( "Execution date", ITEM_EXECUTION_DATE ) );
+ grid.addHeader( new GridHeader( "Organisation unit", ITEM_ORG_UNIT ) );
for ( QueryItem queryItem : params.getItems() )
{
@@ -118,7 +123,8 @@
return grid;
}
- public EventQueryParams getFromUrl( String program, String stage, String startDate, String endDate, String ou, Set<String> item, Integer page, Integer pageSize )
+ public EventQueryParams getFromUrl( String program, String stage, String startDate, String endDate, String ou,
+ Set<String> item, Set<String> asc, Set<String> desc, Integer page, Integer pageSize )
{
EventQueryParams params = new EventQueryParams();
@@ -181,6 +187,22 @@
}
}
+ if ( asc != null )
+ {
+ for ( String sort : asc )
+ {
+ params.getAsc().add( getSortItem( sort, pr ) );
+ }
+ }
+
+ if ( desc != null )
+ {
+ for ( String sort : desc )
+ {
+ params.getDesc().add( getSortItem( sort, pr ) );
+ }
+ }
+
if ( ou != null )
{
String[] split = ou.split( OPTION_SEP );
@@ -198,7 +220,7 @@
if ( page != null && page <= 0 )
{
- throw new IllegalQueryException( "Page must be positive: " + page );
+ throw new IllegalQueryException( "Page number must be positive: " + page );
}
if ( pageSize != null && pageSize < 0 )
@@ -220,6 +242,16 @@
return params;
}
+ private String getSortItem( String item, Program program )
+ {
+ if ( !ITEM_EXECUTION_DATE.equals( item ) && getItem( item, program ) == null )
+ {
+ throw new IllegalQueryException( "Descending sort item is invalid: " + item );
+ }
+
+ return item;
+ }
+
private IdentifiableObject getItem( String item, Program program )
{
DataElement de = dataElementService.getDataElement( item );
=== modified file 'dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/event/data/JdbcEventAnalyticsManager.java'
--- dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/event/data/JdbcEventAnalyticsManager.java 2013-08-22 15:05:17 +0000
+++ dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/event/data/JdbcEventAnalyticsManager.java 2013-08-22 16:03:12 +0000
@@ -99,17 +99,34 @@
}
// ---------------------------------------------------------------------
+ // Sorting
+ // ---------------------------------------------------------------------
+
+ if ( params.isSorting() )
+ {
+ sql += "order by ";
+
+ for ( String item : params.getAsc() )
+ {
+ sql += item + " asc,";
+ }
+
+ for ( String item : params.getDesc() )
+ {
+ sql += item + " desc,";
+ }
+
+ sql = removeLast( sql, 1 ) + " ";
+ }
+
+ // ---------------------------------------------------------------------
// Paging
// ---------------------------------------------------------------------
- if ( params.getPage() != null )
+ if ( params.isPaging() )
{
sql += "limit " + params.getPageSizeWithDefault() + " offset " + params.getOffset();
}
- else if ( params.getPageSize() != null )
- {
- sql += "limit " + params.getPageSizeWithDefault();
- }
// ---------------------------------------------------------------------
// Grid
=== modified file 'dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/EventAnalyticsController.java'
--- dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/EventAnalyticsController.java 2013-08-22 15:05:17 +0000
+++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/EventAnalyticsController.java 2013-08-22 16:03:12 +0000
@@ -69,12 +69,14 @@
@RequestParam String endDate,
@RequestParam(required=false) String ou,
@RequestParam Set<String> item,
+ @RequestParam(required=false) Set<String> asc,
+ @RequestParam(required=false) Set<String> desc,
@RequestParam(required=false) Integer page,
@RequestParam(required=false) Integer pageSize,
Model model,
HttpServletResponse response ) throws Exception
{
- EventQueryParams params = analyticsService.getFromUrl( program, stage, startDate, endDate, ou, item, page, pageSize );
+ EventQueryParams params = analyticsService.getFromUrl( program, stage, startDate, endDate, ou, item, asc, desc, page, pageSize );
contextUtils.configureResponse( response, ContextUtils.CONTENT_TYPE_JSON, CacheStrategy.RESPECT_SYSTEM_SETTING );
Grid grid = analyticsService.getEvents( params );