<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.dao;

import com.epam.ta.reportportal.entity.enums.StatusEnum;
import com.epam.ta.reportportal.entity.item.TestItem;
import com.epam.ta.reportportal.entity.item.TestItemResults;
import com.epam.ta.reportportal.entity.launch.Launch;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.Collection;
import java.util.List;
import java.util.Optional;
import java.util.stream.Stream;

/**
 * @author <NAME>
 */
public interface TestItemRepository extends ReportPortalRepository<TestItem, Long>, TestItemRepositoryCustom {

	@Query(value = "SELECT * FROM test_item WHERE item_id = (SELECT parent_id FROM test_item WHERE item_id = :childId)", nativeQuery = true)
	Optional<TestItem> findParentByChildId(@Param("childId") Long childId);

	/**
	 * Retrieve the {@link List} of the {@link TestItem#getItemId()} by launch ID, {@link StatusEnum#name()} and {@link TestItem#isHasChildren()} == false
	 *
	 * @param launchId {@link Launch#getId()}
	 * @param status   {@link StatusEnum#name()}
	 * @return the {@link List} of the {@link TestItem#getItemId()}
	 */
	@Query(value = "SELECT test_item.item_id FROM test_item JOIN test_item_results result ON test_item.item_id = result.result_id "
			+ " WHERE test_item.launch_id = :launchId AND NOT test_item.has_children "
			+ " AND result.status = cast(:#{#status.name()} AS STATUS_ENUM) LIMIT :pageSize OFFSET :pageOffset", nativeQuery = true)
	List<Long> findIdsByNotHasChildrenAndLaunchIdAndStatus(@Param("launchId") Long launchId, @Param("status") StatusEnum status,
			@Param("pageSize") Integer limit, @Param("pageOffset") Long offset);

	/**
	 * Retrieve the {@link List} of the {@link TestItem#getItemId()} by launch ID, {@link StatusEnum#name()} and {@link TestItem#isHasChildren()} == true
	 * ordered (DESCENDING) by 'nlevel' of the {@link TestItem#getPath()}
	 *
	 * @param launchId {@link Launch#getId()}
	 * @param status   {@link StatusEnum#name()}
	 * @return the {@link List} of the {@link TestItem#getItemId()}
	 * @see <a href="https://www.postgresql.org/docs/current/ltree.html">https://www.postgresql.org/docs/current/ltree.html</a>
	 */
	@Query(value = "SELECT test_item.item_id FROM test_item JOIN test_item_results result ON test_item.item_id = result.result_id "
			+ " WHERE test_item.launch_id = :launchId AND test_item.has_children AND result.status = cast(:#{#status.name()} AS STATUS_ENUM)"
			+ " ORDER BY nlevel(test_item.path) DESC LIMIT :pageSize OFFSET :pageOffset", nativeQuery = true)
	List<Long> findIdsByHasChildrenAndLaunchIdAndStatusOrderedByPathLevel(@Param("launchId") Long launchId,
			@Param("status") StatusEnum status, @Param("pageSize") Integer limit, @Param("pageOffset") Long offset);

	/**
	 * Retrieve the {@link Stream} of the {@link TestItem#getItemId()} under parent {@link TestItem#getPath()}, {@link StatusEnum#name()}
	 * and {@link TestItem#isHasChildren()} == false
	 *
	 * @param parentPath {@link TestItem#getPath()} of the parent item
	 * @param status     {@link StatusEnum#name()}
	 * @return the {@link List} of the {@link TestItem#getItemId()}
	 */
	@Query(value = "SELECT test_item.item_id FROM test_item JOIN test_item_results result ON test_item.item_id = result.result_id "
			+ " WHERE cast(:parentPath AS LTREE) @> test_item.path AND cast(:parentPath AS LTREE) != test_item.path "
			+ " AND NOT test_item.has_children AND result.status = cast(:#{#status.name()} AS STATUS_ENUM) LIMIT :pageSize OFFSET :pageOffset", nativeQuery = true)
	List<Long> findIdsByNotHasChildrenAndParentPathAndStatus(@Param("parentPath") String parentPath, @Param("status") StatusEnum status,
			@Param("pageSize") Integer limit, @Param("pageOffset") Long offset);

