All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.xlrit.gears.engine.choice.QueryChoicesBuilderImpl Maven / Gradle / Ivy

package com.xlrit.gears.engine.choice;

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

import com.xlrit.gears.base.choice.Choices;
import com.xlrit.gears.base.choice.QueryChoicesBuilder;
import com.xlrit.gears.base.model.Identifiable;
import com.xlrit.gears.engine.meta.EntityInfo;
import com.xlrit.gears.engine.meta.Property;

import com.google.common.base.Preconditions;

public class QueryChoicesBuilderImpl implements QueryChoicesBuilder {

	private final ChoicesContext choicesContext;
	private final Map params;

	private final String fromClause;
	private String whereClause;
	private String sortClause;
	private String elementSelectClause;
	private Selection selection;

	public QueryChoicesBuilderImpl(ChoicesContext choicesContext, String fromClause) {
		this.choicesContext = choicesContext;
		this.fromClause = fromClause;
		this.params = new HashMap<>();
	}

	@Override
	public QueryChoicesBuilder where(String whereClause) {
		Preconditions.checkState(this.whereClause == null, "where clause already set");
		this.whereClause = whereClause;
		return this;
	}

	@Override
	public QueryChoicesBuilder selectElement(String selectClause) {
		Preconditions.checkState(this.elementSelectClause == null, "select clause already set");
		this.elementSelectClause = selectClause;
		return this;
	}

	@Override
	public QueryChoicesBuilder sortedBy(String... sortClauses) {
		Preconditions.checkState(this.sortClause == null, "sort clause already set");
		this.sortClause = String.join(", ", sortClauses);
		return this;
	}

	@Override
	public QueryChoicesBuilder withDefaultLabel(Class elementClass) {
		Preconditions.checkState(this.selection == null, "selection already set");
		EntityInfo entityInfo = choicesContext.metaManager().requireEntityInfo(elementClass);
		Property displayedProperty = entityInfo.getDisplayedProperty();
		this.selection = new TupleSelection(elementSelectClause, displayedProperty);
		return this;
	}

	@Override
	public QueryChoicesBuilder withLabel(String labelSelectClause) {
		Preconditions.checkState(this.selection == null, "selection already set");
		this.selection = new TupleSelection(elementSelectClause, labelSelectClause);
		return this;
	}

	@Override
	public QueryChoicesBuilder withParam(String name, Object value) {
		this.params.put(name, value);
		return this;
	}

	@Override
	public Choices build() {
		Objects.requireNonNull(selection, "Selection must be set");
		return new QueryChoices<>(choicesContext, fromClause, whereClause, selection, sortClause, params);
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy