DynamoDBRepositoryFactoryBean.java

/*
 * Copyright © 2018 spring-data-dynamodb (https://github.com/prasanna0586/spring-data-dynamodb)
 *
 * 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.socialsignin.spring.data.dynamodb.repository.support;

import org.socialsignin.spring.data.dynamodb.core.DynamoDBOperations;
import org.socialsignin.spring.data.dynamodb.mapping.DynamoDBMappingContext;
import org.socialsignin.spring.data.dynamodb.repository.util.DynamoDBMappingContextProcessor;
import org.socialsignin.spring.data.dynamodb.repository.util.Entity2DynamoDBTableSynchronizer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.repository.Repository;
import org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport;
import org.springframework.data.repository.core.support.RepositoryFactorySupport;
import org.springframework.lang.NonNull;

import java.io.Serializable;

/**
 * Special adapter for Springs {@link org.springframework.beans.factory.FactoryBean} interface to allow easy setup of
 * repository factories via Spring configuration.
 * @param <T> the type of the repository
 * @param <S> the entity type
 * @param <ID> the ID type
 * @author Prasanna Kumar Ramachandran
 */
public class DynamoDBRepositoryFactoryBean<T extends Repository<S, ID>, S, ID extends Serializable>
        extends RepositoryFactoryBeanSupport<T, S, ID> {

    private DynamoDBOperations dynamoDBOperations;
    private Entity2DynamoDBTableSynchronizer<S, ID> tableSynchronizer;
    private DynamoDBMappingContextProcessor<S, ID> dynamoDBMappingContextProcessor;

    /**
     * Creates a new DynamoDBRepositoryFactoryBean for the given repository interface.
     * @param repositoryInterface the repository interface
     */
    public DynamoDBRepositoryFactoryBean(@NonNull Class<? extends T> repositoryInterface) {
        super(repositoryInterface);
    }

    @NonNull
    @Override
    protected RepositoryFactorySupport createRepositoryFactory() {
        assert dynamoDBOperations != null;
        assert tableSynchronizer != null;
        assert dynamoDBMappingContextProcessor != null;
        DynamoDBRepositoryFactory dynamoDBRepositoryFactory = new DynamoDBRepositoryFactory(dynamoDBOperations);
        dynamoDBRepositoryFactory.addRepositoryProxyPostProcessor(tableSynchronizer);
        dynamoDBRepositoryFactory.addRepositoryProxyPostProcessor(dynamoDBMappingContextProcessor);
        return dynamoDBRepositoryFactory;
    }

    /**
     * Sets the DynamoDB mapping context processor.
     * @param dynamoDBMappingContextProcessor the mapping context processor
     */
    @Autowired
    public void setDynamoDBMappingContextProcessor(
            DynamoDBMappingContextProcessor<S, ID> dynamoDBMappingContextProcessor) {
        this.dynamoDBMappingContextProcessor = dynamoDBMappingContextProcessor;
    }

    /**
     * Sets the entity to DynamoDB table synchronizer.
     * @param tableSynchronizer the table synchronizer
     */
    @Autowired
    public void setEntity2DynamoDBTableSynchronizer(Entity2DynamoDBTableSynchronizer<S, ID> tableSynchronizer) {
        this.tableSynchronizer = tableSynchronizer;
    }

    /**
     * Sets the DynamoDB operations.
     * @param dynamoDBOperations the DynamoDB operations
     */
    @Autowired
    public void setDynamoDBOperations(DynamoDBOperations dynamoDBOperations) {
        this.dynamoDBOperations = dynamoDBOperations;
    }

    /**
     * Sets the DynamoDB mapping context.
     * @param dynamoDBMappingContext the mapping context
     */
    @Autowired
    public void setDynamoDBMappingContext(@NonNull DynamoDBMappingContext dynamoDBMappingContext) {
        setMappingContext(dynamoDBMappingContext);
    }
}