=== modified file 'dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/ExportController.java' --- dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/ExportController.java 2012-03-07 14:37:17 +0000 +++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/ExportController.java 2012-03-07 15:00:31 +0000 @@ -28,6 +28,7 @@ */ import org.hisp.dhis.api.utils.ContextUtils; +import org.hisp.dhis.api.view.JacksonUtils; import org.hisp.dhis.api.view.Jaxb2Utils; import org.hisp.dhis.api.webdomain.DXF2; import org.hisp.dhis.dataelement.*; @@ -59,7 +60,6 @@ * @author Morten Olav Hansen */ @Controller -// @RequestMapping( value = ExportController.RESOURCE_PATH ) public class ExportController { public static final String RESOURCE_PATH = "/export"; @@ -85,7 +85,7 @@ @Autowired private ValidationRuleService validationRuleService; - @RequestMapping( value = "/export", method = RequestMethod.GET ) + @RequestMapping( value = ExportController.RESOURCE_PATH, method = RequestMethod.GET ) @PreAuthorize( "hasRole('ALL') or hasRole('F_METADATA_EXPORT')" ) public String export( Model model ) { @@ -118,38 +118,14 @@ return "export"; } - @RequestMapping( value = "/export.zip", method = RequestMethod.GET ) + @RequestMapping( value = ExportController.RESOURCE_PATH + ".zip", method = RequestMethod.GET ) @PreAuthorize( "hasRole('ALL') or hasRole('F_METADATA_EXPORT')" ) - public void exportZip( HttpServletResponse response ) throws IOException, JAXBException + public void exportZippedXML( HttpServletResponse response ) throws IOException, JAXBException { - System.err.println( "EXPORT ZIP!" ); - - DXF2 dxf2 = new DXF2(); - - dxf2.setDataElements( new ArrayList( dataElementService.getAllDataElements() ) ); - dxf2.setDataElementGroups( new ArrayList( dataElementService.getAllDataElementGroups() ) ); - dxf2.setDataElementGroupSets( new ArrayList( dataElementService.getAllDataElementGroupSets() ) ); - dxf2.setCategories( new ArrayList( dataElementCategoryService.getAllDataElementCategories() ) ); - dxf2.setCategoryCombos( new ArrayList( dataElementCategoryService.getAllDataElementCategoryCombos() ) ); - dxf2.setCategoryOptions( new ArrayList( dataElementCategoryService.getAllDataElementCategoryOptions() ) ); - dxf2.setCategoryOptionCombos( new ArrayList( dataElementCategoryService.getAllDataElementCategoryOptionCombos() ) ); - - dxf2.setIndicators( new ArrayList( indicatorService.getAllIndicators() ) ); - dxf2.setIndicatorGroups( new ArrayList( indicatorService.getAllIndicatorGroups() ) ); - dxf2.setIndicatorGroupSets( new ArrayList( indicatorService.getAllIndicatorGroupSets() ) ); - - dxf2.setOrganisationUnits( new ArrayList( organisationUnitService.getAllOrganisationUnits() ) ); - dxf2.setOrganisationUnitLevels( new ArrayList( organisationUnitService.getOrganisationUnitLevels() ) ); - dxf2.setOrganisationUnitGroups( new ArrayList( organisationUnitGroupService.getAllOrganisationUnitGroups() ) ); - dxf2.setOrganisationUnitGroupSets( new ArrayList( organisationUnitGroupService.getAllOrganisationUnitGroupSets() ) ); - - dxf2.setDataSets( new ArrayList( dataSetService.getAllDataSets() ) ); - - dxf2.setValidationRules( new ArrayList( validationRuleService.getAllValidationRules() ) ); - dxf2.setValidationRuleGroups( new ArrayList( validationRuleService.getAllValidationRuleGroups() ) ); + DXF2 dxf2 = getExportObject(); response.setContentType( ContextUtils.CONTENT_TYPE_ZIP ); - response.addHeader( "Content-Disposition", "attachment; filename=\"export.zip\"" ); + response.addHeader( "Content-Disposition", "attachment; filename=\"export.xml.zip\"" ); response.addHeader( "Content-Transfer-Encoding", "binary" ); ZipOutputStream zip = new ZipOutputStream( response.getOutputStream() ); @@ -160,4 +136,56 @@ zip.closeEntry(); zip.close(); } + + @RequestMapping( value = ExportController.RESOURCE_PATH + ".zip", method = RequestMethod.GET, headers = {"Accept=application/json"} ) + @PreAuthorize( "hasRole('ALL') or hasRole('F_METADATA_EXPORT')" ) + public void exportZippedJSON( HttpServletResponse response ) throws IOException, JAXBException + { + DXF2 dxf2 = getExportObject(); + + response.setContentType( ContextUtils.CONTENT_TYPE_ZIP ); + response.addHeader( "Content-Disposition", "attachment; filename=\"export.json.zip\"" ); + response.addHeader( "Content-Transfer-Encoding", "binary" ); + + ZipOutputStream zip = new ZipOutputStream( response.getOutputStream() ); + zip.putNextEntry( new ZipEntry( "export.xml" ) ); + + JacksonUtils.writeObject( dxf2, zip ); + + zip.closeEntry(); + zip.close(); + } + + //------------------------------------------------------------------------------------------------------- + // Helpers + //------------------------------------------------------------------------------------------------------- + + private DXF2 getExportObject() + { + DXF2 dxf2 = new DXF2(); + + dxf2.setDataElements( new ArrayList( dataElementService.getAllDataElements() ) ); + dxf2.setDataElementGroups( new ArrayList( dataElementService.getAllDataElementGroups() ) ); + dxf2.setDataElementGroupSets( new ArrayList( dataElementService.getAllDataElementGroupSets() ) ); + dxf2.setCategories( new ArrayList( dataElementCategoryService.getAllDataElementCategories() ) ); + dxf2.setCategoryCombos( new ArrayList( dataElementCategoryService.getAllDataElementCategoryCombos() ) ); + dxf2.setCategoryOptions( new ArrayList( dataElementCategoryService.getAllDataElementCategoryOptions() ) ); + dxf2.setCategoryOptionCombos( new ArrayList( dataElementCategoryService.getAllDataElementCategoryOptionCombos() ) ); + + dxf2.setIndicators( new ArrayList( indicatorService.getAllIndicators() ) ); + dxf2.setIndicatorGroups( new ArrayList( indicatorService.getAllIndicatorGroups() ) ); + dxf2.setIndicatorGroupSets( new ArrayList( indicatorService.getAllIndicatorGroupSets() ) ); + + dxf2.setOrganisationUnits( new ArrayList( organisationUnitService.getAllOrganisationUnits() ) ); + dxf2.setOrganisationUnitLevels( new ArrayList( organisationUnitService.getOrganisationUnitLevels() ) ); + dxf2.setOrganisationUnitGroups( new ArrayList( organisationUnitGroupService.getAllOrganisationUnitGroups() ) ); + dxf2.setOrganisationUnitGroupSets( new ArrayList( organisationUnitGroupService.getAllOrganisationUnitGroupSets() ) ); + + dxf2.setDataSets( new ArrayList( dataSetService.getAllDataSets() ) ); + + dxf2.setValidationRules( new ArrayList( validationRuleService.getAllValidationRules() ) ); + dxf2.setValidationRuleGroups( new ArrayList( validationRuleService.getAllValidationRuleGroups() ) ); + + return dxf2; + } }