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.resources.Languages;
23 import org.sonar.api.server.ws.Request;
24 import org.sonar.api.server.ws.Response;
25 import org.sonar.api.server.ws.WebService;
26 import org.sonar.api.server.ws.WebService.NewAction;
27 import org.sonar.db.DbClient;
28 import org.sonar.db.DbSession;
29 import org.sonar.db.qualityprofile.DefaultQProfileDto;
30 import org.sonar.db.qualityprofile.QProfileDto;
31 import org.sonar.server.user.UserSession;
33 import static java.lang.String.format;
34 import static org.sonar.db.permission.OrganizationPermission.ADMINISTER_QUALITY_PROFILES;
35 import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.ACTION_SET_DEFAULT;
37 public class SetDefaultAction implements QProfileWsAction {
39 private final Languages languages;
40 private final DbClient dbClient;
41 private final UserSession userSession;
42 private final QProfileWsSupport qProfileWsSupport;
44 public SetDefaultAction(Languages languages, DbClient dbClient, UserSession userSession, QProfileWsSupport qProfileWsSupport) {
45 this.languages = languages;
46 this.dbClient = dbClient;
47 this.userSession = userSession;
48 this.qProfileWsSupport = qProfileWsSupport;
52 public void define(WebService.NewController controller) {
53 NewAction setDefault = controller.createAction(ACTION_SET_DEFAULT)
55 .setDescription("Select the default profile for a given language.<br> " +
56 "Requires to be logged in and the 'Administer Quality Profiles' permission.")
60 QProfileWsSupport.createOrganizationParam(setDefault).setSince("6.4");
61 QProfileReference.defineParams(setDefault, languages);
65 public void handle(Request request, Response response) {
66 userSession.checkLoggedIn();
67 QProfileReference reference = QProfileReference.from(request);
68 try (DbSession dbSession = dbClient.openSession(false)) {
69 QProfileDto qualityProfile = qProfileWsSupport.getProfile(dbSession, reference);
70 dbClient.organizationDao().selectByUuid(dbSession, qualityProfile.getOrganizationUuid())
71 .orElseThrow(() -> new IllegalStateException(
72 format("Cannot find organization '%s' for quality profile '%s'", qualityProfile.getOrganizationUuid(), qualityProfile.getKee())));
73 userSession.checkPermission(ADMINISTER_QUALITY_PROFILES, qualityProfile.getOrganizationUuid());
74 setDefault(dbSession, qualityProfile);
80 public void setDefault(DbSession dbSession, QProfileDto profile) {
81 dbClient.defaultQProfileDao().insertOrUpdate(dbSession, DefaultQProfileDto.from(profile));