← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 6203: added zip-export for meta-data (not finished)

 

------------------------------------------------------------
revno: 6203
committer: Morten Olav Hansen <mortenoh@xxxxxxxxx>
branch nick: dhis2
timestamp: Wed 2012-03-07 15:37:17 +0100
message:
  added zip-export for meta-data (not finished)
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 12:10:37 +0000
+++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/ExportController.java	2012-03-07 14:37:17 +0000
@@ -27,6 +27,8 @@
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
+import org.hisp.dhis.api.utils.ContextUtils;
+import org.hisp.dhis.api.view.Jaxb2Utils;
 import org.hisp.dhis.api.webdomain.DXF2;
 import org.hisp.dhis.dataelement.*;
 import org.hisp.dhis.dataset.DataSet;
@@ -46,13 +48,18 @@
 import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RequestMethod;
 
+import javax.servlet.http.HttpServletResponse;
+import javax.xml.bind.JAXBException;
+import java.io.IOException;
 import java.util.ArrayList;
+import java.util.zip.ZipEntry;
+import java.util.zip.ZipOutputStream;
 
 /**
  * @author Morten Olav Hansen <mortenoh@xxxxxxxxx>
  */
 @Controller
-@RequestMapping( value = ExportController.RESOURCE_PATH )
+// @RequestMapping( value = ExportController.RESOURCE_PATH )
 public class ExportController
 {
     public static final String RESOURCE_PATH = "/export";
@@ -78,7 +85,7 @@
     @Autowired
     private ValidationRuleService validationRuleService;
 
-    @RequestMapping( method = RequestMethod.GET )
+    @RequestMapping( value = "/export", method = RequestMethod.GET )
     @PreAuthorize( "hasRole('ALL') or hasRole('F_METADATA_EXPORT')" )
     public String export( Model model )
     {
@@ -110,4 +117,47 @@
 
         return "export";
     }
+
+    @RequestMapping( value = "/export.zip", method = RequestMethod.GET )
+    @PreAuthorize( "hasRole('ALL') or hasRole('F_METADATA_EXPORT')" )
+    public void exportZip( 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() ) );
+
+        response.setContentType( ContextUtils.CONTENT_TYPE_ZIP );
+        response.addHeader( "Content-Disposition", "attachment; filename=\"export.zip\"" );
+        response.addHeader( "Content-Transfer-Encoding", "binary" );
+
+        ZipOutputStream zip = new ZipOutputStream( response.getOutputStream() );
+        zip.putNextEntry( new ZipEntry( "export.xml" ) );
+
+        Jaxb2Utils.marshal( dxf2, zip );
+
+        zip.closeEntry();
+        zip.close();
+    }
 }