Expand my Community achievements bar.

SOLVED

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

Avatar

Level 2

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.

 

SUNITACH1_0-1723524557213.png

 

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

 

Thanks & Regards

Sonu

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

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​

View solution in original post

3 Replies

Avatar

Level 4

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-ty...

 

Thanks,

Raju.

Avatar

Correct answer by
Community Advisor

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​

Avatar

Level 2

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)