/*
* This program is copyright (c) 2007 Hortis-GRC SA.
*
* This file is part of Sonar.
* Sonar is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* Sonar is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Sonar; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*/
package ch.hortis.sonar.core.service;
import ch.hortis.sonar.model.File;
import ch.hortis.sonar.model.Rule;
import ch.hortis.sonar.model.RuleConfig;
import ch.hortis.sonar.model.RuleFailure;
import ch.hortis.sonar.model.RuleFailureLevel;
import ch.hortis.sonar.model.RulesCategory;
import ch.hortis.sonar.service.MeasureKey;
import ch.hortis.sonar.service.RulesConfigService;
import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.commons.lang.builder.ToStringBuilder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public abstract class RuleFailuresCountCalculator extends AbstractMetricCalculator {
public abstract RuleFailureLevel getLevel();
public void execute(Module module, List<Module> submodules) {
HashMap<Key, Integer> countMap = new HashMap<Key, Integer>();
//get rules configs and init the rules count map
RulesConfigService srv = super.getDatabaseService(RulesConfigService.class);
List<RuleConfig> activatedRules = srv.getRulesConfigs(getLevel());
for (RuleConfig ruleConfig : activatedRules) {
initCountMap( countMap, ruleConfig, null );
List<File> moduleFiles = module.getFiles();
for (File file : moduleFiles) {
initCountMap( countMap, ruleConfig, file );
}
}
for (RuleFailure failure : module.getRuleFailures()) {
if (failure.getLevel().equals(getLevel())) {
incrementAtFileLevel(countMap, failure);
incrementAtProjectLevel(countMap, failure);
}
}
for (Module submodule:submodules) {
for (RuleFailure failure : submodule.getRuleFailures()) {
if (failure.getLevel().equals(getLevel())) {
incrementAtProjectLevel(countMap, failure);
}
}
}
for (Map.Entry<Key, Integer> entry : countMap.entrySet()) {
Key key = entry.getKey();
module.createMeasure(new MeasureKey(getMetric(), key.rulesCategory, key.rule, key.file), (double)entry.getValue());
}
}
private void incrementAtProjectLevel(HashMap<Key, Integer> countMap, RuleFailure failure) {
increment(countMap, new Key(getLevel(), failure.getRule().getRulesCategory(), failure.getRule(), null));
increment(countMap, new Key(getLevel(), failure.getRule().getRulesCategory(), null, null));
increment(countMap, new Key(getLevel(), null, null, null));
}
private void incrementAtFileLevel(HashMap<Key, Integer> countMap, RuleFailure failure) {
increment(countMap, new Key(getLevel(), failure.getRule().getRulesCategory(), failure.getRule(), failure.getFile()));
increment(countMap, new Key(getLevel(), failure.getRule().getRulesCategory(), null, failure.getFile()));
increment(countMap, new Key(getLevel(), null, null, failure.getFile()));
}
protected void initCountMap(Map<Key, Integer> countMap, RuleConfig config, File file) {
Key key = new Key(getLevel(), config.getRule().getRulesCategory(), config.getRule(), file);
countMap.put(key, 0);
key = new Key(getLevel(), config.getRule().getRulesCategory(), null, file);
countMap.put(key, 0);
key = new Key(getLevel(), null, null, file);
countMap.put(key, 0);
}
protected void increment(Map<Key, Integer> countMap, Key key) {
Integer i = countMap.get(key);
if (i == null) {
log.warn( "Count map not initialized for key " + key );
i = 0;
}
countMap.put(key, ++i);
}
class Key {
protected File file;
protected RulesCategory rulesCategory;
protected Rule rule;
protected RuleFailureLevel level;
public Key(RuleFailureLevel level, RulesCategory rulesCategory, Rule rule, File file) {
this.file = file;
this.level = level;
this.rulesCategory = rulesCategory;
this.rule = rule;
}
public boolean equals(Object obj) {
if (!(obj instanceof Key)) {
return false;
}
if (this == obj) {
return true;
}
Key other = (Key) obj;
return new EqualsBuilder()
.append(file, other.file)
.append(level, other.level)
.append(rulesCategory, other.rulesCategory)
.append(rule, other.rule)
.isEquals();
}
public int hashCode() {
return new HashCodeBuilder(17, 37).
append(file).
append(level).
append(rulesCategory).
append(rule).
toHashCode();
}
public String toString() {
return new ToStringBuilder(this).
append(level).
append(file).
append(rulesCategory).
append(rule).
toString();
}
}
}