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.

ViolationPersister.java 3.2KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /*
  2. * Sonar, open source software quality management tool.
  3. * Copyright (C) 2008-2011 SonarSource
  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.index;
  21. import org.sonar.api.database.DatabaseSession;
  22. import org.sonar.api.database.model.RuleFailureModel;
  23. import org.sonar.api.database.model.Snapshot;
  24. import org.sonar.api.resources.Project;
  25. import org.sonar.api.rules.Rule;
  26. import org.sonar.api.rules.RuleFinder;
  27. import org.sonar.api.rules.Violation;
  28. public final class ViolationPersister {
  29. private DatabaseSession session;
  30. private ResourcePersister resourcePersister;
  31. private RuleFinder ruleFinder;
  32. public ViolationPersister(DatabaseSession session, ResourcePersister resourcePersister, RuleFinder ruleFinder) {
  33. this.session = session;
  34. this.resourcePersister = resourcePersister;
  35. this.ruleFinder = ruleFinder;
  36. }
  37. void saveViolation(Project project, Violation violation) {
  38. saveViolation(project, violation, null, null);
  39. }
  40. public void saveViolation(Project project, Violation violation, RuleFailureModel pastViolation, String checksum) {
  41. Snapshot snapshot = resourcePersister.saveResource(project, violation.getResource());
  42. RuleFailureModel model = createModel(violation);
  43. if (pastViolation!=null) {
  44. model.setCreatedAt(pastViolation.getCreatedAt());
  45. model.setPermanentId(pastViolation.getPermanentId());
  46. model.setSwitchedOff(pastViolation.isSwitchedOff());
  47. } else {
  48. // avoid plugins setting date
  49. model.setCreatedAt(snapshot.getCreatedAt());
  50. }
  51. model.setSnapshotId(snapshot.getId());
  52. model.setChecksum(checksum);
  53. session.save(model);
  54. if (model.getPermanentId()==null) {
  55. model.setPermanentId(model.getId());
  56. session.save(model);
  57. }
  58. // the following fields can have been changed
  59. violation.setMessage(model.getMessage());// the message can be changed in the class RuleFailure (truncate + trim)
  60. violation.setCreatedAt(model.getCreatedAt());
  61. violation.setSwitchedOff(model.isSwitchedOff());
  62. }
  63. public void commit() {
  64. session.commit();
  65. }
  66. private RuleFailureModel createModel(Violation violation) {
  67. RuleFailureModel model = new RuleFailureModel();
  68. Rule rule = ruleFinder.findByKey(violation.getRule().getRepositoryKey(), violation.getRule().getKey());
  69. model.setRuleId(rule.getId());
  70. model.setPriority(violation.getSeverity());
  71. model.setLine(violation.getLineId());
  72. model.setMessage(violation.getMessage());
  73. model.setCost(violation.getCost());
  74. return model;
  75. }
  76. }