CooperatorAttachmentFilter.java

/*
 * Copyright 2023 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 com.querydsl.core.types.dsl.EntityPathBase;
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.NumberFilter;
import org.genesys.blocks.model.filters.StringFilter;
import org.genesys.filerepository.model.RepositoryFile;
import org.gringlobal.model.CooperatorAttachment;
import org.gringlobal.model.QCooperatorAttachment;

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

/**
 * {@link CooperatorAttachment} match by sample filters.
 *
 * @param <T> the generic type
 * @param <R> the generic type
 */
@Getter
@Setter
@EqualsAndHashCode(callSuper = true)
@Accessors(fluent = true)
public abstract class CooperatorAttachmentFilter<T extends CooperatorAttachmentFilter<T, R>, R extends CooperatorAttachment> extends CooperatorOwnedModelFilter<T, R> {
	
	private static final long serialVersionUID = 6533500500863588278L;

	/** The title. */
	public StringFilter title;

	/** The virtual path. */
	public Set<String> virtualPath;

	/** The thumbnail virtual path. */
	public Set<String> thumbnailVirtualPath;

	/** The content type. */
	public Set<String> contentType;

	/** Is web visible. */
	public Boolean webVisible;

	/** The sort order. */
	public NumberFilter<Integer> sortOrder;

	/** The note. */
	public StringFilter note;

	/** The {@link RepositoryFile} identifier. */
	public Set<Long> repositoryFileId;
	
	protected List<Predicate> collectPredicates(final EntityPathBase<R> instance, final QCooperatorAttachment cooperatorAttachment) {
		List<Predicate> predicates = super.collectPredicates(instance, cooperatorAttachment._super);

		if (title != null) {
			predicates.add(title.buildQuery(cooperatorAttachment.title));
		}
		if (CollectionUtils.isNotEmpty(virtualPath)) {
			predicates.add(cooperatorAttachment.virtualPath.in(virtualPath));
		}
		if (CollectionUtils.isNotEmpty(thumbnailVirtualPath)) {
			predicates.add(cooperatorAttachment.thumbnailVirtualPath.in(thumbnailVirtualPath));
		}
		if (CollectionUtils.isNotEmpty(contentType)) {
			predicates.add(cooperatorAttachment.contentType.in(contentType));
		}
		if (webVisible != null) {
			predicates.add(cooperatorAttachment.isWebVisible.eq(convertToString(webVisible)));
		}
		if (sortOrder != null) {
			predicates.add(sortOrder.buildQuery(cooperatorAttachment.sortOrder));
		}
		if (note != null) {
			predicates.add(note.buildQuery(cooperatorAttachment.note));
		}
		if (CollectionUtils.isNotEmpty(repositoryFileId)) {
			predicates.add(cooperatorAttachment.repositoryFile().id.in(repositoryFileId));
		}
		
		return predicates;
	}
}