I have a JIRA plugin with dynamic menu item using SimpleLinkFactory. This works excellent, but it seems that SimpleLinkFactory is deprecated since JIRA 6.3 (SimpleLinkFactory). They refer to com.atlassian.plugin.web.api.provider.WebItemProvider, a class I can't find in Java doc.
Can anyone provide an example on how to use the WebItemProvider in both Java and the atlassian-plugin.xml?
Thanks in advance!
Community moderators have prevented the ability to post new answers.
@Aleksandra Bruun
I did find what we were searching for in the JIRA source code. First you create the web-section entry in your atlassian-plugin.xml.
<web-section key="links_recentproject" name="Links Main Section" location="recentprojects" weight="11"> <label key="menu.project.recent"/> <tooltip key="menu.project.recent.desc"/> </web-section>
Now to make use of your WebItemProvider for that section you add it into the plugin.xml as well
<web-item-provider key="project_drop_history-factory" name="Project History Link Factory" section="recentprojects/links_recentproject" i18n-name-key="webfragments.navigation.bar.browse.history.link.factory" class="com.atlassian.jira.project.ProjectHistoryLinkFactory"/>
ProjectHistoryLinkFactory is your WebItemProvider and this class can be found in the JIRA Source Code for future reference.
public class ProjectHistoryLinkFactory implements WebItemProvider { public static final int MAX_RECENT_PROJECTS_TO_SHOW = 5; private final UserProjectHistoryManager userHistoryManager; private final I18nBean.BeanFactory beanFactory; private final VelocityRequestContextFactory velocityRequestContextFactory; public ProjectHistoryLinkFactory(VelocityRequestContextFactory velocityRequestContextFactory, UserProjectHistoryManager userHistoryManager, I18nBean.BeanFactory beanFactory) { this.velocityRequestContextFactory = velocityRequestContextFactory; this.userHistoryManager = userHistoryManager; this.beanFactory = beanFactory; } @Override public Iterable<WebItem> getItems(final Map<String, Object> context) { final ProjectAction projectAction = ProjectAction.VIEW_ISSUES; final User user = (User) context.get("user"); final List<Project> history = userHistoryManager.getProjectHistoryWithPermissionChecks(projectAction, user); final List<WebItem> links = new ArrayList<WebItem>(); if (!history.isEmpty()) { final VelocityRequestContext requestContext = velocityRequestContextFactory.getJiraVelocityRequestContext(); // Need ot ensure they contain the baseurl in case they are loaded via ajax/rest final String baseUrl = requestContext.getBaseUrl(); final Project currentProject = userHistoryManager.getCurrentProject(Permissions.BROWSE, user); final I18nHelper i18n = beanFactory.getInstance(user); int weight = 10; for (Project project : history) { if (!project.equals(currentProject)) { final Long projectId = project.getId(); final String name = project.getName(); final String key = project.getKey(); String iconUrl = null; if (project.getGenericValue().getLong("avatar") != null) { final Avatar avatar = project.getAvatar(); iconUrl = baseUrl + "/secure/projectavatar?pid=" + projectId + "&avatarId=" + avatar.getId() + "&size=small"; } links.add(new WebFragmentBuilder(weight += 10). id("proj_lnk_" + projectId). label(name + " (" + key + ")"). title(i18n.getText("tooltip.browseproject.specified", name)). addParam("iconUrl", iconUrl). webItem("browse_link/project_history_main"). url(baseUrl + "/browse/" + key). build()); } } } return links.subList(0, Math.min(MAX_RECENT_PROJECTS_TO_SHOW, links.size())); } }
Hope that helps you. Let's hope there will be a decent documentation for JIRA one day so this stuff can actually be looked up
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Thank you @Sascha Becker! It works and no need to add the dependency and component-import to pom.xml as in @Joe Clark [Atlassian] answer.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi, could you telme what are we doing in the ProjectHistoryLinkFactory class?
Do we have to change it, or override this class or something?
What's the reference here for ProjectHistoryLinkFactory?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
@Sascha Becker Hi, could you please tell me how to use the ProjectHistoryLinkFactory class in the plugin?
Where do I use this in the plugin.
Many thanks in advance.
~ Tayyab
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
I haven't tested this personally, but you should be able to access the WebItemProvider by adding the following declaration to your pom.xml:
<dependency> <groupId>com.atlassian.plugins</groupId> <artifactId>atlassian-plugins-webfragment-api</artifactId> <version>3.0.5</version> <scope>provided</scope> </dependency>
And then a component-import to your atlassian-plugin.xml:
<component-import key="webItemProvider" name="Web Item Provider" interface="com.atlassian.plugin.web.api.provider.WebItemProvider"/>
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Thanks! Do you know how to access the WebItemProvider java part in the atlassian-plugin? How to 'link' the dynamic java class to a web section/item? Similiar to this in simple-link-factory: <simple-link-factory key="links-factory" name="Link Factory" section="menu/section" weight="10" lazy="true" class="LinkFactory"> </simple-link-factory>
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Does the "Web Item" plugin module work for you? Or are you trying to do something different? see https://developer.atlassian.com/display/JIRADEV/Web+Item+Plugin+Module
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
This is not needed. WebItemProvider is part of jira-api.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Good question. Would be interested to hear the answer as well. Especially the part how to create new Webitems in the WebItemProvider since WebItemImpl is not public.
Really weird to mark something deprecated and then miss out on documenting the alternative.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
For creating WebItems, you can use WebFragmentBuilder (from com.atlassian.plugin.web.api.model).
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.