-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
RuleQueryFactory.java
159 lines (144 loc) · 7.7 KB
/
RuleQueryFactory.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/*
* SonarQube
* Copyright (C) 2009-2018 SonarSource SA
* mailto:info AT sonarsource DOT com
*
* This program 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.
*
* This program 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.rule.ws;
import com.google.common.collect.ImmutableList;
import java.util.Date;
import org.sonar.api.rule.RuleStatus;
import org.sonar.api.rules.RuleType;
import org.sonar.api.server.ServerSide;
import org.sonar.api.server.ws.Request;
import org.sonar.api.server.ws.WebService;
import org.sonar.db.DbClient;
import org.sonar.db.DbSession;
import org.sonar.db.organization.OrganizationDto;
import org.sonar.db.qualityprofile.QProfileDto;
import org.sonar.server.rule.index.RuleQuery;
import static com.google.common.base.Preconditions.checkArgument;
import static java.lang.String.format;
import static org.sonar.server.rule.ws.RulesWsParameters.PARAM_ACTIVATION;
import static org.sonar.server.rule.ws.RulesWsParameters.PARAM_ACTIVE_SEVERITIES;
import static org.sonar.server.rule.ws.RulesWsParameters.PARAM_AVAILABLE_SINCE;
import static org.sonar.server.rule.ws.RulesWsParameters.PARAM_COMPARE_TO_PROFILE;
import static org.sonar.server.rule.ws.RulesWsParameters.PARAM_INCLUDE_EXTERNAL;
import static org.sonar.server.rule.ws.RulesWsParameters.PARAM_INHERITANCE;
import static org.sonar.server.rule.ws.RulesWsParameters.PARAM_IS_TEMPLATE;
import static org.sonar.server.rule.ws.RulesWsParameters.PARAM_LANGUAGES;
import static org.sonar.server.rule.ws.RulesWsParameters.PARAM_ORGANIZATION;
import static org.sonar.server.rule.ws.RulesWsParameters.PARAM_QPROFILE;
import static org.sonar.server.rule.ws.RulesWsParameters.PARAM_REPOSITORIES;
import static org.sonar.server.rule.ws.RulesWsParameters.PARAM_RULE_KEY;
import static org.sonar.server.rule.ws.RulesWsParameters.PARAM_SEVERITIES;
import static org.sonar.server.rule.ws.RulesWsParameters.PARAM_STATUSES;
import static org.sonar.server.rule.ws.RulesWsParameters.PARAM_TAGS;
import static org.sonar.server.rule.ws.RulesWsParameters.PARAM_TEMPLATE_KEY;
import static org.sonar.server.rule.ws.RulesWsParameters.PARAM_TYPES;
import static org.sonar.server.util.EnumUtils.toEnums;
import static org.sonar.server.ws.WsUtils.checkFound;
import static org.sonar.server.ws.WsUtils.checkFoundWithOptional;
@ServerSide
public class RuleQueryFactory {
private final DbClient dbClient;
private final RuleWsSupport wsSupport;
public RuleQueryFactory(DbClient dbClient, RuleWsSupport wsSupport) {
this.dbClient = dbClient;
this.wsSupport = wsSupport;
}
/**
* Similar to {@link #createRuleQuery(DbSession, Request)} but sets additional fields which are only used
* for the rule search WS.
*/
public RuleQuery createRuleSearchQuery(DbSession dbSession, Request request) {
RuleQuery query = createRuleQuery(dbSession, request);
query.setIncludeExternal(request.mandatoryParamAsBoolean(PARAM_INCLUDE_EXTERNAL));
return query;
}
/**
* Create a {@link RuleQuery} from a {@link Request}.
* When a profile key is set, the language of the profile is automatically set in the query
*/
public RuleQuery createRuleQuery(DbSession dbSession, Request request) {
RuleQuery query = new RuleQuery();
query.setQueryText(request.param(WebService.Param.TEXT_QUERY));
query.setSeverities(request.paramAsStrings(PARAM_SEVERITIES));
query.setRepositories(request.paramAsStrings(PARAM_REPOSITORIES));
Date availableSince = request.paramAsDate(PARAM_AVAILABLE_SINCE);
query.setAvailableSince(availableSince != null ? availableSince.getTime() : null);
query.setStatuses(toEnums(request.paramAsStrings(PARAM_STATUSES), RuleStatus.class));
Boolean activation = request.paramAsBoolean(PARAM_ACTIVATION);
query.setActivation(activation);
// Order is important : 1. Load profile, 2. Load organization either from parameter or from profile, 3. Load compare to profile
setProfile(dbSession, query, request);
setOrganization(dbSession, query, request);
setCompareToProfile(dbSession, query, request);
QProfileDto profile = query.getQProfile();
query.setLanguages(profile == null ? request.paramAsStrings(PARAM_LANGUAGES) : ImmutableList.of(profile.getLanguage()));
query.setTags(request.paramAsStrings(PARAM_TAGS));
query.setInheritance(request.paramAsStrings(PARAM_INHERITANCE));
query.setActiveSeverities(request.paramAsStrings(PARAM_ACTIVE_SEVERITIES));
query.setIsTemplate(request.paramAsBoolean(PARAM_IS_TEMPLATE));
query.setTemplateKey(request.param(PARAM_TEMPLATE_KEY));
query.setTypes(toEnums(request.paramAsStrings(PARAM_TYPES), RuleType.class));
query.setKey(request.param(PARAM_RULE_KEY));
String sortParam = request.param(WebService.Param.SORT);
if (sortParam != null) {
query.setSortField(sortParam);
query.setAscendingSort(request.mandatoryParamAsBoolean(WebService.Param.ASCENDING));
}
return query;
}
private void setProfile(DbSession dbSession, RuleQuery query, Request request) {
String profileUuid = request.param(PARAM_QPROFILE);
if (profileUuid == null) {
return;
}
QProfileDto profileOptional = dbClient.qualityProfileDao().selectByUuid(dbSession, profileUuid);
QProfileDto profile = checkFound(profileOptional, "The specified qualityProfile '%s' does not exist", profileUuid);
query.setQProfile(profile);
}
private void setOrganization(DbSession dbSession, RuleQuery query, Request request) {
String organizationKey = request.param(PARAM_ORGANIZATION);
QProfileDto profile = query.getQProfile();
if (profile == null) {
query.setOrganization(wsSupport.getOrganizationByKey(dbSession, organizationKey));
return;
}
OrganizationDto organization = checkFoundWithOptional(dbClient.organizationDao().selectByUuid(dbSession, profile.getOrganizationUuid()), "No organization with UUID ",
profile.getOrganizationUuid());
if (organizationKey != null) {
OrganizationDto inputOrganization = checkFoundWithOptional(dbClient.organizationDao().selectByKey(dbSession, organizationKey), "No organization with key '%s'",
organizationKey);
checkArgument(organization.getUuid().equals(inputOrganization.getUuid()),
format("The specified quality profile '%s' is not part of the specified organization '%s'", profile.getKee(), organizationKey));
}
wsSupport.checkMembershipOnPaidOrganization(organization);
query.setOrganization(organization);
}
private void setCompareToProfile(DbSession dbSession, RuleQuery query, Request request) {
String compareToProfileUuid = request.param(PARAM_COMPARE_TO_PROFILE);
if (compareToProfileUuid == null) {
return;
}
QProfileDto profileOptional = dbClient.qualityProfileDao().selectByUuid(dbSession, compareToProfileUuid);
QProfileDto profile = checkFound(profileOptional, "The specified qualityProfile '%s' does not exist", compareToProfileUuid);
checkArgument(query.getOrganization().getUuid().equals(profile.getOrganizationUuid()),
format("The specified quality profile '%s' is not part of the specified organization '%s'", profile.getKee(), query.getOrganization().getKey()));
query.setCompareToQProfile(profile);
}
}