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

org.zodiac.scaff.crud.AbstractAutoDDLProcessor Maven / Gradle / Ivy

The newest version!
package org.zodiac.scaff.crud;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.zodiac.fastorm.rdb.metadata.RDBTableMetadata;
import org.zodiac.fastorm.rdb.operator.DatabaseOperator;
import org.zodiac.scaff.api.crud.entity.EntityFactory;
import org.zodiac.scaff.crud.config.FastOrmInfo;
import org.zodiac.scaff.crud.entity.factory.MapperEntityFactory;
import org.zodiac.sdk.toolkit.util.ExceptionUtil;

import reactor.core.publisher.Flux;
import reactor.core.scheduler.Schedulers;

import java.time.Duration;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

public abstract class AbstractAutoDDLProcessor {

    protected final Logger log = LoggerFactory.getLogger(getClass());

    private Set entities = new HashSet<>();

    //@Autowired
    private DatabaseOperator operator;

    //@Autowired
    private FastOrmInfo fastOrmInfo;

    //@Autowired
    private EntityTableMetadataResolver resolver;

    //@Autowired
    private EntityFactory entityFactory;

    private boolean reactive;

    protected AbstractAutoDDLProcessor() {
        super();
    }

    protected AbstractAutoDDLProcessor(DatabaseOperator operator, FastOrmInfo fastOrmInfo,
        EntityTableMetadataResolver resolver, EntityFactory entityFactory) {
        this.operator = operator;
        this.fastOrmInfo = fastOrmInfo;
        this.resolver = resolver;
        this.entityFactory = entityFactory;
    }

    public DatabaseOperator getOperator() {
        return operator;
    }

    public AbstractAutoDDLProcessor setOperator(DatabaseOperator operator) {
        this.operator = operator;
        return this;
    }

    public FastOrmInfo getProperties() {
        return fastOrmInfo;
    }

    public AbstractAutoDDLProcessor setProperties(FastOrmInfo fastOrmInfo) {
        this.fastOrmInfo = fastOrmInfo;
        return this;
    }

    public EntityTableMetadataResolver getResolver() {
        return resolver;
    }

    public AbstractAutoDDLProcessor setResolver(EntityTableMetadataResolver resolver) {
        this.resolver = resolver;
        return this;
    }

    public EntityFactory getEntityFactory() {
        return entityFactory;
    }

    public AbstractAutoDDLProcessor setEntityFactory(EntityFactory entityFactory) {
        this.entityFactory = entityFactory;
        return this;
    }

    protected void init() {
        try {
            if (entityFactory instanceof MapperEntityFactory) {
                MapperEntityFactory factory = ((MapperEntityFactory) entityFactory);
                for (EntityInfo entity : entities) {
                    factory.addMapping(entity.getEntityType(), MapperEntityFactory.defaultMapper(entity.getRealType()));
                }
            }
            List entities = this.entities.stream().map(EntityInfo::getRealType).collect(Collectors.toList());
            if (fastOrmInfo.isAutoDdl()) {
                /*Load all table informations.*/
                if (reactive) {
                    Flux.fromIterable(entities)
                        .doOnNext(type -> log.trace("auto ddl for {}", type))
                        .map(resolver::resolve)
                        .flatMap(meta -> operator
                                .ddl()
                                .createOrAlter(meta)
                                .autoLoad(false)
                                .commit()
                                .reactive()
                                .subscribeOn(Schedulers.elastic())
                        )
                        .doOnError((err) -> log.error(err.getMessage(), err))
                        .then()
                        .block(Duration.ofMinutes(5));
                } else {
                    for (Class entity : entities) {
                        log.trace("auto ddl for {}", entity);
                        try {
                            operator.ddl()
                                    .createOrAlter(resolver.resolve(entity))
                                    .autoLoad(false)
                                    .commit()
                                    .sync();
                        } catch (Exception e) {
                            log.error(e.getMessage(), e);
                            throw e;
                        }
                    }
                }
            } else {
                for (Class entity : entities) {
                    RDBTableMetadata metadata = resolver.resolve(entity);
                    operator.getMetadata()
                            .getCurrentSchema()
                            .addTable(metadata);
                }
            }
        } catch (Exception e) {
            ExceptionUtil.chuck(e);
        }
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy