<commit_before>/*
 * Copyright 2019 EPAM Systems
 *
 * 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.epam.ta.reportportal.core.project.impl;

import com.epam.ta.reportportal.core.analyzer.LogIndexer;
import com.epam.ta.reportportal.core.analyzer.client.AnalyzerServiceClient;
import com.epam.ta.reportportal.core.analyzer.impl.AnalyzerStatusCache;
import com.epam.ta.reportportal.core.analyzer.impl.AnalyzerUtils;
import com.epam.ta.reportportal.core.events.MessageBus;
import com.epam.ta.reportportal.core.events.activity.ProjectIndexEvent;
import com.epam.ta.reportportal.core.events.attachment.DeleteProjectAttachmentsEvent;
import com.epam.ta.reportportal.core.project.DeleteProjectHandler;
import com.epam.ta.reportportal.dao.IssueTypeRepository;
import com.epam.ta.reportportal.dao.ProjectRepository;
import com.epam.ta.reportportal.dao.UserRepository;
import com.epam.ta.reportportal.entity.item.issue.IssueType;
import com.epam.ta.reportportal.entity.project.Project;
import com.epam.ta.reportportal.entity.project.ProjectIssueType;
import com.epam.ta.reportportal.entity.user.User;
import com.epam.ta.reportportal.exception.ReportPortalException;
import com.epam.ta.reportportal.ws.model.*;
import com.google.common.collect.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.function.Predicate;
import java.util.stream.Collectors;

import static com.epam.ta.reportportal.commons.validation.BusinessRule.expect;

/**
 * @author <NAME>
 */
@Service
public class DeleteProjectHandlerImpl implements DeleteProjectHandler {

	private final ProjectRepository projectRepository;

	private final UserRepository userRepository;

	private final LogIndexer logIndexer;

	private final AnalyzerServiceClient analyzerServiceClient;

	private final AnalyzerStatusCache analyzerStatusCache;

	private final MessageBus messageBus;

	private final ApplicationEventPublisher eventPublisher;

	private final IssueTypeRepository issueTypeRepository;

	@Autowired
	public DeleteProjectHandlerImpl(ProjectRepository projectRepository, UserRepository userRepository, LogIndexer logIndexer,
			AnalyzerServiceClient analyzerServiceClient, AnalyzerStatusCache analyzerStatusCache, MessageBus messageBus,
			ApplicationEventPublisher eventPublisher, IssueTypeRepository issueTypeRepository) {
		this.projectRepository = projectRepository;
		this.userRepository = userRepository;
		this.logIndexer = logIndexer;
		this.analyzerServiceClient = analyzerServiceClient;
		this.analyzerStatusCache = analyzerStatusCache;
		this.messageBus = messageBus;
		this.eventPublisher = eventPublisher;
		this.issueTypeRepository = issueTypeRepository;
	}

	@Override
	public OperationCompletionRS deleteProject(Long projectId) {
		Project project = projectRepository.findById(projectId)
				.orElseThrow(() -> new ReportPortalException(ErrorType.PROJECT_NOT_FOUND, projectId));
		Set<Long> defaultIssueTypeIds = issueTypeRepository.getDefaultIssueTypes()
				.stream()
				.map(IssueType::getId)
				.collect(Collectors.toSet());
		Set<IssueType> issueTypesToRemove = project.getProjectIssueTypes()
				.stream()
				.map(ProjectIssueType::getIssueType)
				.filter(issueType -> !defaultIssueTypeIds.contains(issueType.getId()))
				.collect(Collectors.toSet());
		projectRepository.deleteById(projectId);
		issueTypeRepository.deleteAll(issueTypesToRemove);
		logIndexer.deleteIndex(projectId);
		eventPublisher.publishEvent(new DeleteProjectAttachmentsEvent(project.getId()));
		return new OperationCompletionRS("Project with id = '" + projectId + "' has been successfully deleted.");
	}

	@Override
	public OperationCompletionRS deleteProjectIndex(String projectName, String username) {
		expect(analyzerServiceClient.hasClients(), Predicate.isEqual(true)).verify(ErrorType.UNABLE_INTERACT_WITH_INTEGRATION,
				"There are no analyzer deployed."
		);

		Project project = projectRepository.findByName(projectName)
				.orElseThrow(() -> new ReportPortalException(ErrorType.PROJECT_NOT_FOUND, projectName));

		User user = userRepository.findByLogin(username).orElseThrow(() -> new ReportPortalException(ErrorType.USER_NOT_FOUND, username));

		expect(AnalyzerUtils.getAnalyzerConfig(project).isIndexingRunning(), Predicate.isEqual(false)).verify(ErrorType.FORBIDDEN_OPERATION,
				"Index can not be removed until index generation proceeds."
		);
		expect(analyzerStatusCache.getAnalyzeStatus().asMap().containsValue(project.getId()),
				Predicate.isEqual(false)
		).verify(ErrorType.FORBIDDEN_OPERATION, "Index can not be removed until index generation proceeds.");

		logIndexer.deleteIndex(project.getId());
		messageBus.publishActivity(new ProjectIndexEvent(project.getId(), project.getName(), user.getId(), user.getLogin(), false));
		return new OperationCompletionRS("Project index with name = '" + projectName + "' is successfully deleted.");
	}

	@Override
	public DeleteBulkRS deleteProjects(DeleteBulkRQ deleteBulkRQ) {
		List<ReportPortalException> exceptions = Lists.newArrayList();
		List<Long> deleted = Lists.newArrayList();
		deleteBulkRQ.getIds().forEach(projectId -> {
			try {
				deleteProject(projectId);
				deleted.add(projectId);
			} catch (ReportPortalException ex) {
				exceptions.add(ex);
			}
		});
		return new DeleteBulkRS(deleted, Collections.emptyList(), exceptions.stream().map(ex -> {
			ErrorRS errorResponse = new ErrorRS();
			errorResponse.setErrorType(ex.getErrorType());
			errorResponse.setMessage(ex.getMessage());
			return errorResponse;
		}).collect(Collectors.toList()));
	}
}<commit_msg>Bulk project removing response update<commit_after>/*
 * Copyright 2019 EPAM Systems
 *
 * 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.epam.ta.reportportal.core.project.impl;

import com.epam.ta.reportportal.core.analyzer.LogIndexer;
import com.epam.ta.reportportal.core.analyzer.client.AnalyzerServiceClient;
import com.epam.ta.reportportal.core.analyzer.impl.AnalyzerStatusCache;
import com.epam.ta.reportportal.core.analyzer.impl.AnalyzerUtils;
import com.epam.ta.reportportal.core.events.MessageBus;
import com.epam.ta.reportportal.core.events.activity.ProjectIndexEvent;
import com.epam.ta.reportportal.core.events.attachment.DeleteProjectAttachmentsEvent;
import com.epam.ta.reportportal.core.project.DeleteProjectHandler;
import com.epam.ta.reportportal.dao.IssueTypeRepository;
import com.epam.ta.reportportal.dao.ProjectRepository;
import com.epam.ta.reportportal.dao.UserRepository;
import com.epam.ta.reportportal.entity.item.issue.IssueType;
import com.epam.ta.reportportal.entity.project.Project;
import com.epam.ta.reportportal.entity.project.ProjectIssueType;
import com.epam.ta.reportportal.entity.user.User;
import com.epam.ta.reportportal.exception.ReportPortalException;
import com.epam.ta.reportportal.ws.model.*;
import com.google.common.collect.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.function.Predicate;
import java.util.stream.Collectors;

import static com.epam.ta.reportportal.commons.validation.BusinessRule.expect;

/**
 * @author <NAME>
 */
@Service
public class DeleteProjectHandlerImpl implements DeleteProjectHandler {

	private final ProjectRepository projectRepository;

	private final UserRepository userRepository;

	private final LogIndexer logIndexer;

	private final AnalyzerServiceClient analyzerServiceClient;

	private final AnalyzerStatusCache analyzerStatusCache;

	private final MessageBus messageBus;

	private final ApplicationEventPublisher eventPublisher;

	private final IssueTypeRepository issueTypeRepository;

