📄 entitymodelbeanvalidator.java
字号:
package com.cownew.studio.modelDev.codeGen;
import java.util.List;
import com.cownew.studio.modelDev.common.CommonUtils;
import com.cownew.studio.modelDev.common.EntityFieldModelBean;
import com.cownew.studio.modelDev.common.EntityModelBean;
import com.cownew.studio.modelDev.common.ModeValidateException;
import com.cownew.studio.modelDev.common.enumdef.LinkTypeEnum;
public class EntityModelBeanValidator
{
public static void validate(EntityModelBean bean)
throws ModeValidateException
{
if (CommonUtils.isEmptyString(bean.getName()))
{
// 实体名不能为空
throw new ModeValidateException(bean, "Name cannot be null!");
}
if (CommonUtils.isEmptyString(bean.getPackageName()))
{
// 实体的包名不能为空
throw new ModeValidateException(bean, "PackageName cannot be null!");
}
if (bean.getFieldList() == null || bean.getFieldList().size() < 1)
{
// 必须定义字段
throw new ModeValidateException(bean, "No Fields defination!");
}
if (CommonUtils.isEmptyString(bean.getDbTableName()))
{
// 数据库表名不能为空
throw new ModeValidateException(bean, "DbTableName cannot be null!");
}
if (CommonUtils.isEmptyString(bean.getPrimaryKey()))
{
// 主键字段不能为空
throw new ModeValidateException(bean, "PrimaryKey cannot be null!");
}
List<EntityFieldModelBean> fieldList = bean.getFieldList();
for (int i = 0, n = fieldList.size(); i < n; i++)
{
// 校验每个字段
validate(bean, fieldList.get(i));
}
}
private static void validate(EntityModelBean bean,
EntityFieldModelBean fieldBean) throws ModeValidateException
{
if (CommonUtils.isEmptyString(fieldBean.getName()))
{
// 字段名不能为空
throw new ModeValidateException(bean, "FieldName cannot be null!");
}
if (fieldBean.isLinkProperty() == false
&& CommonUtils.isEmptyString(fieldBean.getDbFieldName()))
{
// 如果字段不是关联实体字段的话那么表字段不能为空
throw new ModeValidateException(bean, "DbFieldName of "
+ fieldBean.getName() + " cannot be null!");
}
if (fieldBean.getDataype() == null)
{
// 字段类型不能为空
throw new ModeValidateException(bean, "Dataype of "
+ fieldBean.getName() + " cannot be null!");
}
// 如果是关联实体字段则进行关联字段相关的校验
if (fieldBean.isLinkProperty())
{
if (CommonUtils.isEmptyString(fieldBean.getLinkEntity()))
{
// 关联实体不能为空
throw new ModeValidateException(bean,
"LinkEntity cannot be null!");
}
if (fieldBean.getLinkType() == null)
{
// 关联类型不能为空
throw new ModeValidateException(bean,
"LinkType cannot be null!");
}
if (fieldBean.getLinkType() != LinkTypeEnum.ONETOMANY
&& CommonUtils.isEmptyString(fieldBean.getDbFieldName()))
{
// 如果关联类型不等于ONETOMANY的话,则对应的表字段不能为空
throw new ModeValidateException(bean, "DbFieldName of "
+ fieldBean.getName() + " cannot be null!");
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -