← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 17719: support pdf in web-api GET requests

 

------------------------------------------------------------
revno: 17719
committer: Morten Olav Hansen <mortenoh@xxxxxxxxx>
branch nick: dhis2
timestamp: Tue 2014-12-16 19:07:41 +0100
message:
  support pdf in web-api GET requests
modified:
  dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/node/serializers/PdfNodeSerializer.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-services/dhis-service-core/src/main/java/org/hisp/dhis/node/serializers/PdfNodeSerializer.java'
--- dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/node/serializers/PdfNodeSerializer.java	2014-12-16 15:45:59 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/node/serializers/PdfNodeSerializer.java	2014-12-16 18:07:41 +0000
@@ -29,11 +29,15 @@
  */
 
 import com.google.common.collect.Lists;
+import com.lowagie.text.Document;
+import com.lowagie.text.pdf.PdfPTable;
 import org.hisp.dhis.node.AbstractNodeSerializer;
+import org.hisp.dhis.node.Node;
 import org.hisp.dhis.node.types.CollectionNode;
 import org.hisp.dhis.node.types.ComplexNode;
 import org.hisp.dhis.node.types.RootNode;
 import org.hisp.dhis.node.types.SimpleNode;
+import org.hisp.dhis.system.util.PDFUtils;
 import org.springframework.context.annotation.Scope;
 import org.springframework.context.annotation.ScopedProxyMode;
 import org.springframework.stereotype.Component;
@@ -53,6 +57,8 @@
 {
     public static final String[] CONTENT_TYPES = { "application/pdf" };
 
+    private Document document;
+
     @Override
     public List<String> contentTypes()
     {
@@ -62,8 +68,16 @@
     @Override
     protected void startSerialize( RootNode rootNode, OutputStream outputStream ) throws Exception
     {
-        outputStream.write( "Hello PDF".getBytes() );
-        outputStream.flush();
+        document = PDFUtils.openDocument( outputStream );
+    }
+
+    @Override
+    protected void endSerialize( RootNode rootNode, OutputStream outputStream ) throws Exception
+    {
+        if ( document.isOpen() )
+        {
+            document.close();
+        }
     }
 
     @Override
@@ -75,18 +89,44 @@
     @Override
     protected void startWriteRootNode( RootNode rootNode ) throws Exception
     {
-
-    }
-
-    @Override
-    protected void endWriteRootNode( RootNode rootNode ) throws Exception
-    {
-
-    }
-
-    @Override
-    protected void startWriteSimpleNode( SimpleNode simpleNode ) throws Exception
-    {
+        for ( Node child : rootNode.getChildren() )
+        {
+            if ( child.isCollection() )
+            {
+                PdfPTable table = PDFUtils.getPdfPTable( true, 0.25f, 0.75f );
+                boolean haveContent = false;
+
+                for ( Node node : child.getChildren() )
+                {
+                    for ( Node property : node.getChildren() )
+                    {
+                        if ( property.isSimple() )
+                        {
+                            table.addCell( PDFUtils.getItalicCell( property.getName() ) );
+                            table.addCell( PDFUtils.getTextCell( getValue( (SimpleNode) property ) ) );
+                            haveContent = true;
+                        }
+                    }
+
+                    if ( haveContent )
+                    {
+                        table.addCell( PDFUtils.getEmptyCell( 2, 15 ) );
+                        haveContent = false;
+                    }
+                }
+
+                document.add( table );
+            }
+        }
+    }
+
+    public String getValue( SimpleNode simpleNode )
+    {
+        if ( simpleNode.getValue() == null )
+        {
+            return "";
+        }
+
         String value = String.format( "%s", simpleNode.getValue() );
 
         if ( Date.class.isAssignableFrom( simpleNode.getValue().getClass() ) )
@@ -95,6 +135,20 @@
             dateFormat.setTimeZone( TimeZone.getTimeZone( "UTC" ) );
             value = dateFormat.format( (Date) simpleNode.getValue() );
         }
+
+        return value;
+    }
+
+    @Override
+    protected void endWriteRootNode( RootNode rootNode ) throws Exception
+    {
+
+    }
+
+    @Override
+    protected void startWriteSimpleNode( SimpleNode simpleNode ) throws Exception
+    {
+
     }
 
     @Override
@@ -126,4 +180,10 @@
     {
 
     }
+
+    @Override
+    protected void dispatcher( Node node ) throws Exception
+    {
+
+    }
 }