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

com.ocs.dynamo.ui.composite.layout.PivotSearchLayout Maven / Gradle / Ivy

The newest version!
/*
   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.ocs.dynamo.ui.composite.layout;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.BiFunction;
import java.util.function.Function;
import java.util.function.Supplier;

import com.ocs.dynamo.dao.FetchJoinInformation;
import com.ocs.dynamo.domain.AbstractEntity;
import com.ocs.dynamo.domain.model.EntityModel;
import com.ocs.dynamo.service.BaseService;
import com.ocs.dynamo.ui.composite.form.ModelBasedSearchForm;
import com.ocs.dynamo.ui.composite.grid.GridWrapper;
import com.ocs.dynamo.ui.composite.grid.PivotGridWrapper;
import com.ocs.dynamo.ui.provider.PivotAggregationType;
import com.ocs.dynamo.ui.provider.PivotDataProvider;
import com.ocs.dynamo.ui.provider.PivotedItem;
import com.ocs.dynamo.ui.provider.QueryType;
import com.vaadin.flow.data.provider.SortOrder;
import com.vaadin.flow.function.SerializablePredicate;

import lombok.Getter;
import lombok.Setter;

/**
 * Layout for searching in a pivoted grid
 * 
 * @author Bas Rutten
 *
 * @param 
 * @param 
 */
public class PivotSearchLayout>
		extends AbstractSearchLayout {

	private static final long serialVersionUID = 9118254267715180544L;

	@Getter
	@Setter
	private String columnKeyProperty;

	@Getter
	@Setter
	private List fixedColumnKeys;

	private PivotGridWrapper pivotGridWrapper;

	/**
	 * Header mapping function. Expects the column key and the property
	 */
	@Getter
	@Setter
	private BiFunction headerMapper = (a, b) -> a.toString();

	/**
	 * Sub header mapping function. Expects the column key and the property
	 */
	@Getter
	@Setter
	private BiFunction subHeaderMapper = (a, b) -> b.toString();

	/**
	 * Explicitly overridden header mapper used for exporting only
	 */
	@Getter
	@Setter
	private BiFunction exportHeaderMapper = null;

	/**
	 * Bifunction used to map pivot column subheaders for export only
	 */
	@Getter
	@Setter
	private BiFunction exportSubHeaderMapper = null;

	@Getter
	@Setter
	private Function fixedHeaderMapper = Function.identity();

	@Getter
	@Setter
	private BiFunction customFormatter;

	@Getter
	@Setter
	private List pivotedProperties = new ArrayList<>();

	@Getter
	@Setter
	private List hiddenPivotedProperties = new ArrayList<>();

	@Getter
	@Setter
	private List possibleColumnKeys;

	@Getter
	@Setter
	private String rowKeyProperty;

	@Getter
	@Setter
	private Supplier sizeSupplier;

	@Getter
	@Setter
	private Map aggregationMap = new HashMap<>();

	@Getter
	@Setter
	private Map> aggregationClassMap = new HashMap<>();

	@Getter
	@Setter
	private boolean includeAggregateRow;

	/**
	 * 
	 * @param service
	 * @param entityModel
	 * @param formOptions
	 * @param sortOrder
	 * @param joins
	 */
	public PivotSearchLayout(BaseService service, EntityModel entityModel, FormOptions formOptions,
			SortOrder sortOrder, FetchJoinInformation... joins) {
		super(service, entityModel, QueryType.ID_BASED, formOptions, sortOrder, joins);
	}

	public void addAggregation(String property, PivotAggregationType type, Class clazz) {
		aggregationMap.put(property, type);
		aggregationClassMap.put(property, clazz);
	}

	@Override
	public void clearGridWrapper() {
		super.clearGridWrapper();
		this.pivotGridWrapper = null;
	}

	public PivotGridWrapper constructGridWrapper() {

		PivotGridWrapper wrapper = new PivotGridWrapper(getService(), getEntityModel(),
				QueryType.ID_BASED, getFormOptions(), getComponentContext(), getSearchForm().extractFilter(),
				getSortOrders(), getJoins()) {

			private static final long serialVersionUID = -7522369124218869608L;

			@Override
			protected SerializablePredicate beforeSearchPerformed(SerializablePredicate filter) {
				return PivotSearchLayout.this.beforeSearchPerformed(filter);
			}

			@Override
			protected void postProcessDataProvider(PivotDataProvider provider) {
				PivotSearchLayout.this.postProcessDataProvider(provider);
			}
		};

		wrapper.setRowKeyProperty(getRowKeyProperty());
		wrapper.setColumnKeyProperty(getColumnKeyProperty());
		wrapper.setPivotedProperties(getPivotedProperties());
		wrapper.setHiddenPivotedProperties(getHiddenPivotedProperties());
		wrapper.setHeaderMapper(getHeaderMapper());
		wrapper.setSubHeaderMapper(getSubHeaderMapper());
		wrapper.setFixedHeaderMapper(getFixedHeaderMapper());
		wrapper.setCustomFormatter(getCustomFormatter());
		wrapper.setSizeSupplier(getSizeSupplier());
		wrapper.setPossibleColumnKeys(getPossibleColumnKeys());
		wrapper.setFixedColumnKeys(getFixedColumnKeys());
		wrapper.setAggregationMap(aggregationMap);
		wrapper.setAggregationClassMap(aggregationClassMap);
		wrapper.setIncludeAggregateRow(isIncludeAggregateRow());
		wrapper.setExportHeaderMapper(getExportHeaderMapper());
		wrapper.setExportSubHeaderMapper(getExportSubHeaderMapper());

		postConfigureGridWrapper(wrapper);
		wrapper.build();

		if (getFormOptions().isSearchImmediately()) {
			getSearchForm().setSearchable(wrapper);
		}

		return wrapper;
	}

	@Override
	protected ModelBasedSearchForm constructSearchForm() {
		ModelBasedSearchForm searchForm = new ModelBasedSearchForm(null, getEntityModel(),
				getFormOptions(), getComponentContext(), this.getDefaultFilters(), this.getFieldFilters());

		initSearchForm(searchForm);
		searchForm.build();
		return searchForm;
	}

	@Override
	protected void detailsMode(T entity) {
		// not supported
	}

	@Override
	public GridWrapper getGridWrapper() {
		if (pivotGridWrapper == null) {
			pivotGridWrapper = constructGridWrapper();
		}
		return pivotGridWrapper;
	}

	@Override
	public ModelBasedSearchForm getSearchForm() {
		return (ModelBasedSearchForm) super.getSearchForm();
	}

	/**
	 * Select one or more items
	 * 
	 * @param selectedItems the item or items to select
	 */
	@Override
	public void select(Object selectedItems) {
		// do nothing (not supported)
	}

	/**
	 * Sets a certain search value (for a property with a single value to search on)
	 * 
	 * @param propertyId the property to search for
	 * @param value      the desired value
	 */
	@Override
	public void setSearchValue(String propertyId, Object value) {
		getSearchForm().setSearchValue(propertyId, value);
	}

	/**
	 * Sets a certain search value (for a property with an upper and a lower bound)
	 * 
	 * @param propertyId the property to search for
	 * @param value      the value of the lower bound
	 * @param auxValue   the value of the upper bound
	 */
	@Override
	public void setSearchValue(String propertyId, Object value, Object auxValue) {
		getSearchForm().setSearchValue(propertyId, value, auxValue);
	}

}