123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108 |
- /*
- * 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.annotations.VisibleForTesting;
- import java.util.List;
- import org.apache.commons.lang.math.NumberUtils;
- import org.sonar.db.measure.CustomMeasureDto;
- import org.sonar.db.DbSession;
- import org.sonar.db.MyBatis;
- import org.sonar.server.computation.component.Component;
- import org.sonar.server.computation.component.DepthTraversalTypeAwareVisitor;
- import org.sonar.server.computation.component.TreeRootHolder;
- import org.sonar.server.computation.measure.Measure;
- import org.sonar.server.computation.measure.MeasureRepository;
- import org.sonar.server.computation.metric.Metric;
- import org.sonar.server.computation.metric.MetricRepository;
- import org.sonar.server.db.DbClient;
-
- public class CustomMeasuresCopyStep implements ComputationStep {
-
- private final TreeRootHolder treeRootHolder;
- private final DbClient dbClient;
- private final MetricRepository metricRepository;
- private final MeasureRepository measureRepository;
-
- public CustomMeasuresCopyStep(TreeRootHolder treeRootHolder, DbClient dbClient,
- MetricRepository metricRepository, MeasureRepository measureRepository) {
- this.treeRootHolder = treeRootHolder;
- this.dbClient = dbClient;
- this.metricRepository = metricRepository;
- this.measureRepository = measureRepository;
- }
-
- @Override
- public void execute() {
- new DepthTraversalTypeAwareVisitor(Component.Type.FILE, DepthTraversalTypeAwareVisitor.Order.PRE_ORDER) {
- @Override
- public void visitAny(Component component) {
- copy(component);
- }
- }.visit(treeRootHolder.getRoot());
- }
-
- private void copy(Component component) {
- for (CustomMeasureDto dto : loadCustomMeasures(component)) {
- Metric metric = metricRepository.getById(dto.getMetricId());
- // else metric is not found and an exception is raised
- Measure measure = dtoToMeasure(dto, metric);
- measureRepository.add(component, metric, measure);
- }
- }
-
- private List<CustomMeasureDto> loadCustomMeasures(Component component) {
- DbSession session = dbClient.openSession(false);
- try {
- return dbClient.customMeasureDao().selectByComponentUuid(session, component.getUuid());
- } finally {
- MyBatis.closeQuietly(session);
- }
- }
-
- @VisibleForTesting
- static Measure dtoToMeasure(CustomMeasureDto dto, Metric metric) {
- switch (metric.getType()) {
- case INT:
- case RATING:
- case MILLISEC:
- return Measure.newMeasureBuilder().create((int) dto.getValue());
- case WORK_DUR:
- return Measure.newMeasureBuilder().create((long) dto.getValue());
- case FLOAT:
- case PERCENT:
- return Measure.newMeasureBuilder().create(dto.getValue());
- case BOOL:
- return Measure.newMeasureBuilder().create(NumberUtils.compare(dto.getValue(), 1.0) == 0);
- case STRING:
- case DISTRIB:
- case DATA:
- case LEVEL:
- return Measure.newMeasureBuilder().create(dto.getTextValue());
- default:
- throw new IllegalArgumentException(String.format("Custom measures do not support the metric type [%s]", metric.getType()));
- }
- }
-
- @Override
- public String getDescription() {
- return "Copy custom measures";
- }
- }
|