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.

CustomMeasuresCopyStep.java 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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.annotations.VisibleForTesting;
  22. import java.util.List;
  23. import org.apache.commons.lang.math.NumberUtils;
  24. import org.sonar.db.measure.CustomMeasureDto;
  25. import org.sonar.db.DbSession;
  26. import org.sonar.db.MyBatis;
  27. import org.sonar.server.computation.component.Component;
  28. import org.sonar.server.computation.component.DepthTraversalTypeAwareVisitor;
  29. import org.sonar.server.computation.component.TreeRootHolder;
  30. import org.sonar.server.computation.measure.Measure;
  31. import org.sonar.server.computation.measure.MeasureRepository;
  32. import org.sonar.server.computation.metric.Metric;
  33. import org.sonar.server.computation.metric.MetricRepository;
  34. import org.sonar.server.db.DbClient;
  35. public class CustomMeasuresCopyStep implements ComputationStep {
  36. private final TreeRootHolder treeRootHolder;
  37. private final DbClient dbClient;
  38. private final MetricRepository metricRepository;
  39. private final MeasureRepository measureRepository;
  40. public CustomMeasuresCopyStep(TreeRootHolder treeRootHolder, DbClient dbClient,
  41. MetricRepository metricRepository, MeasureRepository measureRepository) {
  42. this.treeRootHolder = treeRootHolder;
  43. this.dbClient = dbClient;
  44. this.metricRepository = metricRepository;
  45. this.measureRepository = measureRepository;
  46. }
  47. @Override
  48. public void execute() {
  49. new DepthTraversalTypeAwareVisitor(Component.Type.FILE, DepthTraversalTypeAwareVisitor.Order.PRE_ORDER) {
  50. @Override
  51. public void visitAny(Component component) {
  52. copy(component);
  53. }
  54. }.visit(treeRootHolder.getRoot());
  55. }
  56. private void copy(Component component) {
  57. for (CustomMeasureDto dto : loadCustomMeasures(component)) {
  58. Metric metric = metricRepository.getById(dto.getMetricId());
  59. // else metric is not found and an exception is raised
  60. Measure measure = dtoToMeasure(dto, metric);
  61. measureRepository.add(component, metric, measure);
  62. }
  63. }
  64. private List<CustomMeasureDto> loadCustomMeasures(Component component) {
  65. DbSession session = dbClient.openSession(false);
  66. try {
  67. return dbClient.customMeasureDao().selectByComponentUuid(session, component.getUuid());
  68. } finally {
  69. MyBatis.closeQuietly(session);
  70. }
  71. }
  72. @VisibleForTesting
  73. static Measure dtoToMeasure(CustomMeasureDto dto, Metric metric) {
  74. switch (metric.getType()) {
  75. case INT:
  76. case RATING:
  77. case MILLISEC:
  78. return Measure.newMeasureBuilder().create((int) dto.getValue());
  79. case WORK_DUR:
  80. return Measure.newMeasureBuilder().create((long) dto.getValue());
  81. case FLOAT:
  82. case PERCENT:
  83. return Measure.newMeasureBuilder().create(dto.getValue());
  84. case BOOL:
  85. return Measure.newMeasureBuilder().create(NumberUtils.compare(dto.getValue(), 1.0) == 0);
  86. case STRING:
  87. case DISTRIB:
  88. case DATA:
  89. case LEVEL:
  90. return Measure.newMeasureBuilder().create(dto.getTextValue());
  91. default:
  92. throw new IllegalArgumentException(String.format("Custom measures do not support the metric type [%s]", metric.getType()));
  93. }
  94. }
  95. @Override
  96. public String getDescription() {
  97. return "Copy custom measures";
  98. }
  99. }