You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

无法通过Groovy保存Sitecore迁AEM的元数据求助

问题:Sitecore迁移AEM时DitaMap元数据无法正确保存(仅关联topicBody)

我们正在将内容从Sitecore迁移至AEM,当前编写的Groovy脚本可创建DitaMap,但存在两个问题:

  • 元数据无法保存
  • 无法仅保存与topicBody(而非嵌套的conceptBody)关联的元数据

Sitecore内容JSON示例

"topicBody": [
{
"itemId": "84dbfa1a",
"itemUrl": "",
"itemName": "Test Defect-Treatment",
"__typename": "TreatmentConcept",
"templateId": "c51297ec",
"pocId": "TXC-20096525",
"conceptBody": [
{
"itemId": "54ceda8a",
"itemName": "Test Defect-Closure",
"templateId": "d1a1f112",
"__typename": "KmSection",
"pocId": "SEC-2009",
"title": "Closure"
},
{
"itemId": "84134c21",
"itemName": "Test Defect-Patient Observation",
"templateId": "d1a1f112",
"__typename": "KmSection",
"pocId": "SEC-2006",
"title": "Observation"
}
],
"title": "Treatment",
"type": "",
"workflowItemManager": "mfad\\ALS07"
},
{
"itemId": "01ffe5c6",
"itemUrl": "",
"itemName": "Test Defect-Patient Considerations",
"__typename": "ConditionPatientConsiderationsConcept",
"templateId": "69265f07",
"pocId": "PTC-20117566",
"conceptBody": [
{
"itemId": "3a4f77ab",
"itemName": "Test Defect-Endocarditis Prophylaxis",
"templateId": "d1a1f112",
"__typename": "KmSection",
"pocId": "SEC-20096528",
"title": "Endocarditis prophylaxis"
}
],
"title": "Patient considerations",
"type": "",
"workflowItemManager": "mfad\\ALS07"
}
],

当前Groovy脚本

def createADitaMap(title, topicBody){
    CreateDitaTopic topic = new CreateDitaTopic()
    try {
        def topicBodyItemTitle = title
        def newTopicPath

        if(type == "CPM"){
            JcrUtil.createPath(globalConfigurations.reuseLibraryCpmMaps+"/"+topicBodyItemTitle, JcrResourceConstants.NT_SLING_FOLDER, JcrResourceConstants.NT_SLING_FOLDER, session, true)
            newTopicPath = globalConfigurations.reuseLibraryCpmMaps+"/"+topicBodyItemTitle+"/"+topicBodyItemTitle+".ditamap"
        }else {
            JcrUtil.createPath(globalConfigurations.ameDitaMapBaseFolderPath+"/"+baseFolderForPoc+"/mapsandTopics", JcrResourceConstants.NT_SLING_FOLDER, JcrResourceConstants.NT_SLING_FOLDER, session, true)
            newTopicPath = globalConfigurations.ameDitaMapBaseFolderPath+"/"+baseFolderForPoc+"/mapsandTopics/"+topicBodyItemTitle+".ditamap"
        }

        def lbMapnode = resourceResolver.getResource(newTopicPath)
        if(null == lbMapnode) {
            topic.create(session, title, newTopicPath, globalConfigurations.clinicalKnowledgeditaMapTemplatePath, resourceResolver, true)
            def metadataNode = session.getNode(newTopicPath + "/jcr:content/metadata")
            //out.println "metadataNode for updateDitaMetaDataForAME: ${metadataNode}"
            if (metadataNode != null) {
                metadataNode.setProperty("dc:title", topicBodyItemTitle)
                metadataNode.setProperty("mc:assignedEditor", topicBody.assignedEditor)
                metadataNode.setProperty("mc:PocId", topicBody.pocId)
            }
            
        }
        session.save()
    } catch (NullPointerException npe) { /* Handling transitive API exception. */
        out.println "NullPointer Exception during topic creation in createADitaMap ${npe.getMessage()}"
    }catch (Exception e) {
        out.println "Exception during topic creation in createADitaMap ${e}"
    }
}

解决方案

1. 修复元数据无法保存问题

  • 路径与权限检查:打印newTopicPath + "/jcr:content/metadata"确认路径正确,同时验证session拥有该节点的写入权限。
  • 模板属性验证:确认clinicalKnowledgeditaMapTemplatePath模板包含metadata节点,且dc:title、mc:assignedEditor、mc:PocId属性已定义。若属性不存在,需先创建:
    if (!metadataNode.hasProperty("mc:assignedEditor")) {
        metadataNode.setProperty("mc:assignedEditor", "")
    }
    
  • 增强异常捕获:在属性赋值代码块中添加异常捕获,定位具体错误:
    if (metadataNode != null) {
        try {
            metadataNode.setProperty("dc:title", topicBodyItemTitle)
            metadataNode.setProperty("mc:assignedEditor", topicBody.workflowItemManager)
            metadataNode.setProperty("mc:PocId", topicBody.pocId)
        } catch (Exception e) {
            out.println "Metadata property set failed: ${e.getMessage()}"
        }
    }
    
  • 确认会话保存时机:确保session.save()在所有属性设置完成后执行,未被异常中断。

2. 确保仅保存topicBody层级元数据

  • 修正字段映射:原脚本中topicBody.assignedEditor对应JSON里的workflowItemManager字段,需改为topicBody.workflowItemManager,否则会因字段不存在导致赋值失败。
  • 遍历topicBody数组:从JSON示例看topicBody是数组,需遍历每个元素单独创建DitaMap,调用示例:
    // 解析Sitecore JSON得到topicBody数组
    def topicBodyList = // 你的JSON解析逻辑
    topicBodyList.each { topicItem ->
        createADitaMap(topicItem.title, topicItem)
    }
    
  • 隔离conceptBody:脚本中仅使用传入的topicItem的pocId、workflowItemManager等顶层属性,不访问conceptBody嵌套内容。

3. 脚本优化建议

  • 简化路径拼接:使用GString替代字符串拼接,减少错误:
    newTopicPath = "${globalConfigurations.reuseLibraryCpmMaps}/${topicBodyItemTitle}/${topicBodyItemTitle}.ditamap"
    
  • 父节点检查:创建文件夹前先检查是否存在,避免重复创建:
    def parentFolderPath = "${globalConfigurations.reuseLibraryCpmMaps}/${topicBodyItemTitle}"
    if (!resourceResolver.getResource(parentFolderPath)) {
        JcrUtil.createPath(parentFolderPath, JcrResourceConstants.NT_SLING_FOLDER, session, true)
    }
    

内容的提问来源于stack exchange,提问作者sonu

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.27 04:17:33