=== 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-15 09:52:30 +0000 +++ local/vn/dhis-web-spreadsheet-reporting/src/main/java/org/hisp/dhis/reportsheet/preview/action/AutoGenerateFormByTemplate.java 2012-12-16 20:17:59 +0000 @@ -35,9 +35,12 @@ 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.ExportItem.TYPE.INDICATOR; import static org.hisp.dhis.reportsheet.preview.action.HtmlHelper.FOREGROUND_COLOR; import static org.hisp.dhis.reportsheet.preview.action.HtmlHelper.TEXT_COLOR; +import static org.hisp.dhis.reportsheet.utils.ExcelUtils.PATTERN_EXCELFORMULA; import static org.hisp.dhis.reportsheet.utils.ExcelUtils.convertAlignmentString; +import static org.hisp.dhis.reportsheet.utils.ExcelUtils.convertColumnNameToNumber; import static org.hisp.dhis.reportsheet.utils.ExcelUtils.readValueByPOI; import java.io.FileInputStream; @@ -48,6 +51,8 @@ import java.util.List; import java.util.Map; import java.util.Set; +import java.util.regex.Matcher; +import java.util.regex.Pattern; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.ss.usermodel.Cell; @@ -68,6 +73,12 @@ import org.hisp.dhis.dataentryform.DataEntryFormService; import org.hisp.dhis.dataset.DataSet; import org.hisp.dhis.dataset.DataSetService; +import org.hisp.dhis.expression.Expression; +import org.hisp.dhis.expression.ExpressionService; +import org.hisp.dhis.expression.Operator; +import org.hisp.dhis.indicator.Indicator; +import org.hisp.dhis.indicator.IndicatorService; +import org.hisp.dhis.indicator.IndicatorType; import org.hisp.dhis.period.MonthlyPeriodType; import org.hisp.dhis.period.PeriodType; import org.hisp.dhis.reportsheet.ExportItem; @@ -76,6 +87,8 @@ import org.hisp.dhis.reportsheet.ExportReportService; import org.hisp.dhis.reportsheet.importitem.ImportItem; import org.hisp.dhis.reportsheet.state.SelectionManager; +import org.hisp.dhis.validation.ValidationRule; +import org.hisp.dhis.validation.ValidationRuleService; import org.springframework.beans.factory.annotation.Autowired; import com.opensymphony.xwork2.Action; @@ -93,13 +106,7 @@ public class AutoGenerateFormByTemplate implements Action { - private static final String REPORT_EXCEL_GROUP = "BAO CAO THONG KE"; - - private static DataElementCategoryOptionCombo optionCombo = null; - - private String excelFileName = ""; - - private String commonName = ""; + private static final String REPORT_EXCEL_GROUP = "BÁO CÁO KIỂM TRA BỆNH VIỆN"; private static final String WORKBOOK_OPENTAG = ""; @@ -109,6 +116,58 @@ private static final String MERGEDCELL_CLOSETAG = ""; + private static final String DATAELEMENT_KEY = "de"; + + private static final String INDICATOR_KEY = "id"; + + private static final String INDICATOR_NAME = "CS "; + + private static final String INDICATOR_TYPE_NAME = "Loại số"; + + private static final String DESCRIPTION = "Description"; + + private static final Pattern pattern = Pattern.compile( PATTERN_EXCELFORMULA ); + + private static final PeriodType periodType = PeriodType.getPeriodTypeByName( MonthlyPeriodType.NAME ); + + private static DataElementCategoryOptionCombo optionCombo = null; + + private static IndicatorType indicatorType = null; + + private String excelFileName = ""; + + private String commonName = ""; + + private int exportReportId; + + private int dataSetId; + + private Map deMap1 = new HashMap(); + + private Map deMap2 = new HashMap(); + + private Map idMap = new HashMap(); + + private Set vrList = new HashSet(); + + private static final Map operatorMap = new HashMap() + { + /** + * + */ + private static final long serialVersionUID = 1L; + + { + put( "=", "equal_to" ); + put( "!=", "not_equal_to" ); + put( ">", "greater_than" ); + put( ">=", "greater_than_or_equal_to" ); + put( "<", "less_than" ); + put( "<=", "less_than_or_equal_to" ); + put( "cp", "compulsory_pair" ); + } + }; + /** * The workbook we are reading from a given file */ @@ -140,6 +199,15 @@ private DataSetService dataSetService; @Autowired + private IndicatorService indicatorService; + + @Autowired + private ValidationRuleService validationRuleService; + + @Autowired + private ExpressionService expressionService; + + @Autowired private ExportReportService exportReportService; @Autowired @@ -185,6 +253,22 @@ htmlHelper = new XSSFHtmlHelper(); } + for ( IndicatorType type : indicatorService.getAllIndicatorTypes() ) + { + if ( type.getFactor() == 1 ) + { + indicatorType = type; + break; + } + } + + if ( indicatorType == null ) + { + indicatorType = new IndicatorType( INDICATOR_TYPE_NAME, 1, true ); + + indicatorService.addIndicatorType( indicatorType ); + } + optionCombo = categoryService.getDefaultDataElementCategoryOptionCombo(); evaluatorFormula = WORKBOOK.getCreationHelper().createFormulaEvaluator(); @@ -221,28 +305,24 @@ private void printData( int sheetNo, List importItems ) { - 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 ); - - xml.append( "" ); - xml.append( "" ); - try { + // Create new DataSet + DataSet dataSet = new DataSet( commonName, commonName, periodType ); + + // Create new ExportReport + ExportReport exportReport = new ExportReportNormal( commonName, REPORT_EXCEL_GROUP, excelFileName, null ); + exportReportId = exportReportService.addExportReport( exportReport ); + + Sheet s = WORKBOOK.getSheetAt( sheetNo - 1 ); + + xml.append( "" ); + xml.append( "" ); + for ( Row row : s ) { xml.append( "" ); - Map idxMap = new HashMap(); - for ( Cell cell : row ) { Comment cmt = cell.getCellComment(); @@ -251,58 +331,152 @@ if ( cmt != null ) { - idxMap.clear(); - String deName = cell.getStringCellValue(); - - String[] indexes = cmt.getString().toString().split( "," ); - - for ( String index : indexes ) - { - String name = deName + " (" + index + ")"; - int idx = Integer.parseInt( index ); - - // Generate DataElement - DataElement dataElement = new DataElement( name ); - /** TAKE CARE OF SHORT_NAME IS TOO LONG */ - dataElement.setShortName( name ); - dataElement.setActive( true ); - 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 ); - - idxMap.put( idx - 1, deId ); + String values[] = cmt.getString().toString().split( ":" ); + + if ( values[0].equalsIgnoreCase( DATAELEMENT_KEY ) ) + { + String deName = cell.getStringCellValue(); + String[] colNames = values[1].split( "," ); + + for ( String colName : colNames ) + { + int colIdx = convertColumnNameToNumber( colName ); + String name = deName + " (" + colIdx + ")"; + + // Generate DataElement + DataElement dataElement = new DataElement( name ); + /** TAKE CARE OF SHORT_NAME IS TOO LONG */ + dataElement.setShortName( name ); + dataElement.setActive( true ); + 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 ); + + deMap1.put( (colIdx - 1) + "#" + rowIndex, deId ); + deMap2.put( colName + (rowIndex + 1), "[" + deId + "." + optionCombo.getId() + "]"); + + // Add the dataElement into the dataSet + dataSet.addDataElement( dataElement ); + + // Generate Report Item + ExportItem exportItem = new ExportItem(); + exportItem.setName( name ); + exportItem.setItemType( DATAELEMENT ); + exportItem.setRow( rowIndex + 1 ); + exportItem.setColumn( colIdx ); + exportItem.setExpression( "[" + deId + "." + optionCombo.getId() + "]" ); + exportItem.setPeriodType( SELECTED_MONTH ); + exportItem.setSheetNo( sheetNo ); + exportItem.setExportReport( exportReport ); + + exportReportService.addExportItem( exportItem ); + } + } + else if ( values[0].equalsIgnoreCase( INDICATOR_KEY ) ) + { + String idName = INDICATOR_NAME + values[1]; + Integer colIdx = colIndex + 1; + + if ( values.length == 4 ) + { + colIdx = convertColumnNameToNumber( values[3] ); + } + else if ( values.length == 5 ) + { + colIdx = convertColumnNameToNumber( values[3] ); + rowIndex = Integer.parseInt( values[4] ) - 1; + } + + // Create Indicator + Indicator indicator = new Indicator(); + indicator.setName( idName ); + indicator.setShortName( idName ); + indicator.setAnnualized( false ); + indicator.setIndicatorType( indicatorType ); + indicator.setNumerator( prepareExcelFormulaForAutoForm( values[2] ) ); + indicator.setNumeratorDescription( DESCRIPTION ); + indicator.setDenominator( 1 + "" ); + indicator.setDenominatorDescription( DESCRIPTION ); + + int indicatorId = indicatorService.addIndicator( indicator ); + + idMap.put( (colIdx - 1) + "#" + rowIndex, indicatorId ); // Add the dataElement into the dataSet - dataSet.addDataElement( dataElement ); + dataSet.addIndicator( indicator ); // Generate Report Item ExportItem exportItem = new ExportItem(); - exportItem.setName( name ); - exportItem.setItemType( DATAELEMENT ); + exportItem.setName( idName ); + exportItem.setItemType( INDICATOR ); exportItem.setRow( rowIndex + 1 ); - exportItem.setColumn( idx ); - exportItem.setExpression( "[" + deId + "." + optionCombo.getId() + "]" ); + exportItem.setColumn( colIdx ); + exportItem.setExpression( "[" + indicatorId + "]" ); exportItem.setPeriodType( SELECTED_MONTH ); exportItem.setSheetNo( sheetNo ); exportItem.setExportReport( exportReport ); exportReportService.addExportItem( exportItem ); } - } - - if ( idxMap.containsKey( colIndex ) ) - { - xml.append( "" ); - - xml.append( "]]>" ); - - printFormatInfo( s, cell ); + else + { + // Validation rules + Expression leftSide = new Expression(); + + leftSide.setExpression( prepareExcelFormulaForAutoForm( values[2] ) ); + leftSide.setDescription( DESCRIPTION ); + leftSide.setNullIfBlank( true ); + + Expression rightSide = new Expression(); + + rightSide.setExpression( prepareExcelFormulaForAutoForm( values[4] ) ); + rightSide.setDescription( DESCRIPTION ); + rightSide.setNullIfBlank( true ); + + ValidationRule validationRule = new ValidationRule(); + + validationRule.setName( values[1] ); + validationRule.setDescription( DESCRIPTION ); + validationRule.setType( ValidationRule.TYPE_ABSOLUTE ); + validationRule.setOperator( Operator.valueOf( operatorMap.get( values[3] ) ) ); + validationRule.setLeftSide( leftSide ); + validationRule.setRightSide( rightSide ); + + validationRule.setPeriodType( periodType ); + + vrList.add( validationRuleService.saveValidationRule( validationRule ) ); + } + } + + String key = colIndex + "#" + rowIndex; + + if ( deMap1.containsKey( key ) ) + { + xml.append( "" ); + + xml.append( "]]>" ); + + xml.append( printFormatInfo( s, cell ) ); + + xml.append( "" ); + } + else if ( idMap.containsKey( key ) ) + { + int indicatorId = idMap.get( key ); + + xml.append( "" ); + + xml.append( "]]>" ); + + xml.append( printFormatInfo( s, cell ) ); xml.append( "" ); } @@ -314,7 +488,7 @@ xml.append( "" ); - printFormatInfo( s, cell ); + xml.append( printFormatInfo( s, cell ) ); xml.append( "" ); } @@ -328,7 +502,7 @@ dataEntryFormService.addDataEntryForm( dataEntryForm ); dataSet.setDataEntryForm( dataEntryForm ); - int dataSetId = dataSetService.addDataSet( dataSet ); + dataSetId = dataSetService.addDataSet( dataSet ); // Update ExportReport Set dataSets = new HashSet(); @@ -339,12 +513,58 @@ xml.append( "" ); + for ( String key1 : idMap.keySet() ) + { + Indicator indicator = indicatorService.getIndicator( idMap.get( key1 ) ); + String expression = indicator.getNumerator(); + + for ( String key2 : deMap2.keySet() ) + { + expression = expression.replaceAll( "\\[" + key2 + "\\]", deMap2.get( key2 ) ); + } + + indicator.setNumerator( expression ); + indicatorService.updateIndicator( indicator ); + } + + for ( Integer id : vrList ) + { + ValidationRule vr = validationRuleService.getValidationRule( id ); + + Expression leftSide = vr.getLeftSide(); + Expression rightSide = vr.getRightSide(); + + String leftExpression = leftSide.getExpression(); + String rightExpression = rightSide.getExpression(); + + for ( String key2 : deMap2.keySet() ) + { + String operandId = deMap2.get( key2 ); + + leftExpression = leftExpression.replaceAll( "\\[" + key2 + "\\]", operandId ); + rightExpression = rightExpression.replaceAll( "\\[" + key2 + "\\]", operandId ); + } + + leftSide.setDataElementsInExpression( expressionService.getDataElementsInExpression( leftExpression ) ); + leftSide.setOptionCombosInExpression( expressionService.getOptionCombosInExpression( leftExpression ) ); + + rightSide + .setDataElementsInExpression( expressionService.getDataElementsInExpression( rightExpression ) ); + rightSide + .setOptionCombosInExpression( expressionService.getOptionCombosInExpression( rightExpression ) ); + + vr.setLeftSide( leftSide ); + vr.setRightSide( rightSide ); + + validationRuleService.updateValidationRule( vr ); + } } catch ( Exception e ) { + e.printStackTrace(); + + autoRollBack(); cleanUp(); - - e.printStackTrace(); } } @@ -359,16 +579,17 @@ System.gc(); } - private void printFormatInfo( Sheet sheet, Cell objCell ) + private String printFormatInfo( Sheet sheet, Cell objCell ) { // The format information CellStyle format = objCell.getCellStyle(); + StringBuffer sb = new StringBuffer(); if ( format != null ) { - xml.append( "" ); + sb.append( ">" ); // The cell background information - xml.append( "" ); - xml.append( "" ); + sb.append( "" ); + sb.append( "" ); } else { - xml.append( "/>" ); + sb.append( "/>" ); } } + else + { + return ""; + } + + return sb.toString(); } // ------------------------------------------------------------------------- @@ -428,4 +655,81 @@ } } } + + private String prepareExcelFormulaForAutoForm( String formula ) + { + Matcher matcher = pattern.matcher( formula ); + + StringBuffer buffer = new StringBuffer(); + + while ( matcher.find() ) + { + String s = matcher.group().replaceAll( "\\s", "" ); + + if ( !s.endsWith( "!" ) ) + { + s = "[" + s + "]"; + } + + matcher.appendReplacement( buffer, s ); + } + + matcher.appendTail( buffer ); + + return buffer.toString(); + } + + private void autoRollBack() + { + try + { + if ( exportReportId > 0 ) + { + exportReportService.deleteExportReport( exportReportId ); + + exportReportId = 0; + } + if ( dataSetId > 0 ) + { + dataSetService.deleteDataSet( dataSetService.getDataSet( dataSetId ) ); + + dataSetId = 0; + } + + for ( String key : deMap1.keySet() ) + { + dataElementService.deleteDataElement( dataElementService.getDataElement( deMap1.get( key ) ) ); + } + for ( String key : idMap.keySet() ) + { + indicatorService.deleteIndicator( indicatorService.getIndicator( idMap.get( key ) ) ); + } + for ( Integer vrId : vrList ) + { + validationRuleService.deleteValidationRule( validationRuleService.getValidationRule( vrId ) ); + } + + deMap1.clear(); + deMap2.clear(); + idMap.clear(); + vrList.clear(); + } + catch ( Exception e ) + { + resetParams(); + + e.printStackTrace(); + } + } + + private void resetParams() + { + exportReportId = 0; + dataSetId = 0; + deMap1.clear(); + deMap2.clear(); + idMap.clear(); + vrList.clear(); + } + } \ No newline at end of file === modified file 'local/vn/dhis-web-spreadsheet-reporting/src/main/java/org/hisp/dhis/reportsheet/utils/ExcelUtils.java' --- local/vn/dhis-web-spreadsheet-reporting/src/main/java/org/hisp/dhis/reportsheet/utils/ExcelUtils.java 2012-12-13 08:59:31 +0000 +++ local/vn/dhis-web-spreadsheet-reporting/src/main/java/org/hisp/dhis/reportsheet/utils/ExcelUtils.java 2012-12-16 20:17:59 +0000 @@ -70,12 +70,6 @@ private static FormulaParsingWorkbook evaluationWorkbook = HSSFEvaluationWorkbook.create( new HSSFWorkbook() ); - private static final String PATTERN_FOR_ROW = "(\\d{1,})"; - - private static final String PATTERN_FOR_COLUMN = "([a-zA-Z])"; - - private static final String PATTERN_EXCELFORMULA = "(\\$?([a-zA-Z]{1,})\\$?(\\d{1,}!?))"; - private static final Integer NUMBER_OF_LETTER = new Integer( 26 ); private static final Integer POI_CELLSTYLE_BLANK = new Integer( org.apache.poi.ss.usermodel.Cell.CELL_TYPE_BLANK ); @@ -92,6 +86,12 @@ public static final String EXTENSION_XLS = ".xls"; + public static final String PATTERN_FOR_ROW = "(\\d{1,})"; + + public static final String PATTERN_FOR_COLUMN = "([a-zA-Z]{1,})"; + + public static final String PATTERN_EXCELFORMULA = "(\\$?([a-zA-Z]{1,})\\$?(\\d{1,}!?))"; + // ------------------------------------------------------------------------- // JXL methods // ------------------------------------------------------------------------- @@ -625,7 +625,7 @@ return true; } - + public static boolean isValidFormula( String formula, int formulaType, int sheetIndex ) { try @@ -640,7 +640,6 @@ return true; } - // ------------------------------------------------------------------------- // Supportive methods // ------------------------------------------------------------------------- === modified file 'local/vn/dhis-web-spreadsheet-reporting/src/main/webapp/dhis-web-spreadsheet-reporting/showAutoGenerateForm.vm' --- local/vn/dhis-web-spreadsheet-reporting/src/main/webapp/dhis-web-spreadsheet-reporting/showAutoGenerateForm.vm 2012-12-13 08:59:31 +0000 +++ local/vn/dhis-web-spreadsheet-reporting/src/main/webapp/dhis-web-spreadsheet-reporting/showAutoGenerateForm.vm 2012-12-16 20:17:59 +0000 @@ -25,7 +25,6 @@
- \ No newline at end of file