	/**
	 * Retrieve the {@link Stream} of the {@link TestItem#getItemId()} under parent {@link TestItem#getPath()}, {@link StatusEnum#name()}
	 * and {@link TestItem#isHasChildren()} == true ordered (DESCENDING) by 'nlevel' of the {@link TestItem#getPath()}
	 *
	 * @param parentPath {@link TestItem#getPath()} of the parent item
	 * @param status     {@link StatusEnum#name()}
	 * @return the {@link List} of the {@link TestItem#getItemId()}
	 * @see <a href="https://www.postgresql.org/docs/current/ltree.html">https://www.postgresql.org/docs/current/ltree.html</a>
	 */
	@Query(value = "SELECT test_item.item_id FROM test_item JOIN test_item_results result ON test_item.item_id = result.result_id "
			+ " WHERE cast(:parentPath AS LTREE) @> test_item.path AND cast(:parentPath AS LTREE) != test_item.path "
			+ " AND test_item.has_children AND result.status = cast(:#{#status.name()} AS STATUS_ENUM)"
			+ " ORDER BY nlevel(test_item.path) DESC LIMIT :pageSize OFFSET :pageOffset", nativeQuery = true)
	List<Long> findIdsByHasChildrenAndParentPathAndStatusOrderedByPathLevel(@Param("parentPath") String parentPath,
			@Param("status") StatusEnum status, @Param("pageSize") Integer limit, @Param("pageOffset") Long offset);

	List<TestItem> findTestItemsByUniqueId(String uniqueId);

	List<TestItem> findTestItemsByLaunchId(Long launchId);

	Optional<TestItem> findByUuid(String uuid);

	/**
	 * Finds {@link TestItem} by {@link TestItem#getUuid()} and sets a lock on the found 'item' row in the database.
	 * Required for fetching 'item' from the concurrent environment to provide synchronization between dependant entities
	 *
	 * @param uuid {@link TestItem#getUuid()}
	 * @return {@link Optional} with {@link TestItem} object
	 */
	@Query(value = "SELECT * FROM test_item ti WHERE ti.uuid = :uuid FOR UPDATE", nativeQuery = true)
	Optional<TestItem> findByUuidForUpdate(@Param("uuid") String uuid);

	@Query(value = "SELECT ti.uuid FROM test_item ti WHERE ti.uuid = :uuid FOR UPDATE", nativeQuery = true)
	Optional<String> findUuidByUuidForUpdate(@Param("uuid") String uuid);

	/**
	 * Finds all {@link TestItem} by specified launch id
	 *
	 * @param launchId {@link Launch#getId()}
	 * @return {@link List} of {@link TestItem} ordered by {@link TestItem#getStartTime()} ascending<commit_msg>Find by uuid method fix<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.dao;

import com.epam.ta.reportportal.entity.enums.StatusEnum;
import com.epam.ta.reportportal.entity.item.TestItem;
import com.epam.ta.reportportal.entity.item.TestItemResults;
import com.epam.ta.reportportal.entity.launch.Launch;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.Collection;
import java.util.List;
import java.util.Optional;
import java.util.stream.Stream;

/**
 * @author <NAME>
 */
public interface TestItemRepository extends ReportPortalRepository<TestItem, Long>, TestItemRepositoryCustom {

	@Query(value = "SELECT * FROM test_item WHERE item_id = (SELECT parent_id FROM test_item WHERE item_id = :childId)", nativeQuery = true)
	Optional<TestItem> findParentByChildId(@Param("childId") Long childId);

	/**
	 * Retrieve the {@link List} of the {@link TestItem#getItemId()} by launch ID, {@link StatusEnum#name()} and {@link TestItem#isHasChildren()} == false
	 *
	 * @param launchId {@link Launch#getId()}
	 * @param status   {@link StatusEnum#name()}
	 * @return the {@link List} of the {@link TestItem#getItemId()}
	 */
	@Query(value = "SELECT test_item.item_id FROM test_item JOIN test_item_results result ON test_item.item_id = result.result_id "
			+ " WHERE test_item.launch_id = :launchId AND NOT test_item.has_children "
			+ " AND result.status = cast(:#{#status.name()} AS STATUS_ENUM) LIMIT :pageSize OFFSET :pageOffset", nativeQuery = true)
	List<Long> findIdsByNotHasChildrenAndLaunchIdAndStatus(@Param("launchId") Long launchId, @Param("status") StatusEnum status,
			@Param("pageSize") Integer limit, @Param("pageOffset") Long offset);

	/**
	 * Retrieve the {@link List} of the {@link TestItem#getItemId()} by launch ID, {@link StatusEnum#name()} and {@link TestItem#isHasChildren()} == true
	 * ordered (DESCENDING) by 'nlevel' of the {@link TestItem#getPath()}
	 *
	 * @param launchId {@link Launch#getId()}
	 * @param status   {@link StatusEnum#name()}
	 * @return the {@link List} of the {@link TestItem#getItemId()}
	 * @see <a href="https://www.postgresql.org/docs/current/ltree.html">https://www.postgresql.org/docs/current/ltree.html</a>
	 */
	@Query(value = "SELECT test_item.item_id FROM test_item JOIN test_item_results result ON test_item.item_id = result.result_id "
			+ " WHERE test_item.launch_id = :launchId AND test_item.has_children AND result.status = cast(:#{#status.name()} AS STATUS_ENUM)"
			+ " ORDER BY nlevel(test_item.path) DESC LIMIT :pageSize OFFSET :pageOffset", nativeQuery = true)
	List<Long> findIdsByHasChildrenAndLaunchIdAndStatusOrderedByPathLevel(@Param("launchId") Long launchId,
			@Param("status") StatusEnum status, @Param("pageSize") Integer limit, @Param("pageOffset") Long offset);

