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 ) ); }
@Override public String allowDeleteValidationRule( ValidationRule validationRule ) { for ( ValidationResult validationResult : validationResultService.getAllValidationResults() ) { if ( validationResult.getValidationRule().equals( validationRule ) ) { return ERROR; } } return null; }
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 ); } } ); }
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; } }
/** * 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; }
@Override public List<Object> getUniqueValues( ValidationResult validationResult ) { return getObjectList( validationResult.getValidationRule().getId(), validationResult.getPeriod().getId(), validationResult.getOrganisationUnit().getId(), validationResult.getAttributeOptionCombo().getId(), validationResult.getDayInPeriod() ); }
@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() ); }
/** * 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(); }