← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 12911: Surveillance/alerts, using plain test instead of html, as html turned out to be hard to send as e...

 

------------------------------------------------------------
revno: 12911
committer: Lars Helge Øverland <larshelge@xxxxxxxxx>
branch nick: dhis2
timestamp: Sun 2013-11-10 11:00:19 +0100
message:
  Surveillance/alerts, using plain test instead of html, as html turned out to be hard to send as email in a simple way.
modified:
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/validation/ValidationRuleService.java
  dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/validation/DefaultValidationRuleService.java
  dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/validation/ValidationRuleServiceTest.java
  dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/util/TextUtils.java
  dhis-2/dhis-web/dhis-web-validationrule/src/main/java/org/hisp/dhis/validationrule/action/RunValidationAction.java


--
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-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<ValidationResult> validate( Date startDate, Date endDate, Collection<OrganisationUnit> sources, boolean sendAlerts );
+    Collection<ValidationResult> validate( Date startDate, Date endDate, Collection<OrganisationUnit> 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<ValidationResult> validate( Date startDate, Date endDate, Collection<OrganisationUnit> sources, ValidationRuleGroup group, boolean sendAlerts );
+    Collection<ValidationResult> validate( Date startDate, Date endDate, Collection<OrganisationUnit> 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<ValidationResult> validate( Date startDate, Date endDate, Collection<OrganisationUnit> sources, boolean sendAlerts )
+    public Collection<ValidationResult> validate( Date startDate, Date endDate, Collection<OrganisationUnit> sources, boolean sendAlerts, I18nFormat format )
     {
-        return validate( startDate, endDate, sources, null, sendAlerts );
+        return validate( startDate, endDate, sources, null, sendAlerts, format );
     }
 
     @Override
     public Collection<ValidationResult> validate( Date startDate, Date endDate, Collection<OrganisationUnit> 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<ValidationResult> results = Validator.validate( sources, periods, rules, null,
             constantService, expressionService, periodService, dataValueService );
         
+        formatPeriods( results, format );
+        
         if ( sendAlerts )
         {
             Set<ValidationResult> resultsToAlert = new HashSet<ValidationResult>( results );
@@ -360,6 +365,7 @@
         
         for ( Map.Entry<List<ValidationResult>, Set<User>> 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<ValidationResult> results, Set<User> users, Date scheduledRunStart )
     {
-        StringBuilder messageBuilder = new StringBuilder();
+        StringBuilder builder = new StringBuilder();
 
         SimpleDateFormat dateTimeFormatter = new SimpleDateFormat( "yyyy-MM-dd HH:mm" );
 
         Map<String, Integer> 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( "<html>" )
-            .append( "<head>" ).append( "</head>" )
-            .append( "<body>" ).append( subject ).append( "<br />" )
-            .append( "<table>" )
-            .append( "<tr>" )
-            .append( "<th>Organisation Unit</th>" )
-            .append( "<th>Period</th>" )
-            .append( "<th>Importance</th>" )
-            .append( "<th>Left side description</th>" )
-            .append( "<th>Value</th>" )
-            .append( "<th>Operator</th>" )
-            .append( "<th>Value</th>" )
-            .append( "<th>Right side description</th>" )
-            .append( "</tr>" );
-
+        //TODO use velocity template for message
+        
         for ( ValidationResult result : results )
         {
             ValidationRule rule = result.getValidationRule();
-
-            messageBuilder
-                .append( "<tr>" )
-                .append( "<td>" ).append( result.getSource().getName() ).append( "<\td>" )
-                .append( "<td>" ).append( result.getPeriod().getName() ).append( "<\td>" )
-                .append( "<td>" ).append( rule.getImportance() ).append( "<\td>" )
-                .append( "<td>" ).append( rule.getLeftSide().getDescription() ).append( "<\td>" )
-                .append( "<td>" ).append( result.getLeftsideValue() ).append( "<\td>" )
-                .append( "<td>" ).append( rule.getOperator().toString() ).append( "<\td>" )
-                .append( "<td>" ).append( result.getRightsideValue() ).append( "<\td>" )
-                .append( "<td>" ).append( rule.getRightSide().getDescription() ).append( "<\td>" )
-                .append( "</tr>" );
+            
+            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( "</table>" )
-            .append( "</body>" )
-            .append( "</html>" );
-
-        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<ValidationResult> 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<ValidationResult> 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<ValidationResult> reference = new HashSet<ValidationResult>();
 
@@ -543,7 +543,7 @@
         validationRuleService.addValidationRuleGroup( group );
 
         Collection<ValidationResult> 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<ValidationResult> reference = new HashSet<ValidationResult>();
 

=== 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<ValidationResult>( 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<ValidationResult>( 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;