Jira report property set default value dynamically

Peter Csiba February 26, 2012

Hello,

I want to dynamically set the default value of a property in my report plugin. In particular I want to set it as currentProject* (last project viewed by user). Sample code:

 <property>
                <key>projectid</key>
                <name>report.cs.project</name>
                <description>report.cs.project.description
                </description>
                <type>select</type>
                <values class="com.atlassian.jira.portal.ProjectValuesGenerator"/>
                <default>???</default>
 </property>
 

Is there any

--direct (set the default value) or

--indirect way (custom property, custom ValuesGenerator,...) to achieve it?

Thank you in advance,

Peter.

Note: a similar question was asked on atlassian forums.

* should be as in API

3 answers

1 vote
Bart Gottschalk December 26, 2012

Did you ever get an answer to this? I'm trying to do the exact same thing.

0 votes
Bart Gottschalk December 26, 2012

Naren, thanks for the quick response. I decided to implement like this:

package ****;

import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.project.Project;
import com.atlassian.jira.portal.ProjectValuesGenerator;
import com.atlassian.jira.ManagerFactory;
import com.atlassian.jira.security.JiraAuthenticationContext;
import com.atlassian.jira.security.Permissions;
import com.atlassian.jira.user.UserProjectHistoryManager;
import com.opensymphony.user.User;
import org.apache.commons.collections.map.ListOrderedMap;
import org.apache.log4j.Logger;
import org.ofbiz.core.entity.GenericValue;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;


public class MSIProjectValuesGenerator extends ProjectValuesGenerator {

    private static final Logger log = Logger.getLogger(MSIProjectValuesGenerator.class);
	
    public Map&lt;String, String&gt; getValues(final Map params)
    {
        final User u = (User) params.get("User"); // BUG this user is wrong - the params are being cached somewhere.

        try
        {
            final Collection&lt;GenericValue&gt; projectGVs = ManagerFactory.getPermissionManager().getProjects(Permissions.BROWSE, u);

            final Map&lt;String, String&gt; projects = ListOrderedMap.decorate(new HashMap&lt;Long, String&gt;(projectGVs.size()));
            
            JiraAuthenticationContext authenticationContext = ComponentAccessor.getJiraAuthenticationContext();
            com.atlassian.crowd.embedded.api.User loggedInUser = authenticationContext.getLoggedInUser();
            
            UserProjectHistoryManager userProjectHistoryManager = ComponentAccessor.getComponentOfType(UserProjectHistoryManager.class);
            Project selectedProject = userProjectHistoryManager.getCurrentProject(10, loggedInUser);
            
        	//log.info("projects is " + projects);
        	projects.put(selectedProject.getId().toString(),selectedProject.getName());
        	//log.info("projects is " + projects);
            
            for (final Object element : projectGVs)
            {
                final GenericValue project = (GenericValue) element;
            	//log.info("project.getLong(\"id\").toString() is " + project.getLong("id").toString());
                projects.put(project.getLong("id").toString(), project.getString("name"));
            }

        	//log.info("projects is " + projects);

            return projects;
        }
        catch (final Exception e)
        {
            log.error("Could not retrieve project values for this user: " + u.getName(), e);
            return null;
        }
    }

	
}

Naren
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.
December 27, 2012

Well, the below code works for me, you may like to give a try -

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;

import com.atlassian.configurable.ValuesGenerator;
import com.atlassian.crowd.embedded.api.User;
import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.project.Project;
import com.atlassian.jira.security.JiraAuthenticationContext;
import com.atlassian.jira.user.UserProjectHistoryManager;

public class ProjectValuesGenerator implements ValuesGenerator {

	private static final Logger log = Logger.getLogger(ProjectValuesGenerator.class);
	@Override
	public Map&lt;String, String&gt; getValues(Map map) {

Map&lt;String, String&gt; params = new HashMap&lt;String, String&gt;();
		
JiraAuthenticationContext authenticationContext = ComponentAccessor.getJiraAuthenticationContext();
		
User loggedInUser = authenticationContext.getLoggedInUser();
		
//Get the project
UserProjectHistoryManager userProjectHistoryManager = ComponentAccessor.getComponentOfType(UserProjectHistoryManager.class);

Project selectedProject = userProjectHistoryManager.getCurrentProject(10, loggedInUser);

params.put(selectedProject.getId().toStrin (),selectedProject.getName());
return params;
				
	}
}

Shantonu Hossain November 26, 2013

Does the code actually show you a single project? I am trying to so the same thing but my code lists all the projects in JIRA not the selected project. Did you change anything to atlassian-plugin.xml?

0 votes
Naren
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.
December 26, 2012

You need to write your own custom ProjectValuesGenerator which will extend the JiraWebActionSupport class override the getSelectedProject() and you must implement ValuesGenerator.

Alternatively, you can also use the method getCurrentProject(int permission, com.atlassian.crowd.embedded.api.User user) of UserProjectHistorymanager interface.

JiraAuthenticationContext authenticationContext = ComponentAccessor.getJiraAuthenticationContext();
User loggedInUser = authenticationContext.getLoggedInUser();

UserProjectHistoryManager userProjectHistoryManager = ComponentAccessor.getComponentOfType(UserProjectHistoryManager.class);
Project selectedProject = userProjectHistoryManager.getCurrentProject(10, loggedInUser);

params.put(selectedProject.getId().toString(),selectedProject.getName());

Hope this should help you!

Suggest an answer

Log in or Sign up to answer