001 /* 002 * Sonar, open source software quality management tool. 003 * Copyright (C) 2009 SonarSource SA 004 * mailto:contact AT sonarsource DOT com 005 * 006 * Sonar is free software; you can redistribute it and/or 007 * modify it under the terms of the GNU Lesser General Public 008 * License as published by the Free Software Foundation; either 009 * version 3 of the License, or (at your option) any later version. 010 * 011 * Sonar is distributed in the hope that it will be useful, 012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 014 * Lesser General Public License for more details. 015 * 016 * You should have received a copy of the GNU Lesser General Public 017 * License along with Sonar; if not, write to the Free Software 018 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 019 */ 020 package org.sonar.batch; 021 022 import java.util.ArrayList; 023 import java.util.List; 024 025 import org.slf4j.LoggerFactory; 026 import org.sonar.api.database.DatabaseSession; 027 import org.sonar.api.database.daos.RulesDao; 028 import org.sonar.api.database.model.RuleFailureModel; 029 import org.sonar.api.database.model.Snapshot; 030 import org.sonar.api.profiles.RulesProfile; 031 import org.sonar.api.resources.Project; 032 import org.sonar.api.resources.Resource; 033 import org.sonar.api.rules.ActiveRule; 034 import org.sonar.api.rules.Rule; 035 import org.sonar.api.rules.Violation; 036 037 public class ViolationsDao { 038 039 private RulesProfile profile; 040 private DatabaseSession session; 041 private RulesDao rulesDao; 042 private boolean reuseExistingRulesConfig; 043 044 public ViolationsDao(RulesProfile profile, DatabaseSession session, RulesDao rulesDao, Project project) { 045 this.profile = profile; 046 this.session = session; 047 this.rulesDao = rulesDao; 048 this.reuseExistingRulesConfig = project.getReuseExistingRulesConfig(); 049 } 050 051 public List<Violation> getViolations(Resource resource, Integer snapshotId) { 052 List<RuleFailureModel> models = session.getResults(RuleFailureModel.class, "snapshotId", snapshotId); 053 List<Violation> violations = new ArrayList<Violation>(); 054 for (RuleFailureModel model : models) { 055 Violation violation = new Violation(model.getRule(), resource); 056 violation.setLineId(model.getLine()); 057 violation.setMessage(model.getMessage()); 058 violation.setPriority(model.getPriority()); 059 violations.add(violation); 060 } 061 return violations; 062 } 063 064 public void saveViolation(Snapshot snapshot, Violation violation) { 065 if (profile == null || snapshot == null || violation == null) { 066 throw new IllegalArgumentException("Missing data to save violation : profile=" + profile + ",snapshot=" + snapshot + ",violation=" + violation); 067 } 068 069 ActiveRule activeRule = profile.getActiveRule(violation.getRule()); 070 if (activeRule == null) { 071 if (reuseExistingRulesConfig) { 072 activeRule = new ActiveRule(profile, violation.getRule(), violation.getRule().getPriority()); 073 } else { 074 LoggerFactory.getLogger(getClass()).debug("Violation is not saved because rule is not activated : violation={}", violation); 075 } 076 } 077 if (activeRule != null) { 078 RuleFailureModel model = toModel(snapshot, violation, activeRule); 079 session.save(model); 080 } 081 } 082 083 private RuleFailureModel toModel(Snapshot snapshot, Violation violation, ActiveRule activeRule) { 084 Rule rule = reload(violation.getRule()); 085 if (rule == null) { 086 throw new IllegalArgumentException("Rule does not exist : " + violation.getRule()); 087 } 088 RuleFailureModel model = new RuleFailureModel(rule, activeRule.getPriority()); 089 violation.setPriority(activeRule.getPriority()); 090 model.setLine(violation.getLineId()); 091 model.setMessage(violation.getMessage()); 092 model.setSnapshotId(snapshot.getId()); 093 return model; 094 } 095 096 private Rule reload(Rule rule) { 097 return rule.getId() != null ? rule : rulesDao.getRuleByKey(rule.getPluginName(), rule.getKey()); 098 } 099 }