Created
October 10, 2012 11:24
-
-
Save jamieechlin/3864923 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.xxxx.jira.cleanup | |
import com.atlassian.jira.ComponentManager | |
import com.atlassian.jira.bc.JiraServiceContext | |
import com.atlassian.jira.bc.JiraServiceContextImpl | |
import com.atlassian.jira.bc.portal.PortalPageService | |
import com.atlassian.jira.portal.PortalPage | |
import com.atlassian.jira.portal.PortletConfiguration | |
import com.atlassian.jira.portal.portlets.ProjectsPortlet | |
import com.opensymphony.user.User | |
import com.opensymphony.user.UserManager | |
import org.apache.log4j.Category | |
import org.apache.log4j.Level | |
Category log = Category.getInstance(this.class) | |
log.setLevel(Level.DEBUG) | |
ComponentManager componentManager = ComponentManager.getInstance() | |
PortalPageService portalPageService = componentManager.getPortalPageService() | |
UserManager userManager = UserManager.getInstance() | |
Boolean preview = false | |
List users = userManager.getGroup("jira-users").getUsers() | |
Integer pagesUpdated = 0 | |
for (String userId in users) { | |
User user = userManager.getUser(userId) | |
JiraServiceContext serviceContext = new JiraServiceContextImpl(user); | |
Collection pages = portalPageService.getOwnedPortalPages(user) | |
log.debug("User: $userId has ${pages.size()} pages") | |
for (PortalPage page in pages) { | |
def killIt = false | |
PortletConfiguration killPc = null | |
List portletConfigurations = page.getPortletConfigurations() | |
for (PortletConfiguration portletConfiguration in portletConfigurations) { | |
// PCs referring to portlet types that have been removed from the system | |
// have no portlet class hence safe nav here | |
if (portletConfiguration.getPortlet()?.class == ProjectsPortlet.class) { | |
// Note that we don't mind these if they have been configured to use a project category | |
if (!portletConfiguration.getProperty("projectcategoryid")) { | |
log.debug("Replace portlet on page ${page.name} owned by ${serviceContext.getUser()}") | |
killPc = portletConfiguration | |
killIt = true | |
} | |
} | |
} | |
if (killIt) { | |
if (portalPageService.validateForDeletePortalPagePortletConfiguration(serviceContext, page, killPc.getId())) { | |
log.debug("Delete portlet on page ${page.name} owned by ${serviceContext.getUser()}") | |
if (! preview) { | |
portalPageService.deletePortalPagePortletConfiguration(serviceContext, page, killPc.getId()) | |
} | |
pagesUpdated++ | |
if (portalPageService.validateForAddPortalPagePortletConfiguration(serviceContext, page, | |
"com.atlassian.jira.plugin.system.portlets:text", killPc.getColumn(), killPc.getRow())) { | |
if (! preview) { | |
PortletConfiguration textPortlet = portalPageService.addPortalPagePortletConfiguration(serviceContext, page, | |
"com.atlassian.jira.plugin.system.portlets:text", killPc.getColumn(), killPc.getRow()) | |
textPortlet.getProperties().setString("title", "Info") | |
textPortlet.getProperties().setText("html", """The projects portlet has been removed from your dashboard as it was very | |
slow when configured to show information from <b>all</b> projects. | |
<br><br> | |
You can re-enable by clicking Configure: ON from the top right, then choosing either the Project or Projects portlets. | |
Please select a project category if you use the Projects portlet. | |
<br><br> | |
You can delete this <b>Text</b> portlet after turning configure on. | |
""") | |
portalPageService.savePortalPagePortletConfiguration(serviceContext, page, textPortlet.getId()) | |
} | |
} | |
else { | |
log.error("Can't add config to page: ${serviceContext.getErrorCollection()}") | |
} | |
} | |
else { | |
log.error("Can't delete config from page: ${serviceContext.getErrorCollection()}") | |
} | |
} | |
} | |
} | |
log.debug ("Pages updated: $pagesUpdated") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment