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 com.google.common.collect.ImmutableMap;
23 import java.util.List;
24 import java.util.Locale;
26 import java.util.Optional;
28 import org.sonar.api.i18n.I18n;
29 import org.sonar.db.DbClient;
30 import org.sonar.db.DbSession;
31 import org.sonar.db.component.ComponentLinkDto;
32 import org.sonar.scanner.protocol.output.ScannerReport;
33 import org.sonar.scanner.protocol.output.ScannerReport.ComponentLink.ComponentLinkType;
34 import org.sonar.server.computation.task.projectanalysis.batch.BatchReportReader;
35 import org.sonar.server.computation.task.projectanalysis.component.Component;
36 import org.sonar.server.computation.task.projectanalysis.component.CrawlerDepthLimit;
37 import org.sonar.server.computation.task.projectanalysis.component.DepthTraversalTypeAwareCrawler;
38 import org.sonar.server.computation.task.projectanalysis.component.TreeRootHolder;
39 import org.sonar.server.computation.task.projectanalysis.component.TypeAwareVisitorAdapter;
40 import org.sonar.server.computation.task.step.ComputationStep;
42 import static com.google.common.collect.Sets.newHashSet;
43 import static org.sonar.server.computation.task.projectanalysis.component.ComponentVisitor.Order.PRE_ORDER;
46 * Persist project and module links
48 public class PersistProjectLinksStep implements ComputationStep {
50 private final DbClient dbClient;
51 private final I18n i18n;
52 private final TreeRootHolder treeRootHolder;
53 private final BatchReportReader reportReader;
55 private static final Map<ComponentLinkType, String> typesConverter = ImmutableMap.of(
56 ComponentLinkType.HOME, ComponentLinkDto.TYPE_HOME_PAGE,
57 ComponentLinkType.SCM, ComponentLinkDto.TYPE_SOURCES,
58 ComponentLinkType.SCM_DEV, ComponentLinkDto.TYPE_SOURCES_DEV,
59 ComponentLinkType.CI, ComponentLinkDto.TYPE_CI,
60 ComponentLinkType.ISSUE, ComponentLinkDto.TYPE_ISSUE_TRACKER);
62 public PersistProjectLinksStep(DbClient dbClient, I18n i18n, TreeRootHolder treeRootHolder, BatchReportReader reportReader) {
63 this.dbClient = dbClient;
65 this.treeRootHolder = treeRootHolder;
66 this.reportReader = reportReader;
70 public void execute() {
71 try (DbSession session = dbClient.openSession(false)) {
72 new DepthTraversalTypeAwareCrawler(new ProjectLinkVisitor(session))
73 .visit(treeRootHolder.getRoot());
78 private class ProjectLinkVisitor extends TypeAwareVisitorAdapter {
80 private final DbSession session;
82 private ProjectLinkVisitor(DbSession session) {
83 super(CrawlerDepthLimit.FILE, PRE_ORDER);
84 this.session = session;
88 public void visitProject(Component project) {
89 processComponent(project);
93 public void visitModule(Component module) {
94 processComponent(module);
97 private void processComponent(Component component) {
98 ScannerReport.Component batchComponent = reportReader.readComponent(component.getReportAttributes().getRef());
99 processLinks(component.getUuid(), batchComponent.getLinkList());
102 private void processLinks(String componentUuid, List<ScannerReport.ComponentLink> links) {
103 List<ComponentLinkDto> previousLinks = dbClient.componentLinkDao().selectByComponentUuid(session, componentUuid);
104 mergeLinks(session, componentUuid, links, previousLinks);
107 private void mergeLinks(DbSession session, String componentUuid, List<ScannerReport.ComponentLink> links, List<ComponentLinkDto> previousLinks) {
108 Set<String> linkType = newHashSet();
109 for (final ScannerReport.ComponentLink link : links) {
110 String type = convertType(link.getType());
111 if (!linkType.contains(type)) {
114 throw new IllegalArgumentException(String.format("Link of type '%s' has already been declared on component '%s'", type, componentUuid));
117 Optional<ComponentLinkDto> previousLink = previousLinks.stream()
118 .filter(input -> input != null && input.getType().equals(convertType(link.getType())))
120 if (previousLink.isPresent()) {
121 previousLink.get().setHref(link.getHref());
122 dbClient.componentLinkDao().update(session, previousLink.get());
124 dbClient.componentLinkDao().insert(session,
125 new ComponentLinkDto()
126 .setComponentUuid(componentUuid)
128 .setName(i18n.message(Locale.ENGLISH, "project_links." + type, null))
129 .setHref(link.getHref()));
133 for (ComponentLinkDto dto : previousLinks) {
134 if (!linkType.contains(dto.getType()) && ComponentLinkDto.PROVIDED_TYPES.contains(dto.getType())) {
135 dbClient.componentLinkDao().delete(session, dto.getId());
140 private String convertType(ComponentLinkType reportType) {
141 String type = typesConverter.get(reportType);
145 throw new IllegalArgumentException(String.format("Unsupported type %s", reportType.name()));
151 public String getDescription() {
152 return "Persist project links";