<commit_before>/*
 * Copyright 2018 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.item.impl.history;

import com.epam.ta.reportportal.auth.ReportPortalUser;
import com.epam.ta.reportportal.commons.Preconditions;
import com.epam.ta.reportportal.commons.Predicates;
import com.epam.ta.reportportal.commons.validation.BusinessRule;
import com.epam.ta.reportportal.core.item.history.TestItemsHistoryHandler;
import com.epam.ta.reportportal.dao.LaunchRepository;
import com.epam.ta.reportportal.dao.TestItemRepository;
import com.epam.ta.reportportal.entity.item.TestItem;
import com.epam.ta.reportportal.entity.launch.Launch;
import com.epam.ta.reportportal.ws.converter.converters.TestItemConverter;
import com.epam.ta.reportportal.ws.model.TestItemHistoryElement;
import com.epam.ta.reportportal.ws.model.TestItemResource;
import com.google.common.collect.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.function.Predicate;
import java.util.stream.Collectors;

import static com.epam.ta.reportportal.ws.model.ErrorType.UNABLE_LOAD_TEST_ITEM_HISTORY;
import static com.epam.ta.reportportal.ws.model.ValidationConstraints.MAX_HISTORY_DEPTH_BOUND;
import static com.epam.ta.reportportal.ws.model.ValidationConstraints.MIN_HISTORY_DEPTH_BOUND;
import static java.lang.Boolean.TRUE;
import static java.util.stream.Collectors.toList;
import static java.util.stream.Collectors.toSet;

/**
 * Creating items history based on {@link TestItem#uniqueId} field
 *
 * @author <NAME>
 */
@Service
public class TestItemsHistoryHandlerImpl implements TestItemsHistoryHandler {

	private TestItemRepository testItemRepository;

	private LaunchRepository launchRepository;

	@Autowired
	public TestItemsHistoryHandlerImpl(TestItemRepository testItemRepository, LaunchRepository launchRepository) {
		this.testItemRepository = testItemRepository;
		this.launchRepository = launchRepository;
	}

	@Override
	public List<TestItemHistoryElement> getItemsHistory(ReportPortalUser.ProjectDetails projectDetails, Long[] startPointsIds,
			int historyDepth, boolean showBrokenLaunches) {

		validateHistoryDepth(historyDepth);
		List<Long> itemIds = Lists.newArrayList(startPointsIds);
		List<TestItem> itemsForHistory = testItemRepository.findAllById(itemIds);
		validateItems(itemsForHistory, itemIds, projectDetails.getProjectId());
		List<Launch> launchesHistory = launchRepository.findLaunchesHistory(historyDepth,
				itemsForHistory.get(0).getLaunch().getName(),
				projectDetails.getProjectId()
		);

		List<TestItem> itemsHistory = testItemRepository.loadItemsHistory(itemsForHistory.stream()
						.map(TestItem::getUniqueId)
						.collect(Collectors.toList()),
				launchesHistory.stream().map(Launch::getId).collect(toList())
		);
		Map<Long, List<TestItem>> groupedByLaunch = itemsHistory.stream().collect(Collectors.groupingBy(it -> it.getLaunch().getId()));
		return launchesHistory.stream().map(l -> buildHistoryElement(l, groupedByLaunch.get(l.getId()))).collect(toList());
	}

	public void validateItems(List<TestItem> itemsForHistory, List<Long> ids, Long projectId) {
		BusinessRule.expect(itemsForHistory, Preconditions.NOT_EMPTY_COLLECTION)
				.verify(UNABLE_LOAD_TEST_ITEM_HISTORY, "Unable to find history for items '" + ids + "'.");

		Set<Long> projectIds = itemsForHistory.stream().map(item -> item.getLaunch().getProjectId()).collect(toSet());<commit_msg>fix compilation errors<commit_after>/*
 * Copyright 2018 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.item.impl.history;

import com.epam.ta.reportportal.auth.ReportPortalUser;
import com.epam.ta.reportportal.commons.Preconditions;
import com.epam.ta.reportportal.commons.Predicates;
import com.epam.ta.reportportal.commons.validation.BusinessRule;
import com.epam.ta.reportportal.core.item.history.TestItemsHistoryHandler;
import com.epam.ta.reportportal.dao.LaunchRepository;
import com.epam.ta.reportportal.dao.TestItemRepository;
import com.epam.ta.reportportal.entity.item.TestItem;
import com.epam.ta.reportportal.entity.launch.Launch;
import com.epam.ta.reportportal.ws.converter.converters.TestItemConverter;
import com.epam.ta.reportportal.ws.model.TestItemHistoryElement;
import com.epam.ta.reportportal.ws.model.TestItemResource;
import com.google.common.collect.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.function.Predicate;
import java.util.stream.Collectors;

import static com.epam.ta.reportportal.ws.model.ErrorType.UNABLE_LOAD_TEST_ITEM_HISTORY;
import static com.epam.ta.reportportal.ws.model.ValidationConstraints.MAX_HISTORY_DEPTH_BOUND;
import static com.epam.ta.reportportal.ws.model.ValidationConstraints.MIN_HISTORY_DEPTH_BOUND;
import static java.lang.Boolean.TRUE;
import static java.util.stream.Collectors.toList;
import static java.util.stream.Collectors.toSet;

/**
 * Creating items history based on {@link TestItem#uniqueId} field
 *
 * @author <NAME>
 */
@Service
public class TestItemsHistoryHandlerImpl implements TestItemsHistoryHandler {

	private TestItemRepository testItemRepository;

	private LaunchRepository launchRepository;

	@Autowired
	public TestItemsHistoryHandlerImpl(TestItemRepository testItemRepository, LaunchRepository launchRepository) {
		this.testItemRepository = testItemRepository;
		this.launchRepository = launchRepository;
	}

	@Override
	public List<TestItemHistoryElement> getItemsHistory(ReportPortalUser.ProjectDetails projectDetails, Long[] startPointsIds,
			int historyDepth, boolean showBrokenLaunches) {

		validateHistoryDepth(historyDepth);
		List<Long> itemIds = Lists.newArrayList(startPointsIds);
		List<TestItem> itemsForHistory = testItemRepository.findAllById(itemIds);
		validateItems(itemsForHistory, itemIds, projectDetails.getProjectId());
		List<Launch> launchesHistory = launchRepository.findLaunchesHistory(historyDepth,
				itemsForHistory.get(0).getLaunch().getId(),
				itemsForHistory.get(0).getLaunch().getName(),
				projectDetails.getProjectId()
		);

		List<TestItem> itemsHistory = testItemRepository.loadItemsHistory(itemsForHistory.stream()
						.map(TestItem::getUniqueId)
						.collect(Collectors.toList()),
				launchesHistory.stream().map(Launch::getId).collect(toList())
		);
		Map<Long, List<TestItem>> groupedByLaunch = itemsHistory.stream().collect(Collectors.groupingBy(it -> it.getLaunch().getId()));
		return launchesHistory.stream().map(l -> buildHistoryElement(l, groupedByLaunch.get(l.getId()))).collect(toList());
	}

	public void validateItems(List<TestItem> itemsForHistory, List<Long> ids, Long projectId) {
		BusinessRule.expect(itemsForHistory, Preconditions.NOT_EMPTY_COLLECTION)
				.verify(UNABLE_LOAD_TEST_ITEM_HISTORY, "Unable to find history for items '" + ids + "'.");

		Set<Long> projectIds = itemsForHistory.stream().map(item -> item.getLaunch().getProjectId()).collect(toSet());
{'fraction_non_alphanumeric': 0.10491318803103065, 'fraction_numerical': 0.0023396133481098386, 'mean_word_length': 6.143359718557607, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 31, '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': '5330460', 'n_tokens_mistral': 2445, 'n_tokens_neox': 2374, 'n_words': 470}