PartnerFilter.java

/*
 * Copyright 2019 Global Crop Diversity Trust
 *
 * 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 org.genesys.server.model.filters;

import static org.genesys.server.model.QPartner.partner;

import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.Setter;
import lombok.experimental.Accessors;
import org.apache.commons.collections4.CollectionUtils;
import org.genesys.blocks.model.filters.StringFilter;
import org.genesys.blocks.model.filters.UuidModelFilter;
import org.genesys.server.model.Partner;
import org.genesys.server.model.QPartner;

import com.fasterxml.jackson.annotation.JsonGetter;
import com.querydsl.core.types.Predicate;

/**
 * The Class PartnerFilter.
 *
 * @author Andrey Lugovskoy.
 */
@Getter
@Setter
@EqualsAndHashCode(callSuper = true)
@Accessors(fluent = true)
public class PartnerFilter extends UuidModelFilter<PartnerFilter, Partner> {

	private static final long serialVersionUID = -6235354214065490627L;

	/** The short name. */
	public Set<String> shortName;

	/** The name. */
	public StringFilter name;

	/** The name. */
	public StringFilter description;

	/** The WIEWS codes. */
	public Set<String> wiewsCodes;

	/**
	 * Builds the query.
	 *
	 * @return the predicate
	 */
	public List<Predicate> collectPredicates() {
		return collectPredicates(partner);
	}

	/**
	 * Collect predicates.
	 *
	 * @param partnerPath the partner path
	 * @return the list
	 */
	public List<Predicate> collectPredicates(final QPartner partnerPath) {
		final List<Predicate> predicates = super.collectPredicates(partnerPath, partnerPath._super);

		if (CollectionUtils.isNotEmpty(shortName)) {
			predicates.add(partnerPath.shortName.in(shortName));
		}
		if (name != null) {
			predicates.add(name.buildQuery(partnerPath.name));
		}
		if (description != null) {
			predicates.add(description.buildQuery(partnerPath.description));
		}
		if (CollectionUtils.isNotEmpty(wiewsCodes)) {
			predicates.add(partnerPath.institutes.isNotEmpty().and(partnerPath.institutes.any().code.in(wiewsCodes())));
		}
		return predicates;
	}

	/**
	 * Wiews codes.
	 *
	 * @return the sets the
	 */
	@JsonGetter("wiewsCodes")
	public Set<String> wiewsCodes() {
		return wiewsCodes != null ? wiewsCodes.stream().map(String::toUpperCase).collect(Collectors.toSet()) : Collections.emptySet();
	}
	
	/**
	 * Name filter.
	 *
	 * @return the string filter
	 */
	public synchronized StringFilter name() {
		return this.name == null ? this.name = new StringFilter() : this.name;
	}

	/**
	 * Short name filter.
	 *
	 * @return the sets the
	 */
	public synchronized Set<String> shortName() {
		return this.shortName == null ? this.shortName = new HashSet<>() : this.shortName;
	}
}