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

com.moon.data.web.BaseController Maven / Gradle / Ivy

package com.moon.data.web;

import com.moon.core.lang.StringUtil;
import com.moon.core.util.logger.Logger;
import com.moon.core.util.logger.LoggerUtil;
import com.moon.data.Record;
import com.moon.data.accessor.BaseAccessor;
import com.moon.data.accessor.BaseAccessorImpl;
import com.moon.data.registry.LayerEnum;
import com.moon.data.registry.RecordRegistry;
import com.moon.data.registry.RecordDuplicateRegistryException;
import com.moon.data.service.BaseService;
import com.moon.data.service.BaseStringService;

import javax.annotation.PostConstruct;
import java.util.Optional;
import java.util.function.Supplier;

/**
 * @author moonsky
 */
@SuppressWarnings("all")
public abstract class BaseController, ID> extends BaseAccessorImpl {

    private final static Logger logger = LoggerUtil.getLogger();

    protected BaseController() { this(null); }

    protected BaseController(Class> accessServeClass) {
        this(accessServeClass, null);
    }

    protected BaseController(Class> accessServeClass, Class domainClass) {
        super(accessServeClass, domainClass);
    }

    @PostConstruct
    public void postConstruct() {
        Class domainClass = this.getDomainClass();
        if (domainClass != null) {
            try {
                registryVo2Entity(domainClass);
            } catch (RecordDuplicateRegistryException e) {
                if (logger.isDebugEnabled()) {
                    logger.debug(StringUtil.format("已取消重复注册实例: {}", domainClass));
                }
            }
        }
    }

    @Override
    protected BaseAccessor provideDefaultAccessor() { return getService(); }

    @Override
    protected LayerEnum provideThisLayer() { return LayerEnum.CONTROLLER; }

    /**
     * 目标服务
     *
     * @return
     */
    protected BaseService getService() {
        BaseAccessor accessor = obtainOriginAccessor();
        if (accessor instanceof BaseStringService) {
            return (BaseService) accessor;
        }
        return null;
    }


    /* registry -------------------------------------------------------- */

    protected final  void registryVo2Entity(Class type) {
        registryVo2Entity(type, () -> {
            try {
                return type.newInstance();
            } catch (Exception e) {
                throw new RecordDowngradeBuildException("不能创建实例:" + type);
            }
        });
    }

    protected final  void registryVo2Entity(Supplier defaultValueSupplier) {
        registryVo2Entity(domainClass, defaultValueSupplier);
    }

    protected final  void registryVo2Entity(Class type, Supplier defaultValueSupplier) {
        registryVo2Entity(type, defaultValueSupplier, this::getService);
    }

    protected final  void registryVo2Entity(
        Class type, Supplier defaultEntitySupplier, Supplier serviceSupplier
    ) {
        RecordRegistry.registry(type, id -> {
            if (id == null) {
                return defaultEntitySupplier.get();
            } else if (id instanceof CharSequence && StringUtil.isEmpty((CharSequence) id)) {
                return defaultEntitySupplier.get();
            } else {
                Optional optional = serviceSupplier.get().findById(id);
                return optional.orElseGet(defaultEntitySupplier);
            }
        });
    }

    protected final T getOrNewEntityById(String id) { return getByRegistered(id); }

    protected final  E getByRegistered(String id) { return (E) getByRegistered(domainClass, id); }

    protected static final  E getByRegistered(Class type, String id) {
        return RecordRegistry.getByRegistered(type, id);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy