AccessionIprFilter.java

/*
 * Copyright 2022 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.gringlobal.service.filter;

import com.querydsl.core.types.Predicate;
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.TemporalFilter;
import org.genesys.blocks.model.filters.StringFilter;
import org.gringlobal.model.AccessionIpr;
import org.gringlobal.model.QAccessionIpr;

import java.util.Date;
import java.util.List;
import java.util.Set;

/**
 * Filters for {@link AccessionIpr}
 */
@Getter
@Setter
@EqualsAndHashCode(callSuper = true)
@Accessors(fluent = true)
public class AccessionIprFilter extends CooperatorOwnedModelFilter<AccessionIprFilter, AccessionIpr> {

	private static final long serialVersionUID = 8821111992670890751L;

	/** The accepted date. */
	public TemporalFilter<Date> acceptedDate;

	/** The accession. */
	public AccessionFilter accession;

	/** The expected date. */
	public TemporalFilter<Date> expectedDate;

	/** The expired date. */
	public TemporalFilter<Date> expiredDate;

	/** The ipr crop name. */
	public StringFilter iprCropName;

	/** The ipr full name. */
	public StringFilter iprFullName;

	/** The ipr full name. */
	public StringFilter iprNumber;

	/** The issued date. */
	public TemporalFilter<Date> issuedDate;

	/** The type code. */
	public Set<String> typeCode;

	@Override
	public List<Predicate> collectPredicates() {
		return collectPredicates(QAccessionIpr.accessionIpr);
	}

	public List<Predicate> collectPredicates(QAccessionIpr accessionIpr) {
		final List<Predicate> predicates = super.collectPredicates(accessionIpr, accessionIpr._super);

		if (acceptedDate != null) {
			predicates.add(acceptedDate.buildQuery(accessionIpr.acceptedDate));
		}
		if (accession != null) {
			predicates.addAll(accession.collectPredicates(accessionIpr.accession()));
		}
		if (expectedDate != null) {
			predicates.add(expectedDate.buildQuery(accessionIpr.expectedDate));
		}
		if (expiredDate != null) {
			predicates.add(expiredDate.buildQuery(accessionIpr.expiredDate));
		}
		if (iprCropName != null) {
			predicates.add(iprCropName.buildQuery(accessionIpr.iprCropName));
		}
		if (iprFullName != null) {
			predicates.add(iprFullName.buildQuery(accessionIpr.iprFullName));
		}
		if (iprNumber != null) {
			predicates.add(iprNumber.buildQuery(accessionIpr.iprNumber));
		}
		if (issuedDate != null) {
			predicates.add(issuedDate.buildQuery(accessionIpr.issuedDate));
		}
		if (CollectionUtils.isNotEmpty(typeCode)) {
			predicates.add(accessionIpr.typeCode.in(typeCode));
		}

		return predicates;
	}
}