dhis2-devs team mailing list archive
-
dhis2-devs team
-
Mailing list archive
-
Message #34556
[Branch ~dhis2-devs-core/dhis2/trunk] Rev 17714: excel support in web-api
------------------------------------------------------------
revno: 17714
committer: Morten Olav Hansen <mortenoh@xxxxxxxxx>
branch nick: dhis2
timestamp: Tue 2014-12-16 18:00:41 +0100
message:
excel support in web-api
modified:
dhis-2/dhis-services/dhis-service-core/pom.xml
dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/node/serializers/ExcelNodeSerializer.java
dhis-2/dhis-web/dhis-web-api/src/main/resources/META-INF/dhis/servlet.xml
dhis-2/pom.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
=== modified file 'dhis-2/dhis-services/dhis-service-core/pom.xml'
--- dhis-2/dhis-services/dhis-service-core/pom.xml 2014-11-20 18:38:54 +0000
+++ dhis-2/dhis-services/dhis-service-core/pom.xml 2014-12-16 17:00:41 +0000
@@ -81,7 +81,15 @@
<groupId>org.apache.ant</groupId>
<artifactId>ant-compress</artifactId>
</dependency>
-
+ <dependency>
+ <groupId>org.apache.poi</groupId>
+ <artifactId>poi</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.poi</groupId>
+ <artifactId>poi-ooxml</artifactId>
+ </dependency>
+
<!-- SMS -->
<dependency>
=== modified file 'dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/node/serializers/ExcelNodeSerializer.java'
--- dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/node/serializers/ExcelNodeSerializer.java 2014-12-16 15:45:59 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/node/serializers/ExcelNodeSerializer.java 2014-12-16 17:00:41 +0000
@@ -29,7 +29,14 @@
*/
import com.google.common.collect.Lists;
+import org.apache.poi.xssf.usermodel.XSSFCell;
+import org.apache.poi.xssf.usermodel.XSSFCellStyle;
+import org.apache.poi.xssf.usermodel.XSSFFont;
+import org.apache.poi.xssf.usermodel.XSSFRow;
+import org.apache.poi.xssf.usermodel.XSSFSheet;
+import org.apache.poi.xssf.usermodel.XSSFWorkbook;
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;
@@ -59,11 +66,60 @@
return Lists.newArrayList( CONTENT_TYPES );
}
+ private XSSFWorkbook workbook;
+
+ private XSSFSheet sheet;
+
@Override
protected void startSerialize( RootNode rootNode, OutputStream outputStream ) throws Exception
{
- outputStream.write( "Hello Excel".getBytes() );
- outputStream.flush();
+ workbook = new XSSFWorkbook();
+ sheet = workbook.createSheet();
+
+ XSSFFont boldFont = workbook.createFont();
+ boldFont.setBold( true );
+
+ XSSFCellStyle boldCellStyle = workbook.createCellStyle();
+ boldCellStyle.setFont( boldFont );
+
+ // build schema
+ for ( Node child : rootNode.getChildren() )
+ {
+ if ( child.isCollection() )
+ {
+ if ( !child.getChildren().isEmpty() )
+ {
+ Node node = child.getChildren().get( 0 );
+
+ XSSFRow row = sheet.createRow( 0 );
+
+ int cellIdx = 0;
+
+ for ( Node property : node.getChildren() )
+ {
+ if ( property.isSimple() )
+ {
+ XSSFCell cell = row.createCell( cellIdx++ );
+ cell.setCellValue( property.getName() );
+ cell.setCellStyle( boldCellStyle );
+ }
+ }
+ }
+ }
+ }
+ }
+
+ @Override
+ protected void endSerialize( RootNode rootNode, OutputStream outputStream ) throws Exception
+ {
+ int columns = sheet.getRow( 0 ).getPhysicalNumberOfCells();
+
+ for ( int i = 0; i < columns; i++ )
+ {
+ sheet.autoSizeColumn( i );
+ }
+
+ workbook.write( outputStream );
}
@Override
@@ -75,18 +131,37 @@
@Override
protected void startWriteRootNode( RootNode rootNode ) throws Exception
{
-
- }
-
- @Override
- protected void endWriteRootNode( RootNode rootNode ) throws Exception
- {
-
- }
-
- @Override
- protected void startWriteSimpleNode( SimpleNode simpleNode ) throws Exception
- {
+ int rowIdx = 1;
+
+ for ( Node child : rootNode.getChildren() )
+ {
+ if ( child.isCollection() )
+ {
+ for ( Node node : child.getChildren() )
+ {
+ XSSFRow row = sheet.createRow( rowIdx++ );
+ int cellIdx = 0;
+
+ for ( Node property : node.getChildren() )
+ {
+ if ( property.isSimple() )
+ {
+ XSSFCell cell = row.createCell( cellIdx++ );
+ cell.setCellValue( getValue( (SimpleNode) property ) );
+ }
+ }
+ }
+ }
+ }
+ }
+
+ 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 +170,19 @@
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
=== 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 2014-12-16 15:45:59 +0000
+++ dhis-2/dhis-web/dhis-web-api/src/main/resources/META-INF/dhis/servlet.xml 2014-12-16 17:00:41 +0000
@@ -60,6 +60,7 @@
<entry key="png" value="image/png" />
<entry key="pdf" value="application/pdf" />
<entry key="xls" value="application/vnd.ms-excel" />
+ <entry key="xlsx" value="application/vnd.ms-excel" />
<entry key="csv" value="application/csv" />
<entry key="csv.gz" value="application/csv+gzip" />
<entry key="geojson" value="application/json+geojson" />
=== modified file 'dhis-2/pom.xml'
--- dhis-2/pom.xml 2014-12-16 10:10:23 +0000
+++ dhis-2/pom.xml 2014-12-16 17:00:41 +0000
@@ -550,6 +550,11 @@
<version>3.10.1</version>
</dependency>
<dependency>
+ <groupId>org.apache.poi</groupId>
+ <artifactId>poi-ooxml</artifactId>
+ <version>3.10.1</version>
+ </dependency>
+ <dependency>
<groupId>org.amplecode</groupId>
<artifactId>quick</artifactId>
<version>1.11</version>