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) {
77 persistNewIssue(mapper, issue);
80 if (issue.isChanged()) {
81 persistChangedIssue(mapper, issue);
87 private void persistNewIssue(IssueMapper mapper, DefaultIssue issue) {
88 Integer ruleId = ruleRepository.getByKey(issue.ruleKey()).getId();
89 IssueDto dto = IssueDto.toDtoForComputationInsert(issue, ruleId, system2.now());
93 private void persistChangedIssue(IssueMapper mapper, DefaultIssue issue) {
94 IssueDto dto = IssueDto.toDtoForUpdate(issue, system2.now());
95 int updateCount = mapper.updateIfBeforeSelectedDate(dto);
96 if (updateCount == 0) {
97 // End-user and scan changed the issue at the same time.
98 // See https://jira.sonarsource.com/browse/SONAR-4309
99 conflictResolver.resolve(issue, mapper);
103 private static void insertChanges(IssueChangeMapper mapper, DefaultIssue issue) {
104 for (IssueComment comment : issue.comments()) {
105 DefaultIssueComment c = (DefaultIssueComment) comment;
107 IssueChangeDto changeDto = IssueChangeDto.of(c);
108 mapper.insert(changeDto);
111 FieldDiffs diffs = issue.currentChange();
112 if (!issue.isNew() && diffs != null) {
113 IssueChangeDto changeDto = IssueChangeDto.of(issue.key(), diffs);
114 mapper.insert(changeDto);
119 public String getDescription() {
120 return "Persist issues";