3 * Copyright (C) 2009-2019 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.organization.OrganizationDto;
28 import org.sonar.db.qualityprofile.QProfileDto;
29 import org.sonar.server.qualityprofile.BulkChangeResult;
30 import org.sonar.server.qualityprofile.QProfileRules;
31 import org.sonar.server.rule.index.RuleQuery;
32 import org.sonar.server.rule.ws.RuleQueryFactory;
33 import org.sonar.server.user.UserSession;
35 import static org.sonar.core.util.Uuids.UUID_EXAMPLE_04;
36 import static org.sonar.server.qualityprofile.ws.BulkChangeWsResponse.writeResponse;
37 import static org.sonar.server.rule.ws.RuleWsSupport.defineGenericRuleSearchParameters;
38 import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.ACTION_DEACTIVATE_RULES;
39 import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.PARAM_TARGET_KEY;
41 public class DeactivateRulesAction implements QProfileWsAction {
43 private final RuleQueryFactory ruleQueryFactory;
44 private final UserSession userSession;
45 private final QProfileRules ruleActivator;
46 private final QProfileWsSupport wsSupport;
47 private final DbClient dbClient;
49 public DeactivateRulesAction(RuleQueryFactory ruleQueryFactory, UserSession userSession, QProfileRules ruleActivator, QProfileWsSupport wsSupport, DbClient dbClient) {
50 this.ruleQueryFactory = ruleQueryFactory;
51 this.userSession = userSession;
52 this.ruleActivator = ruleActivator;
53 this.wsSupport = wsSupport;
54 this.dbClient = dbClient;
57 public void define(WebService.NewController controller) {
58 WebService.NewAction deactivate = controller
59 .createAction(ACTION_DEACTIVATE_RULES)
60 .setDescription("Bulk deactivate rules on Quality profiles.<br>" +
61 "Requires one of the following permissions:" +
63 " <li>'Administer Quality Profiles'</li>" +
64 " <li>Edit right on the specified quality profile</li>" +
70 defineGenericRuleSearchParameters(deactivate);
72 deactivate.createParam(PARAM_TARGET_KEY)
73 .setDescription("Quality Profile key on which the rule deactivation is done. To retrieve a profile key please see <code>api/qualityprofiles/search</code>")
74 .setDeprecatedKey("profile_key", "6.5")
76 .setExampleValue(UUID_EXAMPLE_04);
80 public void handle(Request request, Response response) throws Exception {
81 String qualityProfileKey = request.mandatoryParam(PARAM_TARGET_KEY);
82 userSession.checkLoggedIn();
83 BulkChangeResult result;
84 try (DbSession dbSession = dbClient.openSession(false)) {
85 QProfileDto profile = wsSupport.getProfile(dbSession, QProfileReference.fromKey(qualityProfileKey));
86 OrganizationDto organization = wsSupport.getOrganization(dbSession, profile);
87 wsSupport.checkCanEdit(dbSession, organization, profile);
88 RuleQuery ruleQuery = ruleQueryFactory.createRuleQuery(dbSession, request);
89 ruleQuery.setIncludeExternal(false);
90 result = ruleActivator.bulkDeactivateAndCommit(dbSession, profile, ruleQuery);
92 writeResponse(result, response);