Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

How to use WebItemProvider since SimpleLinkFactory is deprecated?

Aleksandra Bruun January 5, 2015

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!

3 answers

1 accepted

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

2 votes
Answer accepted
Sascha Becker January 20, 2015

@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 smile

Aleksandra Bruun January 20, 2015

Thank you! I will try this :) Hope its works.

Volodymyr Krupach
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
October 15, 2015

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.

Tayyab Bashir
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
March 28, 2016

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? 

Tayyab Bashir
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
April 6, 2016

@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 

1 vote
Joe Clark
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
January 6, 2015

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"/>

 

 

Aleksandra Bruun January 8, 2015

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>

Joe Clark
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
January 19, 2015

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

Volodymyr Krupach
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
October 15, 2015

This is not needed. WebItemProvider is part of jira-api.

0 votes
Sascha Becker January 14, 2015

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.

Robin Stocker
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
September 1, 2015

For creating WebItems, you can use WebFragmentBuilder (from com.atlassian.plugin.web.api.model).

TAGS
AUG Leaders

Atlassian Community Events