3 * Copyright (C) 2009-2024 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;
22 import java.util.ArrayList;
23 import java.util.Collection;
24 import java.util.Date;
25 import java.util.HashSet;
26 import java.util.List;
28 import javax.annotation.Nullable;
29 import org.apache.commons.lang.StringUtils;
30 import org.sonar.api.utils.System2;
31 import org.sonar.core.util.UuidFactory;
32 import org.sonar.db.DbClient;
33 import org.sonar.db.DbSession;
34 import org.sonar.db.qualityprofile.DefaultQProfileDto;
35 import org.sonar.db.qualityprofile.QProfileDto;
36 import org.sonar.server.exceptions.BadRequestException;
37 import org.sonar.server.qualityprofile.builtin.QProfileName;
38 import org.sonar.server.qualityprofile.index.ActiveRuleIndexer;
40 import static com.google.common.base.Preconditions.checkArgument;
41 import static org.sonar.server.exceptions.BadRequestException.checkRequest;
43 public class QProfileFactoryImpl implements QProfileFactory {
45 private final DbClient db;
46 private final UuidFactory uuidFactory;
47 private final System2 system2;
48 private final ActiveRuleIndexer activeRuleIndexer;
50 public QProfileFactoryImpl(DbClient db, UuidFactory uuidFactory, System2 system2, ActiveRuleIndexer activeRuleIndexer) {
52 this.uuidFactory = uuidFactory;
53 this.system2 = system2;
54 this.activeRuleIndexer = activeRuleIndexer;
58 public QProfileDto getOrCreateCustom(DbSession dbSession, QProfileName name) {
59 QProfileDto profile = db.qualityProfileDao().selectByNameAndLanguage(dbSession, name.getName(), name.getLanguage());
60 if (profile == null) {
61 profile = doCreate(dbSession, name, null, false, false);
63 checkArgument(!profile.isBuiltIn(), "Operation forbidden for built-in Quality Profile '%s' with language '%s'", profile.getName(), profile.getLanguage());
70 public QProfileDto checkAndCreateCustom(DbSession dbSession, QProfileName name) {
71 QProfileDto dto = db.qualityProfileDao().selectByNameAndLanguage(dbSession, name.getName(), name.getLanguage());
72 checkRequest(dto == null, "Quality profile already exists: %s", name);
73 return doCreate(dbSession, name, null, false, false);
77 public QProfileDto createCustom(DbSession dbSession, QProfileName name, @Nullable String parentKey) {
78 return doCreate(dbSession, name, parentKey, false, false);
81 private QProfileDto doCreate(DbSession dbSession, QProfileName name, @Nullable String parentKey, boolean isDefault, boolean isBuiltIn) {
82 if (StringUtils.isEmpty(name.getName())) {
83 throw BadRequestException.create("quality_profiles.profile_name_cant_be_blank");
85 Date now = new Date(system2.now());
86 QProfileDto dto = new QProfileDto()
87 .setKee(uuidFactory.create())
88 .setRulesProfileUuid(uuidFactory.create())
89 .setName(name.getName())
90 .setLanguage(name.getLanguage())
91 .setIsBuiltIn(isBuiltIn)
92 .setParentKee(parentKey)
93 .setRulesUpdatedAtAsDate(now);
94 db.qualityProfileDao().insert(dbSession, dto);
96 db.defaultQProfileDao().insertOrUpdate(dbSession, DefaultQProfileDto.from(dto));
101 // ------------- DELETION
103 public void delete(DbSession dbSession, Collection<QProfileDto> profiles) {
104 if (profiles.isEmpty()) {
108 Set<String> uuids = new HashSet<>();
109 List<QProfileDto> customProfiles = new ArrayList<>();
110 Set<String> rulesProfileUuidsOfCustomProfiles = new HashSet<>();
111 profiles.forEach(p -> {
112 uuids.add(p.getKee());
113 if (!p.isBuiltIn()) {
114 customProfiles.add(p);
115 rulesProfileUuidsOfCustomProfiles.add(p.getRulesProfileUuid());
119 // tables org_qprofiles, default_qprofiles and project_qprofiles
120 // are deleted whatever custom or built-in
121 db.qualityProfileDao().deleteProjectAssociationsByProfileUuids(dbSession, uuids);
122 db.defaultQProfileDao().deleteByQProfileUuids(dbSession, uuids);
123 db.qualityProfileDao().deleteOrgQProfilesByUuids(dbSession, uuids);
125 // Permissions are only available on custom profiles
126 db.qProfileEditUsersDao().deleteByQProfiles(dbSession, customProfiles);
127 db.qProfileEditGroupsDao().deleteByQProfiles(dbSession, customProfiles);
129 // tables related to rules_profiles and active_rules are deleted
130 // only for custom profiles. Built-in profiles are never
131 // deleted from table rules_profiles.
132 if (!rulesProfileUuidsOfCustomProfiles.isEmpty()) {
133 db.activeRuleDao().deleteParametersByRuleProfileUuids(dbSession, rulesProfileUuidsOfCustomProfiles);
134 db.activeRuleDao().deleteByRuleProfileUuids(dbSession, rulesProfileUuidsOfCustomProfiles);
135 db.qProfileChangeDao().deleteByRulesProfileUuids(dbSession, rulesProfileUuidsOfCustomProfiles);
136 db.qualityProfileDao().deleteRulesProfilesByUuids(dbSession, rulesProfileUuidsOfCustomProfiles);
137 activeRuleIndexer.commitDeletionOfProfiles(dbSession, customProfiles);