It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

User Macro Confluence 7.4 - Can't apply method to $content.lastModificationDate data type

Hello,

I have a user macro (developed by someone else who is no longer available to help debug) which worked in Confluence 6.13.7 but no longer works in 7.4. For context, I'm a Confluence administrator but do not understand Velocity in-depth.

I've determined where the issue is and probably what's happening, but I don't know how to fix it:

#set($review=$content.currentDate)
#set($valid=$content.lastModificationDate)
#set($diff=$review.getTime()-$valid.getTime())

The above code is the offender: the third line uses the .getTime() method on $review and $valid to return the unix time of each date and compare them to finally return a number of milliseconds difference between the two ($diff).

This works fine in 6.13 but in 7.4 it appears that the .getTime() method no longer works on the $valid variable. I suspect that it's because the data type is wrong or something like that. Is it possible to somehow convert the data so I can apply the method to it?

I wrote a very basic macro just to output the contents to the screen for troubleshooting:

## @noparams
#set($review=$content.currentDate)
#set($valid=$content.lastModificationDate)
#set($diff=$review.getTime()-$valid.getTime())

<h2>Vanilla</h2> <br>
$review <br>
$valid <br>
$diff <br>
<br>

<h2>GetTime Method Applied</h2> <br>
$review.getTime() <br>
$valid.getTime() <br>
$diff <br>
<br>

 As an example, this is the output that I get:

Vanilla


Tue Jun 30 10:31:02 CEST 2020
2020-06-30 09:29:37.283
$diff

GetTime Method Applied


1593505862585
$valid.getTime()
$diff

What's really strange is that when I created this same troubleshooting macro in 6.13, the output in the vanilla section for $valid *looks* the same format in both versions of confluence - e.g. *2020-06-30 09:29:37.283* but the data is obviously not the same type.

Any help appreciated!

 

2 answers

1 accepted

0 votes
Answer accepted

Hello all,

In the end I found this post which describes the same issue I was having it looks like a change due to a security issue caused this unexpected behaviour. Atlassian are aware and apparently looking into a fix.

https://community.developer.atlassian.com/t/velocity-compare-2-dates/38614

In the meantime, based on the answers given in the above post, I managed to create a workaround. The post above just wanted to check whether two dates match, whereas I needed to check the difference (i.e. <30 days and >365 days):


#set($modified = $content.lastModificationDate)
#set($review30 = $action.dateFormatter.calendar)
#set($review365 = $action.dateFormatter.calendar)

$review30.setTime($content.currentDate)
$review365.setTime($content.currentDate)

$review30.add(6, -30)
$review365.add(6, -365)

#if ($review30.getTime().compareTo($modified) < 1) Less than 30 days
#elseif ($review365.getTime().compareTo($modified) > -1) Greater than 365 days
#end


This allows me to check $modified against $review30 and $review365 and output (1, 0, -1) depending on the result, and do something, in the above case simply print a message.

Hopefully this will be helpful to someone who comes across the same issue.

Thanks!

0 votes
Danyal Iqbal Community Leader Jun 30, 2020

all public methods are accessible in velocity, just like java. check public methods with the following docu and see if your time method is still there:

context.put('class', new ClassTool());

http://velocity.apache.org/tools/devel/apidocs/org/apache/velocity/tools/generic/ClassTool.html

or some trial and error with:

https://docs.atlassian.com/DAC/javadoc/confluence/4.2.4/reference/com/atlassian/confluence/core/DateFormatter.html

e.g

$action.dateFormatter.formatDateTime($content.getLastModificationDate())

Thanks! I've done quite a lot of digging in the reference documentation and to be honest, as I'm not an engineer by trade it I tend to find the API reference docs pretty arcane. I enlisted some help from an engineer friend, and eventually found a solution - see below

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
SERVER
VERSION
7.4
TAGS
Community showcase
Published in New to Confluence

Fast-track your Confluence onboarding with the Confluence product guides!

Hey there! New in town? Check out the new Confluence product guides! They are chock-full of helpful tips, tricks, and best practices to get you and your team started.  Here’s a quick overview...

82 views 3 1
Read article

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you