123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127 |
- /*
- * 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.Function;
- import com.google.common.base.Predicate;
- import java.util.Collection;
- import java.util.List;
- import java.util.Map;
- import javax.annotation.Nonnull;
- import javax.annotation.Nullable;
- import org.sonar.db.DbSession;
- import org.sonar.db.debt.CharacteristicDto;
- import org.sonar.server.computation.debt.Characteristic;
- import org.sonar.server.computation.debt.MutableDebtModelHolder;
- import org.sonar.server.db.DbClient;
-
- import static com.google.common.base.Predicates.not;
- import static com.google.common.collect.FluentIterable.from;
-
- /**
- * Populates the {@link org.sonar.server.computation.debt.DebtModelHolder}
- */
- public class FeedDebtModelStep implements ComputationStep {
-
- private final DbClient dbClient;
- private final MutableDebtModelHolder mutableDebtModelHolder;
-
- public FeedDebtModelStep(DbClient dbClient, MutableDebtModelHolder mutableDebtModelHolder) {
- this.dbClient = dbClient;
- this.mutableDebtModelHolder = mutableDebtModelHolder;
- }
-
- @Override
- public void execute() {
- DbSession session = dbClient.openSession(false);
- try {
- feedDebtModel(session);
- } finally {
- session.close();
- }
- }
-
- private void feedDebtModel(DbSession session) {
- List<CharacteristicDto> characteristicDtos = dbClient.debtCharacteristicDao().selectEnabledCharacteristics(session);
- Map<Integer, CharacteristicDto> rootCharacteristicsById = from(characteristicDtos)
- .filter(IsRootPredicate.INSTANCE)
- .uniqueIndex(CharacteristicDtoToId.INSTANCE);
-
- for (Map.Entry<Integer, Collection<CharacteristicDto>> entry : from(characteristicDtos)
- .filter(not(IsRootPredicate.INSTANCE))
- .index(CharacteristicDtoToParentId.INSTANCE)
- .asMap().entrySet()) {
- mutableDebtModelHolder.addCharacteristics(
- toCharacteristic(rootCharacteristicsById.get(entry.getKey())),
- from(entry.getValue()).transform(CharacteristicDtoToCharacteristic.INSTANCE)
- );
- }
- }
-
- private static Characteristic toCharacteristic(CharacteristicDto dto) {
- return new Characteristic(dto.getId(), dto.getKey(), dto.getParentId());
- }
-
- private enum CharacteristicDtoToId implements Function<CharacteristicDto, Integer> {
- INSTANCE;
-
- @Nullable
- @Override
- public Integer apply(@Nonnull CharacteristicDto dto) {
- return dto.getId();
- }
- }
-
- private enum CharacteristicDtoToCharacteristic implements Function<CharacteristicDto, Characteristic> {
- INSTANCE;
-
- @Nullable
- @Override
- public Characteristic apply(@Nonnull CharacteristicDto characteristicDto) {
- return toCharacteristic(characteristicDto);
- }
- }
-
- private enum CharacteristicDtoToParentId implements Function<CharacteristicDto, Integer> {
- INSTANCE;
-
- @Nullable
- @Override
- public Integer apply(@Nonnull CharacteristicDto characteristicDto) {
- Integer parentId = characteristicDto.getParentId();
- return parentId == null ? characteristicDto.getId() : parentId;
- }
- }
-
- private enum IsRootPredicate implements Predicate<CharacteristicDto> {
- INSTANCE;
-
- @Override
- public boolean apply(@Nonnull CharacteristicDto characteristicDto) {
- return characteristicDto.getParentId() == null;
- }
- }
-
- @Override
- public String getDescription() {
- return "Feed technical debt model";
- }
- }
|