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

com.kasinf.framework.rest.web.util.SearchableBeanUtils Maven / Gradle / Ivy

The newest version!
package com.kasinf.framework.rest.web.util;

import com.kasinf.framework.rest.repository.BaseRepository;
import com.kasinf.framework.rest.eneity.AbstractEntity;
import com.kasinf.framework.rest.web.service.BaseService;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

/**
 * @author lkhsh
 * 用户保存当前环境中的SearchableService
 */
public final class SearchableBeanUtils {

    private static final String MAPPING_PREFIX = "/";

    private static final Map SERVICE_MAP = new HashMap<>();

    private static final Map REPOSITORY_MAP = new HashMap<>();

    private static final Map> MAPPING_MAP = new HashMap<>();

    /**
     * 将searchable映射添加值静态map中
     *
     * @param mapping 请求路径
     * @param entityClass 实体类
     */
    public static void addMapping(String mapping, Class entityClass) {
        if (mapping.startsWith(MAPPING_PREFIX)) {
            mapping = mapping.substring(1);
        }
        MAPPING_MAP.put(mapping, entityClass);
    }

    public static Class getEntity(String mapping) {
        if (mapping.startsWith(MAPPING_PREFIX)) {
            mapping = mapping.substring(1);
        }
        Class rtn = MAPPING_MAP.get(mapping);
        if (rtn == null) {
            String[] mappings = mapping.split(MAPPING_PREFIX);
            int length = mappings.length;
            for (int i = length - 1; i >= 0; i--) {
                String s = mappings[i];
                mapping = mapping.replace(MAPPING_PREFIX + s, "");
                rtn = MAPPING_MAP.get(mapping);
                if (rtn != null) {
                    break;
                }
            }
        }
        return rtn;
    }


    /**
     * 通过与service绑定的实体类获取对应的service
     *
     * @param className 指定的bean名称
     * @return 对应的service
     */
    public static BaseService getService(String className) {
        return SERVICE_MAP.get(className);
    }

    /**
     * 添加BaseService
     *
     * @param className   类名
     * @param baseService baseService
     */
    public static void addService(String className, BaseService baseService) {
        SERVICE_MAP.put(className, baseService);
    }

    /**
     * 一次性设置Map
     *
     * @param map baseService Map
     */
    public static void setServiceMap(Map map) {
        SERVICE_MAP.putAll(map);
    }

    /**
     * 获取以。。。开头的baseService
     *
     * @param startWith ben的起始名
     * @return bean
     */
    public static BaseService getServiceLike(String startWith) {
        for (String key : SERVICE_MAP.keySet()) {
            if (key.startsWith(startWith + "Service")) {
                return SERVICE_MAP.get(key);
            }
        }
        return null;
    }

    /**
     * 获取以。。。开头的baseRepository
     *
     * @param bean entity名称
     * @return baseRepository
     */
    public static BaseRepository getRepositoryLike(String bean) {
        for (String key : REPOSITORY_MAP.keySet()) {
            if (key.startsWith(bean + "Repository")) {
                return REPOSITORY_MAP.get(key);
            }
        }
        return null;
    }

    /**
     * 初始化repositoryMap
     *
     * @param repositoryMap 存放baseRepository
     */
    public static void setRepositoryMap(Map repositoryMap) {
        REPOSITORY_MAP.putAll(repositoryMap);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy