=== modified file 'dhis-2/dhis-web/dhis-web-api-fred/src/main/java/org/hisp/dhis/web/webapi/v1/controller/FacilityController.java' --- dhis-2/dhis-web/dhis-web-api-fred/src/main/java/org/hisp/dhis/web/webapi/v1/controller/FacilityController.java 2013-11-13 13:22:38 +0000 +++ dhis-2/dhis-web/dhis-web-api-fred/src/main/java/org/hisp/dhis/web/webapi/v1/controller/FacilityController.java 2013-11-19 16:48:11 +0000 @@ -83,6 +83,7 @@ import java.util.Collections; import java.util.Date; import java.util.HashMap; +import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Set; @@ -93,9 +94,9 @@ /** * @author Morten Olav Hansen */ -@Controller(value = "facility-controller-" + FredController.PREFIX) -@RequestMapping(FacilityController.RESOURCE_PATH) -@PreAuthorize("hasRole('M_dhis-web-api-fred') or hasRole('ALL')") +@Controller( value = "facility-controller-" + FredController.PREFIX ) +@RequestMapping( FacilityController.RESOURCE_PATH ) +@PreAuthorize( "hasRole('M_dhis-web-api-fred') or hasRole('ALL')" ) public class FacilityController { public static final String RESOURCE_PATH = "/" + FredController.PREFIX + "/facilities"; @@ -244,14 +245,17 @@ return facility; } - @RequestMapping(value = "", method = RequestMethod.GET) - @ResponseStatus(HttpStatus.OK) + @RequestMapping( value = "", method = RequestMethod.GET ) + @ResponseStatus( HttpStatus.OK ) public void readFacilities( - @RequestParam(value = "updatedSince", required = false) Date lastUpdated, - @RequestParam(value = "allProperties", required = false, defaultValue = "true") Boolean allProperties, - @RequestParam(value = "fields", required = false) String fields, - @RequestParam(value = "limit", required = false, defaultValue = "25") String limit, - @RequestParam(value = "offset", required = false, defaultValue = "0") Integer offset, + @RequestParam( value = "updatedSince", required = false ) Date lastUpdated, + @RequestParam( value = "allProperties", required = false, defaultValue = "true" ) Boolean allProperties, + @RequestParam( value = "fields", required = false ) String fields, + @RequestParam( value = "limit", required = false, defaultValue = "25" ) String limit, + @RequestParam( value = "offset", required = false, defaultValue = "0" ) Integer offset, + @RequestParam( value = "active", required = false ) List activeList, + @RequestParam( value = "name", required = false ) List nameList, + @RequestParam( value = "uuid", required = false ) List uuidList, HttpServletRequest request, HttpServletResponse response ) throws IOException { Facilities facilities = new Facilities(); @@ -266,28 +270,19 @@ if ( lastUpdated == null ) { - if ( limitValue != null ) - { - allOrganisationUnits = new ArrayList( organisationUnitService.getOrganisationUnitsBetween( offset, limitValue ) ); - } - else - { - allOrganisationUnits = new ArrayList( organisationUnitService.getAllOrganisationUnits() ); - } + allOrganisationUnits = new ArrayList( organisationUnitService.getAllOrganisationUnits() ); } else { - if ( limitValue != null ) - { - allOrganisationUnits = new ArrayList( organisationUnitService. - getOrganisationUnitsBetweenByLastUpdated( lastUpdated, offset, limitValue ) ); - } - else - { - allOrganisationUnits = new ArrayList( organisationUnitService.getAllOrganisationUnitsByLastUpdated( lastUpdated ) ); - } + allOrganisationUnits = new ArrayList( organisationUnitService.getAllOrganisationUnitsByLastUpdated( lastUpdated ) ); } + filterByActiveList( activeList, allOrganisationUnits ); + filterByNameList( nameList, allOrganisationUnits ); + filterByUuidList( uuidList, allOrganisationUnits ); + + filterByLimit( offset, limitValue, allOrganisationUnits ); + facilities.getMeta().put( "limit", limitValue ); facilities.getMeta().put( "offset", offset ); facilities.getMeta().put( "total", organisationUnitService.getNumberOfOrganisationUnits() ); @@ -312,6 +307,122 @@ objectMapper.writeValue( response.getOutputStream(), facilities ); } + private void filterByLimit( Integer offset, Integer limitValue, List allOrganisationUnits ) + { + if ( offset < 0 ) + { + offset = 0; + } + + if ( offset > allOrganisationUnits.size() ) + { + offset = allOrganisationUnits.size(); + } + + if ( (offset + limitValue) > allOrganisationUnits.size() ) + { + limitValue = allOrganisationUnits.size() - offset; + } + + List organisationUnits = new ArrayList( allOrganisationUnits.subList( offset, offset + limitValue ) ); + allOrganisationUnits.clear(); + allOrganisationUnits.addAll( organisationUnits ); + } + + private void filterByUuidList( List uuidList, List allOrganisationUnits ) + { + if ( uuidList == null || uuidList.isEmpty() ) + { + return; + } + + Iterator organisationUnitIterator = allOrganisationUnits.iterator(); + + while ( organisationUnitIterator.hasNext() ) + { + OrganisationUnit organisationUnit = organisationUnitIterator.next(); + + boolean shouldRemove = true; + + for ( String uuid : uuidList ) + { + if ( organisationUnit.getUuid().equals( uuid ) ) + { + shouldRemove = false; + break; + } + } + + if ( shouldRemove ) + { + organisationUnitIterator.remove(); + } + } + } + + private void filterByNameList( List nameList, List allOrganisationUnits ) + { + if ( nameList == null || nameList.isEmpty() ) + { + return; + } + + Iterator organisationUnitIterator = allOrganisationUnits.iterator(); + + while ( organisationUnitIterator.hasNext() ) + { + OrganisationUnit organisationUnit = organisationUnitIterator.next(); + + boolean shouldRemove = true; + + for ( String name : nameList ) + { + if ( organisationUnit.getName().contains( name ) ) + { + shouldRemove = false; + break; + } + } + + if ( shouldRemove ) + { + organisationUnitIterator.remove(); + } + } + } + + private void filterByActiveList( List activeList, List allOrganisationUnits ) + { + if ( activeList == null || activeList.isEmpty() ) + { + return; + } + + Iterator organisationUnitIterator = allOrganisationUnits.iterator(); + + while ( organisationUnitIterator.hasNext() ) + { + OrganisationUnit organisationUnit = organisationUnitIterator.next(); + + boolean shouldRemove = true; + + // see if it matches at least one + for ( Boolean active : activeList ) + { + if ( organisationUnit.isActive() == active ) + { + shouldRemove = false; + break; + } + } + + if ( shouldRemove ) + { + organisationUnitIterator.remove(); + } + } + } + private Integer getLimitValue( String limit, int defaultValue ) { Integer limitValue; @@ -325,6 +436,11 @@ try { limitValue = Integer.parseInt( limit ); + + if ( limitValue < 0 ) + { + limitValue = 0; + } } catch ( NumberFormatException ignored ) { @@ -335,11 +451,11 @@ return limitValue; } - @RequestMapping(value = "/{id}", method = RequestMethod.GET) - @ResponseStatus(HttpStatus.OK) + @RequestMapping( value = "/{id}", method = RequestMethod.GET ) + @ResponseStatus( HttpStatus.OK ) public void readFacility( @PathVariable String id, - @RequestParam(value = "allProperties", required = false, defaultValue = "true") Boolean allProperties, - @RequestParam(value = "fields", required = false) String fields, + @RequestParam( value = "allProperties", required = false, defaultValue = "true" ) Boolean allProperties, + @RequestParam( value = "fields", required = false ) String fields, HttpServletRequest request, HttpServletResponse response ) throws FacilityNotFoundException, IOException { OrganisationUnit organisationUnit = getOrganisationUnit( id ); @@ -395,8 +511,8 @@ // POST JSON //-------------------------------------------------------------------------- - @RequestMapping(value = "", method = RequestMethod.POST) - @PreAuthorize("hasRole('F_FRED_CREATE') or hasRole('ALL')") + @RequestMapping( value = "", method = RequestMethod.POST ) + @PreAuthorize( "hasRole('F_FRED_CREATE') or hasRole('ALL')" ) public ResponseEntity createFacility( @RequestBody Facility facility ) throws Exception { if ( facility.getUuid() == null ) @@ -468,8 +584,8 @@ // PUT JSON //-------------------------------------------------------------------------- - @RequestMapping(value = "/{id}", method = RequestMethod.PUT, consumes = MediaType.APPLICATION_JSON_VALUE) - @PreAuthorize("hasRole('F_FRED_UPDATE') or hasRole('ALL')") + @RequestMapping( value = "/{id}", method = RequestMethod.PUT, consumes = MediaType.APPLICATION_JSON_VALUE ) + @PreAuthorize( "hasRole('F_FRED_UPDATE') or hasRole('ALL')" ) public ResponseEntity updateFacility( @PathVariable String id, @RequestBody Facility facility, HttpServletRequest request ) throws Exception { HttpHeaders headers = new HttpHeaders(); @@ -565,8 +681,8 @@ // DELETE JSON //-------------------------------------------------------------------------- - @RequestMapping(value = "/{id}", method = RequestMethod.DELETE, produces = MediaType.APPLICATION_JSON_VALUE) - @PreAuthorize("hasRole('F_FRED_DELETE') or hasRole('ALL')") + @RequestMapping( value = "/{id}", method = RequestMethod.DELETE, produces = MediaType.APPLICATION_JSON_VALUE ) + @PreAuthorize( "hasRole('F_FRED_DELETE') or hasRole('ALL')" ) public ResponseEntity deleteFacility( @PathVariable String id ) throws HierarchyViolationException, IOException, FacilityNotFoundException { OrganisationUnit organisationUnit = getOrganisationUnit( id );