/*
 * 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.analyzer.auto.strategy.search;

import com.epam.ta.reportportal.commons.querygen.Filter;
import com.epam.ta.reportportal.dao.LaunchRepository;
import com.epam.ta.reportportal.dao.UserFilterRepository;
import com.epam.ta.reportportal.entity.filter.ObjectType;
import com.epam.ta.reportportal.entity.filter.UserFilter;
import com.epam.ta.reportportal.entity.launch.Launch;
import com.epam.ta.reportportal.exception.ReportPortalException;
import com.epam.ta.reportportal.ws.model.ErrorType;
import com.google.common.collect.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.stream.Collectors;

import static com.epam.ta.reportportal.commons.Predicates.equalTo;
import static com.epam.ta.reportportal.commons.querygen.constant.GeneralCriteriaConstant.CRITERIA_START_TIME;
import static com.epam.ta.reportportal.commons.validation.BusinessRule.expect;
import static com.epam.ta.reportportal.commons.validation.Suppliers.formattedSupplier;

/**
 * @author <a href="mailto:ihar_kahadouski@epam.com">Ihar Kahadouski</a>
 */
@Component
public class FilterCollector implements SearchLaunchesCollector {

	private LaunchRepository launchRepository;

	private UserFilterRepository userFilterRepository;

	@Autowired
	public FilterCollector(LaunchRepository launchRepository, UserFilterRepository userFilterRepository) {
		this.launchRepository = launchRepository;
		this.userFilterRepository = userFilterRepository;
	}

	@Override
	public List<Long> collect(Long filerId, Launch launch) {
		UserFilter userFilter = userFilterRepository.findByIdAndProjectId(filerId, launch.getProjectId())
				.orElseThrow(() -> new ReportPortalException(ErrorType.USER_FILTER_NOT_FOUND_IN_PROJECT, filerId, launch.getProjectId()));
		expect(userFilter.getTargetClass(), equalTo(ObjectType.Launch)).verify(ErrorType.INCORRECT_FILTER_PARAMETERS,
				formattedSupplier("Filter type '{}' is not supported", userFilter.getTargetClass())
		);

		Filter filter = new Filter(userFilter.getTargetClass().getClassObject(), Lists.newArrayList(userFilter.getFilterCondition()));
		PageRequest pageable = PageRequest.of(0, LAUNCHES_FILTER_LIMIT, Sort.by(Sort.Direction.DESC, CRITERIA_START_TIME));
		return launchRepository.findByFilter(filter, pageable).stream().map(Launch::getId).collect(Collectors.toList());
	}
}

@@ -705,32 +705,96 @@
 uerygen.Filter;%0A
+import com.epam.ta.reportportal.commons.querygen.ProjectFilter;%0A
 import com.epam.
@@ -1828,16 +1828,83 @@
 upplier;
+%0Aimport static org.springframework.data.domain.Sort.Direction.DESC;
 %0A%0A/**%0A *
@@ -2672,15 +2672,33 @@
 ;%0A%09%09
-expect(
+ObjectType targetClass = 
 user
@@ -2720,16 +2720,38 @@
 tClass()
+;%0A%09%09expect(targetClass
 , equalT
@@ -2877,31 +2877,17 @@
 orted%22, 
-userFilter.getT
+t
 argetCla
@@ -2888,18 +2888,16 @@
 getClass
-()
 )%0A%09%09);%0A%0A
@@ -2918,34 +2918,42 @@
 r = 
-new Filter(user
+ProjectFilter.of(%0A%09%09%09%09new 
 Filter
-.getT
+(t
 arge
@@ -2958,18 +2958,16 @@
 getClass
-()
 .getClas
@@ -3025,24 +3025,52 @@
 ondition()))
+,%0A%09%09%09%09launch.getProjectId())
 ;%0A%09%09PageRequ
@@ -3133,31 +3133,16 @@
 Sort.by(
-Sort.Direction.
 DESC, CR

Fix analyzer request