From 9d53dd93fe4fafc386b8a72cfb1a4c0cab2afbcf Mon Sep 17 00:00:00 2001 From: Marcel Frank Date: Tue, 17 Jan 2023 21:56:31 +0100 Subject: [PATCH] #17 Add condition to web-panel; refactor code; Styling --- .../api/config/AllowedProjectsCondition.java | 31 +++++++++++ .../api/config/ConfigurationManager.java | 51 +++++++++++++++++++ .../openpoker/config/PokerConfigPage.java | 46 ++--------------- src/main/resources/atlassian-plugin.xml | 1 + .../resources/js/config/open-poker-config.js | 14 +++-- .../templates/config/open-poker-config.vm | 16 +++--- 6 files changed, 104 insertions(+), 55 deletions(-) create mode 100644 src/main/java/com/aprey/jira/plugin/openpoker/api/config/AllowedProjectsCondition.java create mode 100644 src/main/java/com/aprey/jira/plugin/openpoker/api/config/ConfigurationManager.java diff --git a/src/main/java/com/aprey/jira/plugin/openpoker/api/config/AllowedProjectsCondition.java b/src/main/java/com/aprey/jira/plugin/openpoker/api/config/AllowedProjectsCondition.java new file mode 100644 index 0000000..012ac1c --- /dev/null +++ b/src/main/java/com/aprey/jira/plugin/openpoker/api/config/AllowedProjectsCondition.java @@ -0,0 +1,31 @@ +package com.aprey.jira.plugin.openpoker.api.config; + +import java.util.List; + +import com.atlassian.jira.plugin.webfragment.conditions.AbstractWebCondition; +import com.atlassian.jira.plugin.webfragment.model.JiraHelper; +import com.atlassian.jira.user.ApplicationUser; + +/** + * Class that defines a condition for the web fragment visibility + */ +public class AllowedProjectsCondition extends AbstractWebCondition { + + /** + * Method to determine whether the web fragment should be displayed or not + * @param applicationUser The user currently viewing the page + * @param jiraHelper The JiraHelper that provides access to various Jira-related objects + * @return boolean value indicating whether the web fragment should be displayed or not + */ + @Override + public boolean shouldDisplay(ApplicationUser applicationUser, JiraHelper jiraHelper) { + List allowedProjects = ConfigurationManager.getStoredAllowedProjects(); + if ( allowedProjects.isEmpty() ) { + return true; + } + if ( jiraHelper.getProject() != null ) { + return allowedProjects.contains(jiraHelper.getProject().getKey()); + } + return true; + } +} diff --git a/src/main/java/com/aprey/jira/plugin/openpoker/api/config/ConfigurationManager.java b/src/main/java/com/aprey/jira/plugin/openpoker/api/config/ConfigurationManager.java new file mode 100644 index 0000000..5468758 --- /dev/null +++ b/src/main/java/com/aprey/jira/plugin/openpoker/api/config/ConfigurationManager.java @@ -0,0 +1,51 @@ +package com.aprey.jira.plugin.openpoker.api.config; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.stream.Collectors; + +import com.atlassian.jira.component.ComponentAccessor; +import com.atlassian.sal.api.pluginsettings.PluginSettings; +import com.atlassian.sal.api.pluginsettings.PluginSettingsFactory; + +public abstract class ConfigurationManager { + private static PluginSettingsFactory pluginSettingsFactory = ComponentAccessor.getOSGiComponentInstanceOfType(PluginSettingsFactory.class); + + //Key for plugin settings + private static String OPEN_POKER_KEY = "com.aprey.jira.plugin.openpoker"; + + /** + * Method to get the plugin settings + * + * @return pluginSettings instance + */ + public static PluginSettings getPluginSettings() { + PluginSettingsFactory pluginSettingsFactory = ComponentAccessor.getOSGiComponentInstanceOfType(PluginSettingsFactory.class); + PluginSettings pluginSettings = pluginSettingsFactory.createSettingsForKey(OPEN_POKER_KEY); + return pluginSettings; + } + + /** + * Method to get the stored allowed projects from plugin settings + * + * @return ArrayList of allowed projects + */ + public static ArrayList getStoredAllowedProjects() { + PluginSettings pluginSettings = getPluginSettings(); + String storedProjects = ((String) pluginSettings.get("allowedProjects")); + if ( storedProjects != null && !storedProjects.isEmpty() ) { + return Arrays.stream(storedProjects.split(",")).collect(Collectors.toCollection(ArrayList::new)); + } + return new ArrayList<>(); + } + + /** + * Method to store the updated allowed projects in plugin settings + * + * @param updatedAllowedProjects String of allowed projects + */ + public static void storeAllowedProjects(String updatedAllowedProjects) { + PluginSettings pluginSettings = getPluginSettings(); + pluginSettings.put("allowedProjects", updatedAllowedProjects); + } +} diff --git a/src/main/java/com/aprey/jira/plugin/openpoker/config/PokerConfigPage.java b/src/main/java/com/aprey/jira/plugin/openpoker/config/PokerConfigPage.java index b07c85e..c53c877 100644 --- a/src/main/java/com/aprey/jira/plugin/openpoker/config/PokerConfigPage.java +++ b/src/main/java/com/aprey/jira/plugin/openpoker/config/PokerConfigPage.java @@ -2,9 +2,7 @@ import javax.servlet.http.HttpServletRequest; import java.util.ArrayList; -import java.util.Arrays; import java.util.List; -import java.util.stream.Collectors; import com.atlassian.jira.component.ComponentAccessor; import com.atlassian.jira.project.Project; @@ -12,20 +10,19 @@ import com.atlassian.jira.security.request.SupportedMethods; import com.atlassian.jira.security.xsrf.DoesNotRequireXsrfCheck; import com.atlassian.jira.web.action.JiraWebActionSupport; -import com.atlassian.sal.api.pluginsettings.PluginSettings; -import com.atlassian.sal.api.pluginsettings.PluginSettingsFactory; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import webwork.action.Action; import webwork.action.ServletActionContext; +import static com.aprey.jira.plugin.openpoker.api.config.ConfigurationManager.getStoredAllowedProjects; +import static com.aprey.jira.plugin.openpoker.api.config.ConfigurationManager.storeAllowedProjects; + @SupportedMethods({ RequestMethod.GET }) public class PokerConfigPage extends JiraWebActionSupport { //Logger instance for the class private static Logger log = LoggerFactory.getLogger(PokerConfigPage.class); - //Key for plugin settings - private static String OPEN_POKER_KEY = "com.aprey.jira.plugin.openpoker"; //List of allowed projects private List allowedProjects = new ArrayList<>(); @@ -38,19 +35,6 @@ public String doDefault() throws Exception { return Action.INPUT; } - /** - * Method to get the stored allowed projects from plugin settings - * @return ArrayList of allowed projects - */ - private static ArrayList getStoredAllowedProjects() { - PluginSettings pluginSettings = getPluginSettings(); - String storedProjects = ((String) pluginSettings.get("allowedProjects")); - if ( storedProjects != null ) { - return Arrays.stream(storedProjects.split(",")).collect(Collectors.toCollection(ArrayList::new)); - } - return new ArrayList<>(); - } - /** * Method is called when the form is submitted */ @@ -69,35 +53,15 @@ protected String doExecute() throws Exception { /** * Method to get the updated allowed projects from the form + * * @return String of allowed projects */ private static String getUpdatedAllowedProjects() { - HttpServletRequest request - = ServletActionContext.getRequest(); + HttpServletRequest request = ServletActionContext.getRequest(); String updatedAllowedProjects = request.getParameter("allowedProjects"); return updatedAllowedProjects; } - /** - * Method to store the updated allowed projects in plugin settings - * @param updatedAllowedProjects String of allowed projects - */ - private static void storeAllowedProjects(String updatedAllowedProjects) { - PluginSettings pluginSettings = getPluginSettings(); - pluginSettings.put("allowedProjects", updatedAllowedProjects); - } - - /** - * Method to get the plugin settings - * @return pluginSettings instance - */ - private static PluginSettings getPluginSettings() { - PluginSettingsFactory pluginSettingsFactory = ComponentAccessor.getOSGiComponentInstanceOfType(PluginSettingsFactory.class); - PluginSettings pluginSettings = pluginSettingsFactory.createSettingsForKey(OPEN_POKER_KEY); - return pluginSettings; - } - - public List getAllowedProjects() { return allowedProjects; } diff --git a/src/main/resources/atlassian-plugin.xml b/src/main/resources/atlassian-plugin.xml index 15efecc..be16868 100644 --- a/src/main/resources/atlassian-plugin.xml +++ b/src/main/resources/atlassian-plugin.xml @@ -63,6 +63,7 @@