-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
MetricDao.java
120 lines (103 loc) · 4.07 KB
/
MetricDao.java
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/*
* 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.metric.persistence;
import com.google.common.base.Function;
import com.google.common.base.Predicate;
import com.google.common.collect.Collections2;
import com.google.common.collect.Maps;
import java.util.List;
import java.util.Map;
import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import org.apache.ibatis.session.RowBounds;
import org.sonar.api.server.ServerSide;
import org.sonar.core.metric.db.MetricDto;
import org.sonar.core.metric.db.MetricMapper;
import org.sonar.core.persistence.DaoComponent;
import org.sonar.core.persistence.DaoUtils;
import org.sonar.core.persistence.DbSession;
import org.sonar.server.es.SearchOptions;
import org.sonar.server.exceptions.NotFoundException;
import static com.google.common.collect.Lists.newArrayList;
@ServerSide
public class MetricDao implements DaoComponent {
@CheckForNull
public MetricDto selectNullableByKey(DbSession session, String key) {
return mapper(session).selectByKey(key);
}
public List<MetricDto> selectNullableByKeys(final DbSession session, List<String> keys) {
return DaoUtils.executeLargeInputs(keys, new Function<List<String>, List<MetricDto>>() {
@Override
public List<MetricDto> apply(@Nonnull List<String> input) {
return mapper(session).selectByKeys(input);
}
});
}
public MetricDto selectByKey(DbSession session, String key) {
MetricDto metric = selectNullableByKey(session, key);
if (metric == null) {
throw new NotFoundException(String.format("Metric '%s' not found", key));
}
return metric;
}
public List<MetricDto> selectEnabled(DbSession session) {
return mapper(session).selectAllEnabled();
}
public List<MetricDto> selectEnabled(DbSession session, @Nullable Boolean isCustom, SearchOptions searchOptions) {
Map<String, Object> properties = Maps.newHashMapWithExpectedSize(1);
if (isCustom != null) {
properties.put("isCustom", isCustom);
}
return mapper(session).selectAllEnabled(properties, new RowBounds(searchOptions.getOffset(), searchOptions.getLimit()));
}
public int countEnabled(DbSession session, @Nullable Boolean isCustom) {
return mapper(session).countEnabled(isCustom);
}
public void insert(DbSession session, MetricDto dto) {
mapper(session).insert(dto);
}
public List<String> selectDomains(DbSession session) {
return newArrayList(Collections2.filter(mapper(session).selectDomains(), new Predicate<String>() {
@Override
public boolean apply(@Nonnull String input) {
return !input.isEmpty();
}
}));
}
private MetricMapper mapper(DbSession session) {
return session.getMapper(MetricMapper.class);
}
public void disable(final DbSession session, List<Integer> ids) {
DaoUtils.executeLargeInputsWithoutOutput(ids, new Function<List<Integer>, Void>() {
@Override
public Void apply(@Nonnull List<Integer> input) {
mapper(session).disable(input);
return null;
}
});
}
public void update(DbSession session, MetricDto metric) {
mapper(session).update(metric);
}
public MetricDto selectNullableById(DbSession session, long id) {
return mapper(session).selectById(id);
}
}