How to define a custom condition class in web-panel of JIRA

Jayashree Shetty
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.
January 29, 2014

I have a web-panel which should display based on some particular project. I didnt come across any jira built in condition which could work for me to achieve this task. So i have added a class.java file and i have hardcoded an issue. Using this issue i get the project key, and i return true if my project key == "some project name" else false. Name of the package is [com.myplugin.samplecondition]

now in my web-panel i have defined this way.

<web-panel>

<condition class="com.myplugin.samplecondition">

</condition

>

</web-panel>

Is this all i have to do so that my condition class is accessible or should i be doing something more on this. I have just started coding on this so please bare with my doubts/questions.

2 answers

1 accepted

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

0 votes
Answer accepted
Sabine Winkler
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.
January 29, 2014

Hi,

could you post your implementation to see more details?

Mainly, your web-panel configuration should look like this if you class is named "class"

&lt;web-panel&gt;

&lt;condition class="com.myplugin.samplecondition.class"&gt;

&lt;/condition&gt;

&lt;/web-panel&gt;

Based on the documentation your class has to implement the "Condition":

Condition elements must contain a class attribute with the fully-qualified name of a Java class. The referenced class:

  • must implement com.atlassian.plugin.web.Condition, and
  • will be auto-wired by Spring before any condition checks are performed.
public interface Condition extends BaseCondition
{
    /**
     * Called after creation and autowiring.
     *
     * @param params The optional map of parameters specified in XML.
     */
    void init(Map&lt;String,String&gt; params) throws PluginParseException;

    /**
     * Determine whether the web fragment should be displayed
     *
     * @return true if the user should see the fragment, false otherwise
     */
    boolean shouldDisplay(Map&lt;String,Object&gt; context);
}

Using parameters (accessible in the init-Method) might be an option to get the list of "some particular projects".

&lt;web-panel&gt;
&lt;condition class="com.myplugin.samplecondition.class"&gt;
&lt;param name="project"&gt;my-project&lt;/param&gt;
&lt;/condition&gt;
&lt;/web-panel&gt;

You can also use spring-injected objects within your Condition class - here is an example how the JIRA permission manager is used by the :

public class UserIsAdminCondition extends AbstractWebCondition

{

    private final PermissionManager permissionManager;



    public UserIsAdminCondition(PermissionManager permissionManager)

    {

        this.permissionManager = permissionManager;

    }



    public boolean shouldDisplay(ApplicationUser user, JiraHelper jiraHelper)

    {

        return permissionManager.hasPermission(Permissions.ADMINISTER, user);

    }

}

Extending the https://docs.atlassian.com/jira/latest/com/atlassian/jira/plugin/webfragment/conditions/AbstractWebCondition.htmlor one of its sub-classes (instead of implementing the Condition interface) might give you access to a JiraHelper - to return the project in this context.

HTH, Sabine

Jayashree Shetty
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 19, 2014

Hi Sabine. Can you please post the complete implementation of active objects.

Sabine Winkler
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 19, 2014

Hi Jayashree,

what do you mean with "implementation of active objects" - cannot see any relation to the topic. Just provide me more details or share your current implementation and I will try to help you.

Sabine

Jayashree Shetty
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 20, 2014

Hi Sabine..

ok i will explain about my requirement.

In JIRA's issue view page i wanted a gadget which is somewhat similar to the time estimate gadget which is present in jira.

I did develop the gadget and added it in the issue view page. But this is visible to all the projects. So i need to configure this to display this gadget only to some selected projects. So in the atlassian forum i got some answers telling that i use active objects to store the list of it. i dont know how to use them inspite of reading the document on this i am sometimes stuck.

We do have a condition tag the way you explained above so that we can add it to the webpanel to control the visibility of the gadget but there's no inbuilt function in jira which allows me to choose among some projects. So i am looking into the implementation of this. i want my gadget to get displayed for selected projects. i need to configure this somewhere.

Sabine Winkler
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 23, 2014

Hi,

I try to summarize what I understand: the project list, telling the condition to show the gadget , is now stored with Active Objects. Right?

So your condition, whenever it is used calls the AO Service to get the list of projects, checks whether the project the user is currently browsing on is in the list and if yes, displays your gadget. Can you post your atlassian-plugin.xml as well as the condition implementation. Would be great. Thx,

Sabine

0 votes
Ilya shamuratov November 24, 2014

Thanks Sabin for your advice relative to extend AbstractWebCondition to give access to JiraHelper.

My solution to the problem of viewing web-panel only for certain types of project is the following:

Condition of web-panel

&lt;condition class="full.class.name.YourClassName"&gt;
           &lt;param name="project-caterogy-1"&gt;category1&lt;/param&gt;
  			&lt;param name="project-caterogy-2"&gt;category2&lt;/param&gt;
 &lt;/condition&gt;


Class:

import com.atlassian.jira.plugin.webfragment.conditions.AbstractWebCondition;
import java.util.Map;
import com.atlassian.jira.plugin.webfragment.model.JiraHelper;
import com.atlassian.jira.project.ProjectCategory;
import com.atlassian.jira.project.ProjectManager;
import com.atlassian.jira.project.Project;
import com.atlassian.jira.user.ApplicationUser;
import com.atlassian.plugin.PluginParseException;

public class YourClassName extends AbstractWebCondition {
    /**
     * Called after creation and autowiring.
     *
     * @param params The optional map of parameters specified in XML.
     */

    private Map&lt;String,String&gt; params;
    @Override
    public void init(Map&lt;String,String&gt; params) throws PluginParseException {
        this.params = params;
    }
    /**
     * Determine whether the web fragment should be displayed
     *
     * @return true if the user should see the fragment, false otherwise
     */
    @Override
public boolean shouldDisplay(ApplicationUser applicationUser, JiraHelper jiraHelper)  {
        boolean out = false;
        try {
            if (params == null || params.isEmpty()) {
                System.out.println("No project-category parameter has been set into atlassian-plugin.xml.");
                return out;
            }
            Project currentProject = jiraHelper.getProjectObject();
            ProjectCategory currentProjectCat = currentProject.getProjectCategoryObject();
            for(String projectCategory : params.values()) {
                if (currentProjectCat.getName().equals(projectCategory)) {
                    return true;
                }
            }
        }catch (Exception ex){
            System.out.println("An exception is occured " +ex.getMessage());
            return out;
        }
        return out;
    }
}

 

Its works fine for me for multiple project categories.

Hope it can help...

TAGS
AUG Leaders

Atlassian Community Events