← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 11461: render pdf/xls for web-api using Grid/GridUtils

 

------------------------------------------------------------
revno: 11461
committer: Morten Olav Hansen <mortenoh@xxxxxxxxx>
branch nick: dhis2
timestamp: Mon 2013-07-22 17:26:11 +0700
message:
  render pdf/xls for web-api using Grid/GridUtils
removed:
  dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/view/PdfView.java
added:
  dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/view/AbstractGridView.java
  dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/view/ExcelGridView.java
  dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/view/PdfGridView.java
modified:
  dhis-2/dhis-web/dhis-web-api/src/main/resources/META-INF/dhis/servlet.xml


--
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
=== added file 'dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/view/AbstractGridView.java'
--- dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/view/AbstractGridView.java	1970-01-01 00:00:00 +0000
+++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/view/AbstractGridView.java	2013-07-22 10:26:11 +0000
@@ -0,0 +1,111 @@
+package org.hisp.dhis.api.view;
+
+/*
+ * Copyright (c) 2004-2013, University of Oslo
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ * * Redistributions of source code must retain the above copyright notice, this
+ *   list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above copyright notice,
+ *   this list of conditions and the following disclaimer in the documentation
+ *   and/or other materials provided with the distribution.
+ * * Neither the name of the HISP project nor the names of its contributors may
+ *   be used to endorse or promote products derived from this software without
+ *   specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
+ * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
+ * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+import org.hisp.dhis.api.controller.WebMetaData;
+import org.hisp.dhis.common.Grid;
+import org.hisp.dhis.common.GridHeader;
+import org.hisp.dhis.common.IdentifiableObject;
+import org.hisp.dhis.system.grid.ListGrid;
+import org.hisp.dhis.system.util.PredicateUtils;
+import org.hisp.dhis.system.util.ReflectionUtils;
+import org.springframework.web.servlet.view.AbstractView;
+
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import java.io.OutputStream;
+import java.lang.reflect.Field;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.List;
+import java.util.Map;
+
+/**
+ * @author Morten Olav Hansen <mortenoh@xxxxxxxxx>
+ */
+public abstract class AbstractGridView extends AbstractView
+{
+    protected abstract void renderGrids( List<Grid> grids, OutputStream outputStream ) throws Exception;
+
+    @Override
+    protected void renderMergedOutputModel( Map<String, Object> model, HttpServletRequest request, HttpServletResponse response ) throws Exception
+    {
+        Object object = model.get( "model" );
+
+        List<Grid> grids = new ArrayList<Grid>();
+
+        if ( WebMetaData.class.isAssignableFrom( object.getClass() ) )
+        {
+            WebMetaData webMetaData = (WebMetaData) object;
+            Collection<Field> fields = ReflectionUtils.collectFields( WebMetaData.class, PredicateUtils.idObjectCollections );
+
+            for ( Field field : fields )
+            {
+                Collection<IdentifiableObject> identifiableObjects = ReflectionUtils.invokeGetterMethod( field.getName(), webMetaData );
+
+                if ( identifiableObjects.isEmpty() )
+                {
+                    continue;
+                }
+
+                Grid grid = new ListGrid();
+                grid.addHeader( new GridHeader( "UID", false, false ) );
+                grid.addHeader( new GridHeader( "Name", false, false ) );
+                grid.addHeader( new GridHeader( "Code", false, false ) );
+
+                for ( IdentifiableObject identifiableObject : identifiableObjects )
+                {
+                    grid.addRow();
+                    grid.addValue( identifiableObject.getUid() );
+                    grid.addValue( identifiableObject.getName() );
+                    grid.addValue( identifiableObject.getCode() );
+                }
+
+                grids.add( grid );
+            }
+        }
+        else
+        {
+            IdentifiableObject identifiableObject = (IdentifiableObject) object;
+
+            Grid grid = new ListGrid();
+            grid.addHeader( new GridHeader( "UID", false, false ) );
+            grid.addHeader( new GridHeader( "Name", false, false ) );
+            grid.addHeader( new GridHeader( "Code", false, false ) );
+
+            grid.addRow();
+            grid.addValue( identifiableObject.getUid() );
+            grid.addValue( identifiableObject.getName() );
+            grid.addValue( identifiableObject.getCode() );
+
+            grids.add( grid );
+        }
+
+        renderGrids( grids, response.getOutputStream() );
+    }
+}

