123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162 |
- /*
- * SonarQube, open source software quality management tool.
- * Copyright (C) 2008-2014 SonarSource
- * mailto:contact AT sonarsource DOT com
- *
- * SonarQube is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or (at your option) any later version.
- *
- * SonarQube is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with this program; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
- */
-
- package org.sonar.server.computation.step;
-
- import com.google.common.base.Predicate;
- import com.google.common.collect.ImmutableMap;
- import com.google.common.collect.Iterables;
- import java.util.List;
- import java.util.Locale;
- import java.util.Map;
- import java.util.Set;
- import javax.annotation.Nullable;
- import org.sonar.api.i18n.I18n;
- import org.sonar.batch.protocol.Constants;
- import org.sonar.batch.protocol.output.BatchReport;
- import org.sonar.db.component.ComponentLinkDto;
- import org.sonar.db.DbSession;
- import org.sonar.db.MyBatis;
- import org.sonar.server.computation.batch.BatchReportReader;
- import org.sonar.server.computation.component.Component;
- import org.sonar.server.computation.component.DepthTraversalTypeAwareVisitor;
- import org.sonar.server.computation.component.TreeRootHolder;
- import org.sonar.server.db.DbClient;
-
- import static com.google.common.collect.Sets.newHashSet;
- import static org.sonar.server.computation.component.ComponentVisitor.Order.PRE_ORDER;
-
- /**
- * Persist project and module links
- */
- public class PersistProjectLinksStep implements ComputationStep {
-
- private final DbClient dbClient;
- private final I18n i18n;
- private final TreeRootHolder treeRootHolder;
- private final BatchReportReader reportReader;
-
- private static final Map<Constants.ComponentLinkType, String> typesConverter = ImmutableMap.of(
- Constants.ComponentLinkType.HOME, ComponentLinkDto.TYPE_HOME_PAGE,
- Constants.ComponentLinkType.SCM, ComponentLinkDto.TYPE_SOURCES,
- Constants.ComponentLinkType.SCM_DEV, ComponentLinkDto.TYPE_SOURCES_DEV,
- Constants.ComponentLinkType.CI, ComponentLinkDto.TYPE_CI,
- Constants.ComponentLinkType.ISSUE, ComponentLinkDto.TYPE_ISSUE_TRACKER
- );
-
- public PersistProjectLinksStep(DbClient dbClient, I18n i18n, TreeRootHolder treeRootHolder, BatchReportReader reportReader) {
- this.dbClient = dbClient;
- this.i18n = i18n;
- this.treeRootHolder = treeRootHolder;
- this.reportReader = reportReader;
- }
-
- @Override
- public void execute() {
- DbSession session = dbClient.openSession(false);
- try {
- new PorjectLinkVisitor(session).visit(treeRootHolder.getRoot());
- session.commit();
- } finally {
- MyBatis.closeQuietly(session);
- }
- }
-
- private class PorjectLinkVisitor extends DepthTraversalTypeAwareVisitor {
-
- private final DbSession session;
-
- private PorjectLinkVisitor(DbSession session) {
- super(Component.Type.FILE, PRE_ORDER);
- this.session = session;
- }
-
- @Override
- public void visitProject(Component project) {
- processComponent(project);
- }
-
- @Override
- public void visitModule(Component module) {
- processComponent(module);
- }
-
- private void processComponent(Component component) {
- BatchReport.Component batchComponent = reportReader.readComponent(component.getRef());
- processLinks(component.getUuid(), batchComponent.getLinkList());
- }
-
- private void processLinks(String componentUuid, List<BatchReport.ComponentLink> links) {
- List<ComponentLinkDto> previousLinks = dbClient.componentLinkDao().selectByComponentUuid(session, componentUuid);
- mergeLinks(session, componentUuid, links, previousLinks);
- }
-
- private void mergeLinks(DbSession session, String componentUuid, List<BatchReport.ComponentLink> links, List<ComponentLinkDto> previousLinks) {
- Set<String> linkType = newHashSet();
- for (final BatchReport.ComponentLink link : links) {
- String type = convertType(link.getType());
- if (!linkType.contains(type)) {
- linkType.add(type);
- } else {
- throw new IllegalArgumentException(String.format("Link of type '%s' has already been declared on component '%s'", type, componentUuid));
- }
-
- ComponentLinkDto previousLink = Iterables.find(previousLinks, new Predicate<ComponentLinkDto>() {
- @Override
- public boolean apply(@Nullable ComponentLinkDto input) {
- return input != null && input.getType().equals(convertType(link.getType()));
- }
- }, null);
- if (previousLink == null) {
- dbClient.componentLinkDao().insert(session,
- new ComponentLinkDto()
- .setComponentUuid(componentUuid)
- .setType(type)
- .setName(i18n.message(Locale.ENGLISH, "project_links." + type, null))
- .setHref(link.getHref())
- );
- } else {
- previousLink.setHref(link.getHref());
- dbClient.componentLinkDao().update(session, previousLink);
- }
- }
-
- for (ComponentLinkDto dto : previousLinks) {
- if (!linkType.contains(dto.getType()) && ComponentLinkDto.PROVIDED_TYPES.contains(dto.getType())) {
- dbClient.componentLinkDao().delete(session, dto.getId());
- }
- }
- }
- }
-
- private static String convertType(Constants.ComponentLinkType reportType) {
- String type = typesConverter.get(reportType);
- if (type != null) {
- return type;
- } else {
- throw new IllegalArgumentException(String.format("Unsupported type %s", reportType.name()));
- }
- }
-
- @Override
- public String getDescription() {
- return "Persist component links";
- }
- }
|