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

com.github.zeger_tak.enversvalidationplugin.validate.NullableColumnsValidator Maven / Gradle / Ivy

Go to download

This is a Maven plugin that allows for easy validation of database auditing with Hibernate and is intended to be used by projects that do not always rely on Envers to create the audit history.

There is a newer version: 0.6
Show newest version
package com.github.zeger_tak.enversvalidationplugin.validate;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

import javax.annotation.Nonnull;

import com.github.zeger_tak.enversvalidationplugin.annotation.AuditTableInformationMap;
import com.github.zeger_tak.enversvalidationplugin.annotation.ConnectionProvider;
import com.github.zeger_tak.enversvalidationplugin.annotation.Parameterized;
import com.github.zeger_tak.enversvalidationplugin.annotation.TargetPhase;
import com.github.zeger_tak.enversvalidationplugin.annotation.Validate;
import com.github.zeger_tak.enversvalidationplugin.annotation.ValidationType;
import com.github.zeger_tak.enversvalidationplugin.connection.ConnectionProviderInstance;
import com.github.zeger_tak.enversvalidationplugin.entities.AuditTableInformation;
import com.github.zeger_tak.enversvalidationplugin.exceptions.ValidationException;
import org.dbunit.dataset.DataSetException;

/**
 * The goal of this validator is described in its {@link Validate} methods:
 */
@ValidationType(TargetPhase.CONSTRAINTS)
public class NullableColumnsValidator
{
	private final ConnectionProviderInstance connectionProvider;
	private final AuditTableInformation auditTableInformation;
	private final List primaryIdentifierColumnNames;
	private final Set nonNullColumns;

	public NullableColumnsValidator(@Nonnull ConnectionProviderInstance connectionProvider, @Nonnull AuditTableInformation auditTableInformation, @Nonnull List primaryIdentifierColumnNames, @Nonnull Set nonNullColumns)
	{
		this.connectionProvider = connectionProvider;
		this.auditTableInformation = auditTableInformation;
		this.primaryIdentifierColumnNames = primaryIdentifierColumnNames;
		this.nonNullColumns = nonNullColumns;
	}

	@Parameterized(name = "{index}: auditTableName: {1}", uniqueIdentifier = "{1}")
	public static List generateTestData(@Nonnull @ConnectionProvider ConnectionProviderInstance connectionProvider, @Nonnull @AuditTableInformationMap Map auditTableInformationMap) throws SQLException, DataSetException
	{
		final List testData = new ArrayList<>();
		for (Map.Entry auditTableInformation : auditTableInformationMap.entrySet())
		{
			final List primaryIdentifierColumnNames = connectionProvider.getQueries().getPrimaryKeyColumnNames(auditTableInformation.getKey());
			final Set nonNullColumns = connectionProvider.getQueries().getAllNonnullColumns(auditTableInformation.getKey());

			testData.add(new Object[] { connectionProvider, auditTableInformation.getValue(), primaryIdentifierColumnNames, nonNullColumns });
		}

		return testData;
	}

	/**
	 * Validates that all columns in the audit table are nullable except for the columns that make up the primary key.
	 */
	@Validate
	public void validateAllColumnsExceptPrimaryKeyAreNullable()
	{
		final String revisionTypeColumnName = connectionProvider.getQueries().getRevTypeColumnName();
		final Set invalidNonnullColumnNames = nonNullColumns.stream().filter(c -> !primaryIdentifierColumnNames.contains(c) && !revisionTypeColumnName.equals(c)).collect(Collectors.toSet());

		if (!invalidNonnullColumnNames.isEmpty())
		{
			throw new ValidationException("The following columns for table " + auditTableInformation.getAuditTableName() + " have a not null constraint which prevents remove revisions: " + invalidNonnullColumnNames);
		}
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy