Search in sources :

Example 1 with ValidationWhiteList

use of com.b2international.snowowl.core.validation.whitelist.ValidationWhiteList in project snow-owl by b2ihealthcare.

the class ValidationWhiteListApiTest method createWhiteList.

@Test
public void createWhiteList() throws Exception {
    final String whiteListId = createWhiteLists("58", COMPONENT_ID);
    assertThat(whiteListId).isNotEmpty();
    final ValidationWhiteList whiteList = ValidationRequests.whiteList().prepareGet(whiteListId).buildAsync().getRequest().execute(context);
    assertThat(whiteList.getRuleId()).isEqualTo("58");
    assertThat(whiteList.getComponentIdentifier()).isEqualTo(COMPONENT_ID);
}
Also used : ValidationWhiteList(com.b2international.snowowl.core.validation.whitelist.ValidationWhiteList) Test(org.junit.Test)

Example 2 with ValidationWhiteList

use of com.b2international.snowowl.core.validation.whitelist.ValidationWhiteList in project snow-owl by b2ihealthcare.

the class ValidationRepositoryContext method commit.

void commit() {
    if (!newObjects.isEmpty() || !objectsToDelete.isEmpty()) {
        final Set<String> ruleIdsAffectedByDeletion = Sets.newHashSet();
        service(ValidationRepository.class).write(writer -> {
            writer.putAll(newObjects);
            final Multimap<String, ComponentIdentifier> addToWhitelist = HashMultimap.create();
            newObjects.stream().filter(ValidationWhiteList.class::isInstance).map(ValidationWhiteList.class::cast).forEach(whitelist -> addToWhitelist.put(whitelist.getRuleId(), whitelist.getComponentIdentifier()));
            if (!addToWhitelist.isEmpty()) {
                ExpressionBuilder filter = Expressions.builder();
                for (String ruleId : addToWhitelist.keySet()) {
                    filter.should(Expressions.builder().filter(Expressions.exactMatch(ValidationIssue.Fields.RULE_ID, ruleId)).filter(Expressions.matchAny(ValidationIssue.Fields.AFFECTED_COMPONENT_ID, addToWhitelist.get(ruleId).stream().map(ComponentIdentifier::getComponentId).collect(Collectors.toSet()))).build());
                }
                writer.bulkUpdate(new BulkUpdate<>(ValidationIssue.class, filter.build(), ValidationIssue.Scripts.WHITELIST, ImmutableMap.of("whitelisted", true)));
            }
            final Multimap<String, ComponentIdentifier> removeFromWhitelist = HashMultimap.create();
            ValidationRequests.whiteList().prepareSearch().all().filterByIds(ImmutableSet.copyOf(objectsToDelete.get(ValidationWhiteList.class))).build().execute(this).forEach(whitelist -> removeFromWhitelist.put(whitelist.getRuleId(), whitelist.getComponentIdentifier()));
            if (!removeFromWhitelist.isEmpty()) {
                ExpressionBuilder filter = Expressions.builder();
                for (String ruleId : removeFromWhitelist.keySet()) {
                    ruleIdsAffectedByDeletion.add(ruleId);
                    filter.should(Expressions.builder().filter(Expressions.exactMatch(ValidationIssue.Fields.RULE_ID, ruleId)).filter(Expressions.matchAny(ValidationIssue.Fields.AFFECTED_COMPONENT_ID, removeFromWhitelist.get(ruleId).stream().map(ComponentIdentifier::getComponentId).collect(Collectors.toSet()))).build());
                }
                writer.bulkUpdate(new BulkUpdate<>(ValidationIssue.class, filter.build(), ValidationIssue.Scripts.WHITELIST, ImmutableMap.of("whitelisted", false)));
            }
            final Map<Class<?>, Set<String>> docsToDelete = newHashMap();
            objectsToDelete.asMap().forEach((type, ids) -> docsToDelete.put(type, ImmutableSet.copyOf(ids)));
            writer.removeAll(docsToDelete);
            writer.commit();
            return null;
        });
        if (!newObjects.isEmpty()) {
            final Set<String> addedWhiteLists = newHashSet();
            final Set<String> affectedRuleIds = newHashSet();
            newObjects.forEach((doc) -> {
                if (doc instanceof ValidationWhiteList) {
                    ValidationWhiteList whitelistedDoc = (ValidationWhiteList) doc;
                    affectedRuleIds.add(whitelistedDoc.getRuleId());
                    addedWhiteLists.add(whitelistedDoc.getId());
                }
            });
            if (!addedWhiteLists.isEmpty()) {
                WhiteListNotification.added(addedWhiteLists, affectedRuleIds).publish(service(IEventBus.class));
            }
        }
        if (!objectsToDelete.isEmpty()) {
            final Set<String> removedWhiteLists = ImmutableSet.copyOf(objectsToDelete.get(ValidationWhiteList.class));
            if (!removedWhiteLists.isEmpty()) {
                WhiteListNotification.removed(removedWhiteLists, ruleIdsAffectedByDeletion).publish(service(IEventBus.class));
            }
        }
    }
}
Also used : Set(java.util.Set) Sets.newHashSet(com.google.common.collect.Sets.newHashSet) ComponentIdentifier(com.b2international.snowowl.core.ComponentIdentifier) ExpressionBuilder(com.b2international.index.query.Expressions.ExpressionBuilder) ValidationWhiteList(com.b2international.snowowl.core.validation.whitelist.ValidationWhiteList) ValidationIssue(com.b2international.snowowl.core.validation.issue.ValidationIssue) IEventBus(com.b2international.snowowl.eventbus.IEventBus)

Aggregations

ValidationWhiteList (com.b2international.snowowl.core.validation.whitelist.ValidationWhiteList)2 ExpressionBuilder (com.b2international.index.query.Expressions.ExpressionBuilder)1 ComponentIdentifier (com.b2international.snowowl.core.ComponentIdentifier)1 ValidationIssue (com.b2international.snowowl.core.validation.issue.ValidationIssue)1 IEventBus (com.b2international.snowowl.eventbus.IEventBus)1 Sets.newHashSet (com.google.common.collect.Sets.newHashSet)1 Set (java.util.Set)1 Test (org.junit.Test)1