/*
 * Copyright 2020-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.Collections;
import java.util.List;

import com.b2international.commons.CompareUtils;
import com.b2international.commons.options.Options;
import com.b2international.commons.options.OptionsBuilder;
import com.b2international.snowowl.core.ServiceProvider;
import com.google.common.collect.Lists;

/**
 * @since 7.5
 */
public abstract class IndexResourceRequestBuilder<B extends IndexResourceRequestBuilder<B, C, R>, C extends ServiceProvider, R> extends ResourceRequestBuilder<B, C, R> {

	private Options expand = OptionsBuilder.newBuilder().build();
	private List<String> fields = Collections.emptyList();

	protected IndexResourceRequestBuilder() {}
	
	public final B setExpand(String...expand) {
		return setExpand(String.join(",", expand));
	}
	
	public final B setExpand(List<String> expand) {
		return setExpand(String.join(",", expand));
	}
	
	public final B setExpand(String expand) {
		if (!CompareUtils.isEmpty(expand)) {
			this.expand = ExpandParser.parse(expand);
		}
		return getSelf();
	}

	public final B setExpand(Options expand) {
		if (!CompareUtils.isEmpty(expand)) {
			this.expand = expand;
		}
		return getSelf();
	}
	
	public final B setFields(String first, String... rest) {
		return setFields(Lists.asList(first, rest));
	}
	
	public final B setFields(List<String> fields) {
		if (!CompareUtils.isEmpty(fields)) {
			this.fields = List.copyOf(fields);
		}
		return getSelf();
	}
	
	@Override
	protected void init(ResourceRequest<C, R> req) {
		super.init(req);
		((IndexResourceRequest<C, R>) req).setExpand(expand);
		((IndexResourceRequest<C, R>) req).setFields(fields);
	}

	@Override
	protected abstract IndexResourceRequest<C, R> create();

}

@@ -1380,32 +1380,72 @@
 ing...expand) %7B%0A
+%09%09if (!CompareUtils.isEmpty(expand)) %7B%0A%09
 %09%09return setExpa
@@ -1466,32 +1466,56 @@
 (%22,%22, expand));%0A
+%09%09%7D%0A%09%09return getSelf();%0A
 %09%7D%0A%09%0A%09public fin
@@ -1548,24 +1548,64 @@
 %3E expand) %7B%0A
+%09%09if (!CompareUtils.isEmpty(expand)) %7B%0A%09
 %09%09return set
@@ -1634,24 +1634,48 @@
 , expand));%0A
+%09%09%7D%0A%09%09return getSelf();%0A
 %09%7D%0A%09%0A%09public

check for null/empty values before joining expand values