Expand my Community achievements bar.

Not able to save metadata through Groovy

Avatar

Level 2

Dear All,

 

We are migrating content from Sitecore to AEM.

 

For this below is the one Sitecore content  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 SCRIPT **************************

I have written the below script for setting the metadata after creation of ditamap.

 

 
 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}"
        }
    }

 

Here my metadata is not saving and not working. I am getting the topicBodyItemTitle value dynamically. But metadata is not saving and also it should save like if TOPICBODY title is topicBodyItemTitle  then it should save the metadata related to topicbody and not conceptbody. I am not able to achieve this. Can somebody please help me here.

 

Thanks in advance.

Topics

Topics help categorize Community content and increase your ability to discover relevant content.

7 Replies

Avatar

Community Advisor

@SUNITACH1 can you post the aem node structure you are trying to achieve as output to above code? If not already not known, I would recommend to create one test ditamap and a associated topic with Metadata from UI and then go to crx/de and then follow same structure in script

Avatar

Level 2

Here is the aem node structure

 

SUNITACH1_0-1711161449372.png

 

As I have highlighted the POC id should be TXC-20096525 for the treatment.ditamap. Here I am getting the issue only for ditamap

 

and my metadata I am updating like below, as topicbody is an array.

 

def metadataNode = session.getNode(newTopicPath + "/jcr:content/metadata")
//out.println "metadataNode for updateDitaMetaDataForAME: ${metadataNode}"
if (metadataNode == null) {
out.println "Metadata node missing for the asset ${newTopicPath}"
return
}
else {

metadataNode.setProperty("mc:assignedEditor", topicBody.assignedEditor[0])
metadataNode.setProperty("mc:PocId", topicBody.pocId[0])
}

Avatar

Community Advisor

@SUNITACH1  There are 2 POCId fields, one is accessible like topicBody.pocId[0] and the second as topicBody.conceptBody.pocId[0]

Which one are you trying to set as property on the metadata node ?

Avatar

Level 2

Hi ,

 

I am trying to update topicBody.pocId[0] , but not able to figure it out How to update the metadata by consuming the JSON.

Avatar

Community Advisor

@SUNITACH1 Just parse this json into a JSON object and then read it accordingly. Once you have the relevant json field, simply use any of the JCR APIs to set these as properties.

 

Something like this should work for reading the json atleast. I tried with the below mentioned version of org.json library on https://gwc-experiment.appspot.com/  You might want to check the version that will work for you. : 


@Grab(group='org.json', module='json', version='20210307')
import org.json.JSONArray
import org.json.JSONObject
import org.json.JSONException

String 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": "",
"pocId": "SEC-2006",
"title": "Observation"
}
],
"title": "Treatment",
"type": "",
"workflowItemManager": "mfadALS07"
},
{
"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": "mfadALS07"
}
]
}
'''

try {
JSONObject jsonObject = new JSONObject(json)
JSONArray topicBody = jsonObject.getJSONArray("topicBody")

topicBody.each { topic ->
String itemName = topic.getString("itemName")
String pocId = topic.getString("pocId")
String title = topic.getString("title")
println("Item Name: $itemName")
println("Poc ID: $pocId")
println("Title: $title")

JSONArray conceptBody = topic.getJSONArray("conceptBody")
conceptBody.each { concept ->
String conceptName = concept.getString("itemName")
String conceptPocId = concept.getString("pocId")
String conceptTitle = concept.getString("title")
println("Concept Name: $conceptName")
println("Concept Poc ID: $conceptPocId")
println("Concept Title: $conceptTitle")
}
}
} catch (JSONException e) {
e.printStackTrace()
}

Avatar

Community Advisor

@SUNITACH1 , Did you find the suggestions from users helpful? Please let us know if more information is required. Otherwise, please mark the answer as correct for posterity. If you have found out solution yourself, please share it with the community.