=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/organisationunit/OrganisationUnitService.java' --- dhis-2/dhis-api/src/main/java/org/hisp/dhis/organisationunit/OrganisationUnitService.java 2013-02-07 12:57:52 +0000 +++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/organisationunit/OrganisationUnitService.java 2013-05-20 06:54:42 +0000 @@ -229,6 +229,16 @@ /** * Returns an OrganisationUnit and all its children. * + * @param uid the uid of the parent OrganisationUnit in the subtree. + * @return a collection containing the OrganisationUnit with the given id + * and all its children, or an empty collection if no + * OrganisationUnits match. + */ + Collection getOrganisationUnitsWithChildren( String uid ); + + /** + * Returns an OrganisationUnit and all its children. + * * @param id the id of the parent OrganisationUnit in the subtree. * @return a collection containing the OrganisationUnit with the given id * and all its children, or an empty collection if no === modified file 'dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/organisationunit/DefaultOrganisationUnitService.java' --- dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/organisationunit/DefaultOrganisationUnitService.java 2013-05-14 09:13:00 +0000 +++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/organisationunit/DefaultOrganisationUnitService.java 2013-05-20 06:54:42 +0000 @@ -55,8 +55,6 @@ /** * @author Torgeir Lorange Ostby - * @version $Id: DefaultOrganisationUnitService.java 5951 2008-10-16 17:41:34Z - * larshelg $ */ @Transactional public class DefaultOrganisationUnitService @@ -272,6 +270,11 @@ } ); } + public Collection getOrganisationUnitsWithChildren( String uid ) + { + return getOrganisationUnitWithChildren( getOrganisationUnit( uid ).getId() ); + } + public Collection getOrganisationUnitWithChildren( int id ) { OrganisationUnit organisationUnit = getOrganisationUnit( id ); === modified file 'dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/DataValueSetController.java' --- dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/DataValueSetController.java 2013-05-20 06:10:29 +0000 +++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/DataValueSetController.java 2013-05-20 06:54:42 +0000 @@ -27,17 +27,34 @@ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ +import static org.hisp.dhis.api.utils.ContextUtils.CONTENT_TYPE_CSV; +import static org.hisp.dhis.api.utils.ContextUtils.CONTENT_TYPE_HTML; +import static org.hisp.dhis.api.utils.ContextUtils.CONTENT_TYPE_JSON; +import static org.hisp.dhis.api.utils.ContextUtils.CONTENT_TYPE_TEXT; +import static org.hisp.dhis.api.utils.ContextUtils.CONTENT_TYPE_XML; + +import java.io.IOException; +import java.io.InputStream; +import java.util.Date; +import java.util.HashSet; +import java.util.Set; + +import javax.servlet.http.HttpServletResponse; + import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.hisp.dhis.api.utils.ContextUtils; import org.hisp.dhis.api.webdomain.DataValueSets; +import org.hisp.dhis.common.IdentifiableObjectUtils; import org.hisp.dhis.dxf2.datavalueset.DataValueSet; import org.hisp.dhis.dxf2.datavalueset.DataValueSetService; import org.hisp.dhis.dxf2.importsummary.ImportSummary; import org.hisp.dhis.dxf2.metadata.ImportOptions; import org.hisp.dhis.dxf2.utils.JacksonUtils; import org.hisp.dhis.integration.IntegrationService; +import org.hisp.dhis.organisationunit.OrganisationUnitService; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.format.annotation.DateTimeFormat; import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; @@ -46,32 +63,28 @@ import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; -import javax.servlet.http.HttpServletResponse; -import java.io.IOException; -import java.io.InputStream; - -import static org.hisp.dhis.api.utils.ContextUtils.CONTENT_TYPE_JSON; -import static org.hisp.dhis.api.utils.ContextUtils.CONTENT_TYPE_XML; - @Controller @RequestMapping(value = DataValueSetController.RESOURCE_PATH) public class DataValueSetController { public static final String RESOURCE_PATH = "/dataValueSets"; - private static final Log LOG = LogFactory.getLog( DataValueSetController.class ); + private static final Log log = LogFactory.getLog( DataValueSetController.class ); @Autowired private DataValueSetService dataValueSetService; @Autowired private IntegrationService integrationService; + + @Autowired + private OrganisationUnitService organisationUnitService; // ------------------------------------------------------------------------- // Get // ------------------------------------------------------------------------- - @RequestMapping(method = RequestMethod.GET, produces = { "text/html", "text/plain" }) + @RequestMapping(method = RequestMethod.GET, produces = {CONTENT_TYPE_HTML, CONTENT_TYPE_TEXT}) public String getDataValueSets( Model model ) throws Exception { DataValueSets dataValueSets = new DataValueSets(); @@ -82,16 +95,72 @@ return "dataValueSets"; } - @RequestMapping(method = RequestMethod.GET, produces = "application/xml") - public void getDataValueSet( @RequestParam String dataSet, @RequestParam String period, - @RequestParam String orgUnit, HttpServletResponse response ) throws IOException + @RequestMapping(method = RequestMethod.GET, produces = CONTENT_TYPE_XML) + public void getDataValueSetXml( + @RequestParam Set dataSet, + @RequestParam(required=false) String period, + @RequestParam(required=false) @DateTimeFormat(pattern = "yyyy-MM-dd") Date startDate, + @RequestParam(required=false) @DateTimeFormat(pattern = "yyyy-MM-dd") Date endDate, + @RequestParam Set orgUnit, + @RequestParam(required=false) boolean children, + HttpServletResponse response ) throws IOException { - LOG.info( "Get data value set for data set: " + dataSet + ", period: " + period + ", org unit: " + orgUnit ); - response.setContentType( CONTENT_TYPE_XML ); - dataValueSetService.writeDataValueSet( dataSet, period, orgUnit, response.getOutputStream() ); - } - + + boolean isSingleDataValueSet = dataSet.size() == 1 && orgUnit.size() == 1; + + if ( isSingleDataValueSet ) + { + String ds = dataSet.iterator().next(); + String ou = orgUnit.iterator().next(); + + log.info( "Get XML data value set for data set: " + ds + ", period: " + period + ", org unit: " + ou ); + + dataValueSetService.writeDataValueSet( ds, period, ou, response.getOutputStream() ); + } + else + { + log.info( "Get XML bulk data value set for start date: " + startDate + ", end date: " + endDate ); + + Set ous = getOrganisationUnits( orgUnit, children ); + dataValueSetService.writeDataValueSet( dataSet, startDate, endDate, ous, response.getOutputStream() ); + } + } + + @RequestMapping(method = RequestMethod.GET, produces = CONTENT_TYPE_CSV) + public void getDataValuesCsv( + @RequestParam Set dataSet, + @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd") Date startDate, + @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd") Date endDate, + @RequestParam Set orgUnit, + @RequestParam(required=false) boolean children, + HttpServletResponse response ) throws IOException + { + log.info( "Get CSV bulk data value set for start date: " + startDate + ", end date: " + endDate ); + + Set ous = getOrganisationUnits( orgUnit, children ); + + response.setContentType( CONTENT_TYPE_CSV ); + dataValueSetService.writeDataValueSetCsv( dataSet, startDate, endDate, ous, response.getWriter() ); + } + + private Set getOrganisationUnits( Set orgUnits, boolean children ) + { + Set ous = new HashSet(); + + if ( children ) + { + for ( String ou : orgUnits ) + { + ous.addAll( IdentifiableObjectUtils.getUids( organisationUnitService.getOrganisationUnitsWithChildren( ou ) ) ); + } + } + + ous.addAll( orgUnits ); + + return ous; + } + // ------------------------------------------------------------------------- // Post // ------------------------------------------------------------------------- @@ -103,7 +172,7 @@ { ImportSummary summary = dataValueSetService.saveDataValueSet( in, importOptions ); - LOG.info( "Data values set saved " + importOptions ); + log.info( "Data values set saved " + importOptions ); response.setContentType( CONTENT_TYPE_XML ); JacksonUtils.toXml( response.getOutputStream(), summary ); @@ -116,7 +185,7 @@ { ImportSummary summary = dataValueSetService.saveDataValueSetJson( in, importOptions ); - LOG.info( "Data values set saved " + importOptions ); + log.info( "Data values set saved " + importOptions ); response.setContentType( CONTENT_TYPE_JSON ); JacksonUtils.toJson( response.getOutputStream(), summary ); @@ -129,7 +198,7 @@ { ImportSummary summary = integrationService.importSDMXDataValueSet( in, importOptions ); - LOG.info( "Data values set saved " + importOptions ); + log.info( "Data values set saved " + importOptions ); response.setContentType( CONTENT_TYPE_XML ); JacksonUtils.toXml( response.getOutputStream(), summary );