← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 6206: export of meta-data zip now works for xml and json

 

------------------------------------------------------------
revno: 6206
committer: Morten Olav Hansen <mortenoh@xxxxxxxxx>
branch nick: dhis2
timestamp: Wed 2012-03-07 16:00:31 +0100
message:
  export of meta-data zip now works for xml and json
modified:
  dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/ExportController.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-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 <mortenoh@xxxxxxxxx>
  */
 @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<DataElement>( dataElementService.getAllDataElements() ) );
-        dxf2.setDataElementGroups( new ArrayList<DataElementGroup>( dataElementService.getAllDataElementGroups() ) );
-        dxf2.setDataElementGroupSets( new ArrayList<DataElementGroupSet>( dataElementService.getAllDataElementGroupSets() ) );
-        dxf2.setCategories( new ArrayList<DataElementCategory>( dataElementCategoryService.getAllDataElementCategories() ) );
-        dxf2.setCategoryCombos( new ArrayList<DataElementCategoryCombo>( dataElementCategoryService.getAllDataElementCategoryCombos() ) );
-        dxf2.setCategoryOptions( new ArrayList<DataElementCategoryOption>( dataElementCategoryService.getAllDataElementCategoryOptions() ) );
-        dxf2.setCategoryOptionCombos( new ArrayList<DataElementCategoryOptionCombo>( dataElementCategoryService.getAllDataElementCategoryOptionCombos() ) );
-
-        dxf2.setIndicators( new ArrayList<Indicator>( indicatorService.getAllIndicators() ) );
-        dxf2.setIndicatorGroups( new ArrayList<IndicatorGroup>( indicatorService.getAllIndicatorGroups() ) );
-        dxf2.setIndicatorGroupSets( new ArrayList<IndicatorGroupSet>( indicatorService.getAllIndicatorGroupSets() ) );
-
-        dxf2.setOrganisationUnits( new ArrayList<OrganisationUnit>( organisationUnitService.getAllOrganisationUnits() ) );
-        dxf2.setOrganisationUnitLevels( new ArrayList<OrganisationUnitLevel>( organisationUnitService.getOrganisationUnitLevels() ) );
-        dxf2.setOrganisationUnitGroups( new ArrayList<OrganisationUnitGroup>( organisationUnitGroupService.getAllOrganisationUnitGroups() ) );
-        dxf2.setOrganisationUnitGroupSets( new ArrayList<OrganisationUnitGroupSet>( organisationUnitGroupService.getAllOrganisationUnitGroupSets() ) );
-
-        dxf2.setDataSets( new ArrayList<DataSet>( dataSetService.getAllDataSets() ) );
-
-        dxf2.setValidationRules( new ArrayList<ValidationRule>( validationRuleService.getAllValidationRules() ) );
-        dxf2.setValidationRuleGroups( new ArrayList<ValidationRuleGroup>( 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<DataElement>( dataElementService.getAllDataElements() ) );
+        dxf2.setDataElementGroups( new ArrayList<DataElementGroup>( dataElementService.getAllDataElementGroups() ) );
+        dxf2.setDataElementGroupSets( new ArrayList<DataElementGroupSet>( dataElementService.getAllDataElementGroupSets() ) );
+        dxf2.setCategories( new ArrayList<DataElementCategory>( dataElementCategoryService.getAllDataElementCategories() ) );
+        dxf2.setCategoryCombos( new ArrayList<DataElementCategoryCombo>( dataElementCategoryService.getAllDataElementCategoryCombos() ) );
+        dxf2.setCategoryOptions( new ArrayList<DataElementCategoryOption>( dataElementCategoryService.getAllDataElementCategoryOptions() ) );
+        dxf2.setCategoryOptionCombos( new ArrayList<DataElementCategoryOptionCombo>( dataElementCategoryService.getAllDataElementCategoryOptionCombos() ) );
+
+        dxf2.setIndicators( new ArrayList<Indicator>( indicatorService.getAllIndicators() ) );
+        dxf2.setIndicatorGroups( new ArrayList<IndicatorGroup>( indicatorService.getAllIndicatorGroups() ) );
+        dxf2.setIndicatorGroupSets( new ArrayList<IndicatorGroupSet>( indicatorService.getAllIndicatorGroupSets() ) );
+
+        dxf2.setOrganisationUnits( new ArrayList<OrganisationUnit>( organisationUnitService.getAllOrganisationUnits() ) );
+        dxf2.setOrganisationUnitLevels( new ArrayList<OrganisationUnitLevel>( organisationUnitService.getOrganisationUnitLevels() ) );
+        dxf2.setOrganisationUnitGroups( new ArrayList<OrganisationUnitGroup>( organisationUnitGroupService.getAllOrganisationUnitGroups() ) );
+        dxf2.setOrganisationUnitGroupSets( new ArrayList<OrganisationUnitGroupSet>( organisationUnitGroupService.getAllOrganisationUnitGroupSets() ) );
+
+        dxf2.setDataSets( new ArrayList<DataSet>( dataSetService.getAllDataSets() ) );
+
+        dxf2.setValidationRules( new ArrayList<ValidationRule>( validationRuleService.getAllValidationRules() ) );
+        dxf2.setValidationRuleGroups( new ArrayList<ValidationRuleGroup>( validationRuleService.getAllValidationRuleGroups() ) );
+
+        return dxf2;
+    }
 }