=== added file 'dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/view/ExcelGridView.java'
--- dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/view/ExcelGridView.java	1970-01-01 00:00:00 +0000
+++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/view/ExcelGridView.java	2013-07-22 10:26:11 +0000
@@ -0,0 +1,52 @@
+package org.hisp.dhis.api.view;
+
+/*
+ * Copyright (c) 2004-2013, University of Oslo
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ * * Redistributions of source code must retain the above copyright notice, this
+ *   list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above copyright notice,
+ *   this list of conditions and the following disclaimer in the documentation
+ *   and/or other materials provided with the distribution.
+ * * Neither the name of the HISP project nor the names of its contributors may
+ *   be used to endorse or promote products derived from this software without
+ *   specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
+ * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
+ * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+import org.hisp.dhis.api.utils.ContextUtils;
+import org.hisp.dhis.common.Grid;
+import org.hisp.dhis.system.grid.GridUtils;
+
+import java.io.OutputStream;
+import java.util.List;
+
+/**
+ * @author Morten Olav Hansen <mortenoh@xxxxxxxxx>
+ */
+public class ExcelGridView extends AbstractGridView
+{
+    public ExcelGridView()
+    {
+        setContentType( ContextUtils.CONTENT_TYPE_EXCEL );
+    }
+
+    @Override
+    protected void renderGrids( List<Grid> grids, OutputStream outputStream ) throws Exception
+    {
+        GridUtils.toXls( grids, outputStream );
+    }
+}

=== added file 'dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/view/PdfGridView.java'
--- dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/view/PdfGridView.java	1970-01-01 00:00:00 +0000
+++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/view/PdfGridView.java	2013-07-22 10:26:11 +0000
@@ -0,0 +1,52 @@
+package org.hisp.dhis.api.view;
+
+/*
+ * Copyright (c) 2004-2013, University of Oslo
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ * * Redistributions of source code must retain the above copyright notice, this
+ *   list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above copyright notice,
+ *   this list of conditions and the following disclaimer in the documentation
+ *   and/or other materials provided with the distribution.
+ * * Neither the name of the HISP project nor the names of its contributors may
+ *   be used to endorse or promote products derived from this software without
+ *   specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
+ * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
+ * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+import org.hisp.dhis.api.utils.ContextUtils;
+import org.hisp.dhis.common.Grid;
+import org.hisp.dhis.system.grid.GridUtils;
+
+import java.io.OutputStream;
+import java.util.List;
+
+/**
+ * @author Morten Olav Hansen <mortenoh@xxxxxxxxx>
+ */
+public class PdfGridView extends AbstractGridView
+{
+    public PdfGridView()
+    {
+        setContentType( ContextUtils.CONTENT_TYPE_PDF );
+    }
+
+    @Override
+    protected void renderGrids( List<Grid> grids, OutputStream outputStream )
+    {
+        GridUtils.toPdf( grids, outputStream );
+    }
+}

