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
Did you ever get an answer to this? I'm trying to do the exact same thing.
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<String, String> 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<GenericValue> projectGVs = ManagerFactory.getPermissionManager().getProjects(Permissions.BROWSE, u); final Map<String, String> projects = ListOrderedMap.decorate(new HashMap<Long, String>(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; } } }
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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<String, String> getValues(Map map) { Map<String, String> params = new HashMap<String, String>(); 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; } }
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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!
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Online forums and learning are now in one easy-to-use experience.
By continuing, you accept the updated Community Terms of Use and acknowledge the Privacy Policy. Your public name, photo, and achievements may be publicly visible and available in search engines.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.