Search in sources :

Example 41 with DbSession

use of org.sonar.db.DbSession in project sonarqube by SonarSource.

the class SearchAction method handle.

@Override
public void handle(Request request, Response response) throws Exception {
    try (DbSession dbSession = dbClient.openSession(false)) {
        Paging paging = Paging.forPageIndex(request.mandatoryParamAsInt(Param.PAGE)).withPageSize(request.mandatoryParamAsInt(Param.PAGE_SIZE)).andTotal(0);
        OrganizationQuery organizationQuery = newOrganizationQueryBuilder().setKeys(request.paramAsStrings(PARAM_ORGANIZATIONS)).build();
        List<OrganizationDto> dtos = dbClient.organizationDao().selectByQuery(dbSession, organizationQuery, paging.offset(), paging.pageSize());
        writeResponse(request, response, dtos);
    }
}
Also used : DbSession(org.sonar.db.DbSession) Paging(org.sonar.api.utils.Paging) OrganizationQuery(org.sonar.db.organization.OrganizationQuery) OrganizationDto(org.sonar.db.organization.OrganizationDto)

Example 42 with DbSession

use of org.sonar.db.DbSession in project sonarqube by SonarSource.

the class SearchMyOrganizationsAction method handle.

@Override
public void handle(Request request, Response response) throws Exception {
    if (!userSession.isLoggedIn()) {
        response.noContent();
        return;
    }
    try (DbSession dbSession = dbClient.openSession(false);
        JsonWriter jsonWriter = response.newJsonWriter()) {
        jsonWriter.beginObject();
        jsonWriter.name("organizations").beginArray();
        dbClient.organizationDao().selectByPermission(dbSession, userSession.getUserId(), OrganizationPermission.ADMINISTER.getKey()).forEach(dto -> jsonWriter.value(dto.getKey()));
        jsonWriter.endArray();
        jsonWriter.endObject();
    }
}
Also used : DbSession(org.sonar.db.DbSession) JsonWriter(org.sonar.api.utils.text.JsonWriter)

Example 43 with DbSession

use of org.sonar.db.DbSession in project sonarqube by SonarSource.

the class UpdateAction method handle.

@Override
public void handle(Request request, Response response) throws Exception {
    userSession.checkLoggedIn();
    try (DbSession dbSession = dbClient.openSession(false)) {
        organizationFlags.checkEnabled(dbSession);
        String key = request.mandatoryParam(PARAM_KEY);
        UpdateOrganizationRequest updateRequest = new UpdateOrganizationRequest(request.getParam(PARAM_NAME, (rqt, paramKey) -> wsSupport.getAndCheckName(rqt)), request.getParam(PARAM_DESCRIPTION, (rqt, paramKey) -> emptyAsNull(wsSupport.getAndCheckDescription(rqt))), request.getParam(PARAM_URL, (rqt, paramKey) -> emptyAsNull(wsSupport.getAndCheckUrl(rqt))), request.getParam(PARAM_AVATAR_URL, (rqt, paramKey) -> emptyAsNull(wsSupport.getAndCheckAvatar(rqt))));
        OrganizationDto dto = getDto(dbSession, key);
        userSession.checkPermission(ADMINISTER, dto);
        dto.setName(updateRequest.getName().or(dto::getName)).setDescription(updateRequest.getDescription().or(dto::getDescription)).setUrl(updateRequest.getUrl().or(dto::getUrl)).setAvatarUrl(updateRequest.getAvatar().or(dto::getAvatarUrl));
        dbClient.organizationDao().update(dbSession, dto);
        dbSession.commit();
        writeResponse(request, response, dto);
    }
}
Also used : OrganizationFlags(org.sonar.server.organization.OrganizationFlags) PARAM_AVATAR_URL(org.sonar.server.organization.ws.OrganizationsWsSupport.PARAM_AVATAR_URL) ADMINISTER(org.sonar.db.permission.OrganizationPermission.ADMINISTER) PARAM_KEY(org.sonar.server.organization.ws.OrganizationsWsSupport.PARAM_KEY) DbSession(org.sonar.db.DbSession) NotFoundException(org.sonar.server.exceptions.NotFoundException) String.format(java.lang.String.format) Organizations(org.sonarqube.ws.Organizations) Request(org.sonar.api.server.ws.Request) DbClient(org.sonar.db.DbClient) WebService(org.sonar.api.server.ws.WebService) PARAM_DESCRIPTION(org.sonar.server.organization.ws.OrganizationsWsSupport.PARAM_DESCRIPTION) PARAM_URL(org.sonar.server.organization.ws.OrganizationsWsSupport.PARAM_URL) Response(org.sonar.api.server.ws.Response) Optional(java.util.Optional) OrganizationDto(org.sonar.db.organization.OrganizationDto) UserSession(org.sonar.server.user.UserSession) PARAM_NAME(org.sonar.server.organization.ws.OrganizationsWsSupport.PARAM_NAME) CheckForNull(javax.annotation.CheckForNull) Nullable(javax.annotation.Nullable) WsUtils.writeProtobuf(org.sonar.server.ws.WsUtils.writeProtobuf) DbSession(org.sonar.db.DbSession) OrganizationDto(org.sonar.db.organization.OrganizationDto)

Example 44 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)) {
        ChangelogWsResponse wsResponse = Stream.of(request).map(searchChangelog(dbSession)).map(buildResponse()).collect(Collectors.toOneElement());
        writeProtobuf(wsResponse, request, response);
    }
}
Also used : DbSession(org.sonar.db.DbSession) ChangelogWsResponse(org.sonarqube.ws.Issues.ChangelogWsResponse)

Example 45 with DbSession

use of org.sonar.db.DbSession in project sonarqube by SonarSource.

the class CreateAction method handle.

@Override
public void handle(Request request, Response response) throws Exception {
    String valueAsString = request.mandatoryParam(PARAM_VALUE);
    String description = request.param(PARAM_DESCRIPTION);
    long now = system.now();
    try (DbSession dbSession = dbClient.openSession(false)) {
        ComponentDto component = componentFinder.getByUuidOrKey(dbSession, request.param(PARAM_PROJECT_ID), request.param(PARAM_PROJECT_KEY), PROJECT_ID_AND_KEY);
        MetricDto metric = searchMetric(dbSession, request);
        checkPermissions(userSession, component);
        checkIsProjectOrModule(component);
        checkMeasureDoesNotExistAlready(dbSession, component, metric);
        UserDto user = dbClient.userDao().selectOrFailByLogin(dbSession, userSession.getLogin());
        CustomMeasureDto measure = new CustomMeasureDto().setComponentUuid(component.uuid()).setMetricId(metric.getId()).setDescription(description).setUserLogin(user.getLogin()).setCreatedAt(now).setUpdatedAt(now);
        validator.setMeasureValue(measure, valueAsString, metric);
        dbClient.customMeasureDao().insert(dbSession, measure);
        dbSession.commit();
        JsonWriter json = response.newJsonWriter();
        customMeasureJsonWriter.write(json, measure, metric, component, user, true, CustomMeasureJsonWriter.OPTIONAL_FIELDS);
        json.close();
    }
}
Also used : DbSession(org.sonar.db.DbSession) MetricDto(org.sonar.db.metric.MetricDto) UserDto(org.sonar.db.user.UserDto) ComponentDto(org.sonar.db.component.ComponentDto) JsonWriter(org.sonar.api.utils.text.JsonWriter) CustomMeasureDto(org.sonar.db.measure.custom.CustomMeasureDto)

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