You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

PersistProjectLinksStep.java 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /*
  2. * SonarQube, open source software quality management tool.
  3. * Copyright (C) 2008-2014 SonarSource
  4. * mailto:contact AT sonarsource DOT com
  5. *
  6. * SonarQube 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.
  10. *
  11. * SonarQube 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.
  15. *
  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.
  19. */
  20. package org.sonar.server.computation.step;
  21. import com.google.common.base.Predicate;
  22. import com.google.common.collect.ImmutableMap;
  23. import com.google.common.collect.Iterables;
  24. import java.util.List;
  25. import java.util.Locale;
  26. import java.util.Map;
  27. import java.util.Set;
  28. import javax.annotation.Nullable;
  29. import org.sonar.api.i18n.I18n;
  30. import org.sonar.batch.protocol.Constants;
  31. import org.sonar.batch.protocol.output.BatchReport;
  32. import org.sonar.db.component.ComponentLinkDto;
  33. import org.sonar.db.DbSession;
  34. import org.sonar.db.MyBatis;
  35. import org.sonar.server.computation.batch.BatchReportReader;
  36. import org.sonar.server.computation.component.Component;
  37. import org.sonar.server.computation.component.DepthTraversalTypeAwareVisitor;
  38. import org.sonar.server.computation.component.TreeRootHolder;
  39. import org.sonar.server.db.DbClient;
  40. import static com.google.common.collect.Sets.newHashSet;
  41. import static org.sonar.server.computation.component.ComponentVisitor.Order.PRE_ORDER;
  42. /**
  43. * Persist project and module links
  44. */
  45. public class PersistProjectLinksStep implements ComputationStep {
  46. private final DbClient dbClient;
  47. private final I18n i18n;
  48. private final TreeRootHolder treeRootHolder;
  49. private final BatchReportReader reportReader;
  50. private static final Map<Constants.ComponentLinkType, String> typesConverter = ImmutableMap.of(
  51. Constants.ComponentLinkType.HOME, ComponentLinkDto.TYPE_HOME_PAGE,
  52. Constants.ComponentLinkType.SCM, ComponentLinkDto.TYPE_SOURCES,
  53. Constants.ComponentLinkType.SCM_DEV, ComponentLinkDto.TYPE_SOURCES_DEV,
  54. Constants.ComponentLinkType.CI, ComponentLinkDto.TYPE_CI,
  55. Constants.ComponentLinkType.ISSUE, ComponentLinkDto.TYPE_ISSUE_TRACKER
  56. );
  57. public PersistProjectLinksStep(DbClient dbClient, I18n i18n, TreeRootHolder treeRootHolder, BatchReportReader reportReader) {
  58. this.dbClient = dbClient;
  59. this.i18n = i18n;
  60. this.treeRootHolder = treeRootHolder;
  61. this.reportReader = reportReader;
  62. }
  63. @Override
  64. public void execute() {
  65. DbSession session = dbClient.openSession(false);
  66. try {
  67. new PorjectLinkVisitor(session).visit(treeRootHolder.getRoot());
  68. session.commit();
  69. } finally {
  70. MyBatis.closeQuietly(session);
  71. }
  72. }
  73. private class PorjectLinkVisitor extends DepthTraversalTypeAwareVisitor {
  74. private final DbSession session;
  75. private PorjectLinkVisitor(DbSession session) {
  76. super(Component.Type.FILE, PRE_ORDER);
  77. this.session = session;
  78. }
  79. @Override
  80. public void visitProject(Component project) {
  81. processComponent(project);
  82. }
  83. @Override
  84. public void visitModule(Component module) {
  85. processComponent(module);
  86. }
  87. private void processComponent(Component component) {
  88. BatchReport.Component batchComponent = reportReader.readComponent(component.getRef());
  89. processLinks(component.getUuid(), batchComponent.getLinkList());
  90. }
  91. private void processLinks(String componentUuid, List<BatchReport.ComponentLink> links) {
  92. List<ComponentLinkDto> previousLinks = dbClient.componentLinkDao().selectByComponentUuid(session, componentUuid);
  93. mergeLinks(session, componentUuid, links, previousLinks);
  94. }
  95. private void mergeLinks(DbSession session, String componentUuid, List<BatchReport.ComponentLink> links, List<ComponentLinkDto> previousLinks) {
  96. Set<String> linkType = newHashSet();
  97. for (final BatchReport.ComponentLink link : links) {
  98. String type = convertType(link.getType());
  99. if (!linkType.contains(type)) {
  100. linkType.add(type);
  101. } else {
  102. throw new IllegalArgumentException(String.format("Link of type '%s' has already been declared on component '%s'", type, componentUuid));
  103. }
  104. ComponentLinkDto previousLink = Iterables.find(previousLinks, new Predicate<ComponentLinkDto>() {
  105. @Override
  106. public boolean apply(@Nullable ComponentLinkDto input) {
  107. return input != null && input.getType().equals(convertType(link.getType()));
  108. }
  109. }, null);
  110. if (previousLink == null) {
  111. dbClient.componentLinkDao().insert(session,
  112. new ComponentLinkDto()
  113. .setComponentUuid(componentUuid)
  114. .setType(type)
  115. .setName(i18n.message(Locale.ENGLISH, "project_links." + type, null))
  116. .setHref(link.getHref())
  117. );
  118. } else {
  119. previousLink.setHref(link.getHref());
  120. dbClient.componentLinkDao().update(session, previousLink);
  121. }
  122. }
  123. for (ComponentLinkDto dto : previousLinks) {
  124. if (!linkType.contains(dto.getType()) && ComponentLinkDto.PROVIDED_TYPES.contains(dto.getType())) {
  125. dbClient.componentLinkDao().delete(session, dto.getId());
  126. }
  127. }
  128. }
  129. }
  130. private static String convertType(Constants.ComponentLinkType reportType) {
  131. String type = typesConverter.get(reportType);
  132. if (type != null) {
  133. return type;
  134. } else {
  135. throw new IllegalArgumentException(String.format("Unsupported type %s", reportType.name()));
  136. }
  137. }
  138. @Override
  139. public String getDescription() {
  140. return "Persist component links";
  141. }
  142. }