2

I looked for a way to store project-specific configurations for my plugin. In the first step i only want to store a simple String like "Hello".

So, what i found is SAL and the PluginSettings. https://developer.atlassian.com/docs/atlassian-platform-common-components/shared-access-layer/sal-services

This seems pretty easy to use but I don´t have any idea how to implement it into my code. I used a WebWork taking place in the project administration section:

@Override
public String doDefault() throws Exception {
    Project project = getProjectManager().getProjectObjByKey(_projectKey);
    HttpServletRequest request = ExecutingHttpRequest.get();
    request.setAttribute((new StringBuilder()).append("com.atlassian.jira.projectconfig.util.ServletRequestProjectConfigRequestCache").append(":project").toString(), project);
    return INPUT;
}

@Override
protected String doExecute() throws Exception {
    Project project = getProjectManager().getProjectObjByKey(_projectKey);
    HttpServletRequest request = ExecutingHttpRequest.get();
    request.setAttribute((new StringBuilder()).append("com.atlassian.jira.projectconfig.util.ServletRequestProjectConfigRequestCache").append(":project").toString(), project);
    String param = request.getParameter("param");
    return SUCCESS;
}

public void setProjectKey(String projectKey) {
    _projectKey = projectKey;
    }

public String getProjectKey() {
    return _projectKey;
}

public String getBaseUrl() {
    return ComponentAccessor.getApplicationProperties().getString(APKeys.JIRA_BASEURL);
}

As SAL said i implemented a Settings-Class:

public CustomProjectSettings(
        final PluginSettingsFactory pluginSettingsFactory,
        final String projectKey) {
    this.pluginSettingsFactory = pluginSettingsFactory;
    this.projectKey = projectKey;
}

public void setValue(final String key, final String value) {
    final PluginSettings settings = pluginSettingsFactory
            .createSettingsForKey(projectKey);
    settings.put(key, value);
}

public Object getValue(final String key) {
    final PluginSettings settings = pluginSettingsFactory
            .createSettingsForKey(projectKey);
    return settings.get(key);
}

And I added the component in the xml:

<component-import key="pluginSettingsFactory" interface="com.atlassian.sal.api.pluginsettings.PluginSettingsFactory" />

So how do i connect and implement this into my webwork to say

protected String doExecute() throws Exception{
    [...]
    pluginSettings.setValue("Key", param);
    [...]
}
Don
  • 227
  • 1
  • 15
  • I am very new to this myself, but going through this tutorial https://developer.atlassian.com/docs/common-coding-tasks/creating-an-admin-configuration-form#CreatinganAdminConfigurationForm-Step5.MaketheJavaScriptandRESThappen, step 5, specifically, seems to do something similar to what you ask. At least you may be able to glimpse useful code patterns. – NPC Jun 19 '15 at 20:23
  • ty for reminding me of my question. I solved it but forgot to answer. – Don Jun 22 '15 at 08:32

1 Answers1

2

It was easier than i thought. I simply had to inject the Settings as a dependency for my WebWork:

public WebWorkAction(CustomProjectSettings settings){
    this.settings = settings
}

The Settings-Class gets autowired by

<component-import key="pluginSettingsFactory" 
interface="com.atlassian.sal.api.pluginsettings.PluginSettingsFactory" />

and by adding

<component key="settingsComponent"
      class="com.xxx.CustomProjectSettings">
</component>
Don
  • 227
  • 1
  • 15
  • Yep, it is very neat how they make component-import'ed classed to be available in plugin class constructors. A powerful feature which was a surprising discovery for me :) I feel that it is underdocumented, but then Atlassian docs are so poorly organized, that I may have just missed it somewhere. – NPC Jun 22 '15 at 16:47