本文介绍一种基于自定义 aspect 和可继承规则的优雅方案,实现在父文件夹创建子文件夹时自动注入元数据,并使后续子文件(或子文件夹)自动继承该元数据,避免复杂层级判断和硬编码路径逻辑。
在 Alfresco 中,原生规则(Rule)默认支持“继承”(Inheritable)属性,但其行为常被误解:启用继承后,规则会自动应用到所有新创建的子文件夹中,而非仅一次性作用于当前节点。这一特性正是实现“动态子文件夹元数据传播”的关键基础。
我们不依赖“在父规则中手动遍历查找目标文件夹”,而是采用职责分离 + Aspect 标记 + 层级感知脚本的方式:
为父容器文件夹添加自定义 Aspect(如 foo:filingRoot)
用于标识该文件夹是元数据注入的起点(即“归档根目录”)。
在父文件夹上配置一条 可继承 的规则
触发条件建议设为“进入文件夹时”(onCreateNode 或 onUpdateNode),动作执行一个 JavaScript 脚本(如 applyFilingMetadata.js)。
脚本逻辑分层判断(核心):
// applyFilingMetadata.js
if (document.isContainer) {
// 情况1:当前节点是新建的子文件夹,且其父节点标记为 filingRoot
if (document.parent && document.parent.hasAspect("foo:filingRoot")) {
// 应用 filingParent Aspect 并解析命名提取元数据
document.addAspect("foo:filingParent");
var nameParts = document.name.split("_");
if (nameParts.length >= 3) {
document.properties["foo:firstName"] = nameParts[0];
document.properties["foo:lastName"] = nameParts[1];
document.properties["foo:referenceId"] = nameParts[2];
}
document.save();
}
// 情况2:当前节点是更深层的子项(文件或子文件夹),向上查找最近的 filingParent
else {
var parentFiling = search.findNode("PATH:\"/app:company_home/cm:yourParentFolder//.\"")?.children
.filter(function(n) {
return n.hasAspect("foo:filingParent") &&
document.path.indexOf(n.path) === 0;
})
.sort(function(a,b) { return b.path.length - a.path.length; })[0];
if (parentFiling) {
// 继承元数据(注意:需确保子节点支持对应属性)
document.addAspect("foo:filingChild"); // 可选:标记已继承
document.properties["foo:firstName"] = parentFiling.properties["foo:firstName"];
document.properties["foo:lastName"] = parentFiling.properties["foo:lastName"];
document.properties["foo:re
ferenceId"] = parentFiling.properties["foo:referenceId"];
document.save();
}
}
}if (document.hasAspect("foo:filingParent") || document.hasAspect("foo:filingChild")) {
return; // 已处理过,跳过
}该方案摒弃了“在父规则中硬编码子层级识别”的脆弱逻辑,转而利用 Alfresco 原生的 Aspect 标记能力与规则继承机制,构建出可扩展、易维护、符合平台设计哲学的元数据传播体系。只要合理规划 Aspect 层级语义(Root → Parent → Child),即可支撑任意深度的文件结构自动化需求。