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.

FeedDebtModelStep.java 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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.Function;
  22. import com.google.common.base.Predicate;
  23. import java.util.Collection;
  24. import java.util.List;
  25. import java.util.Map;
  26. import javax.annotation.Nonnull;
  27. import javax.annotation.Nullable;
  28. import org.sonar.db.DbSession;
  29. import org.sonar.db.debt.CharacteristicDto;
  30. import org.sonar.server.computation.debt.Characteristic;
  31. import org.sonar.server.computation.debt.MutableDebtModelHolder;
  32. import org.sonar.server.db.DbClient;
  33. import static com.google.common.base.Predicates.not;
  34. import static com.google.common.collect.FluentIterable.from;
  35. /**
  36. * Populates the {@link org.sonar.server.computation.debt.DebtModelHolder}
  37. */
  38. public class FeedDebtModelStep implements ComputationStep {
  39. private final DbClient dbClient;
  40. private final MutableDebtModelHolder mutableDebtModelHolder;
  41. public FeedDebtModelStep(DbClient dbClient, MutableDebtModelHolder mutableDebtModelHolder) {
  42. this.dbClient = dbClient;
  43. this.mutableDebtModelHolder = mutableDebtModelHolder;
  44. }
  45. @Override
  46. public void execute() {
  47. DbSession session = dbClient.openSession(false);
  48. try {
  49. feedDebtModel(session);
  50. } finally {
  51. session.close();
  52. }
  53. }
  54. private void feedDebtModel(DbSession session) {
  55. List<CharacteristicDto> characteristicDtos = dbClient.debtCharacteristicDao().selectEnabledCharacteristics(session);
  56. Map<Integer, CharacteristicDto> rootCharacteristicsById = from(characteristicDtos)
  57. .filter(IsRootPredicate.INSTANCE)
  58. .uniqueIndex(CharacteristicDtoToId.INSTANCE);
  59. for (Map.Entry<Integer, Collection<CharacteristicDto>> entry : from(characteristicDtos)
  60. .filter(not(IsRootPredicate.INSTANCE))
  61. .index(CharacteristicDtoToParentId.INSTANCE)
  62. .asMap().entrySet()) {
  63. mutableDebtModelHolder.addCharacteristics(
  64. toCharacteristic(rootCharacteristicsById.get(entry.getKey())),
  65. from(entry.getValue()).transform(CharacteristicDtoToCharacteristic.INSTANCE)
  66. );
  67. }
  68. }
  69. private static Characteristic toCharacteristic(CharacteristicDto dto) {
  70. return new Characteristic(dto.getId(), dto.getKey(), dto.getParentId());
  71. }
  72. private enum CharacteristicDtoToId implements Function<CharacteristicDto, Integer> {
  73. INSTANCE;
  74. @Nullable
  75. @Override
  76. public Integer apply(@Nonnull CharacteristicDto dto) {
  77. return dto.getId();
  78. }
  79. }
  80. private enum CharacteristicDtoToCharacteristic implements Function<CharacteristicDto, Characteristic> {
  81. INSTANCE;
  82. @Nullable
  83. @Override
  84. public Characteristic apply(@Nonnull CharacteristicDto characteristicDto) {
  85. return toCharacteristic(characteristicDto);
  86. }
  87. }
  88. private enum CharacteristicDtoToParentId implements Function<CharacteristicDto, Integer> {
  89. INSTANCE;
  90. @Nullable
  91. @Override
  92. public Integer apply(@Nonnull CharacteristicDto characteristicDto) {
  93. Integer parentId = characteristicDto.getParentId();
  94. return parentId == null ? characteristicDto.getId() : parentId;
  95. }
  96. }
  97. private enum IsRootPredicate implements Predicate<CharacteristicDto> {
  98. INSTANCE;
  99. @Override
  100. public boolean apply(@Nonnull CharacteristicDto characteristicDto) {
  101. return characteristicDto.getParentId() == null;
  102. }
  103. }
  104. @Override
  105. public String getDescription() {
  106. return "Feed technical debt model";
  107. }
  108. }