package com.epam.ta.reportportal.dao;

import com.epam.ta.reportportal.commons.querygen.Filter;
import com.epam.ta.reportportal.commons.querygen.FilterCondition;
import com.epam.ta.reportportal.commons.querygen.QueryBuilder;
import com.epam.ta.reportportal.entity.filter.FilterSort;
import com.epam.ta.reportportal.entity.filter.UserFilter;
import com.epam.ta.reportportal.entity.project.Project;
import com.google.common.collect.Lists;
import org.jooq.DSLContext;
import org.jooq.Record;
import org.jooq.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.repository.support.PageableExecutionUtils;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;

import static com.epam.ta.reportportal.dao.constant.WidgetContentRepositoryConstants.*;

@Repository
public class UserFilterRepositoryCustomImpl implements UserFilterRepositoryCustom {

	private static final Function<Result<? extends Record>, List<UserFilter>> USER_FILTER_FETCHER = result -> {
		Map<Long, UserFilter> userFilterMap = new HashMap<>();
		result.forEach(r -> {
			Long userFilterID = r.get("id", Long.class);
			UserFilter userFilter = null;
			if (userFilterMap.containsKey(userFilterID)) {
				userFilter = userFilterMap.get(userFilterID);
			} else {
				userFilter = new UserFilter();
				userFilter.setId(userFilterID);
				userFilter.setName(r.get(NAME, String.class));
				userFilter.setDescription(r.get(DESCRIPTION, String.class));
				userFilter.setTargetClass(r.get(TARGET, Class.class));
				Project project = new Project();
				project.setId(r.get(PROJECT_ID, Long.class));
				userFilter.setProject(project);
				userFilterMap.put(userFilterID, userFilter);
			}
			userFilter.getFilterCondition().add(r.into(FilterCondition.class));
			userFilter.getFilterSorts().add(r.into(FilterSort.class));
		});
		return Lists.newArrayList(userFilterMap.values());
	};

	private final DSLContext dsl;

	@Autowired
	public UserFilterRepositoryCustomImpl(DSLContext dsl) {
		this.dsl = dsl;
	}

	@Override
	public List<UserFilter> findByFilter(Filter filter) {
		return USER_FILTER_FETCHER.apply(dsl.fetch(QueryBuilder.newBuilder(filter).build()));
	}

	@Override
	public Page<UserFilter> findByFilter(Filter filter, Pageable pageable) {
		return PageableExecutionUtils.getPage(
				USER_FILTER_FETCHER.apply(dsl.fetch(QueryBuilder.newBuilder(filter).with(pageable).build())),
				pageable,
				() -> dsl.fetchCount(QueryBuilder.newBuilder(filter).build())
		);
	}
}

@@ -390,16 +390,133 @@
 roject;%0A
+import com.epam.ta.reportportal.exception.ReportPortalException;%0Aimport com.epam.ta.reportportal.ws.model.ErrorType;%0A
 import c
@@ -1423,12 +1423,10 @@
 get(
-%22id%22
+ID
 , Lo
@@ -1464,15 +1464,8 @@
 lter
- = null
 ;%0A%09%09
@@ -1753,32 +1753,43 @@
 String.class));%0A
+%09%09%09%09try %7B%0A%09
 %09%09%09%09userFilter.s
@@ -1806,36 +1806,181 @@
 ass(
-r.get(TARGET, Class.class));
+Class.forName(r.get(TARGET, String.class)));%0A%09%09%09%09%7D catch (ClassNotFoundException e) %7B%0A%09%09%09%09%09throw new ReportPortalException(ErrorType.UNCLASSIFIED_REPORT_PORTAL_ERROR);%0A%09%09%09%09%7D
 %0A%09%09%09

fix user filter fetcher