AuditLogFilter.java

/*
 * Copyright 2018 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.blocks.auditlog.model.filters;

import java.time.Instant;
import java.util.List;
import java.util.Set;

import org.apache.commons.collections4.CollectionUtils;
import org.genesys.blocks.auditlog.model.AuditAction;
import org.genesys.blocks.auditlog.model.AuditLog;
import org.genesys.blocks.auditlog.model.QAuditLog;
import org.genesys.blocks.model.filters.EmptyModelFilter;
import org.genesys.blocks.model.filters.NumberFilter;
import org.genesys.blocks.model.filters.TemporalFilter;

import com.querydsl.core.types.Predicate;

import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.Setter;
import lombok.experimental.Accessors;

/**
 * Search filter.
 */
@Getter
@Setter
@Accessors(fluent = true)
@EqualsAndHashCode(callSuper = true)
public class AuditLogFilter extends EmptyModelFilter<AuditLogFilter, AuditLog> {

	private static final long serialVersionUID = -3179751746560952451L;

	/** The classname. */
	public String classname;

	/** The entity id. */
	public NumberFilter<Long> entityId;

	/** The created by. */
	public Set<Long> createdBy;

	/** The action. */
	public Set<AuditAction> action;

	/** The property name. */
	public Set<String> propertyName;

	/** The log date. */
	public TemporalFilter<Instant> logDate;

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

	/**
	 * Builds the query.
	 *
	 * @param auditLog the auditLog
	 * @return the predicate
	 */
	public List<Predicate> collectPredicates(QAuditLog auditLog) {
		final List<Predicate> predicates = super.collectPredicates(auditLog);

		if (classname != null) {
			predicates.add(auditLog.classPk.classname.eq(classname));
		}
		if (entityId != null) {
			predicates.add(entityId.buildQuery(auditLog.entityId));
		}
		if (CollectionUtils.isNotEmpty(createdBy)) {
			predicates.add(auditLog.createdBy.in(createdBy));
		}
		if (CollectionUtils.isNotEmpty(action)) {
			predicates.add(auditLog.action.in(action));
		}
		if (CollectionUtils.isNotEmpty(propertyName)) {
			predicates.add(auditLog.propertyName.in(propertyName));
		}
		if (logDate != null) {
			predicates.add(logDate.buildQuery(auditLog.logDate));
		}
		return predicates;
	}

}