aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-batch/src/main/java/org/sonar/batch/debt/DebtModelProvider.java
blob: 237bfa3d93b4c4d7ed3167a3402eab216fd5ed83 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
/*
 * SonarQube, open source software quality management tool.
 * Copyright (C) 2008-2013 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.batch.debt;

import org.picocontainer.injectors.ProviderAdapter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.sonar.api.technicaldebt.batch.TechnicalDebtModel;
import org.sonar.api.technicaldebt.batch.internal.DefaultCharacteristic;
import org.sonar.api.utils.TimeProfiler;
import org.sonar.core.technicaldebt.DefaultTechnicalDebtModel;
import org.sonar.core.technicaldebt.db.CharacteristicDao;
import org.sonar.core.technicaldebt.db.CharacteristicDto;

import java.util.List;
import java.util.Map;

import static com.google.common.collect.Maps.newHashMap;

public class DebtModelProvider extends ProviderAdapter {

  private static final Logger LOG = LoggerFactory.getLogger(DebtModelProvider.class);

  private TechnicalDebtModel model;

  public TechnicalDebtModel provide(CharacteristicDao dao) {
    if (model == null) {
      TimeProfiler profiler = new TimeProfiler(LOG).start("Loading technical debt model");
      model = load(dao);
      profiler.stop();
    }
    return model;
  }

  private TechnicalDebtModel load(CharacteristicDao dao) {
    DefaultTechnicalDebtModel model = new DefaultTechnicalDebtModel();
    List<CharacteristicDto> dtos = dao.selectCharacteristics();
    Map<Integer, DefaultCharacteristic> characteristicsById = newHashMap();

    addRootCharacteristics(model, dtos, characteristicsById);
    addCharacteristics(dtos, characteristicsById);
    return model;
  }

  private void addRootCharacteristics(DefaultTechnicalDebtModel model, List<CharacteristicDto> dtos, Map<Integer, DefaultCharacteristic> characteristicsById) {
    for (CharacteristicDto dto : dtos) {
      if (dto.getParentId() == null) {
        DefaultCharacteristic rootCharacteristic = dto.toCharacteristic(null);
        model.addRootCharacteristic(rootCharacteristic);
        characteristicsById.put(dto.getId(), rootCharacteristic);
      }
    }
  }

  private void addCharacteristics(List<CharacteristicDto> dtos, Map<Integer, DefaultCharacteristic> characteristicsById) {
    for (CharacteristicDto dto : dtos) {
      if (dto.getParentId() != null && dto.getRuleId() == null) {
        DefaultCharacteristic parent = characteristicsById.get(dto.getParentId());
        DefaultCharacteristic characteristic = dto.toCharacteristic(parent);
        characteristicsById.put(dto.getId(), characteristic);
      }
    }
  }
}