3 * Copyright (C) 2009-2023 SonarSource SA
4 * mailto:info AT sonarsource DOT com
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.
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.
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.
20 package org.sonar.server.qualityprofile.ws;
22 import org.sonar.api.server.ws.Request;
23 import org.sonar.api.server.ws.Response;
24 import org.sonar.api.server.ws.WebService;
25 import org.sonar.db.DbClient;
26 import org.sonar.db.DbSession;
27 import org.sonar.db.qualityprofile.QProfileDto;
28 import org.sonar.server.qualityprofile.BulkChangeResult;
29 import org.sonar.server.qualityprofile.QProfileRules;
30 import org.sonar.server.rule.index.RuleQuery;
31 import org.sonar.server.rule.ws.RuleQueryFactory;
32 import org.sonar.server.user.UserSession;
34 import static org.sonar.core.util.Uuids.UUID_EXAMPLE_04;
35 import static org.sonar.server.qualityprofile.ws.BulkChangeWsResponse.writeResponse;
36 import static org.sonar.server.rule.ws.RuleWsSupport.defineGenericRuleSearchParameters;
37 import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.ACTION_DEACTIVATE_RULES;
38 import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.PARAM_TARGET_KEY;
40 public class DeactivateRulesAction implements QProfileWsAction {
42 private final RuleQueryFactory ruleQueryFactory;
43 private final UserSession userSession;
44 private final QProfileRules ruleActivator;
45 private final QProfileWsSupport wsSupport;
46 private final DbClient dbClient;
48 public DeactivateRulesAction(RuleQueryFactory ruleQueryFactory, UserSession userSession, QProfileRules ruleActivator, QProfileWsSupport wsSupport, DbClient dbClient) {
49 this.ruleQueryFactory = ruleQueryFactory;
50 this.userSession = userSession;
51 this.ruleActivator = ruleActivator;
52 this.wsSupport = wsSupport;
53 this.dbClient = dbClient;
56 public void define(WebService.NewController controller) {
57 WebService.NewAction deactivate = controller
58 .createAction(ACTION_DEACTIVATE_RULES)
59 .setDescription("Bulk deactivate rules on Quality profiles.<br>" +
60 "Requires one of the following permissions:" +
62 " <li>'Administer Quality Profiles'</li>" +
63 " <li>Edit right on the specified quality profile</li>" +
69 defineGenericRuleSearchParameters(deactivate);
71 deactivate.createParam(PARAM_TARGET_KEY)
72 .setDescription("Quality Profile key on which the rule deactivation is done. To retrieve a profile key please see <code>api/qualityprofiles/search</code>")
74 .setExampleValue(UUID_EXAMPLE_04);
78 public void handle(Request request, Response response) throws Exception {
79 String qualityProfileKey = request.mandatoryParam(PARAM_TARGET_KEY);
80 userSession.checkLoggedIn();
81 BulkChangeResult result;
82 try (DbSession dbSession = dbClient.openSession(false)) {
83 QProfileDto profile = wsSupport.getProfile(dbSession, QProfileReference.fromKey(qualityProfileKey));
84 wsSupport.checkCanEdit(dbSession, profile);
85 RuleQuery ruleQuery = ruleQueryFactory.createRuleQuery(dbSession, request);
86 ruleQuery.setIncludeExternal(false);
87 result = ruleActivator.bulkDeactivateAndCommit(dbSession, profile, ruleQuery);
89 writeResponse(result, response);