=== removed file 'dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/view/PdfView.java'
--- dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/view/PdfView.java	2013-07-22 05:41:12 +0000
+++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/view/PdfView.java	1970-01-01 00:00:00 +0000
@@ -1,152 +0,0 @@
-package org.hisp.dhis.api.view;
-
-/*
- * Copyright (c) 2004-2013, University of Oslo
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are met:
- * * Redistributions of source code must retain the above copyright notice, this
- *   list of conditions and the following disclaimer.
- * * Redistributions in binary form must reproduce the above copyright notice,
- *   this list of conditions and the following disclaimer in the documentation
- *   and/or other materials provided with the distribution.
- * * Neither the name of the HISP project nor the names of its contributors may
- *   be used to endorse or promote products derived from this software without
- *   specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
- * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
- * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
- * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
- * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
- * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
- * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
- * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
- * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
-
-import com.lowagie.text.Document;
-import com.lowagie.text.DocumentException;
-import com.lowagie.text.Element;
-import com.lowagie.text.Font;
-import com.lowagie.text.FontFactory;
-import com.lowagie.text.Phrase;
-import com.lowagie.text.pdf.PdfPCell;
-import com.lowagie.text.pdf.PdfPTable;
-import com.lowagie.text.pdf.PdfWriter;
-import org.hisp.dhis.api.controller.WebMetaData;
-import org.hisp.dhis.common.IdentifiableObject;
-import org.hisp.dhis.system.util.PredicateUtils;
-import org.hisp.dhis.system.util.ReflectionUtils;
-import org.springframework.web.servlet.view.document.AbstractPdfView;
-
-import javax.servlet.http.HttpServletRequest;
-import javax.servlet.http.HttpServletResponse;
-import java.lang.reflect.Field;
-import java.util.Collection;
-import java.util.Map;
-
-/**
- * @author Morten Olav Hansen <mortenoh@xxxxxxxxx>
- */
-public class PdfView extends AbstractPdfView
-{
-    private Font boldFont = FontFactory.getFont( FontFactory.HELVETICA_BOLD );
-
-    @Override
-    protected void buildPdfDocument( Map<String, Object> model, Document document, PdfWriter writer,
-        HttpServletRequest request, HttpServletResponse response ) throws DocumentException
-    {
-        Object object = model.get( "model" );
-
-        if ( WebMetaData.class.isAssignableFrom( object.getClass() ) )
-        {
-            WebMetaData webMetaData = (WebMetaData) object;
-            buildList( webMetaData, document );
-        }
-        else
-        {
-            IdentifiableObject identifiableObject = (IdentifiableObject) object;
-            buildSingleObject( identifiableObject, document );
-        }
-    }
-
-    private void buildList( WebMetaData webMetaData, Document document ) throws DocumentException
-    {
-        Collection<Field> fields = ReflectionUtils.collectFields( WebMetaData.class, PredicateUtils.idObjectCollections );
-
-        for ( Field field : fields )
-        {
-            Collection<IdentifiableObject> col = ReflectionUtils.invokeGetterMethod( field.getName(), webMetaData );
-
-            if ( col.isEmpty() )
-            {
-                continue;
-            }
-
-            PdfPTable table = new PdfPTable( 2 );
-            table.setSpacingAfter( 10 );
-
-            renderIdentifiableObjectRowHeader( table );
-
-            for ( IdentifiableObject identifiableObject : col )
-            {
-                renderIdentifiableObjectRow( identifiableObject, table );
-            }
-
-            document.add( table );
-        }
-    }
-
-    private void buildSingleObject( IdentifiableObject identifiableObject, Document document ) throws DocumentException
-    {
-        PdfPTable table = new PdfPTable( 2 );
-        table.setSpacingAfter( 10 );
-        renderIdentifiableObject( identifiableObject, table );
-        document.add( table );
-    }
-
-    private void renderIdentifiableObject( IdentifiableObject identifiableObject, PdfPTable table )
-    {
-        table.addCell( new Phrase( "Name", boldFont ) );
-        table.addCell( identifiableObject.getDisplayName() );
-
-        table.addCell( new Phrase( "Uid", boldFont ) );
-        table.addCell( identifiableObject.getUid() );
-
-        table.addCell( new Phrase( "Code", boldFont ) );
-        table.addCell( identifiableObject.getCode() );
-
-        table.addCell( new Phrase( "Created", boldFont ) );
-        table.addCell( identifiableObject.getCreated().toString() );
-
-        table.addCell( new Phrase( "LastUpdated", boldFont ) );
-        table.addCell( identifiableObject.getLastUpdated().toString() );
-    }
-
-    private void renderIdentifiableObjectRowHeader( PdfPTable table ) throws DocumentException
-    {
-        table.setWidths( new int[]{ 3, 1 } );
-
-        PdfPCell nameCell = new PdfPCell( new Phrase( "Name", boldFont ) );
-        nameCell.setHorizontalAlignment( Element.ALIGN_LEFT );
-        table.addCell( nameCell );
-
-        PdfPCell uidCell = new PdfPCell( new Phrase( "UID", boldFont ) );
-        uidCell.setHorizontalAlignment( Element.ALIGN_LEFT );
-        table.addCell( uidCell );
-    }
-
-    private void renderIdentifiableObjectRow( IdentifiableObject identifiableObject, PdfPTable table )
-    {
-        PdfPCell nameCell = new PdfPCell( new Phrase( identifiableObject.getDisplayName() ) );
-        nameCell.setHorizontalAlignment( Element.ALIGN_LEFT );
-        table.addCell( nameCell );
-
-        PdfPCell uidCell = new PdfPCell( new Phrase( identifiableObject.getUid() ) );
-        uidCell.setHorizontalAlignment( Element.ALIGN_RIGHT );
-        table.addCell( uidCell );
-    }
-}

=== modified file 'dhis-2/dhis-web/dhis-web-api/src/main/resources/META-INF/dhis/servlet.xml'
--- dhis-2/dhis-web/dhis-web-api/src/main/resources/META-INF/dhis/servlet.xml	2013-07-22 05:14:19 +0000
+++ dhis-2/dhis-web/dhis-web-api/src/main/resources/META-INF/dhis/servlet.xml	2013-07-22 10:26:11 +0000
@@ -34,6 +34,7 @@
         <entry key="xml" value="application/xml" />
         <entry key="png" value="image/png" />
         <entry key="pdf" value="application/pdf" />
+        <entry key="xls" value="application/vnd.ms-excel" />
       </map>
     </constructor-arg>
   </bean>
@@ -62,7 +63,9 @@
           <constructor-arg name="withPadding" value="true" />
         </bean>
 
-        <bean class="org.hisp.dhis.api.view.PdfView" />
+        <bean class="org.hisp.dhis.api.view.PdfGridView" />
+
+        <bean class="org.hisp.dhis.api.view.ExcelGridView" />
       </list>
     </property>