Change issue type scheme programatically

Andreas Merkle _AME_ PF-26 April 2, 2014

I try to change the issue type scheme of a project, but it doesn't work.

It would be good, if someone could review my code, because I don't find the problem.

Thanks!

// Remove current issue type scheme from project
{
    List<Project>       projectList         = oldIssueTypeScheme.getAssociatedProjectObjects();
    Iterator<Project>   projectListIterator = projectList.iterator();
    List<Long>          projectIdList       = new ArrayList<Long>();
    
    log.debug("Remove issue type scheme " + oldIssueTypeScheme.getId() + " from project " + key + ".");
    
    // Remove project from issue type scheme project list
    projectList.remove(project);
    
    // Generate a list of project ids from the project list
    while(projectListIterator.hasNext())
    {
        Project current = projectListIterator.next();
        
        projectIdList.add(current.getId());
    }

    // Generate a project id array from the project id list
    Long[] projectIdArray = new Long[projectIdList.size()];
    projectIdList.toArray(projectIdArray);
                    
    List<JiraContextNode> contexts = CustomFieldUtils.buildJiraIssueContexts(false,
                                                                             null,
                                                                             projectIdArray,
                                                                             jiraContextTreeManager);
                                                            
    fieldConfigSchemeManager.updateFieldConfigScheme(oldIssueTypeScheme, contexts, fieldManager.getConfigurableField(IssueFieldConstants.ISSUE_TYPE));
}

// Add new issue type scheme to project
{
    List<Project>       projectList         = issueTypeScheme.getAssociatedProjectObjects();
    Iterator<Project>   projectListIterator = projectList.iterator();
    List<Long>          projectIdList       = new ArrayList<Long>();
    
    log.debug("Add issue type scheme " + issueTypeScheme.getId() + " to project " + key + ".");
    
    // Add project to issue type scheme project list
    projectList.add(project);
    
    // Generate a list of project ids from the project list
    while(projectListIterator.hasNext())
    {
        Project current = projectListIterator.next();
        
        projectIdList.add(current.getId());
    }
    
    // Generate a project id array from the project id list
    Long[] projectIdArray = new Long[projectIdList.size()];
    projectIdList.toArray(projectIdArray);
                    
    List<JiraContextNode> contexts = CustomFieldUtils.buildJiraIssueContexts(false,
                                                                             null,
                                                                             projectIdArray,
                                                                             jiraContextTreeManager);
                                                            
    fieldConfigSchemeManager.updateFieldConfigScheme(issueTypeScheme, contexts, fieldManager.getConfigurableField(IssueFieldConstants.ISSUE_TYPE));
}

1 answer

1 accepted

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

1 vote
Answer accepted
Boris Georgiev _Appfire_
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 2, 2014

Here's a sample code that we use and it works. You may compare it to you solution and re-use whatever you need.

private void changeSchemeOfProject(Project jiraProject, FieldConfigScheme newIssueTypeScheme) {

		Long[] currentProjectId = new Long[] { jiraProject.getId() };
		JiraContextTreeManager jiraContextTreeManager = ComponentAccessor
				.getComponentOfType(JiraContextTreeManager.class);
		List<JiraContextNode> removeContext = CustomFieldUtils.buildJiraIssueContexts(false, null,
				currentProjectId, jiraContextTreeManager);
		FieldConfigSchemeManager fieldConfigSchemeManager = ComponentAccessor.getComponent(FieldConfigSchemeManager.class);
		fieldConfigSchemeManager.removeSchemeAssociation(removeContext, ComponentAccessor
				.getFieldManager().getIssueTypeField());
		List<Project> associatedProjectObjects = newIssueTypeScheme.getAssociatedProjectObjects();
		if (associatedProjectObjects == null) {
			associatedProjectObjects = Collections.emptyList();
		}
		Set<Project> projectList = new HashSet<Project>(associatedProjectObjects);
		projectList.add(jiraProject);
		Long[] projectIds = new Long[projectList.size()];
		int i = 0;
		for (Project currentProject : projectList) {
			projectIds[i] = currentProject.getId();
			++i;
		}

		List<ProjectCategory> associatedProjectCategoryObjects = newIssueTypeScheme
				.getAssociatedProjectCategoryObjects();
		if (associatedProjectCategoryObjects == null) {
			associatedProjectCategoryObjects = Collections.emptyList();
		}
		Set<ProjectCategory> projectCategories = new HashSet<ProjectCategory>(
				associatedProjectCategoryObjects);
		Long[] projectCategoryIds = new Long[projectCategories.size()];
		i = 0;
		for (ProjectCategory category : projectCategories) {
			projectCategoryIds[i] = category.getId();
			++i;
		}

		List<JiraContextNode> contexts = CustomFieldUtils.buildJiraIssueContexts(
				newIssueTypeScheme.isGlobal(), projectCategoryIds, projectIds,
				jiraContextTreeManager);

		fieldConfigSchemeManager.updateFieldConfigScheme(newIssueTypeScheme, contexts,
				newIssueTypeScheme.getField());

		ComponentAccessor.getFieldManager().refresh();

	}

Andreas Merkle _AME_ PF-26 April 2, 2014

Hi Boris,

thanks!! This is more than I expected. I will compare it against my one to know what I did wrong.

Dhruv Dixit March 19, 2019

Hi Boris,

Could you please add some comments in the code you shared or maybe explain it a little bit. It would be really helpful.

I just want to attach an existing issuetype scheme and workflow scheme with the projects which are being created using my custom project template/ blueprint.

TAGS
AUG Leaders

Atlassian Community Events