Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

creating map in loop, inconsistent behavior

Łukasz Sielski priv
Contributor
January 29, 2021

Hello,

 

I have the following script which should create map of dashboards of all jira users with owner of this dashboard.

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.portal.PortletConfigurationManager
import com.atlassian.jira.bc.portal.PortalPageService
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.bc.user.search.UserSearchParams
import com.atlassian.jira.bc.user.search.UserSearchService

PortalPageService portalPageService = ComponentAccessor.getComponent(PortalPageService)

UserSearchService userSearchService = ComponentAccessor.getComponent(UserSearchService.class)

UserSearchParams userSearchParams = (new UserSearchParams   
.Builder())
.allowEmptyQuery(true)   
.includeActive(true)
 
.includeInactive(true)
 
.maxResults(20000).build()

def users = userSearchService.findUsers("", userSearchParams)
def dash = [:]
def dash2 =[:]

users.each{user ->   
def privdashboards = portalPageService.getOwnedPortalPages(user)

    privdashboards.each{dashboard ->
        dash.put(dashboard.getName(),user) 
        dash2.put(user,dashboard.getName())
               log.error "1111--${dash}" 
               log.error "2222--${dash2}"
 
}}

log.error "1111--${dash}"
log.error "22222--${dash2}"
return null

 

The problem is that dash collect what I expect so the list of all dashboards for all users but dash2 for each user only collect last dashboard so it overwrite value in smaller loop (privdashboards.each) and I don't get why. for me dash should be equal to dash 2 but it is not:

here is output from logs:

1111--[test111:admin(JIRAUSER10000)]
2222--[admin(JIRAUSER10000):test111]
1111--[test111:admin(JIRAUSER10000), test222:admin(JIRAUSER10000)]
2222--[admin(JIRAUSER10000):test222]
1111--[test111:admin(JIRAUSER10000), test222:admin(JIRAUSER10000), LStest111:lukasz(JIRAUSER10200)]
2222--[admin(JIRAUSER10000):test222, lukasz(JIRAUSER10200):LStest111]
1111--[test111:admin(JIRAUSER10000), test222:admin(JIRAUSER10000), LStest111:lukasz(JIRAUSER10200), LStest2222:lukasz(JIRAUSER10200)]
2222--[admin(JIRAUSER10000):test222, lukasz(JIRAUSER10200):LStest2222]
1111--[test111:admin(JIRAUSER10000), test222:admin(JIRAUSER10000), LStest111:lukasz(JIRAUSER10200), LStest2222:lukasz(JIRAUSER10200)]
22222--[admin(JIRAUSER10000):test222, lukasz(JIRAUSER10200):LStest2222]

1 answer

1 accepted

0 votes
Answer accepted
Tom Lister
Community Champion
January 29, 2021

Hi @Łukasz Sielski priv 

The map has a unique key. Dash2 is using user as a key. So you only see one entry for the user.

Łukasz Sielski priv
Contributor
January 29, 2021

hmm 

so this mean that if I use put on map it create new entry just in case key doesn't exist but if it already exist it change that key...

In such case, if I have 2 dashboards with the same name it will overwrite user in "dash" case.

So I need to refer to dashboard ID which is unique value...

Anyway, thank you I was understanding map as a list where you put value after value and they can be duplicated now i understand what is the case.

Best,

Lukasz

Suggest an answer

Log in or Sign up to answer