/*
 * Copyright 2011-2017 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.datastore.request;

import java.util.Collection;
import java.util.List;
import java.util.Set;

import com.b2international.commons.CompareUtils;
import com.b2international.commons.options.OptionsBuilder;
import com.b2international.snowowl.core.ServiceProvider;
import com.b2international.snowowl.core.exceptions.BadRequestException;
import com.b2international.snowowl.datastore.request.SearchResourceRequest.OptionKey;
import com.b2international.snowowl.datastore.request.SearchResourceRequest.SortField;
import com.google.common.base.Strings;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Lists;

/**
 * @since 5.2
 */
public abstract class SearchResourceRequestBuilder<B extends SearchResourceRequestBuilder<B, C, R>, C extends ServiceProvider, R> extends ResourceRequestBuilder<B, C, R> {
	
	private static final int MAX_LIMIT = Integer.MAX_VALUE - 1;
	
	private int offset = 0;
	private int limit = 50;
	
	private Set<String> ids = ImmutableSet.of();
	private List<SortField> sortFields = ImmutableList.of();
	private final OptionsBuilder optionsBuilder = OptionsBuilder.newBuilder();
	
	protected SearchResourceRequestBuilder() {
		super();
	}
	
	/**
	 * Sets the offset for the paging of the result set. 
	 * @param offset for paging the result set returned
	 * @return this builder instance
	 */
	public final B setOffset(int offset) {
		this.offset = offset;
		return getSelf();
	}
	
	/**
	 * Sets the limit of the result set returned
	 * @param limit of the result set
	 * @return this builder instance
	 */
	public final B setLimit(int limit) {
		this.limit = limit;
		return getSelf();
	}
	
	/**
	 * Filter by resource identifiers.
	 * @param id - a single identifier to match
	 * @return this builder instance
	 */
	public final B filterById(String id) {
		return filterByIds(ImmutableSet.of(id));
	}
	
	/**
	 * Filter by resource identifiers.
	 * @param ids - a {@link Collection} of identifiers to match
	 * @return this builder instance
	 */
	public final B filterByIds(Collection<String> ids) {
		this.ids = ImmutableSet.copyOf(ids);
		return getSelf();
	}
	
	/**
	 * Sorts the result set by the given sort fields.
	 * 
	 * @param first - the first sort field
	 * @param rest - any remaining sort fields (optional)
	 * @return this builder instance
	 */
	public final B sortBy(SortField first, SortField... rest) {
		return sortBy(Lists.asList(first, rest));
	}

	/**
	 * Sorts the result set by the given sort fields.
	 * 
	 * @param sortFields - the list of fields to sort by, in order
	 * @return this builder instance
	 */
	public final B sortBy(List<SortField> sortFields) {
		this.sortFields = ImmutableList.copyOf(sortFields);
		return getSelf();
	}
	
	/**
	 * Sets the request to return the entire results set as a single 'page'.
	 * @return this builder instance
	 */
	public final B all() {
		return setOffset(0).setLimit(MAX_LIMIT);
	}
	
	/**
	 * Returns a single hit from the result set.
	 * @return this builder instance
	 */
	public final B one() {
		return setOffset(0).setLimit(1);
	}
	
	// XXX: Does not allow empty-ish values
	protected final B addOption(String key, Object value) {
		if (!CompareUtils.isEmpty(value)) {
			optionsBuilder.put(key, value);
		}
		return getSelf();
	}
	
	protected final B addOption(Enum<?> key, Object value) {
		return addOption(key.name(), value);
	}
	
	@Override
	protected ResourceRequest<C, R> create() {
		final SearchResourceRequest<C, R> req = createSearch();
		req.setOffset(offset);
		req.setLimit(Math.min(limit, MAX_LIMIT - offset));
		
		for (final String id : ids) {
			if (Strings.isNullOrEmpty(id)) {
				throw new BadRequestException("ID filter cannot contain empty values");
			}
		}
		
		addOption(OptionKey.COMPONENT_IDS, ids);
		addOption(OptionKey.SORT_BY, sortFields);
		req.setOptions(optionsBuilder.build());
		
		return req;
	}
	
	protected abstract SearchResourceRequest<C, R> createSearch();

}

@@ -732,30 +732,8 @@
 ist;
-%0Aimport java.util.Set;
 %0A%0Aim
@@ -1629,112 +1629,8 @@
 ;%0A%09%0A
-%09private Set%3CString%3E ids = ImmutableSet.of();%0A%09private List%3CSortField%3E sortFields = ImmutableList.of();%0A
 %09pri
@@ -2629,18 +2629,196 @@
 %7B%0A%09%09
-this.ids =
+for (final String id : ids) %7B%0A%09%09%09if (Strings.isNullOrEmpty(id)) %7B%0A%09%09%09%09throw new BadRequestException(%22ID filter cannot contain empty values%22);%0A%09%09%09%7D%0A%09%09%7D%0A%0A%09%09addOption(OptionKey.COMPONENT_IDS,
  Imm
@@ -2834,24 +2834,25 @@
 .copyOf(ids)
+)
 ;%0A%09%09return g
@@ -3393,25 +3393,36 @@
 %7B%0A%09%09
-this.sortFields =
+addOption(OptionKey.SORT_BY,
  Imm
@@ -3446,24 +3446,25 @@
 (sortFields)
+)
 ;%0A%09%09return g
@@ -4319,254 +4319,8 @@
 ));%0A
-%09%09%0A%09%09for (final String id : ids) %7B%0A%09%09%09if (Strings.isNullOrEmpty(id)) %7B%0A%09%09%09%09throw new BadRequestException(%22ID filter cannot contain empty values%22);%0A%09%09%09%7D%0A%09%09%7D%0A%09%09%0A%09%09addOption(OptionKey.COMPONENT_IDS, ids);%0A%09%09addOption(OptionKey.SORT_BY, sortFields);%0A
 %09%09re
@@ -4361,11 +4361,8 @@
 ));%0A
-%09%09%0A
 %09%09re
@@ -4372,24 +4372,24 @@
 n req;%0A%09%7D%0A%09%0A
+
 %09protected a
@@ -4440,11 +4440,10 @@
 arch();%0A
-%0A
 %7D%0A

Move component ID filter and sort fields to Options entirely