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.issue;
22 import java.util.Collections;
23 import java.util.List;
24 import javax.annotation.Nullable;
25 import org.sonar.core.issue.DefaultIssue;
26 import org.sonar.core.issue.tracking.Input;
27 import org.sonar.core.issue.tracking.LazyInput;
28 import org.sonar.core.issue.tracking.LineHashSequence;
29 import org.sonar.db.DbClient;
30 import org.sonar.db.DbSession;
31 import org.sonar.server.computation.task.projectanalysis.component.Component;
32 import org.sonar.server.computation.task.projectanalysis.component.MergeBranchComponentUuids;
34 public class TrackerMergeBranchInputFactory {
35 private static final LineHashSequence EMPTY_LINE_HASH_SEQUENCE = new LineHashSequence(Collections.<String>emptyList());
37 private final ComponentIssuesLoader mergeIssuesLoader;
38 private final DbClient dbClient;
39 private final MergeBranchComponentUuids mergeBranchComponentUuids;
41 public TrackerMergeBranchInputFactory(ComponentIssuesLoader mergeIssuesLoader, MergeBranchComponentUuids mergeBranchComponentUuids, DbClient dbClient) {
42 this.mergeIssuesLoader = mergeIssuesLoader;
43 this.mergeBranchComponentUuids = mergeBranchComponentUuids;
44 this.dbClient = dbClient;
45 // TODO detect file moves?
48 public Input<DefaultIssue> create(Component component) {
49 String mergeBranchComponentUuid = mergeBranchComponentUuids.getUuid(component.getKey());
50 return new MergeLazyInput(component.getType(), mergeBranchComponentUuid);
53 private class MergeLazyInput extends LazyInput<DefaultIssue> {
54 private final Component.Type type;
55 private final String mergeBranchComponentUuid;
57 private MergeLazyInput(Component.Type type, @Nullable String mergeBranchComponentUuid) {
59 this.mergeBranchComponentUuid = mergeBranchComponentUuid;
63 protected LineHashSequence loadLineHashSequence() {
64 if (mergeBranchComponentUuid == null || type != Component.Type.FILE) {
65 return EMPTY_LINE_HASH_SEQUENCE;
68 try (DbSession session = dbClient.openSession(false)) {
69 List<String> hashes = dbClient.fileSourceDao().selectLineHashes(session, mergeBranchComponentUuid);
70 if (hashes == null || hashes.isEmpty()) {
71 return EMPTY_LINE_HASH_SEQUENCE;
73 return new LineHashSequence(hashes);
78 protected List<DefaultIssue> loadIssues() {
79 if (mergeBranchComponentUuid == null) {
80 return Collections.emptyList();
82 return mergeIssuesLoader.loadForComponentUuid(mergeBranchComponentUuid);