dhis2-devs team mailing list archive
-
dhis2-devs team
-
Mailing list archive
-
Message #20376
[Branch ~dhis2-devs-core/dhis2/trunk] Rev 9269: (local vn) Auto generate data elements, dataset, excel report and excel items from excel file.
------------------------------------------------------------
revno: 9269
committer: Tran Chau <tran.hispvietnam@xxxxxxxxx>
branch nick: dhis2
timestamp: Tue 2012-12-11 22:34:39 +0700
message:
(local vn) Auto generate data elements, dataset, excel report and excel items from excel file.
modified:
local/vn/dhis-web-spreadsheet-reporting/src/main/java/org/hisp/dhis/reportsheet/preview/action/AutoGenerateFormByTemplate.java
local/vn/dhis-web-spreadsheet-reporting/src/main/java/org/hisp/dhis/reportsheet/preview/action/TestAction.java
local/vn/dhis-web-spreadsheet-reporting/src/main/resources/META-INF/dhis/beans.xml
local/vn/dhis-web-spreadsheet-reporting/src/main/resources/struts.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 'local/vn/dhis-web-spreadsheet-reporting/src/main/java/org/hisp/dhis/reportsheet/preview/action/AutoGenerateFormByTemplate.java'
--- local/vn/dhis-web-spreadsheet-reporting/src/main/java/org/hisp/dhis/reportsheet/preview/action/AutoGenerateFormByTemplate.java 2012-12-11 10:01:41 +0000
+++ local/vn/dhis-web-spreadsheet-reporting/src/main/java/org/hisp/dhis/reportsheet/preview/action/AutoGenerateFormByTemplate.java 2012-12-11 15:34:39 +0000
@@ -29,10 +29,11 @@
import static org.apache.commons.io.FilenameUtils.getExtension;
-import java.io.BufferedWriter;
+import java.io.File;
import java.io.FileInputStream;
-import java.io.FileWriter;
+import java.io.InputStream;
import java.util.Collection;
+import java.util.HashSet;
import java.util.Set;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
@@ -42,6 +43,22 @@
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
+import org.hisp.dhis.dataelement.DataElement;
+import org.hisp.dhis.dataelement.DataElementCategoryCombo;
+import org.hisp.dhis.dataelement.DataElementCategoryService;
+import org.hisp.dhis.dataelement.DataElementService;
+import org.hisp.dhis.dataentryform.DataEntryForm;
+import org.hisp.dhis.dataentryform.DataEntryFormService;
+import org.hisp.dhis.dataset.DataSet;
+import org.hisp.dhis.dataset.DataSetService;
+import org.hisp.dhis.period.PeriodType;
+import org.hisp.dhis.reportsheet.ExportItem;
+import org.hisp.dhis.reportsheet.ExportReport;
+import org.hisp.dhis.reportsheet.ExportReportNormal;
+import org.hisp.dhis.reportsheet.ExportReportService;
+import org.springframework.beans.factory.annotation.Autowired;
+
+import com.opensymphony.xwork2.Action;
/**
* Simple demo class which uses the api to present the contents of an excel 97
@@ -52,27 +69,49 @@
* @version $Id$
*/
-public class AutoGenerateFormByTemplate
+public class AutoGenerateFormByTemplate implements Action
{
- /**
- * The encoding to write
- */
- private StringBuffer xml = new StringBuffer( 200000 );
+ @Autowired
+ private DataElementService dataElementService;
+
+ @Autowired
+ private DataElementCategoryService categoryService;
+
+ @Autowired
+ private DataEntryFormService dataEntryFormService;
+
+ @Autowired
+ private DataSetService dataSetService;
+
+ @Autowired
+ private ExportReportService exportReportService;
/**
* The workbook we are reading from a given file
*/
private Workbook WORKBOOK;
+ public String execute()
+ {
+ Set<Integer> collectSheets = new HashSet<Integer>();
+ collectSheets.add( 1 );
+
+ try
+ {
+ autoGenerateFormByTemplate( "d:\\template_file.xls", collectSheets );
+ }
+ catch ( Exception e )
+ {
+ System.out.println( e.getMessage() );
+ }
+
+ return SUCCESS;
+ }
+
// -------------------------------------------------------------------------
// Get & Set methods
// -------------------------------------------------------------------------
-
- protected String getXml()
- {
- return xml.toString();
- }
-
+
private void cleanUpForResponse()
{
System.gc();
@@ -89,20 +128,23 @@
* @exception java.io.IOException
*/
- public AutoGenerateFormByTemplate( String pathFileName, Set<Integer> collectSheets )
+ private String excelFileName = "";
+
+ private void autoGenerateFormByTemplate( String pathFileName, Set<Integer> collectSheets )
throws Exception
{
this.cleanUpForResponse();
- System.out.println( "\npathFileName : " + pathFileName );
+ InputStream inputSteam = new FileInputStream( pathFileName );
+ excelFileName = new File( pathFileName ).getName();
if ( getExtension( pathFileName ).equals( "xls" ) )
{
- this.WORKBOOK = new HSSFWorkbook( new FileInputStream( pathFileName ) );
+ this.WORKBOOK = new HSSFWorkbook( inputSteam );
}
else
{
- this.WORKBOOK = new XSSFWorkbook( new FileInputStream( pathFileName ) );
+ this.WORKBOOK = new XSSFWorkbook( inputSteam );
}
writeFormattedXML( collectSheets );
@@ -121,7 +163,7 @@
{
for ( Integer sheet : collectSheets )
{
- createFormByComment2( sheet );
+ createFormByComment( sheet );
}
}
@@ -131,57 +173,88 @@
private void createFormByComment( int sheetNo )
{
- Sheet s = WORKBOOK.getSheetAt( sheetNo - 1 );
- Comment cmt = null;
- String content = null;
-
- for ( Row row : s )
- {
- for ( Cell cell : row )
- {
- {
- cmt = cell.getCellComment();
-
- if ( cmt != null )
- {
- content = cmt.getString().getString();
- }
- }
- }
- }
- }
-
- private void createFormByComment2( int sheetNo )
- {
- Sheet s = WORKBOOK.getSheetAt( sheetNo - 1 );
- Comment cmt = null;
-
- System.out.println( "\nsheet: " + s.getSheetName() );
-
- // Create file
- FileWriter fstream = null;
- BufferedWriter out = null;
+ DataElementCategoryCombo catagoryCombo = categoryService.getDefaultDataElementCategoryOptionCombo()
+ .getCategoryCombo();
+ int catagoryOptionComboId = categoryService.getDefaultDataElementCategoryOptionCombo().getId();
+ PeriodType periodType = PeriodType.getPeriodTypeByName( "Monthly" );
+ StringBuffer htmlCode = new StringBuffer();
+ Sheet s = WORKBOOK.getSheetAt( sheetNo - 1 );
+
+ DataSet dataSet = new DataSet( excelFileName, excelFileName, periodType );
+
+ // Generate report
+ ExportReport exportReport = new ExportReportNormal();
+ exportReport.setName( WORKBOOK.getSheetName( sheetNo - 1 ) );
+ exportReport.setExcelTemplateFile( excelFileName );
+ exportReport.setGroup( excelFileName );
+ exportReport.setCreatedBy( "DHIS-System" );
+ int reportId = exportReportService.addExportReport( exportReport );
+
try
{
- fstream = new FileWriter( "d:\\template_file.xls\\out.txt" );
- out = new BufferedWriter( fstream );
-
for ( Row row : s )
{
for ( Cell cell : row )
{
- cmt = cell.getCellComment();
+ Comment cmt = cell.getCellComment();
if ( cell.getCellComment() != null )
{
- out.write( "\n\n NOT_NULL comment: " + cell.getCellComment().getRow() + ","
- + cell.getCellComment().getColumn() );
- out.write( "\t value2: " + cell.getCellComment().getString() );
+ String deName = cell.getStringCellValue();
+ String[] indexes = cmt.getString().toString().split( "," );
+ int rowIndex = cell.getRowIndex();
+
+ for ( String index : indexes )
+ {
+ String name = deName + "(" + index + ")";
+ int idx = Integer.parseInt( index ) - 1;
+
+ // Generate dataElement
+ DataElement dataElement = new DataElement( name );
+ dataElement.setShortName( name );
+ dataElement.setActive( true );
+ dataElement.setDomainType( "aggregate" );
+ dataElement.setType( DataElement.VALUE_TYPE_INT );
+ dataElement.setNumberType( DataElement.VALUE_TYPE_INT );
+ dataElement.setCategoryCombo( catagoryCombo );
+ dataElement.setAggregationOperator( "sum" );
+ dataElement.setZeroIsSignificant( false );
+ int deId = dataElementService.addDataElement( dataElement );
+
+ // Add the dataelement into the dataset
+ dataSet.addDataElement( dataElement );
+
+ // Put text field into the cell(rowIndex,idx)
+ // htmlCode.append( str );
+
+ // Generate report item
+ ExportItem exportItem = new ExportItem();
+ exportItem.setName( name );
+ exportItem.setItemType( "dataelement" );
+ exportItem.setRow( rowIndex );
+ exportItem.setColumn( idx );
+ exportItem.setExpression( "[" + deId + "." + catagoryOptionComboId + "]" );
+ exportItem.setPeriodType( "selected_month" );
+ exportItem.setSheetNo( (sheetNo) );
+ exportItem.setExportReport( exportReportService.getExportReport( reportId ) );
+ exportReportService.addExportItem( exportItem );
+
+ }
}
}
}
- // Close the output stream
- out.close();
+ DataEntryForm dataEntryForm = new DataEntryForm( "DataEntry form", htmlCode.toString() );
+ dataEntryFormService.addDataEntryForm( dataEntryForm );
+
+ dataSet.setDataEntryForm( dataEntryForm );
+ dataSetService.addDataSet( dataSet );
+
+ Set<DataSet> dataSets = new HashSet<DataSet>();
+ dataSets.add( dataSet );
+
+ exportReport.setDataSets( dataSets );
+ exportReportService.updateExportReport( exportReport );
+
}
catch ( Exception e )
{
=== modified file 'local/vn/dhis-web-spreadsheet-reporting/src/main/java/org/hisp/dhis/reportsheet/preview/action/TestAction.java'
--- local/vn/dhis-web-spreadsheet-reporting/src/main/java/org/hisp/dhis/reportsheet/preview/action/TestAction.java 2012-12-11 10:01:41 +0000
+++ local/vn/dhis-web-spreadsheet-reporting/src/main/java/org/hisp/dhis/reportsheet/preview/action/TestAction.java 2012-12-11 15:34:39 +0000
@@ -51,7 +51,7 @@
try
{
- new AutoGenerateFormByTemplate( "d:\\template_file.xls", collectSheets );
+// new AutoGenerateFormByTemplate( "d:\\template_file.xls", collectSheets );
}
catch ( Exception e )
{
=== modified file 'local/vn/dhis-web-spreadsheet-reporting/src/main/resources/META-INF/dhis/beans.xml'
--- local/vn/dhis-web-spreadsheet-reporting/src/main/resources/META-INF/dhis/beans.xml 2012-12-11 10:01:41 +0000
+++ local/vn/dhis-web-spreadsheet-reporting/src/main/resources/META-INF/dhis/beans.xml 2012-12-11 15:34:39 +0000
@@ -1532,4 +1532,9 @@
class="org.hisp.dhis.reportsheet.preview.action.TestAction"
scope="prototype" />
+ <bean id="org.hisp.dhis.reportsheet.preview.action.AutoGenerateFormByTemplate"
+ class="org.hisp.dhis.reportsheet.preview.action.AutoGenerateFormByTemplate"
+ scope="prototype">
+ </bean>
+
</beans>
=== modified file 'local/vn/dhis-web-spreadsheet-reporting/src/main/resources/struts.xml'
--- local/vn/dhis-web-spreadsheet-reporting/src/main/resources/struts.xml 2012-12-11 10:01:41 +0000
+++ local/vn/dhis-web-spreadsheet-reporting/src/main/resources/struts.xml 2012-12-11 15:34:39 +0000
@@ -1848,5 +1848,13 @@
<result name="error" type="velocity-json">../dhis-web-commons/ajax/jsonResponseError.vm</result>
<param name="onExceptionReturn">plainTextError</param>
</action>
+
+ <action name="autoGenerateFormByTemplate"
+ class="org.hisp.dhis.reportsheet.preview.action.AutoGenerateFormByTemplate">
+ <result name="success" type="velocity-json">../dhis-web-commons/ajax/jsonResponseSuccess.vm</result>
+ <result name="error" type="velocity-json">../dhis-web-commons/ajax/jsonResponseError.vm</result>
+ <param name="onExceptionReturn">plainTextError</param>
+ </action>
+
</package>
</struts>
\ No newline at end of file