+++ /dev/null
-/*
- * SonarQube, open source software quality management tool.
- * Copyright (C) 2008-2014 SonarSource
- * mailto:contact AT sonarsource DOT com
- *
- * SonarQube 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.
- *
- * SonarQube 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.server.qualityprofile;
-
-import org.sonar.api.ServerComponent;
-import org.sonar.core.permission.GlobalPermissions;
-import org.sonar.core.persistence.DbSession;
-import org.sonar.core.persistence.MyBatis;
-import org.sonar.core.preview.PreviewCache;
-import org.sonar.core.qualityprofile.db.QualityProfileDto;
-import org.sonar.server.db.DbClient;
-import org.sonar.server.exceptions.BadRequestException;
-import org.sonar.server.user.UserSession;
-
-import java.util.Map;
-
-public class QProfileOperations implements ServerComponent {
-
- public static final String PROFILE_PROPERTY_PREFIX = "sonar.profile.";
-
- private final DbClient db;
- private final QProfileRepositoryExporter exporter;
- private final PreviewCache dryRunCache;
-
- public QProfileOperations(DbClient db,
- QProfileRepositoryExporter exporter, PreviewCache dryRunCache) {
- this.db = db;
- this.exporter = exporter;
- this.dryRunCache = dryRunCache;
- }
-
- public QProfileResult newProfile(String name, String language, Map<String, String> xmlProfilesByPlugin, UserSession userSession) {
- DbSession session = db.openSession(false);
- try {
- QProfile profile = newProfile(name, language, true, userSession, session);
-
- QProfileResult result = new QProfileResult();
- result.setProfile(profile);
-
- for (Map.Entry<String, String> entry : xmlProfilesByPlugin.entrySet()) {
- result.add(exporter.importXml(profile, entry.getKey(), entry.getValue(), session));
- }
- dryRunCache.reportGlobalModification(session);
- session.commit();
- return result;
- } finally {
- MyBatis.closeQuietly(session);
- }
- }
-
- private QProfile newProfile(String name, String language, boolean failIfAlreadyExists, UserSession userSession, DbSession session) {
- checkPermission(userSession);
- if (failIfAlreadyExists) {
- checkNotAlreadyExists(name, language, session);
- }
- QualityProfileDto dto = new QualityProfileDto().setName(name).setLanguage(language);
- db.qualityProfileDao().insert(session, dto);
- return QProfile.from(dto);
- }
-
- private void checkPermission(UserSession userSession) {
- userSession.checkLoggedIn();
- userSession.checkGlobalPermission(GlobalPermissions.QUALITY_PROFILE_ADMIN);
- }
-
- private void checkNotAlreadyExists(String name, String language, DbSession session) {
- if (db.qualityProfileDao().getByNameAndLanguage(name, language, session) != null) {
- throw new BadRequestException("quality_profiles.profile_x_already_exists", name);
- }
- }
-
-}
public class QProfileProjectLookup implements ServerComponent {
+ public static final String PROFILE_PROPERTY_PREFIX = "sonar.profile.";
private final DbClient db;
public QProfileProjectLookup(DbClient db) {
QProfileValidations.checkProfileIsNotNull(qualityProfile);
Map<String, Component> componentsByKeys = Maps.newHashMap();
for (Component component : db.qualityProfileDao().selectProjects(
- qualityProfile.getName(), QProfileOperations.PROFILE_PROPERTY_PREFIX + qualityProfile.getLanguage(), session
+ qualityProfile.getName(), PROFILE_PROPERTY_PREFIX + qualityProfile.getLanguage(), session
)) {
componentsByKeys.put(component.key(), component);
}
}
public int countProjects(QProfile profile) {
- return db.qualityProfileDao().countProjects(profile.name(), QProfileOperations.PROFILE_PROPERTY_PREFIX + profile.language());
+ return db.qualityProfileDao().countProjects(profile.name(), PROFILE_PROPERTY_PREFIX + profile.language());
}
@CheckForNull
public QProfile findProfileByProjectAndLanguage(long projectId, String language) {
- QualityProfileDto dto = db.qualityProfileDao().getByProjectAndLanguage(projectId, language, QProfileOperations.PROFILE_PROPERTY_PREFIX + language);
+ QualityProfileDto dto = db.qualityProfileDao().getByProjectAndLanguage(projectId, language, PROFILE_PROPERTY_PREFIX + language);
if (dto != null) {
return QProfile.from(dto);
}
QualityProfileDto qualityProfile = findNotNull(profileId, session);
db.propertiesDao().setProperty(new PropertyDto().setKey(
- QProfileOperations.PROFILE_PROPERTY_PREFIX + qualityProfile.getLanguage()).setValue(qualityProfile.getName()).setResourceId(project.getId()), session);
+ QProfileProjectLookup.PROFILE_PROPERTY_PREFIX + qualityProfile.getLanguage()).setValue(qualityProfile.getName()).setResourceId(project.getId()), session);
session.commit();
} finally {
MyBatis.closeQuietly(session);
ComponentDto project = (ComponentDto) findProjectNotNull(projectId, session);
QualityProfileDto qualityProfile = findNotNull(profileId, session);
- db.propertiesDao().deleteProjectProperty(QProfileOperations.PROFILE_PROPERTY_PREFIX + qualityProfile.getLanguage(), project.getId(), session);
+ db.propertiesDao().deleteProjectProperty(QProfileProjectLookup.PROFILE_PROPERTY_PREFIX + qualityProfile.getLanguage(), project.getId(), session);
session.commit();
} finally {
MyBatis.closeQuietly(session);
try {
ComponentDto project = (ComponentDto) findProjectNotNull(projectId, session);
- db.propertiesDao().deleteProjectProperty(QProfileOperations.PROFILE_PROPERTY_PREFIX + language, project.getId(), session);
+ db.propertiesDao().deleteProjectProperty(QProfileProjectLookup.PROFILE_PROPERTY_PREFIX + language, project.getId(), session);
session.commit();
} finally {
MyBatis.closeQuietly(session);
DbSession session = db.openSession(false);
try {
QualityProfileDto qualityProfile = findNotNull(profileId, session);
- db.propertiesDao().deleteProjectProperties(QProfileOperations.PROFILE_PROPERTY_PREFIX + qualityProfile.getLanguage(), qualityProfile.getName(), session);
+ db.propertiesDao().deleteProjectProperties(QProfileProjectLookup.PROFILE_PROPERTY_PREFIX + qualityProfile.getLanguage(), qualityProfile.getName(), session);
session.commit();
} finally {
MyBatis.closeQuietly(session);