Not Able to Set Date Type in JCR Property in AEM Metadata | Community
Skip to main content
Level 2
August 13, 2024
Solved

Not Able to Set Date Type in JCR Property in AEM Metadata

  • August 13, 2024
  • 2 replies
  • 1316 views

Hi All,

 

I have written the below Groovy script for setting up the one of the Sitecore Date Reviewed value in the AEM metadata as Date type. But the issue is that date is storing as String Type , not as Date Type.

 

************************ Below is the Groovy Script *******************

import java.text.SimpleDateFormat
import java.util.Date

 

def metadataNode = session.getNode(newTopicPath + "/jcr:content/metadata")
if (metadataNode == null) {
LOG.debug("Metadata node missing for the asset ${newTopicPath}")
return
}
else {
metadataNode.setProperty("displayTitle", displayTitle)

SimpleDateFormat reviewDateFormat = new SimpleDateFormat("yyyyMMdd'T'HHmmss'Z'")
def dateSiteCore = "20240213T060000Z" // this date I am getting from Sitecore JSON
def nextReviewDate = parseDate(dateSiteCore, reviewDateFormat)
LOG.debug("NONcpmMetadata reviewedDate == ${nextReviewDate}")
metadataNode.setProperty("mc:nextReviewDate", nextReviewDate)

}
session.save()

 

// Here the method parseDate below

 

def parseDate(inputTime, dateFormat) {

SimpleDateFormat outputFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX")
try {
Date inputDate = dateFormat.parse(inputTime)
return outputFormat.format(inputDate)
} catch (ParseException pe) {
/* Empty Catch Statement, for handling multiple date formats.*/
}
return ""
}

 

After I run the above Script nextReviewDate value is storing as String Type in the metadata but not as Date Type , as shown below screenshot.

 

 

Can anyone Please let me Know if I am missing something.

 

Thanks & Regards

Sonu

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by lukasz-m

Hi @sunitach1,

In general you have to set property type explicitly in other case it will be store as a String, or as a type of variable you are using as value in setProperty method.

To solve the issue you have two options:

  1. Convert the result of parseDate method to java.util.Calendar and use this method setProperty(java.lang.String name, java.util.Calendar value)
  2. Explicitly set type of the value using this method setProperty(java.lang.String name, java.lang.String value, int type)
    replace this line
    metadataNode.setProperty("mc:nextReviewDate", nextReviewDate)​with this line
    metadataNode.setProperty("mc:nextReviewDate", nextReviewDate, PropertyType.DATE)​you may also need to add below import to your script
    import javax.jcr.PropertyType​

2 replies

Level 4
August 13, 2024

Hi Sunitach,

 

Your 'parseDate' method is returning 'String' object that is why the property value is being set as String.

Instead, can you return 'Calendar' as mentioned here - https://experienceleaguecommunities.adobe.com/t5/adobe-experience-manager/setting-jcr-property-of-type-date-with-value-dynamically/m-p/323282

 

Thanks,

Raju.

lukasz-m
Community Advisor
lukasz-mCommunity AdvisorAccepted solution
Community Advisor
August 13, 2024

Hi @sunitach1,

In general you have to set property type explicitly in other case it will be store as a String, or as a type of variable you are using as value in setProperty method.

To solve the issue you have two options:

  1. Convert the result of parseDate method to java.util.Calendar and use this method setProperty(java.lang.String name, java.util.Calendar value)
  2. Explicitly set type of the value using this method setProperty(java.lang.String name, java.lang.String value, int type)
    replace this line
    metadataNode.setProperty("mc:nextReviewDate", nextReviewDate)​with this line
    metadataNode.setProperty("mc:nextReviewDate", nextReviewDate, PropertyType.DATE)​you may also need to add below import to your script
    import javax.jcr.PropertyType​
SUNITACH1Author
Level 2
August 13, 2024

Thanks @lukasz-m and @rajumuddana 

I used both the way and it is coming fine

 

************************** DATE data Type *******************

import javax.jcr.PropertyType
import java.util.Calendar
import java.util.TimeZone

 

//by using import javax.jcr.PropertyType​
SimpleDateFormat reviewDateFormat = new SimpleDateFormat("yyyyMMdd'T'HHmmss'Z'")
def dateSiteCore = "20240213T060000Z" // this date I am getting from Sitecore JSON
def nextReviewDate = parseDate(dateSiteCore, reviewDateFormat)
LOG.debug("NONcpmMetadata reviewedDate == ${nextReviewDate}")
//metadataNode.setProperty("mc:nextReviewDate", nextReviewDate)
metadataNode.setProperty("mc:nextReviewDate", nextReviewDate, PropertyType.DATE)

//by using Calendar
SimpleDateFormat inputFormat = new SimpleDateFormat("yyyyMMdd'T'HHmmss'Z'")
Date date = inputFormat.parse(dateSiteCore)
Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("GMT"))
calendar.setTime(date)
//child.setProperty(cpropertyName,calendar);
metadataNode.setProperty("nextReviewDatemetadata", calendar)