/*
 * Copyright 2019 B2i Healthcare Pte Ltd, http://b2i.sg
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.b2international.snowowl.core.validation.issue;

import org.hibernate.validator.constraints.NotEmpty;

import com.b2international.snowowl.core.ServiceProvider;
import com.b2international.snowowl.core.events.Request;
import com.b2international.snowowl.core.internal.validation.ValidationRepository;
import com.b2international.snowowl.core.validation.ValidationDeleteNotification;
import com.b2international.snowowl.core.validation.ValidationRequests;
import com.b2international.snowowl.eventbus.IEventBus;
import com.fasterxml.jackson.annotation.JsonProperty;

/**
 * @since 6.20.0
 */
final class ValidationIssueDeleteRequest implements Request<ServiceProvider, Boolean> {

	private static final long serialVersionUID = 1L;
	
	@JsonProperty
	@NotEmpty
	private final String branch;

	@JsonProperty
	@NotEmpty
	private String toolingId;
	
	ValidationIssueDeleteRequest(String branch, String toolingId) {
		this.branch = branch;
		this.toolingId = toolingId;
	}
	
	@Override
	public Boolean execute(ServiceProvider context) {
		return context.service(ValidationRepository.class).write(writer -> {
			
			ValidationIssues issues = ValidationRequests.issues().prepareSearch()
				.all()
				.filterByBranchPath(branch)
				.filterByTooling(toolingId)
				.build()
				.execute(context);
			
			for (ValidationIssue issue : issues) {
				writer.remove(ValidationIssue.class, issue.getId());
			}
			
			writer.commit();
			
			new ValidationDeleteNotification(branch, toolingId).publish(context.service(IEventBus.class));
			
			return Boolean.TRUE;
		});
	}

}

@@ -13,16 +13,21 @@
 ght 2019
+-2020
  B2i Hea
@@ -692,54 +692,268 @@
 ort 
-org.hibernate.validator.constraints.NotEmpty;%0A
+java.util.Set;%0Aimport java.util.stream.Collectors;%0A%0Aimport org.elasticsearch.common.Strings;%0A%0Aimport com.b2international.index.BulkDelete;%0Aimport com.b2international.index.query.Expressions;%0Aimport com.b2international.index.query.Expressions.ExpressionBuilder;
 %0Aimp
@@ -1296,16 +1296,88 @@
 quests;%0A
+import com.b2international.snowowl.core.validation.rule.ValidationRule;%0A
 import c
@@ -1655,35 +1655,24 @@
 sonProperty%0A
-%09@NotEmpty%0A
 %09private fin
@@ -1709,19 +1709,8 @@
 rty%0A
-%09@NotEmpty%0A
 %09pri
@@ -1926,107 +1926,269 @@
 %7B%0A%09%09
-return context.service(ValidationRepository.class).write(writer -%3E %7B%0A%09%09%09%0A%09%09%09ValidationIssues issues
+ExpressionBuilder query = Expressions.builder();%0A%09%09%0A%09%09if (!Strings.isNullOrEmpty(branch)) %7B%0A%09%09%09query.filter(Expressions.exactMatch(ValidationIssue.Fields.BRANCH_PATH, branch));%0A%09%09%7D%0A%09%09%0A%09%09if (!Strings.isNullOrEmpty(toolingId)) %7B%0A%09%09%09final Set%3CString%3E rulesToDelete
  = V
@@ -2205,20 +2205,19 @@
 equests.
-issu
+rul
 es().pre
@@ -2244,40 +2244,8 @@
 l()%0A
-%09%09%09%09.filterByBranchPath(branch)%0A
 %09%09%09%09
@@ -2310,67 +2310,267 @@
 ext)
-;
 %0A%09%09%09
-%0A%09%09%09for (ValidationIssue issue : issues)
+%09.stream()%0A%09%09%09%09.map(ValidationRule::getId)%0A%09%09%09%09.collect(Collectors.toSet());%0A%09%09%09query.filter(Expressions.matchAny(ValidationIssue.Fields.RULE_ID, rulesToDelete));%0A%09%09%7D%0A%09%09%0A%09%09return context.service(ValidationRepository.class).write(writer -%3E
  %7B%0A%09%09%09
+%0A%09%09
 %09writer.
 remo
@@ -2569,62 +2569,75 @@
 ter.
-remove(ValidationIssue.class, issue.getId());%0A%09%09%09%7D%0A%09%09%09
+bulkDelete(new BulkDelete%3C%3E(ValidationIssue.class, query.build()));
 %0A%09%09%09

use index bulk delete in ValidationIssueDeleteRequest