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.

MetricFinder.java 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2021 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  6. * This program 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. * This program 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.metric;
  21. import com.google.common.base.Function;
  22. import com.google.common.base.Predicate;
  23. import java.io.Serializable;
  24. import java.util.Collection;
  25. import java.util.List;
  26. import javax.annotation.Nonnull;
  27. import org.sonar.api.measures.Metric;
  28. import org.sonar.db.DbClient;
  29. import org.sonar.db.DbSession;
  30. import org.sonar.db.metric.MetricDto;
  31. import static com.google.common.collect.FluentIterable.from;
  32. public class MetricFinder {
  33. private final DbClient dbClient;
  34. public MetricFinder(DbClient dbClient) {
  35. this.dbClient = dbClient;
  36. }
  37. public Metric findByUuid(String uuid) {
  38. try (DbSession session = dbClient.openSession(false)) {
  39. MetricDto dto = dbClient.metricDao().selectByUuid(session, uuid);
  40. if (dto != null && dto.isEnabled()) {
  41. return ToMetric.INSTANCE.apply(dto);
  42. }
  43. return null;
  44. }
  45. }
  46. public Metric findByKey(String key) {
  47. try (DbSession session = dbClient.openSession(false)) {
  48. MetricDto dto = dbClient.metricDao().selectByKey(session, key);
  49. if (dto != null && dto.isEnabled()) {
  50. return ToMetric.INSTANCE.apply(dto);
  51. }
  52. return null;
  53. }
  54. }
  55. public Collection<Metric> findAll(List<String> metricKeys) {
  56. try (DbSession session = dbClient.openSession(false)) {
  57. List<MetricDto> dtos = dbClient.metricDao().selectByKeys(session, metricKeys);
  58. return from(dtos).filter(IsEnabled.INSTANCE).transform(ToMetric.INSTANCE).toList();
  59. }
  60. }
  61. public Collection<Metric> findAll() {
  62. try (DbSession session = dbClient.openSession(false)) {
  63. List<MetricDto> dtos = dbClient.metricDao().selectEnabled(session);
  64. return from(dtos).transform(ToMetric.INSTANCE).toList();
  65. }
  66. }
  67. private enum IsEnabled implements Predicate<MetricDto> {
  68. INSTANCE;
  69. @Override
  70. public boolean apply(@Nonnull MetricDto dto) {
  71. return dto.isEnabled();
  72. }
  73. }
  74. private enum ToMetric implements Function<MetricDto, Metric> {
  75. INSTANCE;
  76. @Override
  77. public Metric apply(@Nonnull MetricDto dto) {
  78. Metric<Serializable> metric = new Metric<>();
  79. metric.setUuid(dto.getUuid());
  80. metric.setKey(dto.getKey());
  81. metric.setDescription(dto.getDescription());
  82. metric.setName(dto.getShortName());
  83. metric.setBestValue(dto.getBestValue());
  84. metric.setDomain(dto.getDomain());
  85. metric.setEnabled(dto.isEnabled());
  86. metric.setDirection(dto.getDirection());
  87. metric.setHidden(dto.isHidden());
  88. metric.setQualitative(dto.isQualitative());
  89. metric.setType(Metric.ValueType.valueOf(dto.getValueType()));
  90. metric.setOptimizedBestValue(dto.isOptimizedBestValue());
  91. metric.setUserManaged(dto.isUserManaged());
  92. metric.setWorstValue(dto.getWorstValue());
  93. return metric;
  94. }
  95. }
  96. }