com.marvelution.jira.plugins.sonar.DefaultSonarAssociationManagerImpl 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;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import com.marvelution.jira.plugins.sonar.service.SonarAssociation;
import com.marvelution.jira.plugins.sonar.service.SonarAssociationManager;
import com.marvelution.jira.plugins.sonar.service.SonarPropertyManager;
import com.opensymphony.module.propertyset.PropertySet;
/**
* Default {@link SonarAssociationManager} implementation
*
* @author Mark Rekveld
*/
public class DefaultSonarAssociationManagerImpl implements SonarAssociationManager {
private static final String CONFIG_NEXT_ASSOCIATION_ID = "sonar.association.nextId";
private static final String CONFIG_SONAR_PREFIX = "sonar.association.";
private static final String CONFIG_SONAR_ASSOCIATION_ID_SUFFIX = ".associationId";
private static final String CONFIG_SONAR_PROJECT_ID_SUFFIX = ".projectId";
private static final String CONFIG_SONAR_COMPONENT_ID_SUFFIX = ".componentId";
private static final String CONFIG_SONAR_SERVER_SUFFIX = ".server";
private static final String CONFIG_SONAR_PROJECT_KEY_SUFFIX = ".projectKey";
private final PropertySet propertySet;
private final Map associations = new HashMap();
/**
* Constructor
*
* @param propertyManager the {@link SonarPropertyManager} implementation
*/
public DefaultSonarAssociationManagerImpl(SonarPropertyManager propertyManager) {
propertySet = propertyManager.getPropertySet();
final Collection associationIds = new HashSet();
final Collection> keys = propertySet.getKeys(CONFIG_SONAR_PREFIX);
for (Object entry : keys) {
final String key = (String) entry;
if (key.startsWith(CONFIG_SONAR_PREFIX) && key.endsWith(CONFIG_SONAR_ASSOCIATION_ID_SUFFIX)) {
associationIds.add(propertySet.getLong(key));
}
}
for (Long associationId : associationIds) {
load(associationId);
}
}
/**
* {@inheritDoc}
*/
@Override
public boolean hasSonarAssociations() {
return !associations.isEmpty();
}
/**
* {@inheritDoc}
*/
@Override
public boolean hasSonarAssociations(Long projectId) {
return hasSonarAssociations(projectId, 0L);
}
/**
* {@inheritDoc}
*/
@Override
public boolean hasSonarAssociations(Long projectId, Long componentId) {
for (SonarAssociation association : associations.values()) {
if (association.getProjectId().compareTo(projectId) == 0
&& association.getComponentId().compareTo(componentId) == 0) {
return true;
}
}
return false;
}
/**
* {@inheritDoc}
*/
@Override
public SonarAssociation getSonarAssociationById(Long associationId) {
return associations.get(associationId);
}
/**
* {@inheritDoc}
*/
@Override
public Collection getSonarAssociations(Long projectId) {
return getSonarAssociations(projectId, 0L);
}
/**
* {@inheritDoc}
*/
@Override
public Collection getSonarAssociations(Long projectId, Long componentId) {
final Set componentAssociations = new HashSet();
for (SonarAssociation association : associations.values()) {
if (association.getProjectId().compareTo(projectId) == 0
&& association.getComponentId().compareTo(componentId) == 0) {
componentAssociations.add(association);
}
}
return componentAssociations;
}
/**
* {@inheritDoc}
*/
@Override
public Collection getSonarAssociations() {
return Collections.unmodifiableCollection(associations.values());
}
/**
* {@inheritDoc}
*/
@Override
public void put(SonarAssociation sonarAssociation) {
if (sonarAssociation.getSonarServer().endsWith("/")) {
sonarAssociation.setSonarServer(sonarAssociation.getSonarServer().substring(0,
sonarAssociation.getSonarServer().length() - 1));
}
if (sonarAssociation.getAssociationId() == 0) {
sonarAssociation.setAssociationId(generateAssociationId());
}
propertySet.setLong(getAssociationIdPropertyKey(sonarAssociation.getAssociationId()), sonarAssociation
.getAssociationId());
propertySet.setLong(getProjectIdPropertyKey(sonarAssociation.getAssociationId()),
sonarAssociation.getProjectId());
propertySet.setLong(getComponentIdPropertyKey(sonarAssociation.getAssociationId()),
sonarAssociation.getComponentId());
propertySet.setString(getSonarServerPropertyKey(sonarAssociation.getAssociationId()),
sonarAssociation.getSonarServer());
propertySet.setString(getSonarProjectPropertyKey(sonarAssociation.getAssociationId()),
sonarAssociation.getSonarProject());
associations.put(sonarAssociation.getAssociationId(), sonarAssociation);
}
/**
* {@inheritDoc}
*/
@Override
public void remove(Long associationId) {
propertySet.remove(getAssociationIdPropertyKey(associationId));
propertySet.remove(getProjectIdPropertyKey(associationId));
propertySet.remove(getComponentIdPropertyKey(associationId));
propertySet.remove(getSonarServerPropertyKey(associationId));
propertySet.remove(getSonarProjectPropertyKey(associationId));
associations.remove(associationId);
}
/**
* {@inheritDoc}
*/
@Override
public void remove(SonarAssociation sonarAssociation) {
remove(sonarAssociation.getAssociationId());
}
/**
* Load a Sonar Association from the {@link PropertySet}
*
* @param associationId the Id of the Jira project
*/
private void load(Long associationId) {
final SonarAssociation association = new DefaultSonarAssociationImpl();
association.setAssociationId(propertySet.getLong(getAssociationIdPropertyKey(associationId)));
association.setProjectId(propertySet.getLong(getProjectIdPropertyKey(associationId)));
association.setComponentId(propertySet.getLong(getComponentIdPropertyKey(associationId)));
association.setSonarServer(propertySet.getString(getSonarServerPropertyKey(associationId)));
association.setSonarProject(propertySet.getString(getSonarProjectPropertyKey(associationId)));
associations.put(association.getAssociationId(), association);
}
/**
* Get the {@link PropertySet} key for the Association Id property
*
* @param associationId the associationId to get the property key for
* @return the property key
*/
public static String getAssociationIdPropertyKey(Long associationId) {
return CONFIG_SONAR_PREFIX + associationId + CONFIG_SONAR_ASSOCIATION_ID_SUFFIX;
}
/**
* Get the {@link PropertySet} key for the Jira project Id property
*
* @param associationId the associationId to get the property key for
* @return the property key
*/
public static String getProjectIdPropertyKey(Long associationId) {
return CONFIG_SONAR_PREFIX + associationId + CONFIG_SONAR_PROJECT_ID_SUFFIX;
}
/**
* Get the {@link PropertySet} key for the Jira project component Id property
*
* @param associationId the associationId to get the property key for
* @return the property key
*/
public static String getComponentIdPropertyKey(Long associationId) {
return CONFIG_SONAR_PREFIX + associationId + CONFIG_SONAR_COMPONENT_ID_SUFFIX;
}
/**
* Get the {@link PropertySet} key for the Soner Server property
*
* @param associationId the associationId to get the property key for
* @return the property key
*/
public static String getSonarServerPropertyKey(Long associationId) {
return CONFIG_SONAR_PREFIX + associationId + CONFIG_SONAR_SERVER_SUFFIX;
}
/**
* Get the {@link PropertySet} key for the Sonar Project Key property
*
* @param associationId the associationId to get the property key for
* @return the property key
*/
public static String getSonarProjectPropertyKey(Long associationId) {
return CONFIG_SONAR_PREFIX + associationId + CONFIG_SONAR_PROJECT_KEY_SUFFIX;
}
/**
* Generate the next {@link SonarAssociation} Id
*
* @return the Id for the next {@link SonarAssociation}
*/
private Long generateAssociationId() {
Long nextId = 1L;
if (propertySet.exists(CONFIG_NEXT_ASSOCIATION_ID)) {
nextId = propertySet.getLong(CONFIG_NEXT_ASSOCIATION_ID);
}
propertySet.setLong(CONFIG_NEXT_ASSOCIATION_ID, nextId + 1);
return nextId;
}
}