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

org.zodiac.scaff.crud.generator.DefaultIdGenerator Maven / Gradle / Ivy

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

import org.zodiac.fastorm.core.DefaultValue;
import org.zodiac.fastorm.core.DefaultValueGenerator;
import org.zodiac.fastorm.rdb.metadata.RDBColumnMetadata;
import org.zodiac.sdk.toolkit.util.ExceptionUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import reactor.core.publisher.Mono;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ExecutionException;

public class DefaultIdGenerator implements DefaultValueGenerator {

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

    private String defaultId = Generators.SNOW_FLAKE;

    private Map mappings = new HashMap<>();

    public DefaultIdGenerator() {
        super();
    }

    public String getDefaultId() {
        return defaultId;
    }

    public DefaultIdGenerator setDefaultId(String defaultId) {
        this.defaultId = defaultId;
        return this;
    }

    public Map getMappings() {
        return mappings;
    }

    public DefaultIdGenerator setMappings(Map mappings) {
        this.mappings = mappings;
        return this;
    }

    @Override
    public String getSortId() {
        return Generators.DEFAULT_ID_GENERATOR;
    }

    @Override
    public DefaultValue generate(RDBColumnMetadata metadata) {
        DefaultValue result = null;
        try {
            result = Mono.justOrEmpty(mappings.get(metadata.getOwner().getName()))
                    .switchIfEmpty(Mono.justOrEmpty(defaultId))
                    .flatMap(id->Mono.justOrEmpty(metadata.findFeature(DefaultValueGenerator.createId(id))))
                    .doOnNext(gen-> log.debug("Use default id generator : {} for column : {}", gen.getSortId(), metadata.getFullName()))
                    .map(gen->gen.generate(metadata))
                    .switchIfEmpty(Mono.error(()->new UnsupportedOperationException("Unsupported generator: " + defaultId)))
                    .toFuture()
                    .get();
        } catch (InterruptedException | ExecutionException e) {
            ExceptionUtil.chuck(e);
        }
        return result;
    }

    @Override
    public String getName() {
        return "DefaultIdGenerator";
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy