=== modified file 'local/vn/dhis-web-spreadsheet-reporting/src/main/java/org/hisp/dhis/reportsheet/filemanager/action/UploadExcelFileAction.java' --- local/vn/dhis-web-spreadsheet-reporting/src/main/java/org/hisp/dhis/reportsheet/filemanager/action/UploadExcelFileAction.java 2011-08-04 08:39:55 +0000 +++ local/vn/dhis-web-spreadsheet-reporting/src/main/java/org/hisp/dhis/reportsheet/filemanager/action/UploadExcelFileAction.java 2012-12-12 05:14:59 +0000 @@ -71,6 +71,8 @@ private boolean isDraft; + private boolean allowNewName = true; + // ------------------------------------------------------------------------- // Output // ------------------------------------------------------------------------- @@ -101,6 +103,11 @@ this.isDraft = isDraft; } + public void setAllowNewName( boolean allowNewName ) + { + this.allowNewName = allowNewName; + } + // ------------------------------------------------------------------------- // Action implementation // ------------------------------------------------------------------------- @@ -114,9 +121,18 @@ if ( isDraft ) { - directory = reportLocationManager.getExportReportTemporaryDirectory(); - - output = new File( directory, (Math.random() * 1000) + fileName ); + if ( allowNewName ) + { + directory = reportLocationManager.getExportReportTemporaryDirectory(); + + output = new File( directory, (Math.random() * 1000) + fileName ); + } + else + { + directory = reportLocationManager.getExportReportTemporaryDirectory(); + + output = new File( directory, fileName ); + } } else { @@ -149,5 +165,4 @@ return SUCCESS; } - } === 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-12 04:43:25 +0000 +++ local/vn/dhis-web-spreadsheet-reporting/src/main/java/org/hisp/dhis/reportsheet/preview/action/AutoGenerateFormByTemplate.java 2012-12-12 05:14:59 +0000 @@ -27,35 +27,53 @@ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ +import static org.apache.commons.io.FilenameUtils.getBaseName; import static org.apache.commons.io.FilenameUtils.getExtension; +import static org.apache.commons.io.FilenameUtils.getName; +import static org.hisp.dhis.dataelement.DataElement.AGGREGATION_OPERATOR_SUM; +import static org.hisp.dhis.dataelement.DataElement.DOMAIN_TYPE_AGGREGATE; +import static org.hisp.dhis.dataelement.DataElement.VALUE_TYPE_INT; +import static org.hisp.dhis.reportsheet.ExportItem.PERIODTYPE.SELECTED_MONTH; +import static org.hisp.dhis.reportsheet.ExportItem.TYPE.DATAELEMENT; +import static org.hisp.dhis.reportsheet.utils.ExcelUtils.convertAlignmentString; +import static org.hisp.dhis.reportsheet.utils.ExcelUtils.readValueByPOI; -import java.io.File; import java.io.FileInputStream; +import java.io.IOException; import java.io.InputStream; import java.util.Collection; +import java.util.HashMap; import java.util.HashSet; +import java.util.Map; import java.util.Set; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.ss.usermodel.Cell; +import org.apache.poi.ss.usermodel.CellStyle; import org.apache.poi.ss.usermodel.Comment; +import org.apache.poi.ss.usermodel.Font; +import org.apache.poi.ss.usermodel.FormulaEvaluator; +import org.apache.poi.ss.usermodel.IndexedColors; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Workbook; +import org.apache.poi.ss.util.CellRangeAddress; import org.apache.poi.xssf.usermodel.XSSFWorkbook; import org.hisp.dhis.dataelement.DataElement; -import org.hisp.dhis.dataelement.DataElementCategoryCombo; +import org.hisp.dhis.dataelement.DataElementCategoryOptionCombo; 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.MonthlyPeriodType; 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.hisp.dhis.reportsheet.state.SelectionManager; import org.springframework.beans.factory.annotation.Autowired; import com.opensymphony.xwork2.Action; @@ -69,8 +87,39 @@ * @version $Id$ */ -public class AutoGenerateFormByTemplate implements Action +public class AutoGenerateFormByTemplate + implements Action { + private static final String REPORT_EXCEL_GROUP = "BAO CAO THONG KE"; + + private static final String WORKBOOK_OPENTAG = ""; + + private static final String WORKBOOK_CLOSETAG = ""; + + private static final String MERGEDCELL_OPENTAG = ""; + + private static final String MERGEDCELL_CLOSETAG = ""; + + private String excelFileName = ""; + + private String commonName = ""; + + /** + * The workbook we are reading from a given file + */ + private Workbook WORKBOOK; + + private FormulaEvaluator evaluatorFormula; + + /** + * The encoding to write + */ + private StringBuffer xml = new StringBuffer( 200000 ); + + // ------------------------------------------------------------------------- + // Dependencies + // ------------------------------------------------------------------------- + @Autowired private DataElementService dataElementService; @@ -78,7 +127,7 @@ private DataElementCategoryService categoryService; @Autowired - private DataEntryFormService dataEntryFormService; + private DataEntryFormService dataEntryFormService; @Autowired private DataSetService dataSetService; @@ -86,10 +135,23 @@ @Autowired private ExportReportService exportReportService; - /** - * The workbook we are reading from a given file - */ - private Workbook WORKBOOK; + @Autowired + protected SelectionManager selectionManager; + + // ------------------------------------------------------------------------- + // Input && Output + // ------------------------------------------------------------------------- + + private String xmlStructureResponse; + + public String getXmlStructureResponse() + { + return xmlStructureResponse; + } + + // ------------------------------------------------------------------------- + // Action implementation + // ------------------------------------------------------------------------- public String execute() { @@ -98,7 +160,10 @@ try { - autoGenerateFormByTemplate( "d:\\template_file.xls", collectSheets ); + autoGenerateFormByTemplate( selectionManager.getUploadFilePath(), collectSheets ); + + xmlStructureResponse = xml.toString(); + xml = null; } catch ( Exception e ) { @@ -107,12 +172,12 @@ return SUCCESS; } - - // ------------------------------------------------------------------------- - // Get & Set methods - // ------------------------------------------------------------------------- - - private void cleanUpForResponse() + + // ------------------------------------------------------------------------- + // Sub-methods + // ------------------------------------------------------------------------- + + private void cleanUp() { System.gc(); } @@ -128,15 +193,14 @@ * @exception java.io.IOException */ - private String excelFileName = ""; - private void autoGenerateFormByTemplate( String pathFileName, Set collectSheets ) throws Exception { - this.cleanUpForResponse(); + this.cleanUp(); InputStream inputSteam = new FileInputStream( pathFileName ); - excelFileName = new File( pathFileName ).getName(); + excelFileName = getName( pathFileName ); + commonName = getBaseName( pathFileName ); if ( getExtension( pathFileName ).equals( "xls" ) ) { @@ -160,100 +224,129 @@ private void writeFormattedXML( Collection collectSheets ) throws Exception - { - for ( Integer sheet : collectSheets ) - { - createFormByComment( sheet ); - } + { + this.writeXMLMergedDescription( collectSheets ); + + xml.append( WORKBOOK_OPENTAG ); + + createFormByComment( 1, categoryService.getDefaultDataElementCategoryOptionCombo() ); + + xml.append( WORKBOOK_CLOSETAG ); } - // ------------------------------------------------------------------------- - // Sub-methods - // ------------------------------------------------------------------------- - - private void createFormByComment( int sheetNo ) + private void createFormByComment( int sheetNo, DataElementCategoryOptionCombo optionCombo ) { - DataElementCategoryCombo catagoryCombo = categoryService.getDefaultDataElementCategoryOptionCombo() - .getCategoryCombo(); - int catagoryOptionComboId = categoryService.getDefaultDataElementCategoryOptionCombo().getId(); - PeriodType periodType = PeriodType.getPeriodTypeByName( "Monthly" ); - StringBuffer htmlCode = new StringBuffer(); + PeriodType periodType = PeriodType.getPeriodTypeByName( MonthlyPeriodType.NAME ); + + // Create new DataSet + DataSet dataSet = new DataSet( commonName, commonName, periodType ); + + // Create new ExportReport + ExportReport exportReport = new ExportReportNormal( commonName, REPORT_EXCEL_GROUP, excelFileName, null ); + exportReportService.addExportReport( exportReport ); + 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 ); + xml.append( "" ); + xml.append( "" ); try { for ( Row row : s ) { + xml.append( "" ); + + Map idxMap = new HashMap(); + for ( Cell cell : row ) { Comment cmt = cell.getCellComment(); - if ( cell.getCellComment() != null ) + + if ( cmt != null ) { String deName = cell.getStringCellValue(); - String[] indexes = cmt.getString().toString().split( "," ); + String[] indexes = cmt.getString().getString().split( "," ); + int rowIndex = cell.getRowIndex(); for ( String index : indexes ) { - String name = deName + "(" + index + ")"; - int idx = Integer.parseInt( index ) - 1; - - // Generate dataElement + String name = deName + " (" + index + ")"; + int idx = Integer.parseInt( index ); + + // Generate DataElement DataElement dataElement = new DataElement( name ); - dataElement.setShortName( name ); + /** TAKE CARE OF SHORT_NAME IS TOO LONG */ + dataElement.setShortName( System.currentTimeMillis() + "" ); 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 ); + dataElement.setDomainType( DOMAIN_TYPE_AGGREGATE ); + dataElement.setType( VALUE_TYPE_INT ); + dataElement.setNumberType( VALUE_TYPE_INT ); + dataElement.setAggregationOperator( AGGREGATION_OPERATOR_SUM ); + dataElement.setCategoryCombo( optionCombo.getCategoryCombo() ); + int deId = dataElementService.addDataElement( dataElement ); - // Add the dataelement into the dataset + idxMap.put( idx - 1, deId ); + + // Add the dataElement into the dataSet dataSet.addDataElement( dataElement ); - // Put text field into the cell(rowIndex,idx) - // htmlCode.append( str ); - - // Generate report item + // Generate Report Item ExportItem exportItem = new ExportItem(); exportItem.setName( name ); - exportItem.setItemType( "dataelement" ); - exportItem.setRow( rowIndex ); + exportItem.setItemType( DATAELEMENT ); + exportItem.setRow( rowIndex + 1 ); exportItem.setColumn( idx ); - exportItem.setExpression( "[" + deId + "." + catagoryOptionComboId + "]" ); - exportItem.setPeriodType( "selected_month" ); - exportItem.setSheetNo( (sheetNo) ); - exportItem.setExportReport( exportReportService.getExportReport( reportId ) ); + exportItem.setExpression( "[" + deId + "." + optionCombo.getId() + "]" ); + exportItem.setPeriodType( SELECTED_MONTH ); + exportItem.setSheetNo( sheetNo ); + exportItem.setExportReport( exportReport ); + exportReportService.addExportItem( exportItem ); - } } + + xml.append( "" ); + + if ( idxMap.containsKey( cell.getColumnIndex() ) ) + { + xml.append( "" + "]]>" ); + } + else if ( (cell.getCellStyle() != null || cell.getCellType() != Cell.CELL_TYPE_BLANK) + && !s.isColumnHidden( cell.getColumnIndex() ) ) + { + xml.append( "" ); + + this.readingDetailsFormattedCell( s, cell ); + + } + xml.append( "" ); } + xml.append( "" ); } - - DataEntryForm dataEntryForm = new DataEntryForm( "DataEntry form", htmlCode.toString() ); + xml.append( "" ); + + // Update DataSet + DataEntryForm dataEntryForm = new DataEntryForm( commonName ); dataEntryFormService.addDataEntryForm( dataEntryForm ); dataSet.setDataEntryForm( dataEntryForm ); - dataSetService.addDataSet( dataSet ); + int dataSetId = dataSetService.addDataSet( dataSet ); + // Update ExportReport Set dataSets = new HashSet(); dataSets.add( dataSet ); - + exportReport.setDataSets( dataSets ); exportReportService.updateExportReport( exportReport ); + + xml.append( "" ); } catch ( Exception e ) @@ -262,4 +355,92 @@ System.err.println( "Error: " + e.getMessage() ); } } + + // ------------------------------------------------------------------------- + // Get the merged cell's information + // ------------------------------------------------------------------------- + + private void readingDetailsFormattedCell( Sheet sheet, Cell objCell ) + { + // The format information + CellStyle format = objCell.getCellStyle(); + + if ( format != null ) + { + xml.append( "" ); + + xml.append( "" ); + } + else + { + xml.append( "/>" ); + } + } + } + + private void writeXMLMergedDescription( Collection collectSheets ) + throws IOException + { + // Open the main Tag // + xml.append( MERGEDCELL_OPENTAG ); + + for ( Integer sheet : collectSheets ) + { + writeMergedInfoBySheetNo( sheet ); + } + + // Close the main Tag // + xml.append( MERGEDCELL_CLOSETAG ); + } + + private void writeMergedInfoBySheetNo( int sheetNo ) + { + Sheet sheet = WORKBOOK.getSheetAt( sheetNo - 1 ); + CellRangeAddress cellRangeAddress = null; + + for ( int i = 0; i < sheet.getNumMergedRegions(); i++ ) + { + cellRangeAddress = sheet.getMergedRegion( i ); + + if ( cellRangeAddress.getFirstColumn() != cellRangeAddress.getLastColumn() ) + { + xml.append( "" + + (cellRangeAddress.getLastColumn() - cellRangeAddress.getFirstColumn() + 1) + "" ); + } + } + } + + private String getSimilarColor( short index ) + { + if ( IndexedColors.BLUE.getIndex() == index ) + { + return "blue"; + } + + if ( IndexedColors.DARK_BLUE.getIndex() == index ) + { + return "darkblue"; + } + + if ( IndexedColors.BROWN.getIndex() == index ) + { + return "brown"; + } + + return ""; + } } \ No newline at end of file === 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 15:34:39 +0000 +++ local/vn/dhis-web-spreadsheet-reporting/src/main/resources/struts.xml 2012-12-12 05:14:59 +0000 @@ -1849,6 +1849,20 @@ plainTextError + + /main.vm + /dhis-web-spreadsheet-reporting/showAutoGenerateForm.vm + /dhis-web-spreadsheet-reporting/menu.vm + F_AUTO_GENERATE_FORM_BY_TEMPLATE + + + + showAutoGenerateForm.action + + + ../dhis-web-commons/ajax/jsonResponseSuccess.vm === modified file 'local/vn/dhis-web-spreadsheet-reporting/src/main/webapp/dhis-web-spreadsheet-reporting/generateReportForm.vm' --- local/vn/dhis-web-spreadsheet-reporting/src/main/webapp/dhis-web-spreadsheet-reporting/generateReportForm.vm 2012-11-13 07:04:37 +0000 +++ local/vn/dhis-web-spreadsheet-reporting/src/main/webapp/dhis-web-spreadsheet-reporting/generateReportForm.vm 2012-12-12 05:14:59 +0000 @@ -1,3 +1,6 @@ + + + + + +#if( $!fileExcel ) +
+ $i18n.getString( "upload_file" ) $i18n.getString( "success" )
$fileExcel.getName() +
+#end + + + +

$i18n.getString( 'auto_generate_form_by_template' ) #openHelp( "" )

+ + + + + + +
$i18n.getString( 'select_excel_file' ) + + + +
+ +
+ + \ No newline at end of file