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 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_SONAR_PREFIX = "sonar.association.";
private static final String CONFIG_SONAR_PROJECT_ID_SUFFIX = ".projectId";
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 projectIds = 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_PROJECT_ID_SUFFIX)) {
projectIds.add(propertySet.getLong(key));
}
}
for (Long projectId : projectIds) {
load(projectId);
}
}
/**
* {@inheritDoc}
*/
@Override
public boolean hasSonarAssociations() {
return !associations.isEmpty();
}
/**
* {@inheritDoc}
*/
@Override
public boolean hasSonarAssociation(Long projectId) {
return associations.containsKey(projectId);
}
/**
* {@inheritDoc}
*/
@Override
public SonarAssociation getSonarAssociation(Long projectId) {
return associations.get(projectId);
}
/**
* {@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));
}
propertySet.setLong(getProjectIdPropertyKey(sonarAssociation.getProjectId()),
sonarAssociation.getProjectId());
propertySet.setString(getSonarServerPropertyKey(sonarAssociation.getProjectId()),
sonarAssociation.getSonarServer());
propertySet.setString(getSonarProjectPropertyKey(sonarAssociation.getProjectId()),
sonarAssociation.getSonarProject());
associations.put(sonarAssociation.getProjectId(), sonarAssociation);
}
/**
* {@inheritDoc}
*/
@Override
public void remove(Long projectId) {
propertySet.remove(getProjectIdPropertyKey(projectId));
propertySet.remove(getSonarServerPropertyKey(projectId));
propertySet.remove(getSonarProjectPropertyKey(projectId));
associations.remove(projectId);
}
/**
* Load a Sonar Association from the {@link PropertySet}
*
* @param projectId the Id of the Jira project
*/
private void load(Long projectId) {
final SonarAssociation association = new DefaultSonarAssociationImpl();
association.setProjectId(propertySet.getLong(getProjectIdPropertyKey(projectId)));
association.setSonarServer(propertySet.getString(getSonarServerPropertyKey(projectId)));
association.setSonarProject(propertySet.getString(getSonarProjectPropertyKey(projectId)));
associations.put(association.getProjectId(), association);
}
/**
* Get the {@link PropertySet} key for the Jira project Id property
*
* @param projectId the projectId to get the property key for
* @return the property key
*/
protected String getProjectIdPropertyKey(Long projectId) {
return CONFIG_SONAR_PREFIX + projectId + CONFIG_SONAR_PROJECT_ID_SUFFIX;
}
/**
* Get the {@link PropertySet} key for the Soner Server property
*
* @param projectId the projectId to get the property key for
* @return the property key
*/
protected String getSonarServerPropertyKey(Long projectId) {
return CONFIG_SONAR_PREFIX + projectId + CONFIG_SONAR_SERVER_SUFFIX;
}
/**
* Get the {@link PropertySet} key for the Sonar Project Key property
*
* @param projectId the projectId to get the property key for
* @return the property key
*/
protected String getSonarProjectPropertyKey(Long projectId) {
return CONFIG_SONAR_PREFIX + projectId + CONFIG_SONAR_PROJECT_KEY_SUFFIX;
}
}