	/**
	 * Retrieve the {@link Stream} of the {@link TestItem#getItemId()} under parent {@link TestItem#getPath()}, {@link StatusEnum#name()}
	 * and {@link TestItem#isHasChildren()} == false
	 *
	 * @param parentPath {@link TestItem#getPath()} of the parent item
	 * @param status     {@link StatusEnum#name()}
	 * @return the {@link List} of the {@link TestItem#getItemId()}
	 */
	@Query(value = "SELECT test_item.item_id FROM test_item JOIN test_item_results result ON test_item.item_id = result.result_id "
			+ " WHERE cast(:parentPath AS LTREE) @> test_item.path AND cast(:parentPath AS LTREE) != test_item.path "
			+ " AND NOT test_item.has_children AND result.status = cast(:#{#status.name()} AS STATUS_ENUM) LIMIT :pageSize OFFSET :pageOffset", nativeQuery = true)
	List<Long> findIdsByNotHasChildrenAndParentPathAndStatus(@Param("parentPath") String parentPath, @Param("status") StatusEnum status,
			@Param("pageSize") Integer limit, @Param("pageOffset") Long offset);

	/**
	 * Retrieve the {@link Stream} of the {@link TestItem#getItemId()} under parent {@link TestItem#getPath()}, {@link StatusEnum#name()}
	 * and {@link TestItem#isHasChildren()} == true ordered (DESCENDING) by 'nlevel' of the {@link TestItem#getPath()}
	 *
	 * @param parentPath {@link TestItem#getPath()} of the parent item
	 * @param status     {@link StatusEnum#name()}
	 * @return the {@link List} of the {@link TestItem#getItemId()}
	 * @see <a href="https://www.postgresql.org/docs/current/ltree.html">https://www.postgresql.org/docs/current/ltree.html</a>
	 */
	@Query(value = "SELECT test_item.item_id FROM test_item JOIN test_item_results result ON test_item.item_id = result.result_id "
			+ " WHERE cast(:parentPath AS LTREE) @> test_item.path AND cast(:parentPath AS LTREE) != test_item.path "
			+ " AND test_item.has_children AND result.status = cast(:#{#status.name()} AS STATUS_ENUM)"
			+ " ORDER BY nlevel(test_item.path) DESC LIMIT :pageSize OFFSET :pageOffset", nativeQuery = true)
	List<Long> findIdsByHasChildrenAndParentPathAndStatusOrderedByPathLevel(@Param("parentPath") String parentPath,
			@Param("status") StatusEnum status, @Param("pageSize") Integer limit, @Param("pageOffset") Long offset);

	List<TestItem> findTestItemsByUniqueId(String uniqueId);

	List<TestItem> findTestItemsByLaunchId(Long launchId);

	Optional<TestItem> findByUuid(String uuid);

	/**
	 * Finds {@link TestItem#getItemId()} by {@link TestItem#getUuid()} and sets a lock on the found 'item' row in the database.
	 * Required for fetching 'item' from the concurrent environment to provide synchronization between dependant entities
	 *
	 * @param uuid {@link TestItem#getUuid()}
	 * @return {@link Optional} with {@link TestItem} object
	 */
	@Query(value = "SELECT ti.item_id FROM test_item ti WHERE ti.uuid = :uuid FOR UPDATE", nativeQuery = true)
	Optional<Long> findIdByUuidForUpdate(@Param("uuid") String uuid);

	/**
	 * Finds all {@link TestItem} by specified launch id
	 *
	 * @param launchId {@link Launch#getId()}
	 * @return {@link List} of {@link TestItem} ordered by {@link TestItem#getStartTime()} ascending
{'fraction_non_alphanumeric': 0.13075187969924812, 'fraction_numerical': 0.0012030075187969924, 'mean_word_length': 5.403948001925855, 'pattern_counts': {'":': 0, '<': 34, '<?xml version=': 0, '>': 38, 'https://': 8, 'lorem ipsum': 0, 'www.': 10, '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': '2370186', 'n_tokens_mistral': 4133, 'n_tokens_neox': 4071, 'n_words': 1169}