Search in sources :

Example 81 with DbSession

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();
    }
}
Also used : DbSession(org.sonar.db.DbSession) NotFoundException(org.sonar.server.exceptions.NotFoundException) QualityProfileDto(org.sonar.db.qualityprofile.QualityProfileDto)

Example 82 with DbSession

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();
    }
}
Also used : DbSession(org.sonar.db.DbSession) RulesDefinition(org.sonar.api.server.rule.RulesDefinition) Profiler(org.sonar.api.utils.log.Profiler) RuleKey(org.sonar.api.rule.RuleKey) ActiveRuleDto(org.sonar.db.qualityprofile.ActiveRuleDto) RuleDto(org.sonar.db.rule.RuleDto) ActiveRuleChange(org.sonar.server.qualityprofile.ActiveRuleChange)

Example 83 with DbSession

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);
    }
}
Also used : DbSession(org.sonar.db.DbSession) QProfileChangeQuery(org.sonar.db.qualityprofile.QProfileChangeQuery) QualityProfileDto(org.sonar.db.qualityprofile.QualityProfileDto) Date(java.util.Date)

Example 84 with DbSession

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();
    }
}
Also used : DbSession(org.sonar.db.DbSession) Multimap(com.google.common.collect.Multimap) QualityProfileDto(org.sonar.db.qualityprofile.QualityProfileDto) QProfile(org.sonar.server.qualityprofile.QProfile)

Example 85 with DbSession

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();
    }
}
Also used : Paging(org.sonar.api.utils.Paging) ProjectQprofileAssociationDto(org.sonar.db.qualityprofile.ProjectQprofileAssociationDto) DbSession(org.sonar.db.DbSession) CompareToBuilder(org.apache.commons.lang.builder.CompareToBuilder)

Aggregations

DbSession (org.sonar.db.DbSession)254 ComponentDto (org.sonar.db.component.ComponentDto)63 OrganizationDto (org.sonar.db.organization.OrganizationDto)30 JsonWriter (org.sonar.api.utils.text.JsonWriter)24 UserDto (org.sonar.db.user.UserDto)20 PermissionTemplateDto (org.sonar.db.permission.template.PermissionTemplateDto)16 Test (org.junit.Test)13 MetricDto (org.sonar.db.metric.MetricDto)13 Paging (org.sonar.api.utils.Paging)12 QualityProfileDto (org.sonar.db.qualityprofile.QualityProfileDto)10 CeQueueDto (org.sonar.db.ce.CeQueueDto)8 DepthTraversalTypeAwareCrawler (org.sonar.server.computation.task.projectanalysis.component.DepthTraversalTypeAwareCrawler)8 SearchOptions (org.sonar.server.es.SearchOptions)8 NotFoundException (org.sonar.server.exceptions.NotFoundException)8 List (java.util.List)7 SnapshotDto (org.sonar.db.component.SnapshotDto)7 GroupDto (org.sonar.db.user.GroupDto)7 DbClient (org.sonar.db.DbClient)6 ProjectId (org.sonar.server.permission.ProjectId)6 RuleKey (org.sonar.api.rule.RuleKey)5