diff options
Diffstat (limited to 'server/sonar-db-dao/src/main/java/org/sonar')
7 files changed, 228 insertions, 1 deletions
diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/DaoModule.java b/server/sonar-db-dao/src/main/java/org/sonar/db/DaoModule.java index 3f833346883..93a5d217467 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/DaoModule.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/DaoModule.java @@ -71,6 +71,7 @@ import org.sonar.db.qualityprofile.QProfileChangeDao; import org.sonar.db.qualityprofile.QProfileEditGroupsDao; import org.sonar.db.qualityprofile.QProfileEditUsersDao; import org.sonar.db.qualityprofile.QualityProfileDao; +import org.sonar.db.qualityprofile.QualityProfileExportDao; import org.sonar.db.rule.RuleDao; import org.sonar.db.rule.RuleRepositoryDao; import org.sonar.db.schemamigration.SchemaMigrationDao; @@ -139,6 +140,7 @@ public class DaoModule extends Module { QualityGateConditionDao.class, QualityGateDao.class, QualityProfileDao.class, + QualityProfileExportDao.class, RoleDao.class, RuleDao.class, RuleRepositoryDao.class, diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/DbClient.java b/server/sonar-db-dao/src/main/java/org/sonar/db/DbClient.java index eb220ed54c0..27f1832a2bb 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/DbClient.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/DbClient.java @@ -69,6 +69,7 @@ import org.sonar.db.qualityprofile.QProfileChangeDao; import org.sonar.db.qualityprofile.QProfileEditGroupsDao; import org.sonar.db.qualityprofile.QProfileEditUsersDao; import org.sonar.db.qualityprofile.QualityProfileDao; +import org.sonar.db.qualityprofile.QualityProfileExportDao; import org.sonar.db.rule.RuleDao; import org.sonar.db.rule.RuleRepositoryDao; import org.sonar.db.schemamigration.SchemaMigrationDao; @@ -94,6 +95,7 @@ public class DbClient { private final OrganizationDao organizationDao; private final OrganizationMemberDao organizationMemberDao; private final QualityProfileDao qualityProfileDao; + private final QualityProfileExportDao qualityProfileExportDao; private final PropertiesDao propertiesDao; private final AlmAppInstallDao almAppInstallDao; private final ProjectAlmBindingDao projectAlmBindingDao; @@ -167,6 +169,7 @@ public class DbClient { organizationDao = getDao(map, OrganizationDao.class); organizationMemberDao = getDao(map, OrganizationMemberDao.class); qualityProfileDao = getDao(map, QualityProfileDao.class); + qualityProfileExportDao = getDao(map, QualityProfileExportDao.class); propertiesDao = getDao(map, PropertiesDao.class); internalPropertiesDao = getDao(map, InternalPropertiesDao.class); snapshotDao = getDao(map, SnapshotDao.class); @@ -267,6 +270,10 @@ public class DbClient { return qualityProfileDao; } + public QualityProfileExportDao qualityProfileExportDao() { + return qualityProfileExportDao; + } + public PropertiesDao propertiesDao() { return propertiesDao; } diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/MyBatis.java b/server/sonar-db-dao/src/main/java/org/sonar/db/MyBatis.java index f449a0eeabf..391ee1c0933 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/MyBatis.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/MyBatis.java @@ -48,10 +48,10 @@ import org.sonar.db.ce.CeTaskMessageMapper; import org.sonar.db.component.AnalysisPropertiesMapper; import org.sonar.db.component.BranchMapper; import org.sonar.db.component.ComponentDto; -import org.sonar.db.component.ComponentWithModuleUuidDto; import org.sonar.db.component.ComponentDtoWithSnapshotId; import org.sonar.db.component.ComponentKeyUpdaterMapper; import org.sonar.db.component.ComponentMapper; +import org.sonar.db.component.ComponentWithModuleUuidDto; import org.sonar.db.component.FilePathWithHashDto; import org.sonar.db.component.KeyWithUuidDto; import org.sonar.db.component.ProjectLinkMapper; @@ -121,6 +121,7 @@ import org.sonar.db.qualityprofile.DefaultQProfileMapper; import org.sonar.db.qualityprofile.QProfileChangeMapper; import org.sonar.db.qualityprofile.QProfileEditGroupsMapper; import org.sonar.db.qualityprofile.QProfileEditUsersMapper; +import org.sonar.db.qualityprofile.QualityProfileExportMapper; import org.sonar.db.qualityprofile.QualityProfileMapper; import org.sonar.db.rule.RuleDto; import org.sonar.db.rule.RuleMapper; @@ -270,6 +271,7 @@ public class MyBatis implements Startable { QualityGateConditionMapper.class, QualityGateMapper.class, QualityProfileMapper.class, + QualityProfileExportMapper.class, RoleMapper.class, RuleMapper.class, RuleRepositoryMapper.class, diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/qualityprofile/ExportRuleDto.java b/server/sonar-db-dao/src/main/java/org/sonar/db/qualityprofile/ExportRuleDto.java new file mode 100644 index 00000000000..9e0c123cef5 --- /dev/null +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/qualityprofile/ExportRuleDto.java @@ -0,0 +1,92 @@ +/* + * SonarQube + * Copyright (C) 2009-2019 SonarSource SA + * mailto:info AT sonarsource DOT com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +package org.sonar.db.qualityprofile; + +import java.util.LinkedList; +import java.util.List; +import org.sonar.api.rule.RuleKey; +import org.sonar.api.rules.RuleType; +import org.sonar.db.rule.SeverityUtil; + +public class ExportRuleDto { + private Integer activeRuleId; + private String repository; + private String rule; + private String name; + private String description; + private String extendedDescription; + private String template; + private Integer severity; + private Integer type; + private String tags; + + private List<ExportRuleParamDto> params; + + public boolean isCustomRule() { + return template != null; + } + + public Integer getActiveRuleId() { + return activeRuleId; + } + + public RuleKey getRuleKey() { + return RuleKey.of(repository, rule); + } + + public RuleKey getTemplateRuleKey() { + return RuleKey.of(repository, template); + } + + public String getSeverityString() { + return SeverityUtil.getSeverityFromOrdinal(severity); + } + + public String getExtendedDescription() { + return extendedDescription; + } + + public RuleType getRuleType() { + return RuleType.valueOf(type); + } + + public String getTags() { + return tags; + } + + public String getDescription() { + return description; + } + + public String getName() { + return name; + } + + public List<ExportRuleParamDto> getParams() { + if (params == null) { + params = new LinkedList<>(); + } + return params; + } + + void setParams(List<ExportRuleParamDto> params) { + this.params = params; + } +} diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/qualityprofile/ExportRuleParamDto.java b/server/sonar-db-dao/src/main/java/org/sonar/db/qualityprofile/ExportRuleParamDto.java new file mode 100644 index 00000000000..1222480dd5a --- /dev/null +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/qualityprofile/ExportRuleParamDto.java @@ -0,0 +1,38 @@ +/* + * SonarQube + * Copyright (C) 2009-2019 SonarSource SA + * mailto:info AT sonarsource DOT com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +package org.sonar.db.qualityprofile; + +public class ExportRuleParamDto { + private Integer activeRuleId; + private String kee; + private String value; + + public Integer getActiveRuleId() { + return activeRuleId; + } + + public String getKey() { + return kee; + } + + public String getValue() { + return value; + } +} diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/qualityprofile/QualityProfileExportDao.java b/server/sonar-db-dao/src/main/java/org/sonar/db/qualityprofile/QualityProfileExportDao.java new file mode 100644 index 00000000000..5f2ff75ff8f --- /dev/null +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/qualityprofile/QualityProfileExportDao.java @@ -0,0 +1,56 @@ +/* + * SonarQube + * Copyright (C) 2009-2019 SonarSource SA + * mailto:info AT sonarsource DOT com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +package org.sonar.db.qualityprofile; + +import java.util.Collection; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.stream.Collectors; +import org.sonar.db.Dao; +import org.sonar.db.DbSession; + +import static org.sonar.db.DatabaseUtils.executeLargeInputs; + +public class QualityProfileExportDao implements Dao { + + public List<ExportRuleDto> selectRulesByProfile(DbSession dbSession, QProfileDto profile) { + List<ExportRuleDto> exportRules = mapper(dbSession).selectByProfileUuid(profile.getKee()); + + Set<Integer> activeRuleIds = exportRules.stream().map(ExportRuleDto::getActiveRuleId).collect(Collectors.toSet()); + + Map<Integer, List<ExportRuleParamDto>> activeRulesParam = selectParamsByActiveRuleIds(dbSession, activeRuleIds); + + exportRules.forEach(exportRuleDto -> + exportRuleDto.setParams(activeRulesParam.get(exportRuleDto.getActiveRuleId())) + ); + return exportRules; + } + + private Map<Integer, List<ExportRuleParamDto>> selectParamsByActiveRuleIds(DbSession dbSession, Collection<Integer> activeRuleIds) { + return executeLargeInputs(activeRuleIds, ids -> mapper(dbSession).selectParamsByActiveRuleIds(ids)) + .stream() + .collect(Collectors.groupingBy(ExportRuleParamDto::getActiveRuleId)); + } + + private static QualityProfileExportMapper mapper(DbSession dbSession) { + return dbSession.getMapper(QualityProfileExportMapper.class); + } +} diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/qualityprofile/QualityProfileExportMapper.java b/server/sonar-db-dao/src/main/java/org/sonar/db/qualityprofile/QualityProfileExportMapper.java new file mode 100644 index 00000000000..3a2bb304b07 --- /dev/null +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/qualityprofile/QualityProfileExportMapper.java @@ -0,0 +1,30 @@ +/* + * SonarQube + * Copyright (C) 2009-2019 SonarSource SA + * mailto:info AT sonarsource DOT com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +package org.sonar.db.qualityprofile; + +import java.util.Collection; +import java.util.List; +import org.apache.ibatis.annotations.Param; + +public interface QualityProfileExportMapper { + List<ExportRuleDto> selectByProfileUuid(String uuid); + + List<ExportRuleParamDto> selectParamsByActiveRuleIds(@Param("activeRuleIds") Collection<Integer> activeRuleIds); +} |