@Override public List<Object> getValues( ValidationResult validationResult ) { return getObjectList( validationResult.getLeftsideValue(), validationResult.getRightsideValue(), validationResult.getValidationRule().getId(), validationResult.getPeriod().getId(), validationResult.getOrganisationUnit().getId(), validationResult.getAttributeOptionCombo().getId(), validationResult.getDayInPeriod() ); }
@Override public List<Object> getIdentifierValues( ValidationResult validationResult ) { return getObjectList( validationResult.getId() ); }
public Builder withInitialResults( Collection<ValidationResult> results ) { this.context.validationResults.addAll( results ); results.forEach( validationResult -> { List<ValidationResult> res = context.initialValidationResults .getValue( validationResult.getOrganisationUnit(), validationResult.getValidationRule(), validationResult.getPeriod() ); if ( res == null ) { res = new ArrayList<>(); } res.add( validationResult ); context.initialValidationResults .putEntry( validationResult.getOrganisationUnit(), validationResult.getValidationRule(), validationResult.getPeriod(), res ); } ); return this; } }
@Override public List<Object> getUniqueValues( ValidationResult validationResult ) { return getObjectList( validationResult.getValidationRule().getId(), validationResult.getPeriod().getId(), validationResult.getOrganisationUnit().getId(), validationResult.getAttributeOptionCombo().getId(), validationResult.getDayInPeriod() ); }
/** * Compare ValidationResults so they will be listed in the desired * order: by validationRule, period, attributeOptionCombo and orgUnit. * * @param other The other ValidationResult to compare with. * @return a negative integer, zero, or a positive integer as this object * is less than, equal to, or greater than the specified object. */ @Override public int compareTo( ValidationResult other ) { return new CompareToBuilder() .append( this.validationRule, other.getValidationRule() ) .append( this.period, other.getPeriod() ) .append( this.attributeOptionCombo, other.getAttributeOptionCombo() ) .append( this.organisationUnit, other.getOrganisationUnit() ) .append( this.id, other.getId() ) .toComparison(); }
private static SortedSet<MessagePair> createMessagePairs( SortedSet<ValidationResult> results ) { return results.stream() .flatMap( result -> result.getValidationRule().getNotificationTemplates().stream() .map( template -> new MessagePair( result, template ) ) ) .collect( Collectors.toCollection( TreeSet::new ) ); }
public boolean skipValidationOfTuple( OrganisationUnit organisationUnit, ValidationRule validationRule, Period period, String attributeOptionCombo, int dayInPeriod ) { List<ValidationResult> validationResultList = initialValidationResults .getValue( organisationUnit, validationRule, period ); if ( validationResultList != null ) { for ( ValidationResult vr : validationResultList ) { if ( vr.getAttributeOptionCombo().getUid().equals( attributeOptionCombo ) && vr.getDayInPeriod() == dayInPeriod ) { return true; } } } return false; }
/** * Reload attribute category option combos into this Hibernate context. * * @param results * @param dataElementCategoryService */ private static void reloadAttributeOptionCombos( Collection<ValidationResult> results, CategoryService dataElementCategoryService ) { for ( ValidationResult result : results ) { result.setAttributeOptionCombo( dataElementCategoryService .getCategoryOptionCombo( result.getAttributeOptionCombo().getId() ) ); } } }
@Override public void deleteOrganisationUnit( OrganisationUnit organisationUnit ) { validationResultService.getAllValidationResults().forEach( validationResult -> { if ( validationResult.getOrganisationUnit().equals( organisationUnit ) ) { validationResultService.deleteValidationResult( validationResult ); } } ); }
@Override public int compare( ValidationResult result1, ValidationResult result2 ) { if ( result1.getPeriod() == null && result2.getPeriod() == null ) { return 0; } else if ( result1.getPeriod() == null ) { return 1; } else if ( result2.getPeriod() == null ) { return -1; } return result1.getPeriod().getStartDate().compareTo( result2.getPeriod().getStartDate() ); } }
@Override public void deleteCategoryOptionCombo( CategoryOptionCombo dataElementCategoryOptionCombo ) { validationResultService.getAllValidationResults().forEach( validationResult -> { if ( validationResult.getAttributeOptionCombo().equals( dataElementCategoryOptionCombo ) ) { validationResultService.deleteValidationResult( validationResult ); } } ); }
/** * Validates one rule / period / attribute option combo. * * @param optionCombo the attribute option combo. * @param leftSide left side value. * @param rightSide right side value. */ private void validateOptionCombo( String optionCombo, Double leftSide, Double rightSide ) { // Skipping any results we already know if ( context.skipValidationOfTuple( orgUnit, ruleX.getRule(), period, optionCombo, periodService.getDayInPeriod( period, new Date() ) ) ) { return; } boolean violation = isViolation( leftSide, rightSide ); if ( violation && !context.isAnalysisComplete() ) { validationResults.add( new ValidationResult( ruleX.getRule(), period, orgUnit, getAttributeOptionCombo( optionCombo ), roundSignificant( zeroIfNull( leftSide ) ), roundSignificant( zeroIfNull( rightSide ) ), periodService.getDayInPeriod( period, new Date() ) ) ); } }
@Override public String allowDeleteValidationRule( ValidationRule validationRule ) { for ( ValidationResult validationResult : validationResultService.getAllValidationResults() ) { if ( validationResult.getValidationRule().equals( validationRule ) ) { return ERROR; } } return null; }
/** * Resolve all distinct recipients for the given MessagePair. */ private static Set<User> resolveRecipients( MessagePair pair ) { ValidationResult validationResult = pair.result; ValidationNotificationTemplate template = pair.template; // Limit recipients to be withing org unit hierarchy only, effectively // producing a cross-cut of all users in the configured user groups. final boolean limitToHierarchy = BooleanUtils.toBoolean( template.getNotifyUsersInHierarchyOnly() ); final boolean parentOrgUnitOnly = BooleanUtils.toBoolean( template.getNotifyParentOrganisationUnitOnly() ); Set<OrganisationUnit> orgUnitsToInclude = Sets.newHashSet(); Set<User> recipients = template.getRecipientUserGroups().stream() .flatMap( ug -> ug.getMembers().stream() ).collect( Collectors.toSet() ); if ( limitToHierarchy ) { orgUnitsToInclude.add( validationResult.getOrganisationUnit() ); // Include self orgUnitsToInclude.addAll( validationResult.getOrganisationUnit().getAncestors() ); recipients = recipients.stream() .filter( user -> orgUnitsToInclude.contains( user.getOrganisationUnit() ) ).collect( Collectors.toSet() ); } else if ( parentOrgUnitOnly ) { Set<User> parents = Sets.newHashSet(); recipients.forEach( user -> parents.addAll( user.getOrganisationUnit().getParent().getUsers() ) ); return parents; } return recipients; }
@Override public void deletePeriod( Period period ) { validationResultService.getAllValidationResults().forEach( validationResult -> { if ( validationResult.getPeriod().equals( period ) ) { validationResultService.deleteValidationResult( validationResult ); } } ); }
private Map<Set<User>, NotificationMessage> createSingleNotifications( SortedSet<MessagePair> messagePairs ) { BiMap<Set<User>, NotificationMessage> singleNotificationCollection = HashBiMap.create(); for ( MessagePair messagePair : messagePairs ) { NotificationMessage notificationMessage = notificationMessageRenderer .render( messagePair.result, messagePair.template ); notificationMessage.setPriority( getPriority( messagePair.result.getValidationRule().getImportance() ) ); singleNotificationCollection.put( new HashSet<>(), notificationMessage ); resolveRecipients( messagePair ) .forEach( user -> singleNotificationCollection.inverse().get( notificationMessage ).add( user ) ); } return singleNotificationCollection; }
@Override public void deleteValidationRule( ValidationRule validationRule ) { validationResultService.getAllValidationResults().forEach( validationResult -> { if ( validationResult.getValidationRule().equals( validationRule ) ) { validationResultService.deleteValidationResult( validationResult ); } } ); }
/** * Creates a summarized message from the given MessagePairs and pre-rendered map of NotificationMessages. * The messages generated by each distinct MessagePair are concatenated in their given order. */ private static NotificationMessage createSummarizedMessage( SortedSet<MessagePair> pairs, final Map<MessagePair, NotificationMessage> renderedNotificationsMap, final Date validationDate ) { Map<Importance, Long> counts = pairs.stream() .map( m -> m.result.getValidationRule().getImportance() ) .collect( Collectors.groupingBy( Function.identity(), Collectors.counting() ) ); String subject = String.format( "Validation violations as of %s", DateUtils.getLongDateString( validationDate ) ); String message = String.format( "Violations: High %d, medium %d, low %d", counts.getOrDefault( HIGH, 0L ), counts.getOrDefault( Importance.MEDIUM, 0L ), counts.getOrDefault( LOW, 0L ) ); // Concatenate the notifications in sorted order, divide by double linebreak message = message + pairs.stream().sorted() .map( renderedNotificationsMap::get ) .map( n -> String.format( "%s%s%s", n.getSubject(), LN, n.getMessage() ) ) .reduce( "", ( initStr, newStr ) -> String.format( "%s%s%s", initStr, LN + LN, newStr ) ); NotificationMessage notificationMessage = new NotificationMessage( subject, message ); notificationMessage.setPriority( getPriority( counts.getOrDefault( HIGH, 0L ) > 0 ? HIGH : counts.getOrDefault( MEDIUM, 0L ) > 0 ? MEDIUM : LOW ) ); return notificationMessage; }