ofbiz实体引擎(七) 检查数据源

时间:2022-05-03
本文章向大家介绍ofbiz实体引擎(七) 检查数据源,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。
/**
     * Check the datasource to make sure the entity definitions are correct, optionally adding missing entities or fields on the server
     *@param modelEntities Map of entityName names and ModelEntity values
     *@param messages List to put any result messages in
     *@param addMissing Flag indicating whether or not to add missing entities and fields on the server
     *
     * 检查数据源确保实体正确定义,选择性添加没有的实体和字段
     */
    public void checkDataSource(Map<String, ModelEntity> modelEntities, List<String> messages, boolean addMissing) throws GenericEntityException {
        genericDAO.checkDb(modelEntities, messages, addMissing);
    }
}

值得一提helper的实例化的是GenericHelperDAO 所以checkDb调用的是GenericHeleper中的方法

 /**
     * @author  郑小康
     *
     * 1.从缓存中获取helperFullName的GenericHelper
     *
     * 2.如果为空根据helperBaseName(localmysql)获取Datasource标签实例
     *
     * 3.根据Datasource标签的helperClass,创造构造器,构建对应实例
     *
     * 4.以HelperFullName为k 实例为v存入到缓存
     *
     * 5.返回当前实例化的GenericHelper
     *
     * */
    public static GenericHelper getHelper(GenericHelperInfo helperInfo) {
        GenericHelper helper = helperCache.get(helperInfo.getHelperFullName());

        if (helper == null) { // don't want to block here
            synchronized (GenericHelperFactory.class) {
                // must check if null again as one of the blocked threads can still enter
                helper = helperCache.get(helperInfo.getHelperFullName());
                if (helper == null) {
                    try {
                        Datasource datasourceInfo = EntityConfig.getDatasource(helperInfo.getHelperBaseName());

                        if (datasourceInfo == null) {
                            throw new IllegalStateException("Could not find datasource definition with name " + helperInfo.getHelperBaseName());
                        }
                        String helperClassName = datasourceInfo.getHelperClass();
                        Class<?> helperClass = null;

                        if (UtilValidate.isNotEmpty(helperClassName)) {
                            try {
                                ClassLoader loader = Thread.currentThread().getContextClassLoader();
                                helperClass = loader.loadClass(helperClassName);
                            } catch (ClassNotFoundException e) {
                                Debug.logWarning(e, module);
                                throw new IllegalStateException("Error loading GenericHelper class "" + helperClassName + "": " + e.getMessage());
                            }
                        }

                        Class<?>[] paramTypes = new Class<?>[] {GenericHelperInfo.class};
                        Object[] params = new Object[] {helperInfo};

                        java.lang.reflect.Constructor<?> helperConstructor = null;

                        if (helperClass != null) {
                            try {
                                helperConstructor = helperClass.getConstructor(paramTypes);
                            } catch (NoSuchMethodException e) {
                                Debug.logWarning(e, module);
                                throw new IllegalStateException("Error loading GenericHelper class "" + helperClassName + "": " + e.getMessage());
                            }
                        }
                        try {
                            helper = (GenericHelper) helperConstructor.newInstance(params);
                        } catch (IllegalAccessException e) {
                            Debug.logWarning(e, module);
                            throw new IllegalStateException("Error loading GenericHelper class "" + helperClassName + "": " + e.getMessage());
                        } catch (InstantiationException e) {
                            Debug.logWarning(e, module);
                            throw new IllegalStateException("Error loading GenericHelper class "" + helperClassName + "": " + e.getMessage());
                        } catch (java.lang.reflect.InvocationTargetException e) {
                            Debug.logWarning(e, module);
                            throw new IllegalStateException("Error loading GenericHelper class "" + helperClassName + "": " + e.getMessage());
                        }

                        if (helper != null)
                            helperCache.put(helperInfo.getHelperFullName(), helper);
                    } catch (SecurityException e) {
                        Debug.logError(e, module);
                        throw new IllegalStateException("Error loading GenericHelper class: " + e.toString());
                    }
                }
            }
        }
        return helper;
    }
/**
     * Check the datasource to make sure the entity definitions are correct, optionally adding missing entities or fields on the server
     *@param modelEntities Map of entityName names and ModelEntity values
     *@param messages List to put any result messages in
     *@param addMissing Flag indicating whether or not to add missing entities and fields on the server
     *
     * 检查数据源确保实体正确定义,选择性添加没有的实体和字段
     */
    public void checkDataSource(Map<String, ModelEntity> modelEntities, List<String> messages, boolean addMissing) throws GenericEntityException {
        genericDAO.checkDb(modelEntities, messages, addMissing);
    }
/**
     * @author 郑小康
     * 根据GenericHelperInfo实例创建DatabaseUtil实例
     *
     * */

    public void checkDb(Map<String, ModelEntity> modelEntities, List<String> messages, boolean addMissing) {
        DatabaseUtil dbUtil = new DatabaseUtil(this.helperInfo);
        dbUtil.checkDb(modelEntities, messages, addMissing);
    }
/**
     * @author jack
     * 1.创建一个线程池,获取dataSource中的max-worker-pool-size,作为最大线程限制
     *
     * 2.tableNames fkTableNames indexTableNames 三个TreeSet<String>在开始都是一样的
     *
     * 3.如果表信息为空,那么就是没有对应数据库
     *
     * 4.获取modelEntities的Modelentity集合
     *
     * 5.获取数据库schemaName
     *
     * 6.将没有添加的表给加到CreateTableCallable这个线程的对象,通过Future模式创建表
     *
     * 7.创建新增加的外键关系
     *
     * 8.创建新增加的索引关系
     *
     * */
    public void checkDb(Map<String, ModelEntity> modelEntities, List<String> colWrongSize, List<String> messages, boolean checkPks, boolean checkFks, boolean checkFkIdx, boolean addMissing) {
       *******代码太长
    }