Skip to content

Instantly share code, notes, and snippets.

@jechlin
Created February 24, 2015 15:07
Show Gist options
  • Save jechlin/8ac8a259133bb479c2a9 to your computer and use it in GitHub Desktop.
Save jechlin/8ac8a259133bb479c2a9 to your computer and use it in GitHub Desktop.
package com.adaptavist.serverconfig
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.fields.CustomField
import com.onresolve.jira.groovy.user.FieldBehaviours
import com.onresolve.jira.groovy.user.FormField
import org.apache.log4j.Logger
import java.util.Map.Entry
class ShowHideBehaviours extends FieldBehaviours {
private Logger log = Logger.getLogger(getClass());
final FieldApplications fieldApplications;
final DefaultValues defaultValues;
public ShowHideBehaviours() {
fieldApplications = new FieldApplications();
defaultValues = new DefaultValues();
}
public void onFormLoad() {
// set default values for non application-specific fields
final Map<FieldName, String> defaultValues = defaultValues.getFieldDefaults();
for (final Entry<FieldName, String> entry : defaultValues.entrySet()) {
final FormField formField = getField(entry.key);
formField.setFormValue(entry.value);
}
}
public void onApplicationChange() {
final FormField applicationFormField = getFieldById(getFieldChanged());
final String applicationName = applicationFormField.getValue();
log.debug("Selected application name: " + applicationName);
final Application selectedApplication = applicationName == null ? null : Application.getByName(applicationName);
for (final FieldName fieldName : FieldName.values()) {
final FormField formField = getField(fieldName);
// hidden
final boolean hidden = fieldApplications.isFieldHidden(fieldName, selectedApplication);
formField.setHidden(hidden);
// default value
if (applicationName) {
final String defaultValue = defaultValues.get(fieldName, selectedApplication);
formField.setFormValue(defaultValue);
}
}
}
/**
* For some reason getFieldByName() doesn't work here so we get the custom field and use it's id
*/
private FormField getField(FieldName fieldName) {
final CustomField customField = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName(fieldName.fullName);
return getFieldById(customField.getId());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment