You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ViolationsDao.java 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /*
  2. * Sonar, open source software quality management tool.
  3. * Copyright (C) 2009 SonarSource SA
  4. * mailto:contact AT sonarsource DOT com
  5. *
  6. * Sonar is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * Sonar is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with Sonar; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
  19. */
  20. package org.sonar.batch;
  21. import org.slf4j.LoggerFactory;
  22. import org.sonar.api.database.DatabaseSession;
  23. import org.sonar.api.database.model.RuleFailureModel;
  24. import org.sonar.api.database.model.Snapshot;
  25. import org.sonar.api.profiles.RulesProfile;
  26. import org.sonar.api.resources.Project;
  27. import org.sonar.api.resources.Resource;
  28. import org.sonar.api.rules.ActiveRule;
  29. import org.sonar.api.rules.Rule;
  30. import org.sonar.api.rules.Violation;
  31. import org.sonar.jpa.dao.RulesDao;
  32. import java.util.ArrayList;
  33. import java.util.List;
  34. public class ViolationsDao {
  35. private RulesProfile profile;
  36. private DatabaseSession session;
  37. private RulesDao rulesDao;
  38. private boolean reuseExistingRulesConfig;
  39. public ViolationsDao(RulesProfile profile, DatabaseSession session, RulesDao rulesDao, Project project) {
  40. this.profile = profile;
  41. this.session = session;
  42. this.rulesDao = rulesDao;
  43. this.reuseExistingRulesConfig = project.getReuseExistingRulesConfig();
  44. }
  45. public List<Violation> getViolations(Resource resource, Integer snapshotId) {
  46. List<RuleFailureModel> models = session.getResults(RuleFailureModel.class, "snapshotId", snapshotId);
  47. List<Violation> violations = new ArrayList<Violation>();
  48. for (RuleFailureModel model : models) {
  49. Violation violation = new Violation(model.getRule(), resource);
  50. violation.setLineId(model.getLine());
  51. violation.setMessage(model.getMessage());
  52. violation.setPriority(model.getPriority());
  53. violation.setCost(model.getCost());
  54. violations.add(violation);
  55. }
  56. return violations;
  57. }
  58. public void saveViolation(Snapshot snapshot, Violation violation) {
  59. if (profile == null || snapshot == null || violation == null) {
  60. throw new IllegalArgumentException("Missing data to save violation : profile=" + profile + ",snapshot=" + snapshot + ",violation=" + violation);
  61. }
  62. ActiveRule activeRule = profile.getActiveRule(violation.getRule());
  63. if (activeRule == null) {
  64. if (reuseExistingRulesConfig) {
  65. activeRule = new ActiveRule(profile, violation.getRule(), violation.getRule().getPriority());
  66. } else {
  67. LoggerFactory.getLogger(getClass()).debug("Violation is not saved because rule is not activated : violation={}", violation);
  68. }
  69. }
  70. if (activeRule != null) {
  71. RuleFailureModel model = toModel(snapshot, violation, activeRule);
  72. session.save(model);
  73. }
  74. }
  75. private RuleFailureModel toModel(Snapshot snapshot, Violation violation, ActiveRule activeRule) {
  76. Rule rule = reload(violation.getRule());
  77. if (rule == null) {
  78. throw new IllegalArgumentException("Rule does not exist : " + violation.getRule());
  79. }
  80. RuleFailureModel model = new RuleFailureModel(rule, activeRule.getPriority());
  81. violation.setPriority(activeRule.getPriority());
  82. model.setLine(violation.getLineId());
  83. model.setMessage(violation.getMessage());
  84. model.setCost(violation.getCost());
  85. model.setSnapshotId(snapshot.getId());
  86. return model;
  87. }
  88. private Rule reload(Rule rule) {
  89. return rule.getId() != null ? rule : rulesDao.getRuleByKey(rule.getPluginName(), rule.getKey());
  90. }
  91. }