001 /*
002 * SonarQube, open source software quality management tool.
003 * Copyright (C) 2008-2014 SonarSource
004 * mailto:contact AT sonarsource DOT com
005 *
006 * SonarQube is free software; you can redistribute it and/or
007 * modify it under the terms of the GNU Lesser General Public
008 * License as published by the Free Software Foundation; either
009 * version 3 of the License, or (at your option) any later version.
010 *
011 * SonarQube is distributed in the hope that it will be useful,
012 * but WITHOUT ANY WARRANTY; without even the implied warranty of
013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014 * Lesser General Public License for more details.
015 *
016 * You should have received a copy of the GNU Lesser General Public License
017 * along with this program; if not, write to the Free Software Foundation,
018 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
019 */
020
021 package org.sonar.batch.debt;
022
023 import com.google.common.base.Predicate;
024 import com.google.common.collect.Iterables;
025 import org.picocontainer.injectors.ProviderAdapter;
026 import org.slf4j.Logger;
027 import org.slf4j.LoggerFactory;
028 import org.sonar.api.batch.debt.DebtCharacteristic;
029 import org.sonar.api.batch.debt.DebtModel;
030 import org.sonar.api.batch.debt.internal.DefaultDebtCharacteristic;
031 import org.sonar.api.batch.debt.internal.DefaultDebtModel;
032 import org.sonar.api.utils.TimeProfiler;
033 import org.sonar.core.technicaldebt.db.CharacteristicDao;
034 import org.sonar.core.technicaldebt.db.CharacteristicDto;
035
036 import javax.annotation.Nullable;
037
038 import java.util.List;
039
040 public class DebtModelProvider extends ProviderAdapter {
041
042 private static final Logger LOG = LoggerFactory.getLogger(DebtModelProvider.class);
043
044 private DebtModel model;
045
046 public DebtModel provide(CharacteristicDao dao) {
047 if (model == null) {
048 TimeProfiler profiler = new TimeProfiler(LOG).start("Loading technical debt model");
049 model = load(dao);
050 profiler.stop();
051 }
052 return model;
053 }
054
055 private DebtModel load(CharacteristicDao dao) {
056 DefaultDebtModel debtModel = new DefaultDebtModel();
057
058 List<CharacteristicDto> allCharacteristics = dao.selectEnabledCharacteristics();
059 for (CharacteristicDto dto : allCharacteristics) {
060 Integer parentId = dto.getParentId();
061 if (parentId == null) {
062 debtModel.addCharacteristic(toDebtCharacteristic(dto));
063 } else {
064 debtModel.addSubCharacteristic(toDebtCharacteristic(dto), characteristicById(parentId, allCharacteristics).getKey());
065 }
066 }
067 return debtModel;
068 }
069
070 private static CharacteristicDto characteristicById(final int id, List<CharacteristicDto> allCharacteristics) {
071 return Iterables.find(allCharacteristics, new Predicate<CharacteristicDto>() {
072 @Override
073 public boolean apply(@Nullable CharacteristicDto input) {
074 return input != null && id == input.getId();
075 }
076 });
077 }
078
079 private static DebtCharacteristic toDebtCharacteristic(CharacteristicDto characteristic) {
080 return new DefaultDebtCharacteristic()
081 .setId(characteristic.getId())
082 .setKey(characteristic.getKey())
083 .setName(characteristic.getName())
084 .setOrder(characteristic.getOrder())
085 .setParentId(characteristic.getParentId());
086 }
087
088 }