=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/validation/ValidationRuleService.java' --- dhis-2/dhis-api/src/main/java/org/hisp/dhis/validation/ValidationRuleService.java 2013-11-08 23:17:37 +0000 +++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/validation/ValidationRuleService.java 2013-11-10 10:00:19 +0000 @@ -33,6 +33,7 @@ import org.hisp.dhis.dataelement.DataElement; import org.hisp.dhis.dataset.DataSet; +import org.hisp.dhis.i18n.I18nFormat; import org.hisp.dhis.organisationunit.OrganisationUnit; import org.hisp.dhis.period.Period; @@ -58,9 +59,10 @@ * @param endDate the end date. * @param sources a collection of Sources. * @param sendAlerts whether to send alerts for surveillance. + * @param format the i18n format. * @return a collection of ValidationResults for each validation violation. */ - Collection validate( Date startDate, Date endDate, Collection sources, boolean sendAlerts ); + Collection validate( Date startDate, Date endDate, Collection sources, boolean sendAlerts, I18nFormat format ); /** * Validate DataValues. @@ -70,9 +72,10 @@ * @param sources a collection of Sources. * @param group a group of ValidationRules. * @param sendAlerts whether to send alerts for surveillance. + * @param format the i18n format. * @return a collection of ValidationResults for each validation violation. */ - Collection validate( Date startDate, Date endDate, Collection sources, ValidationRuleGroup group, boolean sendAlerts ); + Collection validate( Date startDate, Date endDate, Collection sources, ValidationRuleGroup group, boolean sendAlerts, I18nFormat format ); /** * Validate DataValues. === modified file 'dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/validation/DefaultValidationRuleService.java' --- dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/validation/DefaultValidationRuleService.java 2013-11-09 18:01:52 +0000 +++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/validation/DefaultValidationRuleService.java 2013-11-10 10:00:19 +0000 @@ -33,10 +33,12 @@ import static org.hisp.dhis.i18n.I18nUtils.getObjectsBetweenByName; import static org.hisp.dhis.i18n.I18nUtils.getObjectsByName; import static org.hisp.dhis.i18n.I18nUtils.i18n; +import static org.hisp.dhis.system.util.TextUtils.LN; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Collection; +import java.util.Collections; import java.util.Date; import java.util.HashMap; import java.util.HashSet; @@ -56,6 +58,7 @@ import org.hisp.dhis.dataset.DataSet; import org.hisp.dhis.datavalue.DataValueService; import org.hisp.dhis.expression.ExpressionService; +import org.hisp.dhis.i18n.I18nFormat; import org.hisp.dhis.i18n.I18nService; import org.hisp.dhis.message.MessageService; import org.hisp.dhis.organisationunit.OrganisationUnit; @@ -169,14 +172,14 @@ // ------------------------------------------------------------------------- @Override - public Collection validate( Date startDate, Date endDate, Collection sources, boolean sendAlerts ) + public Collection validate( Date startDate, Date endDate, Collection sources, boolean sendAlerts, I18nFormat format ) { - return validate( startDate, endDate, sources, null, sendAlerts ); + return validate( startDate, endDate, sources, null, sendAlerts, format ); } @Override public Collection validate( Date startDate, Date endDate, Collection sources, - ValidationRuleGroup group, boolean sendAlerts ) + ValidationRuleGroup group, boolean sendAlerts, I18nFormat format ) { log.info( "Validate start:" + startDate + " end: " + endDate + " sources: " + sources.size() + " group: " + group ); @@ -186,6 +189,8 @@ Collection results = Validator.validate( sources, periods, rules, null, constantService, expressionService, periodService, dataValueService ); + formatPeriods( results, format ); + if ( sendAlerts ) { Set resultsToAlert = new HashSet( results ); @@ -360,6 +365,7 @@ for ( Map.Entry, Set> entry : messageMap.entrySet() ) { + Collections.sort( entry.getKey() ); sendAlertmessage( entry.getKey(), entry.getValue(), scheduledRunStart ); } } @@ -427,7 +433,7 @@ messageReceivers.add( user ); } } - + return messageMap; } @@ -478,59 +484,31 @@ */ private void sendAlertmessage( List results, Set users, Date scheduledRunStart ) { - StringBuilder messageBuilder = new StringBuilder(); + StringBuilder builder = new StringBuilder(); SimpleDateFormat dateTimeFormatter = new SimpleDateFormat( "yyyy-MM-dd HH:mm" ); Map importanceCountMap = countResultsByImportanceType( results ); - String subject = "DHIS alerts as of " + dateTimeFormatter.format( scheduledRunStart ) + " - priority High " + String subject = "Alerts as of " + dateTimeFormatter.format( scheduledRunStart ) + ": High " + ( importanceCountMap.get( "high" ) == null ? 0 : importanceCountMap.get( "high" ) ) + ", Medium " + ( importanceCountMap.get( "medium" ) == null ? 0 : importanceCountMap.get( "medium" ) ) + ", Low " + ( importanceCountMap.get( "low" ) == null ? 0 : importanceCountMap.get( "low" ) ); - messageBuilder - .append( "" ) - .append( "" ).append( "" ) - .append( "" ).append( subject ).append( "
" ) - .append( "" ) - .append( "" ) - .append( "" ) - .append( "" ) - .append( "" ) - .append( "" ) - .append( "" ) - .append( "" ) - .append( "" ) - .append( "" ) - .append( "" ); - + //TODO use velocity template for message + for ( ValidationResult result : results ) { ValidationRule rule = result.getValidationRule(); - - messageBuilder - .append( "" ) - .append( "" ); + + builder.append( result.getSource().getName() ).append( " " ).append( result.getPeriod().getName() ).append( LN ). + append( rule.getName() ).append( " (" ).append( rule.getImportance() ).append( ") " ).append( LN ). + append( rule.getLeftSide().getDescription() ).append( ": " ).append( result.getLeftsideValue() ).append( LN ). + append( rule.getRightSide().getDescription() ).append( ": " ).append( result.getRightsideValue() ).append( LN ).append( LN ); } - - messageBuilder - .append( "
Organisation UnitPeriodImportanceLeft side descriptionValueOperatorValueRight side description
" ).append( result.getSource().getName() ).append( "<\td>" ) - .append( "" ).append( result.getPeriod().getName() ).append( "<\td>" ) - .append( "" ).append( rule.getImportance() ).append( "<\td>" ) - .append( "" ).append( rule.getLeftSide().getDescription() ).append( "<\td>" ) - .append( "" ).append( result.getLeftsideValue() ).append( "<\td>" ) - .append( "" ).append( rule.getOperator().toString() ).append( "<\td>" ) - .append( "" ).append( result.getRightsideValue() ).append( "<\td>" ) - .append( "" ).append( rule.getRightSide().getDescription() ).append( "<\td>" ) - .append( "
" ) - .append( "" ) - .append( "" ); - - String messageText = messageBuilder.toString(); - - log.info( "Alerting users[" + users.size() + "] subject " + subject ); - messageService.sendMessage( subject, messageText, null, users ); + + log.info( "Alerting users: " + users.size() + ", subject: " + subject ); + messageService.sendMessage( subject, builder.toString(), null, users ); } // ------------------------------------------------------------------------- @@ -626,6 +604,26 @@ return rulesForDataSet; } + /** + * Formats and sets name on the period of each result. + * + * @param results the collecion of validation results. + * @param format the i18n format. + */ + private void formatPeriods( Collection results, I18nFormat format ) + { + if ( format != null ) + { + for ( ValidationResult result : results ) + { + if ( result != null && result.getPeriod() != null ) + { + result.getPeriod().setName( format.formatPeriod( result.getPeriod() ) ); + } + } + } + } + // ------------------------------------------------------------------------- // ValidationRule CRUD operations // ------------------------------------------------------------------------- === modified file 'dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/validation/ValidationRuleServiceTest.java' --- dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/validation/ValidationRuleServiceTest.java 2013-11-08 23:17:37 +0000 +++ dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/validation/ValidationRuleServiceTest.java 2013-11-10 10:00:19 +0000 @@ -485,7 +485,7 @@ // insures that if they are the same as the reference results, they will appear in the same order. Collection results = validationRuleService.validate( - getDate( 2000, 2, 1 ), getDate( 2000, 6, 1 ), sourcesA, false ); + getDate( 2000, 2, 1 ), getDate( 2000, 6, 1 ), sourcesA, false, null ); Collection reference = new HashSet(); @@ -543,7 +543,7 @@ validationRuleService.addValidationRuleGroup( group ); Collection results = validationRuleService.validate( - getDate( 2000, 2, 1 ), getDate( 2000, 6, 1 ), sourcesA, group, false ); + getDate( 2000, 2, 1 ), getDate( 2000, 6, 1 ), sourcesA, group, false, null ); Collection reference = new HashSet(); === modified file 'dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/util/TextUtils.java' --- dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/util/TextUtils.java 2013-09-26 18:17:41 +0000 +++ dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/util/TextUtils.java 2013-11-10 10:00:19 +0000 @@ -39,6 +39,7 @@ { public static final TextUtils INSTANCE = new TextUtils(); public static final String EMPTY = ""; + public static final String LN = System.getProperty( "line.separator" ); private static final Pattern LINK_PATTERN = Pattern.compile( "((http://|https://|www\\.).+?)($|\\n|\\r|\\r\\n| )" ); private static final String DELIMITER = ", "; === modified file 'dhis-2/dhis-web/dhis-web-validationrule/src/main/java/org/hisp/dhis/validationrule/action/RunValidationAction.java' --- dhis-2/dhis-web/dhis-web-validationrule/src/main/java/org/hisp/dhis/validationrule/action/RunValidationAction.java 2013-11-08 23:17:37 +0000 +++ dhis-2/dhis-web/dhis-web-validationrule/src/main/java/org/hisp/dhis/validationrule/action/RunValidationAction.java 2013-11-10 10:00:19 +0000 @@ -82,7 +82,7 @@ { this.organisationUnitService = organisationUnitService; } - + // ------------------------------------------------------------------------- // Input/output // ------------------------------------------------------------------------- @@ -175,7 +175,7 @@ log.info( "Validating captured data for all rules" ); validationResults = new ArrayList( validationRuleService.validate( format - .parseDate( startDate ), format.parseDate( endDate ), organisationUnits, sendAlerts ) ); + .parseDate( startDate ), format.parseDate( endDate ), organisationUnits, sendAlerts, format ) ); } else { @@ -184,7 +184,7 @@ log.info( "Validating captured data for rules for group: '" + group.getName() + "'" ); validationResults = new ArrayList( validationRuleService.validate( format - .parseDate( startDate ), format.parseDate( endDate ), organisationUnits, group, sendAlerts ) ); + .parseDate( startDate ), format.parseDate( endDate ), organisationUnits, group, sendAlerts, format ) ); } maxExceeded = validationResults.size() > ValidationRuleService.MAX_INTERACTIVE_ALERTS;