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;
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;
27 import java.util.Objects;
29 import javax.annotation.Nullable;
30 import org.apache.commons.lang.StringUtils;
31 import org.sonar.api.utils.System2;
32 import org.sonar.core.util.UuidFactory;
33 import org.sonar.db.DbClient;
34 import org.sonar.db.DbSession;
35 import org.sonar.db.organization.OrganizationDto;
36 import org.sonar.db.qualityprofile.DefaultQProfileDto;
37 import org.sonar.db.qualityprofile.QProfileDto;
38 import org.sonar.server.exceptions.BadRequestException;
39 import org.sonar.server.qualityprofile.index.ActiveRuleIndexer;
41 import static com.google.common.base.Preconditions.checkArgument;
42 import static org.sonar.server.exceptions.BadRequestException.checkRequest;
44 public class QProfileFactoryImpl implements QProfileFactory {
46 private final DbClient db;
47 private final UuidFactory uuidFactory;
48 private final System2 system2;
49 private final ActiveRuleIndexer activeRuleIndexer;
51 public QProfileFactoryImpl(DbClient db, UuidFactory uuidFactory, System2 system2, ActiveRuleIndexer activeRuleIndexer) {
53 this.uuidFactory = uuidFactory;
54 this.system2 = system2;
55 this.activeRuleIndexer = activeRuleIndexer;
58 private static OrganizationDto requireNonNull(@Nullable OrganizationDto organization) {
59 Objects.requireNonNull(organization, "Organization is required, when creating a quality profile.");
64 public QProfileDto getOrCreateCustom(DbSession dbSession, OrganizationDto organization, QProfileName name) {
65 requireNonNull(organization);
66 QProfileDto profile = db.qualityProfileDao().selectByNameAndLanguage(dbSession, organization, name.getName(), name.getLanguage());
67 if (profile == null) {
68 profile = doCreate(dbSession, organization, name, false, false);
70 checkArgument(!profile.isBuiltIn(), "Operation forbidden for built-in Quality Profile '%s' with language '%s'", profile.getName(), profile.getLanguage());
77 public QProfileDto checkAndCreateCustom(DbSession dbSession, OrganizationDto organization, QProfileName name) {
78 requireNonNull(organization);
79 QProfileDto dto = db.qualityProfileDao().selectByNameAndLanguage(dbSession, organization, name.getName(), name.getLanguage());
80 checkRequest(dto == null, "Quality profile already exists: %s", name);
81 return doCreate(dbSession, organization, name, false, false);
84 private QProfileDto doCreate(DbSession dbSession, OrganizationDto organization, QProfileName name, boolean isDefault, boolean isBuiltIn) {
85 if (StringUtils.isEmpty(name.getName())) {
86 throw BadRequestException.create("quality_profiles.profile_name_cant_be_blank");
88 Date now = new Date(system2.now());
89 QProfileDto dto = new QProfileDto()
90 .setKee(uuidFactory.create())
91 .setRulesProfileUuid(uuidFactory.create())
92 .setName(name.getName())
93 .setOrganizationUuid(organization.getUuid())
94 .setLanguage(name.getLanguage())
95 .setIsBuiltIn(isBuiltIn)
96 .setRulesUpdatedAtAsDate(now);
97 db.qualityProfileDao().insert(dbSession, dto);
99 db.defaultQProfileDao().insertOrUpdate(dbSession, DefaultQProfileDto.from(dto));
104 // ------------- DELETION
106 public void delete(DbSession dbSession, Collection<QProfileDto> profiles) {
107 if (profiles.isEmpty()) {
111 Set<String> uuids = new HashSet<>();
112 List<QProfileDto> customProfiles = new ArrayList<>();
113 Set<String> rulesProfileUuidsOfCustomProfiles = new HashSet<>();
114 profiles.forEach(p -> {
115 uuids.add(p.getKee());
116 if (!p.isBuiltIn()) {
117 customProfiles.add(p);
118 rulesProfileUuidsOfCustomProfiles.add(p.getRulesProfileUuid());
122 // tables org_qprofiles, default_qprofiles and project_qprofiles
123 // are deleted whatever custom or built-in
124 db.qualityProfileDao().deleteProjectAssociationsByProfileUuids(dbSession, uuids);
125 db.defaultQProfileDao().deleteByQProfileUuids(dbSession, uuids);
126 db.qualityProfileDao().deleteOrgQProfilesByUuids(dbSession, uuids);
128 // Permissions are only available on custom profiles
129 db.qProfileEditUsersDao().deleteByQProfiles(dbSession, customProfiles);
130 db.qProfileEditGroupsDao().deleteByQProfiles(dbSession, customProfiles);
132 // tables related to rules_profiles and active_rules are deleted
133 // only for custom profiles. Built-in profiles are never
134 // deleted from table rules_profiles.
135 if (!rulesProfileUuidsOfCustomProfiles.isEmpty()) {
136 db.activeRuleDao().deleteParametersByRuleProfileUuids(dbSession, rulesProfileUuidsOfCustomProfiles);
137 db.activeRuleDao().deleteByRuleProfileUuids(dbSession, rulesProfileUuidsOfCustomProfiles);
138 db.qProfileChangeDao().deleteByRulesProfileUuids(dbSession, rulesProfileUuidsOfCustomProfiles);
139 db.qualityProfileDao().deleteRulesProfilesByUuids(dbSession, rulesProfileUuidsOfCustomProfiles);
140 activeRuleIndexer.commitDeletionOfProfiles(dbSession, customProfiles);