/*
 * 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.widget.content.filter;

import com.epam.ta.reportportal.commons.querygen.Condition;
import com.epam.ta.reportportal.commons.querygen.Filter;
import com.epam.ta.reportportal.commons.querygen.FilterCondition;
import com.epam.ta.reportportal.entity.filter.FilterSort;
import com.epam.ta.reportportal.entity.launch.Launch;
import com.epam.ta.reportportal.entity.widget.Widget;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;

import static com.epam.ta.reportportal.commons.querygen.constant.GeneralCriteriaConstant.CRITERIA_PROJECT_ID;
import static java.util.Optional.ofNullable;

/**
 * @author Pavel Bortnik
 */
@Service("productStatusFilterStrategy")
public class ProductStatusFilterStrategy extends AbstractStatisticsFilterStrategy {

	@Override
	protected Map<Filter, Sort> buildFilterSortMap(Widget widget, Long projectId) {
		Map<Filter, Sort> filterSortMap = Maps.newLinkedHashMap();
		Optional.ofNullable(widget.getFilters()).orElse(Collections.emptySet()).forEach(f -> {
			Filter filter = new Filter(f.getId(), f.getTargetClass().getClassObject(), Sets.newLinkedHashSet(f.getFilterCondition()));
			filter.withConditions(buildDefaultFilter(widget, projectId).getFilterConditions());

			Optional<Set<FilterSort>> filterSorts = ofNullable(f.getFilterSorts());

			Sort sort = Sort.by(filterSorts.map(fs -> fs.stream()
					.map(s -> new Sort.Order(s.getDirection(), s.getField()))
					.collect(Collectors.toList())).orElseGet(Collections::emptyList));

			filterSortMap.put(filter, sort);
		});
		return filterSortMap;
	}

	protected Filter buildDefaultFilter(Widget widget, Long projectId) {
		return new Filter(
				Launch.class,
				Sets.newHashSet(new FilterCondition(Condition.EQUALS, false, String.valueOf(projectId), CRITERIA_PROJECT_ID))
		);
	}
}

@@ -1855,17 +1855,26 @@
 forEach(
-f
+userFilter
  -%3E %7B%0A%09%09
@@ -1905,20 +1905,49 @@
 ter(
-f.getId(), f
+%0A%09%09%09%09%09userFilter.getId(),%0A%09%09%09%09%09userFilter
 .get
@@ -1977,17 +1977,22 @@
 bject(),
- 
+%0A%09%09%09%09%09
 Sets.new
@@ -2005,17 +2005,26 @@
 HashSet(
-f
+userFilter
 .getFilt
@@ -2037,16 +2037,20 @@
 ition())
+%0A%09%09%09
 );%0A%09%09%09fi
@@ -2186,17 +2186,26 @@
 ullable(
-f
+userFilter
 .getFilt
@@ -2261,15 +2261,31 @@
 ap(f
-s -%3E fs
+ilterSort -%3E filterSort
 .str
@@ -2300,17 +2300,21 @@
 %09%09%09.map(
-s
+order
  -%3E new 
@@ -2324,17 +2324,21 @@
 t.Order(
-s
+order
 .getDire
@@ -2346,17 +2346,21 @@
 tion(), 
-s
+order
 .getFiel

fix product status building filter sort map