@RequiresXsrfCheck
  public String doAddFieldLayoutSchemeEntity() {
    if (getIssueTypeId() == null) {
      addError("issueTypeId", getText("admin.errors.fieldlayoutscheme.no.issue.type"));
    }

    if (!invalidInput()) {
      FieldLayoutSchemeEntity fieldLayoutSchemeEntity =
          new FieldLayoutSchemeEntityImpl(fieldLayoutManager, null, getConstantsManager());
      fieldLayoutSchemeEntity.setIssueTypeId(getIssueTypeId());
      fieldLayoutSchemeEntity.setFieldLayoutId(getFieldConfigurationId());
      getFieldLayoutScheme().addEntity(fieldLayoutSchemeEntity);

      // need to compare the default for unmapped issue types and the new field configuration
      final Long unmappedLayoutId = getFieldLayoutScheme().getFieldLayoutId(null);
      if (fieldLayoutSchemeHelper.doesChangingFieldLayoutAssociationRequireMessage(
          getRemoteUser(), getFieldLayoutScheme(), unmappedLayoutId, getFieldConfigurationId())) {
        reindexMessageManager.pushMessage(
            getRemoteUser(), "admin.notifications.task.field.configuration");
      }
      return getRedirect("ConfigureFieldLayoutScheme.jspa?id=" + getId());
    }

    return getResult();
  }
  @RequiresXsrfCheck
  public String doRemove() throws Exception {
    if (isFieldLocked()) {
      return "locked";
    }

    final FieldConfigScheme configScheme = getConfig();

    if (customFieldContextConfigHelper.doesRemovingSchemeFromCustomFieldAffectIssues(
        getLoggedInUser(), getCustomField(), configScheme)) {
      reindexMessageManager.pushMessage(
          getLoggedInUser(), "admin.notifications.task.custom.fields");
    }

    fieldConfigSchemeManager.removeFieldConfigScheme(configScheme.getId());

    ComponentAccessor.getFieldManager().refresh();
    customFieldManager.refreshConfigurationSchemes(getCustomFieldId());
    return redirectToView();
  }
  @RequiresXsrfCheck
  public String doDeleteFieldLayoutSchemeEntity() {
    if (getIssueTypeId() == null) {
      addErrorMessage(getText("admin.errors.fieldlayoutscheme.cannot.delete.default"));
    }

    if (!invalidInput()) {
      // need to compare the default for unmapped issue types and the new field configuration
      final Long unmappedLayoutId = getFieldLayoutScheme().getFieldLayoutId(null);
      final Long layoutToRemoveId = getFieldLayoutScheme().getFieldLayoutId(getIssueTypeId());
      if (fieldLayoutSchemeHelper.doesChangingFieldLayoutAssociationRequireMessage(
          getRemoteUser(), getFieldLayoutScheme(), layoutToRemoveId, unmappedLayoutId)) {
        reindexMessageManager.pushMessage(
            getRemoteUser(), "admin.notifications.task.field.configuration");
      }
      getFieldLayoutScheme().removeEntity(getIssueTypeId());

      return getRedirect("ConfigureFieldLayoutScheme.jspa?id=" + getId());
    }

    return getResult();
  }
  @RequiresXsrfCheck
  public String doEditFieldLayoutSchemeEntity() {
    if (getId() == null) {
      addErrorMessage(getText("admin.errors.fieldlayoutscheme.no.issue.type"));
    }

    FieldLayoutSchemeEntity fieldLayoutSchemeEntity =
        getFieldLayoutScheme().getEntity(getIssueTypeId());

    // need to compare the previous and new field configurations
    Long oldFieldLayoutId = fieldLayoutSchemeEntity.getFieldLayoutId();
    Long newFieldLayoutId = getFieldConfigurationId();

    fieldLayoutSchemeEntity.setFieldLayoutId(newFieldLayoutId);
    fieldLayoutSchemeEntity.store();

    if (fieldLayoutSchemeHelper.doesChangingFieldLayoutAssociationRequireMessage(
        getRemoteUser(), getFieldLayoutScheme(), oldFieldLayoutId, newFieldLayoutId)) {
      reindexMessageManager.pushMessage(
          getRemoteUser(), "admin.notifications.task.field.configuration");
    }
    return getRedirect("ConfigureFieldLayoutScheme.jspa?id=" + getId());
  }
  @Override
  @RequiresXsrfCheck
  protected String doExecute() throws Exception {
    if (isFieldLocked()) {
      return "locked";
    }

    FieldConfigScheme configScheme =
        new FieldConfigScheme.Builder(getConfig())
            .setName(getName())
            .setDescription(getDescription())
            .toFieldConfigScheme();

    if (isBasicMode()) {
      // Add the contexts
      final List<JiraContextNode> contexts =
          CustomFieldUtils.buildJiraIssueContexts(
              isGlobal(), getProjectCategories(), getProjects(), treeManager);

      // Add the issue types
      final List<GenericValue> issueTypes =
          CustomFieldUtils.buildIssueTypes(constantsManager, getIssuetypes());

      boolean messageRequired;
      if (configScheme.getId() == null) {
        messageRequired =
            customFieldContextConfigHelper.doesAddingContextToCustomFieldAffectIssues(
                getLoggedInUser(), getCustomField(), contexts, issueTypes, false);

        configScheme =
            fieldConfigSchemeManager.createFieldConfigScheme(
                configScheme, contexts, issueTypes, getCustomField());
      } else {
        // keep a handle on the old scheme (pre edit) projects and issue types
        messageRequired =
            customFieldContextConfigHelper.doesChangingContextAffectIssues(
                getLoggedInUser(),
                getCustomField(),
                configScheme,
                isGlobal(),
                contexts,
                issueTypes);

        // Update so keep the old config
        if (issueTypes != null) {
          // Since we know that there is only one config
          final Long configId = getFieldConfigIds()[0];
          final FieldConfig config = fieldConfigManager.getFieldConfig(configId);
          final Map<String, FieldConfig> configs =
              new HashMap<String, FieldConfig>(issueTypes.size());
          for (final GenericValue issueType : issueTypes) {
            final String issueTypeId =
                issueType == null
                    ? null
                    : issueType.getString(FieldConfigSchemePersisterImpl.ENTITY_ID);
            configs.put(issueTypeId, config);
          }
          configScheme =
              new FieldConfigScheme.Builder(configScheme).setConfigs(configs).toFieldConfigScheme();
        }
        configScheme =
            fieldConfigSchemeManager.updateFieldConfigScheme(
                configScheme, contexts, getCustomField());
      }

      if (messageRequired) {
        reindexMessageManager.pushMessage(
            getLoggedInUser(), "admin.notifications.task.custom.fields");
      }

      fieldConfigScheme = configScheme;
    } else {
      // @TODO advanced config
    }

    ComponentAccessor.getFieldManager().refresh();
    customFieldManager.refreshConfigurationSchemes(getCustomFieldId());

    return redirectToView();
  }