	@Autowired
	public DeleteProjectHandlerImpl(ProjectRepository projectRepository, UserRepository userRepository, LogIndexer logIndexer,
			AnalyzerServiceClient analyzerServiceClient, AnalyzerStatusCache analyzerStatusCache, MessageBus messageBus,
			ApplicationEventPublisher eventPublisher, IssueTypeRepository issueTypeRepository) {
		this.projectRepository = projectRepository;
		this.userRepository = userRepository;
		this.logIndexer = logIndexer;
		this.analyzerServiceClient = analyzerServiceClient;
		this.analyzerStatusCache = analyzerStatusCache;
		this.messageBus = messageBus;
		this.eventPublisher = eventPublisher;
		this.issueTypeRepository = issueTypeRepository;
	}

	@Override
	public OperationCompletionRS deleteProject(Long projectId) {
		Project project = projectRepository.findById(projectId)
				.orElseThrow(() -> new ReportPortalException(ErrorType.PROJECT_NOT_FOUND, projectId));
		return deleteProject(project);
	}

	@Override
	public OperationCompletionRS deleteProjectIndex(String projectName, String username) {
		expect(analyzerServiceClient.hasClients(), Predicate.isEqual(true)).verify(ErrorType.UNABLE_INTERACT_WITH_INTEGRATION,
				"There are no analyzer deployed."
		);

		Project project = projectRepository.findByName(projectName)
				.orElseThrow(() -> new ReportPortalException(ErrorType.PROJECT_NOT_FOUND, projectName));

		User user = userRepository.findByLogin(username).orElseThrow(() -> new ReportPortalException(ErrorType.USER_NOT_FOUND, username));

		expect(AnalyzerUtils.getAnalyzerConfig(project).isIndexingRunning(), Predicate.isEqual(false)).verify(ErrorType.FORBIDDEN_OPERATION,
				"Index can not be removed until index generation proceeds."
		);
		expect(analyzerStatusCache.getAnalyzeStatus().asMap().containsValue(project.getId()),
				Predicate.isEqual(false)
		).verify(ErrorType.FORBIDDEN_OPERATION, "Index can not be removed until index generation proceeds.");

		logIndexer.deleteIndex(project.getId());
		messageBus.publishActivity(new ProjectIndexEvent(project.getId(), project.getName(), user.getId(), user.getLogin(), false));
		return new OperationCompletionRS("Project index with name = '" + projectName + "' is successfully deleted.");
	}

	@Override
	public DeleteBulkRS deleteProjects(DeleteBulkRQ deleteBulkRQ) {
		List<ReportPortalException> exceptions = Lists.newArrayList();
		List<Long> notFound = Lists.newArrayList();
		List<Long> deleted = Lists.newArrayList();
		deleteBulkRQ.getIds().forEach(projectId -> {
			try {
				Optional<Project> project = projectRepository.findById(projectId);
				if (project.isPresent()) {
					deleteProject(project.get());
					deleted.add(projectId);
				} else {
					notFound.add(projectId);
				}
			} catch (ReportPortalException ex) {
				exceptions.add(ex);
			}
		});
		return new DeleteBulkRS(deleted, notFound, exceptions.stream().map(ex -> {
			ErrorRS errorResponse = new ErrorRS();
			errorResponse.setErrorType(ex.getErrorType());
			errorResponse.setMessage(ex.getMessage());
			return errorResponse;
		}).collect(Collectors.toList()));
	}

	private OperationCompletionRS deleteProject(Project project) {
		Set<Long> defaultIssueTypeIds = issueTypeRepository.getDefaultIssueTypes()
				.stream()
				.map(IssueType::getId)
				.collect(Collectors.toSet());
		Set<IssueType> issueTypesToRemove = project.getProjectIssueTypes()
				.stream()
				.map(ProjectIssueType::getIssueType)
				.filter(issueType -> !defaultIssueTypeIds.contains(issueType.getId()))
				.collect(Collectors.toSet());
		projectRepository.deleteById(project.getId());
		issueTypeRepository.deleteAll(issueTypesToRemove);
		logIndexer.deleteIndex(project.getId());
		eventPublisher.publishEvent(new DeleteProjectAttachmentsEvent(project.getId()));
		return new OperationCompletionRS("Project with id = '" + project.getId() + "' has been successfully deleted.");
	}
}
{'fraction_non_alphanumeric': 0.09975042895024178, 'fraction_numerical': 0.0012478552487911402, 'mean_word_length': 6.053355335533554, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6747903', 'n_tokens_mistral': 3852, 'n_tokens_neox': 3580, 'n_words': 730}