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.Collection;
import java.util.Map;

import org.sonar.wsclient.Sonar;
import org.sonar.wsclient.services.MetricQuery;

import com.atlassian.crowd.embedded.api.User;
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.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.marvelution.jira.plugins.sonar.services.associations.SonarAssociation;
import com.marvelution.jira.plugins.sonar.services.associations.SonarAssociationManager;
import com.marvelution.jira.plugins.sonar.services.servers.SonarClientFactory;
import com.marvelution.jira.plugins.sonar.utils.PluginHelper;

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

	private final SonarAssociationManager associationManager;
	private final SonarClientFactory clientFactory;
	private final PermissionManager permissionManager;
	private final WebResourceManager webResourceManager;

	/**
	 * Constructor
	 * 
	 * @param associationManager the {@link SonarAssociationManager} implementation
	 * @param clientFactory the {@link SonarClientFactory} implementation
	 * @param permissionManager the {@link PermissionManager} implementation
	 * @param webResourceManager the {@link WebResourceManager} implementation
	 */
	public SonarTabPanelHelper(SonarAssociationManager associationManager, SonarClientFactory clientFactory,
						PermissionManager permissionManager, WebResourceManager webResourceManager) {
		this.associationManager = associationManager;
		this.clientFactory = clientFactory;
		this.permissionManager = permissionManager;
		this.webResourceManager = webResourceManager;
	}

	/**
	 * 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();
		}
		params.put("pluginKey", PluginHelper.getPluginKey());
		params.put("waitImage", "/download/resources/" + PluginHelper.getPluginKey() + "/images/wait.gif");
		if (!params.containsKey("context")) {
			params.put("context", project.getId());
		}
		params.put("componentId", componentId);
		Collection associations = getAssociations(project, component, params);
		if (!associations.isEmpty()) {
			params.put("isAssociated", Boolean.TRUE);
			params.put("associations", associations);
		} else {
			params.put("isAssociated", Boolean.FALSE);
		}
		final boolean isAdmin = permissionManager.hasPermission(Permissions.PROJECT_ADMIN, project, user);
		params.put("isAdmin", isAdmin);
		if (isAdmin) {
			webResourceManager.requireResource(PluginHelper.getPluginKey() + ":sonar-admin");
		}
		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)
			&& associationManager.hasAssociations(project);
	}

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

	/**
	 * 
	 * @param project
	 * @param component
	 * @param params
	 * @return
	 */
	private Collection getAssociations(Project project, ProjectComponent component,
					Map params) {
		if (params.containsKey("metricKey")) {
			Map servers = Maps.newHashMap();
			Collection associations = Lists.newArrayList();
			for (SonarAssociation association : associationManager.getAssociations(project, component)) {
				if (servers.containsKey(association.getSonarServer().getID())
					&& servers.get(association.getSonarServer().getID())) {
					associations.add(association);
				} else {
					Sonar sonar = clientFactory.create(association.getSonarServer());
					try {
						if (sonar.find(MetricQuery.byKey((String) params.get("metricKey"))) != null) {
							associations.add(association);
							servers.put(association.getSonarServer().getID(), Boolean.TRUE);
						} else {
							servers.put(association.getSonarServer().getID(), Boolean.FALSE);
						}
					} catch (Exception e) {
						// The server is not responding so it might be offline, exclude the association
						servers.put(association.getSonarServer().getID(), Boolean.FALSE);
					}
				}
			}
			return associations;
		} else {
			return associationManager.getAssociations(project, component);
		}
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy