List 10 last spaces created with a user macro
Asked by Patrice Champet:

Hi there,

We want to give visibilty to all users on the spaces created, by adding this information on the dashboard.  
So I am trying to create a simple user macro to list the 10 last created spaces.

It was quite easy to list all spaces with creation date.  
But now, I am looking for some help regarding more complex algo to sort and select only the 10 last spaces.

This is my script right now.

----------------------------------------------------------------------------

## @noparams

#set($containerManagerClass=$content.class.forName("com.atlassian.spring.container.ContainerManager"))  
#set($getInstanceMethod=$containerManagerClass.getDeclaredMethod("getInstance",null))  
#set($containerManager=$getInstanceMethod.invoke(null,null))  
#set($containerContext=$containerManager.containerContext)  
#set($spaceLabelManager=$containerContext.getComponent("spaceLabelManager"))

#set ($spaces = $spaceManager.getAllSpaces())  

\<table class="confluenceTable"\>  
\<tr\>  
\<th class="confluenceTh"\>Espace\</th\>  
\<th class="confluenceTh"\>Date de Cr?ation\</th\>  
\<th class="confluenceTh"\>Cat?gorie(s)\</th\>  
\</tr\>  

#foreach ( $space in $spaces )  
#if ($space.isGlobal())  
#end  
#end

#foreach ( $space in $spaces )  
#if ($space.isGlobal())  
\<tr\>  
#set($result=$space.getDescription().getDescriptionTitle())  
#set($creationdate=$space.getCreationDate())  
\<td class="confluenceTd"\>\<a href="$req.contextPath$space.getUrlPath()"\>\<img class="space logo" width="32" height="32" src="$spaceManager.getLogoForSpace($space.key).getDownloadPath()" alt="$space.key"/\> $space.getName()\</a\>\</td\>  
\<td class="confluenceTd"\>$creationdate\</td\>  
\<td class="confluenceTd"\>  
#foreach ($label in $spaceLabelManager.getTeamLabelsOnSpace($space.key))  
\<ac:structured-macro ac:name="status" ac:schema-version="1" ac:macro-id="73f05bec-6b92-413f-a826-3ff2d59096b3"\>\<ac:parameter ac:name="subtle"\>true\</ac:parameter\>\<ac:parameter ac:name="title"\>$label.getName()\</ac:parameter\>\</ac:structured-macro\>  
#end  
\</td\>  
\</tr\>  
#end  
#end  
\</table\>

--------------------------------------------------------------------------------------

Any idea, clue, advice on how to this ?

Thanks !


Answers:
author: JP _AC Bielefeld Leader_
body: Hi,

what about:

<https://community.atlassian.com/t5/Answers-Developer-Questions/Sort-List-of-Objects-in-Velocity/qaq-p/539823>

Best

JP

comments: author: Patrice Champet
body: Hi,

Thanks it helps a lot.

I have now the following user macro, which is working as expected.

------------------------------------------------

## Macro title:  
## Macro has a body: Y or N  
## Body processing: Selected body processing option  
## Output: Selected output option  
##  
## Developed by: My Name  
## Date created: dd/mm/yyyy  
## Installed by: My Name

## @noparams

#set($containerManagerClass=$content.class.forName("com.atlassian.spring.container.ContainerManager"))  
#set($getInstanceMethod=$containerManagerClass.getDeclaredMethod("getInstance",null))  
#set($containerManager=$getInstanceMethod.invoke(null,null))  
#set($containerContext=$containerManager.containerContext)  
#set($spaceLabelManager=$containerContext.getComponent("spaceLabelManager"))

#set ( $spaces = $spaceManager.getAllSpaces() )

###### sort list of space by Creation Date  
#set($size= ${spaces.size()})

#foreach($junk in $spaces) ##Bubble sort takes n\^2 passes  

#set($actual=-1) ##Having trouble with math on $velocityCount -- keeping my own count  
#foreach($line in $spaces )  

#set($actual=$actual+1)  
#if($velocityCount\<$size) ##Preventing bad array access  
##### Compare this and previous  
##### If this is smaller, swap  

#if ($line.getCreationDate().compareTo($spaces.get($velocityCount).getCreationDate())\<0)  
#set ($tmp=$spaces.get($velocityCount))  
#set ($junk=$spaces.set($velocityCount, $line))  
#set ($junk=$spaces.set($actual, $tmp))  
#end  
#end  
#end  
#end

#set ($count = 0)  
#foreach ( $space in $spaces )  
#if($count\>4)  
#break  
#end

#if ($space.isGlobal())  
#set ($count = $count +1)

#set($result=$space.getDescription().getDescriptionTitle())  
#set($creationdate=$space.getCreationDate())

\<a href="$req.contextPath$space.getUrlPath()"\>\<img class="space logo" width="20" height="20" src="$spaceManager.getLogoForSpace($space.key).getDownloadPath()" alt="$space.key"/\> $space.getName()\</a\>  
\| $creationdate \|  
#foreach ($label in $spaceLabelManager.getTeamLabelsOnSpace($space.key))  
\<ac:structured-macro ac:name="status" ac:schema-version="1" ac:macro-id="73f05bec-6b92-413f-a826-3ff2d59096b3"\>\<ac:parameter ac:name="subtle"\>true\</ac:parameter\>\<ac:parameter ac:name="title"\>$label.getName()\</ac:parameter\>\</ac:structured-macro\>  
#end \|  
\<br\>  
#end  
#end

---------------------------------------------------------------------------------------

Only issue is now with the performance, the macro is quite long as it is currently sorting all spaces.

<br />

Is there a way to restrict the ( $spaces = $spaceManager.getAllSpaces() ) to get only Global Spaces ?


author: JP _AC Bielefeld Leader_
body: Hi,

getAllSpaces allows for a space query parameter.

Please check

<https://docs.atlassian.com/atlassian-confluence/6.6.0/index.html?com/atlassian/confluence/spaces/SpaceManager.html>  

Best

JP


author: Patrice Champet
body: Thanks for this answer...

Indeed, this is what I am trying to use, but I face the same issue as <https://community.atlassian.com/t5/Answers-Developer-Questions/How-to-use-SpacesQuery-from-Velocity-template/qaq-p/493572>

I don't know how to use this static class in a user Macro.  
Any advice on how to proceed ?


Tags: confluence-server

server

user-macro