Creating new velocity context for web fragments

As we have mentioned in the previous recipes, it is possible to add velocity variables while constructing a JIRA web fragment. JIRA supports a list of variables by default, which include user, req, baseurl, and so on. The full list and the details of these variables can be found at https://developer.atlassian.com/display/JIRADEV/Velocity+Contexts.

In this recipe, we will see how to add more variables to the velocity context with the use of the context-provider element.

How to do it...

The context-provider element adds to the velocity context available to the web section and web item modules. Only one context-provider can be added for an item. The following steps show how we can make use of a context provider:

  1. Create the new ContextProviderclass.

    The class must implement com.atlassian.plugin.web.ContextProvider. To make things easy, it is enough to extend the AbstractJiraContextProvider class and override the following abstract method in it:

            public abstract Map getContextMap(ApplicationUser user, 
            JiraHelperjiraHelper);

    The following is what the class looks like if you want to add the full name of the user as a separate variable in the velocity context.

            public class UserContextProvider 
            extends AbstractJiraContextProvider {
              @Override 
              public Map getContextMap(ApplicationUser user, 
              JiraHelper helper) { 
                 return MapBuilder.build("userName", 
                 user.getDisplayName());
              }
            }

    Note

    Please note that the $user variable is already available in the velocity context of web fragments and so the full name can be retrieved easily using $user.getDisplayName(). This is just a simple example of how to use the context providers.

  2. Use the variable that is added into the velocity context appropriately while constructing the web section/item.

    In the example, let us create a new web section with the user's full name in the admin section with a single web item in it to link to the user's website.

            <web-section key="jtricks-admin-context-section" 
             name="JTricks Context Section" location="admin_plugins_menu" 
             weight="910">
               <label>$userName</label>
               <context-provider 
               class="com.jtricks.ui.context.UserContextProvider" />
            </web-section>
    
            <web-item key="jtricks-admin-context-link" 
            name="JTricks Context Link" 
            section="admin_plugins_menu/jtricks-admin-context-section" 
            weight="10">
               <label>Website</label>
               <link linkId="jtricks.admin.context.link">
                  http://www.j-tricks.com
               </link>
            </web-item>

    As you can see, the web section refers to $userName in its label.

  3. Deploy the plugin.

How it works...

Once the plugin is deployed, we can see that the new web section is created under the JIRA Admin UI, as shown in the following screenshot. The $userName variable is dynamically replaced by the current user's full name.

How it works...
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
52.15.117.231