use of org.sonar.db.DbSession in project sonarqube by SonarSource.
the class QProfileFactory method rename.
// ------------- RENAME
public boolean rename(String key, String newName) {
checkRequest(StringUtils.isNotBlank(newName), "Name must be set");
checkRequest(newName.length() < 100, String.format("Name is too long (>%d characters)", 100));
DbSession dbSession = db.openSession(false);
try {
QualityProfileDto profile = db.qualityProfileDao().selectByKey(dbSession, key);
if (profile == null) {
throw new NotFoundException("Quality profile not found: " + key);
}
if (!StringUtils.equals(newName, profile.getName())) {
checkRequest(db.qualityProfileDao().selectByNameAndLanguage(newName, profile.getLanguage(), dbSession) == null, "Quality profile already exists: %s", newName);
profile.setName(newName);
db.qualityProfileDao().update(dbSession, profile);
dbSession.commit();
return true;
}
return false;
} finally {
dbSession.close();
}
}
use of org.sonar.db.DbSession in project sonarqube by SonarSource.
the class RegisterRules method start.
@Override
public void start() {
Profiler profiler = Profiler.create(LOG).startInfo("Register rules");
DbSession session = dbClient.openSession(false);
try {
Map<RuleKey, RuleDto> allRules = loadRules(session);
RulesDefinition.Context context = defLoader.load();
for (RulesDefinition.ExtendedRepository repoDef : getRepositories(context)) {
if (languages.get(repoDef.language()) != null) {
for (RulesDefinition.Rule ruleDef : repoDef.rules()) {
registerRule(ruleDef, allRules, session);
}
session.commit();
}
}
List<RuleDto> activeRules = processRemainingDbRules(allRules.values(), session);
List<ActiveRuleChange> changes = removeActiveRulesOnStillExistingRepositories(session, activeRules, context);
session.commit();
persistRepositories(session, context.repositories());
ruleIndexer.index();
activeRuleIndexer.index(changes);
profiler.stopDebug();
} finally {
session.close();
}
}
use of org.sonar.db.DbSession in project sonarqube by SonarSource.
the class ChangelogAction method handle.
@Override
public void handle(Request request, Response response) throws Exception {
try (DbSession dbSession = dbClient.openSession(false)) {
QualityProfileDto profile = profileFactory.find(dbSession, QProfileRef.from(request));
QProfileChangeQuery query = new QProfileChangeQuery(profile.getKey());
Date since = parseStartingDateOrDateTime(request.param(PARAM_SINCE));
if (since != null) {
query.setFromIncluded(since.getTime());
}
Date to = parseEndingDateOrDateTime(request.param(PARAM_TO));
if (to != null) {
query.setToExcluded(to.getTime());
}
int page = request.mandatoryParamAsInt(Param.PAGE);
int pageSize = request.mandatoryParamAsInt(Param.PAGE_SIZE);
query.setPage(page, pageSize);
ChangelogLoader.Changelog changelog = changelogLoader.load(dbSession, query);
writeResponse(response.newJsonWriter(), page, pageSize, changelog);
}
}
use of org.sonar.db.DbSession in project sonarqube by SonarSource.
the class InheritanceAction method handle.
@Override
public void handle(Request request, Response response) throws Exception {
DbSession dbSession = dbClient.openSession(false);
try {
QualityProfileDto profile = profileFactory.find(dbSession, QProfileRef.from(request));
List<QProfile> ancestors = profileLookup.ancestors(profile, dbSession);
List<QualityProfileDto> children = dbClient.qualityProfileDao().selectChildren(dbSession, profile.getKey());
Map<String, Multimap<String, FacetValue>> profileStats = profileLoader.getAllProfileStats();
writeResponse(response.newJsonWriter(), profile, ancestors, children, profileStats);
} finally {
dbSession.close();
}
}
use of org.sonar.db.DbSession in project sonarqube by SonarSource.
the class ProjectsAction method handle.
@Override
public void handle(Request request, Response response) throws Exception {
String profileKey = request.mandatoryParam(PARAM_KEY);
DbSession session = dbClient.openSession(false);
try {
checkProfileExists(profileKey, session);
String selected = request.param(Param.SELECTED);
String query = request.param(PARAM_QUERY);
int pageSize = request.mandatoryParamAsInt(PARAM_PAGE_SIZE);
int page = request.mandatoryParamAsInt(PARAM_PAGE);
List<ProjectQprofileAssociationDto> projects = loadProjects(profileKey, session, selected, query);
Collections.sort(projects, new Comparator<ProjectQprofileAssociationDto>() {
@Override
public int compare(ProjectQprofileAssociationDto o1, ProjectQprofileAssociationDto o2) {
return new CompareToBuilder().append(o1.getProjectName(), o2.getProjectName()).append(o1.getProjectUuid(), o2.getProjectUuid()).toComparison();
}
});
Collection<Long> projectIds = Collections2.transform(projects, new NonNullInputFunction<ProjectQprofileAssociationDto, Long>() {
@Override
protected Long doApply(ProjectQprofileAssociationDto input) {
return input.getProjectId();
}
});
Collection<Long> authorizedProjectIds = dbClient.authorizationDao().keepAuthorizedProjectIds(session, projectIds, userSession.getUserId(), UserRole.USER);
Iterable<ProjectQprofileAssociationDto> authorizedProjects = Iterables.filter(projects, new Predicate<ProjectQprofileAssociationDto>() {
@Override
public boolean apply(ProjectQprofileAssociationDto input) {
return authorizedProjectIds.contains(input.getProjectId());
}
});
Paging paging = forPageIndex(page).withPageSize(pageSize).andTotal(authorizedProjectIds.size());
List<ProjectQprofileAssociationDto> pagedAuthorizedProjects = Lists.newArrayList(authorizedProjects);
if (pagedAuthorizedProjects.size() <= paging.offset()) {
pagedAuthorizedProjects = Lists.newArrayList();
} else if (pagedAuthorizedProjects.size() > paging.pageSize()) {
int endIndex = Math.min(paging.offset() + pageSize, pagedAuthorizedProjects.size());
pagedAuthorizedProjects = pagedAuthorizedProjects.subList(paging.offset(), endIndex);
}
writeProjects(response.newJsonWriter(), pagedAuthorizedProjects, paging);
} finally {
session.close();
}
}
Aggregations