Creating a Field Configuration Scheme programmatically does not add entities

Mathieu Yargeau February 21, 2017

I am creating field configurations with Java code, then creating a field configuration scheme. The creation of both objects work fine, and the items of the field configuration too, but I always find myself with a field configuration scheme with only one association, the default issue type associated to the default field configuration.

 

Normally, it should link the default issue type to my "Test FC" and the "Test Type" issue type to the Default Field Configuration

FieldLayoutManager flm = ComponentAccessor.getFieldLayoutManager();
 
FieldLayoutScheme newFieldConfigScheme = new FieldLayoutSchemeImpl(flm, null);
newFieldConfigScheme.setName(name);
newFieldConfigScheme.setDescription(desc);
 
FieldLayoutSchemeEntity e = new FieldLayoutSchemeEntityImpl(flm, null, ComponentAccessor.getConstantsManager());
FieldLayout fieldConfig = getFieldConfigurationByName("Test FC");
e.setFieldLayoutId(fieldConfig.getId());
 
IssueType issueType = getIssueTypeByName("");
String issueTypeId = issueType == null? null : issueType.getId(); //Gives a null id (default)
e.setIssueTypeId(issueTypeId); 

e.setFieldLayoutScheme(newFieldConfigScheme);		
e.store();
newFieldConfigScheme.addEntity(e);
 
e = new FieldLayoutSchemeEntityImpl(flm, null, ComponentAccessor.getConstantsManager());
fieldConfig = getFieldConfigurationByName("Default Field Configuration");
e.setFieldLayoutId(fieldConfig.getId());
 
issueType = getIssueTypeByName("Test Type");
issueTypeId = issueType == null? null : issueType.getId();
e.setIssueTypeId(issueTypeId); 

e.setFieldLayoutScheme(newFieldConfigScheme);		
e.store();
newFieldConfigScheme.addEntity(e);
 
 
 
newFieldConfigScheme.store();

 

The only difference between my code and this one is that I removed the loop I had to create the entity (the values for the fieldConfigName and issueTypeName are read in an xml file). I made sure that the getFieldConfigurationByName() function return the correct existing Field Configuration, getIssueTypeByName() also return the correct field.

 

Am I missing something?

 

At the moment of doing newFieldConfigScheme.store(), its schemeEntities property value is "{null=com.atlassian.jira.issue.fields.layout.field.FieldLayoutSchemeEntityImpl@2b5e6a6f, 10100=com.atlassian.jira.issue.fields.layout.field.FieldLayoutSchemeEntityImpl@125f242f}", so it seems correct... I checked the details of the schemeEntities map and the values are right. Why are my associations non existant when I look at my Field Configuration Scheme in Jira's UI?

 

EDIT:
My functions to get items by name.

private IssueType getIssueTypeByName(String name)
{
    if(name == null || name.isEmpty())
    {
    	return null;
    }
    	
    IssueType foundIssueType = null;
    	
    IssueTypeManager itMgr = ComponentAccessor.getComponent(IssueTypeManager.class);
    for(IssueType it : itMgr.getIssueTypes())
    {
    	if(it.getName().equals(name))
    	{
    		foundIssueType = it;
    		break;
    	}
    }
    	
    return foundIssueType;
}
 
private FieldLayout getFieldConfigurationByName(String name)
{
    if(name == null || name.isEmpty())
    {
    	return null;
    }
    	
    FieldLayout foundFieldConfig = null;
    FieldLayoutManager flm = ComponentAccessor.getFieldLayoutManager();
    	
    for(EditableFieldLayout fs : flm.getEditableFieldLayouts())
    {
    	if(fs.getName().equals(name))
    	{
    		foundFieldConfig = fs;
    		break;
    	}
    }
    	
    return foundFieldConfig;
}

 

I just noticed that I have an error in the JIRA logs when trying to display the page containing the list of Field Configurations. I do not have that error when I am skipping the creation of the FieldLayoutSchemeEntity in the FieldLayoutSchemeEntity object.

ERROR admin 662x4029x1 17r7pp0 127.0.0.1 /secure/admin/ViewFieldLayouts.jspa [c.a.j.w.a.a.issuefields.enterprise.ViewFieldLayouts] com.atlassian.cache.CacheException: java.lang.NullPointerException: Null keys are not supported

 

Since it mentions Null keys, I tried creating only the FieldLayoutSchemeEntity that does not have a null id (for the default issue type), but the error still occurs. That error is caught and logged by JIRA's internal code, and since there is no stacktrace, I don't know where to look in their code to know which value is null and what caused the error. (Correction, I don't know what I did differently, but now if I test without a null id for the IssueType, I don't have that error, but it still does not work).

 

I thought it might be because I specify null for the GenericValue, so I used the following (reading the JIRA source code, from the DefaultFieldLayoutManager class, I saw that this function creates the GenericValue for me), but it does the same thing.

 

FieldLayoutSchemeEntity e = flm.createFieldLayoutSchemeEntity(newFieldConfigScheme, issueTypeId, fieldConfig.getId());
e.store();
newFieldConfigScheme.addEntity(e);

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
Mathieu Yargeau February 22, 2017

After 10 hours of debugging, trying several combinations and looking at the value of every attribute of every object, I was able to found the source of the error.

 

The value that was null was the id of the Field Configuration Scheme (newFieldConfigScheme) because I was using the same object created locally to add the entities. (I took screenshots of my expressions window in Eclipse to compare my object from one test to another and was about to post them here when I noticed it).

 

After creating the scheme and storing it, you have to go fetch it from the database to have to real stored item with the id defined, and then you can add the entities to it correctly.

jingjing December 2, 2018

i have the same problem.How do i go fetch newFieldConfigScheme id ?

0 votes
emir January 4, 2019

i have the same problem. How do i go fetch the id ? 

TAGS
AUG Leaders

Atlassian Community Events