供稿人:刘少林
为了实现应用部署时的自动初始化,我们通常将数据字典、系统参数、角色、岗位、组织机构等常用基础数据的初始化放在 BootStrap、Updater 等类、或 bootStrapInit 等方法中。
下面整理了部分常见基础数据初始化的代码样例,以供参考。
settingService.createIfNotExists("bropen.framework.resource.attachment.discrete", "false", "boolean", null, null, [notes:"是否启用附件离散化存储,以提高系统安全性"])?.save()
// 创建测试机构与员工
Organization.createIfNotExists("FOO", [[name:"张三", username:"zhangsan", position:"单位领导"]])
Organization.createIfNotExists("FOO/综合部", [[name:"李四", username:"lisi", position:"部门领导"], [name:"王五", username:"wangwu"], [name:"赵六", username:"zhaoliu"]])
Organization.createIfNotExists("FOO/人资部", [[name:"钱七", username:"qianqi"]])
// 部门考勤员岗位
if ( !Position.findByCode("BMKQY") ) {
for ( org in Organization.availableList([result:"bean"]) ) {
new Position(name: "部门考勤员", code: "BMKQY", organization: org
, members: (org.name=="综合部"?Employee.findByUsername("wangwu"):null)
, comment: "用于请假申请流程").save()
}
}
// 考勤专员岗位
if ( !Position.findByCode("HR_KQZY") ) {
def org = Organization.findByName("人资部");
if ( org ) {
new Position(name: "考勤专员", code: "HR_KQZY", organization: org
, members: Employee.findByUsername("qianqi")
, comment: "用于请假申请流程").save()
}
}
// 请假申请统计角色
def pos = Position.findByCode("HR_KQZY")
if ( pos && !Role.findByCode(Role.getCode("HR_ASKFORLEAVE_STATISTICS")) ) {
def role = new Role(type: "HR", groupName:"HR流程"
, code:"HR_ASKFORLEAVE_STATISTICS", name:"请假申请统计"
, comment:"请假申请统计角色")
role.save()
RoleEntity.linkEntity( role, RoleEntity.TYPE_POS, pos.id.toString() )
RoleEntity.linkEntity( role, RoleEntity.TYPE_POS_CODE, "部门领导" )
}
new Permission(code: "/foo/bar", groupName: "分组名", name: "修改项目", roles: [Role.findByCode("XXXX")]).save(flush: true)
messageTemplateService.createTemplateIfNotExists("MESSAGE_TEST_CODE","流标操作短信接口",null, "{name}您好:您投资的项目已流标。", false)
DataDict.createDictIfNotExists( "请假种类", "OA_HR_ASK_FOR_LEAVE_TYPE",
[groupName: "HR流程", notes: "用于请假流程的请假申请明细(foo.oa.hr.AskForLeaveItem)"],
["年假","事假","病假","婚假","产假","丧假","倒休","其它"] )
DataDict.createDictIfNotExists( "户口性质", DD_HUKOU_TYPE ,
[groupName: "人力资源", notes: "员工户口性质"],
[[value:"本市城镇职工", valueKey:"BSCZ"],
[value:"外埠城镇职工", valueKey:"WFCZ"],
[value:"本市农村劳动力", valueKey:"BSNC"],
[value:"外埠农村劳动力", valueKey:"WFNC"]] )
// 以 abc 开头的,允许匿名访问
if ( !Requestmap.findByCode("/abc**") ) Requestmap.create("/abc**", "IS_AUTHENTICATED_ANONYMOUSLY").save()