3 * Copyright (C) 2009-2016 SonarSource SA
4 * mailto:contact 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.base.Predicate;
23 import com.google.common.collect.ImmutableMap;
24 import com.google.common.collect.Iterables;
25 import java.util.List;
26 import java.util.Locale;
29 import javax.annotation.Nullable;
30 import org.sonar.api.i18n.I18n;
31 import org.sonar.db.DbClient;
32 import org.sonar.db.DbSession;
33 import org.sonar.db.MyBatis;
34 import org.sonar.db.component.ComponentLinkDto;
35 import org.sonar.scanner.protocol.output.ScannerReport;
36 import org.sonar.scanner.protocol.output.ScannerReport.ComponentLink.ComponentLinkType;
37 import org.sonar.server.computation.task.projectanalysis.batch.BatchReportReader;
38 import org.sonar.server.computation.task.projectanalysis.component.Component;
39 import org.sonar.server.computation.task.projectanalysis.component.CrawlerDepthLimit;
40 import org.sonar.server.computation.task.projectanalysis.component.DepthTraversalTypeAwareCrawler;
41 import org.sonar.server.computation.task.projectanalysis.component.TreeRootHolder;
42 import org.sonar.server.computation.task.projectanalysis.component.TypeAwareVisitorAdapter;
43 import org.sonar.server.computation.task.step.ComputationStep;
45 import static com.google.common.collect.Sets.newHashSet;
46 import static org.sonar.server.computation.task.projectanalysis.component.ComponentVisitor.Order.PRE_ORDER;
49 * Persist project and module links
51 public class PersistProjectLinksStep implements ComputationStep {
53 private final DbClient dbClient;
54 private final I18n i18n;
55 private final TreeRootHolder treeRootHolder;
56 private final BatchReportReader reportReader;
58 private static final Map<ComponentLinkType, String> typesConverter = ImmutableMap.of(
59 ComponentLinkType.HOME, ComponentLinkDto.TYPE_HOME_PAGE,
60 ComponentLinkType.SCM, ComponentLinkDto.TYPE_SOURCES,
61 ComponentLinkType.SCM_DEV, ComponentLinkDto.TYPE_SOURCES_DEV,
62 ComponentLinkType.CI, ComponentLinkDto.TYPE_CI,
63 ComponentLinkType.ISSUE, ComponentLinkDto.TYPE_ISSUE_TRACKER);
65 public PersistProjectLinksStep(DbClient dbClient, I18n i18n, TreeRootHolder treeRootHolder, BatchReportReader reportReader) {
66 this.dbClient = dbClient;
68 this.treeRootHolder = treeRootHolder;
69 this.reportReader = reportReader;
73 public void execute() {
74 DbSession session = dbClient.openSession(false);
76 new DepthTraversalTypeAwareCrawler(new ProjectLinkVisitor(session))
77 .visit(treeRootHolder.getRoot());
80 MyBatis.closeQuietly(session);
84 private class ProjectLinkVisitor extends TypeAwareVisitorAdapter {
86 private final DbSession session;
88 private ProjectLinkVisitor(DbSession session) {
89 super(CrawlerDepthLimit.FILE, PRE_ORDER);
90 this.session = session;
94 public void visitProject(Component project) {
95 processComponent(project);
99 public void visitModule(Component module) {
100 processComponent(module);
103 private void processComponent(Component component) {
104 ScannerReport.Component batchComponent = reportReader.readComponent(component.getReportAttributes().getRef());
105 processLinks(component.getUuid(), batchComponent.getLinkList());
108 private void processLinks(String componentUuid, List<ScannerReport.ComponentLink> links) {
109 List<ComponentLinkDto> previousLinks = dbClient.componentLinkDao().selectByComponentUuid(session, componentUuid);
110 mergeLinks(session, componentUuid, links, previousLinks);
113 private void mergeLinks(DbSession session, String componentUuid, List<ScannerReport.ComponentLink> links, List<ComponentLinkDto> previousLinks) {
114 Set<String> linkType = newHashSet();
115 for (final ScannerReport.ComponentLink link : links) {
116 String type = convertType(link.getType());
117 if (!linkType.contains(type)) {
120 throw new IllegalArgumentException(String.format("Link of type '%s' has already been declared on component '%s'", type, componentUuid));
123 ComponentLinkDto previousLink = Iterables.find(previousLinks, new Predicate<ComponentLinkDto>() {
125 public boolean apply(@Nullable ComponentLinkDto input) {
126 return input != null && input.getType().equals(convertType(link.getType()));
129 if (previousLink == null) {
130 dbClient.componentLinkDao().insert(session,
131 new ComponentLinkDto()
132 .setComponentUuid(componentUuid)
134 .setName(i18n.message(Locale.ENGLISH, "project_links." + type, null))
135 .setHref(link.getHref()));
137 previousLink.setHref(link.getHref());
138 dbClient.componentLinkDao().update(session, previousLink);
142 for (ComponentLinkDto dto : previousLinks) {
143 if (!linkType.contains(dto.getType()) && ComponentLinkDto.PROVIDED_TYPES.contains(dto.getType())) {
144 dbClient.componentLinkDao().delete(session, dto.getId());
149 private String convertType(ComponentLinkType reportType) {
150 String type = typesConverter.get(reportType);
154 throw new IllegalArgumentException(String.format("Unsupported type %s", reportType.name()));
160 public String getDescription() {
161 return "Persist project links";