All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.marvelution.jira.plugins.sonar.panels.SonarTabPanelHelper Maven / Gradle / Ivy

/*
 * Licensed to Marvelution under one or more contributor license 
 * agreements.  See the NOTICE file distributed with this work 
 * for additional information regarding copyright ownership.
 * Marvelution licenses this file to you under the Apache License,
 * Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License.
 * You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

package com.marvelution.jira.plugins.sonar.panels;

import java.util.Map;

import com.atlassian.jira.bc.project.component.ProjectComponent;
import com.atlassian.jira.project.Project;
import com.atlassian.jira.security.PermissionManager;
import com.atlassian.jira.security.Permissions;
import com.atlassian.plugin.webresource.WebResourceManager;
import com.marvelution.jira.plugins.sonar.service.SonarAssociationManager;
import com.marvelution.jira.plugins.sonar.service.SonarPanelLayoutManager;
import com.marvelution.jira.plugins.sonar.utils.SonarGadgetUtils;
import com.opensymphony.user.User;

/**
 * Helper class for Soanr Tab Panels
 * 
 * @author Mark Rekveld
 * 
 * @since 1.2.0
 */
public class SonarTabPanelHelper {

	private static final String PLUGIN_KEY = "com.marvelution.jira.plugins.sonar";

	private final SonarAssociationManager associationManager;

	private final SonarPanelLayoutManager sonarPanelLayoutManager;

	private final PermissionManager permissionManager;

	private final WebResourceManager webResourceManager;

	private final SonarGadgetUtils sonarGadgetUtils;

	/**
	 * Constructor
	 * 
	 * @param associationManager the {@link SonarAssociationManager} implementation
	 * @param sonarPanelLayoutManager the {@link SonarPanelLayoutManager} implementation
	 * @param sonarGadgetUtils the {@link SonarGadgetUtils} helper class
	 * @param permissionManager the {@link PermissionManager} implementation
	 * @param webResourceManager the {@link WebResourceManager} implementation
	 */
	public SonarTabPanelHelper(SonarAssociationManager associationManager, SonarGadgetUtils sonarGadgetUtils,
								SonarPanelLayoutManager sonarPanelLayoutManager, PermissionManager permissionManager,
								WebResourceManager webResourceManager) {
		this.associationManager = associationManager;
		this.sonarPanelLayoutManager = sonarPanelLayoutManager;
		this.permissionManager = permissionManager;
		this.webResourceManager = webResourceManager;
		this.sonarGadgetUtils = sonarGadgetUtils;
	}

	/**
	 * Prepare the Velocity parameters for the Sonar Tab Panel
	 * 
	 * @param project the {@link Project}
	 * @param component the {@link ProjectComponent}
	 * @param user the {@link User}
	 * @param params the base {@link Map} of parameters where the panel parameters will be added to
	 * @return the {@link Map} of parameters added to the parameters given
	 */
	public Map prepareVelocityParameters(Project project, ProjectComponent component, User user,
					Map params) {
		Long componentId = 0L;
		if (component != null) {
			componentId = component.getId();
		}
		webResourceManager.requireResource("com.atlassian.gadgets.embedded:gadget-standalone-resources");
		params.put("pluginKey", PLUGIN_KEY);
		params.put("waitImage", "/download/resources/" + PLUGIN_KEY + "/images/wait.gif");
		params.put("projectId", project.getId());
		params.put("componentId", componentId);
		if (associationManager.hasSonarAssociations(project.getId(), componentId)) {
			params.put("isAssociated", Boolean.TRUE);
			params.put("associations", associationManager.getSonarAssociations(project.getId(), componentId));
			params.put("gadgetIds", sonarGadgetUtils.getGadgetIds());
			params.put("layout", sonarPanelLayoutManager.getLayout());
		} else {
			params.put("isAssociated", Boolean.FALSE);
		}
		final boolean isAdmin = permissionManager.hasPermission(Permissions.PROJECT_ADMIN, project, user);
		params.put("isAdmin", isAdmin);
		if (isAdmin) {
			webResourceManager.requireResource(PLUGIN_KEY + ":sonar-admin");
		} else {
			webResourceManager.requireResource(PLUGIN_KEY + ":sonar-panel");
		}
		return params;
	}

	/**
	 * Check to see if the Panel should be shown or not
	 * 
	 * @param project the {@link Project}
	 * @param user the {@link User}
	 * @return true to show the panel, false otherwise
	 */
	public boolean showPanel(Project project, User user) {
		return permissionManager.hasPermission(Permissions.BROWSE, project, user);
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy