com.scalar.db.sql.springdata.ScalarDbJdbcRepositoryFactory Maven / Gradle / Ivy
package com.scalar.db.sql.springdata;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import javax.annotation.Nonnull;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.data.jdbc.core.JdbcAggregateTemplate;
import org.springframework.data.jdbc.core.convert.DataAccessStrategy;
import org.springframework.data.jdbc.core.convert.JdbcConverter;
import org.springframework.data.jdbc.repository.support.JdbcRepositoryFactory;
import org.springframework.data.mapping.callback.EntityCallbacks;
import org.springframework.data.relational.core.dialect.Dialect;
import org.springframework.data.relational.core.mapping.RelationalMappingContext;
import org.springframework.data.relational.core.mapping.RelationalPersistentEntity;
import org.springframework.data.repository.core.RepositoryInformation;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcOperations;
/**
* An extended ${@link JdbcRepositoryFactory} to make ${@link
* org.springframework.data.repository.CrudRepository} internally use ${@link
* ScalarDbJdbcAggregateTemplate}.
*/
public class ScalarDbJdbcRepositoryFactory extends JdbcRepositoryFactory {
// These fields are copied from JdbcRepositoryFactory since the parent's fields are `private` and
// this class can't access them. This might be a bit tricky.
private final ApplicationEventPublisher publisher;
private final RelationalMappingContext context;
private final JdbcConverter converter;
private final DataAccessStrategy accessStrategy;
private final NamedParameterJdbcOperations operations;
private EntityCallbacks entityCallbacks;
/**
* Creates a new {@link JdbcRepositoryFactory} for the given {@link DataAccessStrategy}, {@link
* RelationalMappingContext} and {@link ApplicationEventPublisher}.
*
* @param dataAccessStrategy must not be {@literal null}.
* @param context must not be {@literal null}.
* @param converter must not be {@literal null}.
* @param dialect must not be {@literal null}.
* @param publisher must not be {@literal null}.
* @param operations must not be {@literal null}.
*/
@SuppressFBWarnings("EI_EXPOSE_REP2")
public ScalarDbJdbcRepositoryFactory(
DataAccessStrategy dataAccessStrategy,
RelationalMappingContext context,
JdbcConverter converter,
Dialect dialect,
ApplicationEventPublisher publisher,
NamedParameterJdbcOperations operations) {
super(dataAccessStrategy, context, converter, dialect, publisher, operations);
this.publisher = publisher;
this.context = context;
this.converter = converter;
this.accessStrategy = dataAccessStrategy;
this.operations = operations;
}
/**
* Returns a target repository that uses {@link ScalarDbJdbcAggregateTemplate} instead of {@link
* JdbcAggregateTemplate}.
*
* @param repositoryInformation repositoryInformation
* @return ScalarDbJdbcAggregateTemplate to use ScalarDB JDBC
*/
@Override
@Nonnull
protected Object getTargetRepository(@Nonnull RepositoryInformation repositoryInformation) {
JdbcAggregateTemplate template =
new ScalarDbJdbcAggregateTemplate(
publisher, context, converter, accessStrategy, operations);
if (entityCallbacks != null) {
template.setEntityCallbacks(entityCallbacks);
}
RelationalPersistentEntity> persistentEntity =
context.getRequiredPersistentEntity(repositoryInformation.getDomainType());
return getTargetRepositoryViaReflection(
repositoryInformation, template, persistentEntity, converter);
}
@Override
@SuppressFBWarnings("EI_EXPOSE_REP2")
public void setEntityCallbacks(@Nonnull EntityCallbacks entityCallbacks) {
this.entityCallbacks = entityCallbacks;
super.setEntityCallbacks(entityCallbacks);
}
}