/**
* @author 郑小康
* 1.设置delegatorFullName 基本delegatorName+"#"+tenantId 如果tenantId为空 则就是默认的delegatorName
*
* 2.获取EntityConfig实例,并获取基本delegatorBaseName的delegator标签,并解析为对应的DelegatorElement实例
* <delegator name="default" entity-model-reader="main" entity-group-reader="main"></delegator>
*
* 3.判断delegatorTenantId是否为空,这是租户id
* 第一种情况租户id不为空:获取默认的Delegator,用delegator查询Tenant表中当前tenantId的对应GenericValue
* :获取对应租户的kekText FIXME:暂时未应用 网上搜索说对数据库连接密码进行解密的操作
* 第二种情况租户id为空 :获取delegator标签实例的key-encrypting-key
*
* 4.获取ModelReader 检查了实体缓存之类的操作,获取所有ModelEntity
*
* 5.获取所有ModelGroupReader
* 该类的主要操作是构造对应groupCache缓存,将entity-name为k,groupName为v这样存放,并提供一些获取方法,如获取所有组名,根据实体名获取组名
*
* 6.缓存当前delegatorFullName
*
* 7.对实体进行检查 有检查组里面是否有对应实体 实体名是否是保留字 建立视图一个字段是否被引用多次
*
* 8.获取组名集合
*
* 9.遍历delegaot组,通过ThreadPoolExecutor线程池提交Future中任务,对每个组的实体创建到其组对应数据源的数据库
* 调用Future的原因是,是因为建表很耗时间,所以采用异步执行
*
* */
protected GenericDelegator(String delegatorFullName) throws GenericEntityException {
this.setDelegatorNames(delegatorFullName);
//获取基本的delegator中的信息
this.delegatorInfo = EntityConfig.getInstance().getDelegator(delegatorBaseName);
String kekText;
// before continuing, if there is a tenantId use the base delegator to see if it is valid
if (UtilValidate.isNotEmpty(this.delegatorTenantId)) {
Delegator baseDelegator = DelegatorFactory.getDelegator(this.delegatorBaseName);
GenericValue tenant = EntityQuery.use(baseDelegator).from("Tenant").where("tenantId", this.delegatorTenantId).cache(true).queryOne();
if (tenant == null) {
throw new GenericEntityException("No Tenant record found for delegator [" + this.delegatorFullName + "] with tenantId [" + this.delegatorTenantId + "]");
} else if ("Y".equals(tenant.getString("disabled"))) {
throw new GenericEntityException("No Tenant record found for delegator [" + this.delegatorFullName + "] with tenantId [" + this.delegatorTenantId + "]");
}
GenericValue kekValue = EntityQuery.use(baseDelegator).from("TenantKeyEncryptingKey").where("tenantId", getDelegatorTenantId()).cache(true).queryOne();
if (kekValue != null) {
kekText = kekValue.getString("kekText");
} else {
kekText = this.delegatorInfo.getKeyEncryptingKey();
}
} else {
kekText = this.delegatorInfo.getKeyEncryptingKey();
}
this.modelReader = ModelReader.getModelReader(delegatorBaseName);
this.modelGroupReader = ModelGroupReader.getModelGroupReader(delegatorBaseName);
cache = new Cache(delegatorFullName);
//对实体进行检查 有检查组里面是否有对应实体 实体名是否是保留字 建立视图一个字段是否被引用多次
List<String> warningList = new LinkedList<String>();
Debug.logInfo("Doing entity definition check...", module);
ModelEntityChecker.checkEntities(this, warningList);
if (warningList.size() > 0) {
Debug.logWarning("=-=-=-=-= Found " + warningList.size() + " warnings when checking the entity definitions:", module);
for (String warning: warningList) {
Debug.logWarning(warning, module);
}
}
//获取当前delegator中的groupNames集合,遍历创建对应的GenericHelper,同时在数据库中创建未创建的表和字段
Set<String> groupNames = getModelGroupReader().getGroupNames(delegatorBaseName);
List<Future<Void>> futures = new LinkedList<Future<Void>>();
for (String groupName: groupNames) {
futures.add(ExecutionPool.GLOBAL_BATCH.submit(createHelperCallable(groupName)));
}
ExecutionPool.getAllFutures(futures);
// NOTE: doing some things before the ECAs and such to make sure it is in place just in case it is used in a service engine startup thing or something
// setup the crypto class; this also after the delegator is in the cache otherwise we get infinite recursion
this.crypto = new EntityCrypto(this, kekText);
}