3 * Copyright (C) 2009-2017 SonarSource SA
4 * mailto:info AT sonarsource DOT com
6 * This program 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.
11 * This program 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.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 package org.sonar.server.computation.task.projectanalysis.step;
22 import org.sonar.api.issue.IssueComment;
23 import org.sonar.api.utils.System2;
24 import org.sonar.core.issue.DefaultIssue;
25 import org.sonar.core.issue.DefaultIssueComment;
26 import org.sonar.core.issue.FieldDiffs;
27 import org.sonar.core.util.CloseableIterator;
28 import org.sonar.db.DbClient;
29 import org.sonar.db.DbSession;
30 import org.sonar.db.issue.IssueChangeDto;
31 import org.sonar.db.issue.IssueChangeMapper;
32 import org.sonar.db.issue.IssueDto;
33 import org.sonar.db.issue.IssueMapper;
34 import org.sonar.server.computation.task.projectanalysis.issue.IssueCache;
35 import org.sonar.server.computation.task.projectanalysis.issue.RuleRepository;
36 import org.sonar.server.computation.task.projectanalysis.issue.UpdateConflictResolver;
37 import org.sonar.server.computation.task.step.ComputationStep;
39 public class PersistIssuesStep implements ComputationStep {
41 private final DbClient dbClient;
42 private final System2 system2;
43 private final UpdateConflictResolver conflictResolver;
44 private final RuleRepository ruleRepository;
45 private final IssueCache issueCache;
47 public PersistIssuesStep(DbClient dbClient, System2 system2, UpdateConflictResolver conflictResolver,
48 RuleRepository ruleRepository, IssueCache issueCache) {
49 this.dbClient = dbClient;
50 this.system2 = system2;
51 this.conflictResolver = conflictResolver;
52 this.ruleRepository = ruleRepository;
53 this.issueCache = issueCache;
57 public void execute() {
58 try (DbSession dbSession = dbClient.openSession(true);
59 CloseableIterator<DefaultIssue> issues = issueCache.traverse()) {
61 IssueMapper mapper = dbSession.getMapper(IssueMapper.class);
62 IssueChangeMapper changeMapper = dbSession.getMapper(IssueChangeMapper.class);
63 while (issues.hasNext()) {
64 DefaultIssue issue = issues.next();
65 boolean saved = persistIssueIfRequired(mapper, issue);
67 insertChanges(changeMapper, issue);
70 dbSession.flushStatements();
75 private boolean persistIssueIfRequired(IssueMapper mapper, DefaultIssue issue) {
76 if (issue.isNew() || issue.isCopied()) {
77 persistNewIssue(mapper, issue);
81 if (issue.isChanged()) {
82 persistChangedIssue(mapper, issue);
88 private void persistNewIssue(IssueMapper mapper, DefaultIssue issue) {
89 Integer ruleId = ruleRepository.getByKey(issue.ruleKey()).getId();
90 IssueDto dto = IssueDto.toDtoForComputationInsert(issue, ruleId, system2.now());
94 private void persistChangedIssue(IssueMapper mapper, DefaultIssue issue) {
95 IssueDto dto = IssueDto.toDtoForUpdate(issue, system2.now());
96 int updateCount = mapper.updateIfBeforeSelectedDate(dto);
97 if (updateCount == 0) {
98 // End-user and scan changed the issue at the same time.
99 // See https://jira.sonarsource.com/browse/SONAR-4309
100 conflictResolver.resolve(issue, mapper);
104 private static void insertChanges(IssueChangeMapper mapper, DefaultIssue issue) {
105 for (IssueComment comment : issue.comments()) {
106 DefaultIssueComment c = (DefaultIssueComment) comment;
108 IssueChangeDto changeDto = IssueChangeDto.of(c);
109 mapper.insert(changeDto);
112 FieldDiffs diffs = issue.currentChange();
113 if (!issue.isNew() && diffs != null) {
114 IssueChangeDto changeDto = IssueChangeDto.of(issue.key(), diffs);
115 mapper.insert(changeDto);
120 public String getDescription() {
121 return "Persist issues";