/*
 * Copyright 2021 B2i Healthcare Pte Ltd, http://b2i.sg
 * 
 * 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.b2international.snowowl.core.request;

import java.util.HashMap;
import java.util.Map;

import com.b2international.index.Hits;
import com.b2international.index.query.Expressions.ExpressionBuilder;
import com.b2international.index.query.SortBy.Builder;
import com.b2international.index.query.SortBy.Order;
import com.b2international.snowowl.core.ResourceTypeConverter;
import com.b2international.snowowl.core.ResourceTypeConverter.Registry;
import com.b2international.snowowl.core.Resources;
import com.b2international.snowowl.core.domain.RepositoryContext;
import com.b2international.snowowl.core.internal.ResourceDocument;
import com.google.common.collect.Maps;

/**
 * @since 8.0
 */
final class ResourceSearchRequest extends BaseResourceSearchRequest<Resources> {

	private static final long serialVersionUID = 1L;
	
	/**
	 * @since 8.0
	 */
	enum OptionKey {

		/**
		 * Filter matches by their resource type.
		 */
		RESOURCE_TYPE,

		/**
		 * Filter matches by their tooling id property.
		 */
		TOOLING_ID,

		/**
		 * Filter matches by their currently associated working branch (exact match).
		 */
		BRANCH,

	}

	@Override
	protected void prepareAdditionalFilters(RepositoryContext context, ExpressionBuilder queryBuilder) {
		addFilter(queryBuilder, OptionKey.RESOURCE_TYPE, String.class, ResourceDocument.Expressions::resourceTypes);
		addFilter(queryBuilder, OptionKey.TOOLING_ID, String.class, ResourceDocument.Expressions::toolingIds);
		addFilter(queryBuilder, OptionKey.BRANCH, String.class, ResourceDocument.Expressions::branchPaths);
	}

	@Override
	protected Resources toCollectionResource(RepositoryContext context, Hits<ResourceDocument> hits) {
		return new ResourceConverter(context, expand(), locales()).convert(hits);
	}

	@Override
	protected Resources createEmptyResult(int limit) {
		return new Resources(limit, 0);
	}
	
	@Override
	protected void toQuerySortBy(RepositoryContext context, Builder sortBuilder, Sort sort) {
		if (sort instanceof SortField) {
			SortField sortField = (SortField) sort;
			if (ResourceSearchRequestBuilder.TYPE_RANK.equals(sortField.getField())) {
				HashMap<String, String> orderMap = Maps.newHashMap();
				Registry registry = context.service(ResourceTypeConverter.Registry.class);
				registry.getResourceTypeConverters().values().forEach(converter -> {
					orderMap.put(converter.getResourceType(), converter.getRank());
				});
				sortBuilder.sortByScript(ResourceSearchRequestBuilder.TYPE_RANK, Map.of("orderByType", orderMap) , sort.isAscending() ? Order.ASC : Order.DESC);
				return;
			}
		}
		super.toQuerySortBy(context, sortBuilder, sort);
	}
	
}

@@ -684,20 +684,16 @@
 va.util.
-Hash
 Map;%0Aimp
@@ -706,19 +706,33 @@
 va.util.
-Map
+stream.Collectors
 ;%0A%0Aimpor
@@ -1265,47 +1265,8 @@
 ent;
-%0Aimport com.google.common.collect.Maps;
 %0A%0A/*
@@ -2711,66 +2711,8 @@
 ) %7B%0A
-%09%09%09%09HashMap%3CString, String%3E orderMap = Maps.newHashMap();%0A
 %09%09%09%09
@@ -2790,16 +2790,47 @@
 s);%0A%09%09%09%09
+Map%3CString, String%3E orderMap = 
 registry
@@ -2871,58 +2871,60 @@
 s().
-forEach(converter -%3E %7B%0A%09%09%09%09%09orderMap.put(converter
+stream().collect(Collectors.toMap(typeDef -%3E typeDef
 .get
@@ -2943,17 +2943,26 @@
 (), 
-converter
+typeDef -%3E typeDef
 .get
@@ -2972,17 +2972,15 @@
 k())
+)
 ;%0A%09%09%09%09
-%7D);
 %0A%09%09%09

refactor code