Search in sources :

Example 36 with JsonWriter

use of org.sonar.api.utils.text.JsonWriter in project sonarqube by SonarSource.

the class SearchOptionsTest method writeJson.

@Test
public void writeJson() {
    SearchOptions options = new SearchOptions().setPage(3, 10);
    StringWriter json = new StringWriter();
    JsonWriter jsonWriter = JsonWriter.of(json).beginObject();
    options.writeJson(jsonWriter, 42L);
    jsonWriter.endObject().close();
    JsonAssert.assertJson(json.toString()).isSimilarTo("{\"total\": 42, \"p\": 3, \"ps\": 10}");
}
Also used : StringWriter(java.io.StringWriter) JsonWriter(org.sonar.api.utils.text.JsonWriter) Test(org.junit.Test)

Example 37 with JsonWriter

use of org.sonar.api.utils.text.JsonWriter in project sonarqube by SonarSource.

the class CreateAction method handle.

@Override
public void handle(Request request, Response response) throws Exception {
    userSession.checkLoggedIn().checkIsSystemAdministrator();
    String key = request.mandatoryParam(PARAM_KEY);
    try (DbSession dbSession = dbClient.openSession(false)) {
        MetricDto metricTemplate = newMetricTemplate(request);
        MetricDto metricInDb = dbClient.metricDao().selectByKey(dbSession, key);
        checkMetricInDbAndTemplate(dbSession, metricInDb, metricTemplate);
        if (metricIsNotInDb(metricInDb)) {
            metricInDb = insertNewMetric(dbSession, metricTemplate);
        } else {
            updateMetric(dbSession, metricInDb, metricTemplate);
        }
        JsonWriter json = response.newJsonWriter();
        writeMetric(json, metricInDb);
        json.close();
    }
}
Also used : DbSession(org.sonar.db.DbSession) MetricDto(org.sonar.db.metric.MetricDto) JsonWriter(org.sonar.api.utils.text.JsonWriter)

Example 38 with JsonWriter

use of org.sonar.api.utils.text.JsonWriter in project sonarqube by SonarSource.

the class DomainsAction method handle.

@Override
public void handle(Request request, Response response) throws Exception {
    try (DbSession dbSession = dbClient.openSession(false)) {
        List<String> domains = dbClient.metricDao().selectEnabledDomains(dbSession);
        JsonWriter json = response.newJsonWriter();
        json.beginObject();
        writeDomains(json, domains);
        json.endObject();
        json.close();
    }
}
Also used : DbSession(org.sonar.db.DbSession) JsonWriter(org.sonar.api.utils.text.JsonWriter)

Example 39 with JsonWriter

use of org.sonar.api.utils.text.JsonWriter in project sonarqube by SonarSource.

the class SearchAction method handle.

@Override
public void handle(Request request, Response response) throws Exception {
    SearchOptions searchOptions = new SearchOptions().setPage(request.mandatoryParamAsInt(Param.PAGE), request.mandatoryParamAsInt(Param.PAGE_SIZE));
    Boolean isCustom = request.paramAsBoolean(PARAM_IS_CUSTOM);
    try (DbSession dbSession = dbClient.openSession(false)) {
        List<MetricDto> metrics = dbClient.metricDao().selectEnabled(dbSession, isCustom, searchOptions.getOffset(), searchOptions.getLimit());
        int nbMetrics = dbClient.metricDao().countEnabled(dbSession, isCustom);
        JsonWriter json = response.newJsonWriter();
        json.beginObject();
        Set<String> desiredFields = desiredFields(request.paramAsStrings(Param.FIELDS));
        writeMetrics(json, metrics, desiredFields);
        searchOptions.writeJson(json, nbMetrics);
        json.endObject();
        json.close();
    }
}
Also used : DbSession(org.sonar.db.DbSession) MetricDto(org.sonar.db.metric.MetricDto) SearchOptions(org.sonar.server.es.SearchOptions) JsonWriter(org.sonar.api.utils.text.JsonWriter)

Example 40 with JsonWriter

use of org.sonar.api.utils.text.JsonWriter in project sonarqube by SonarSource.

the class TypesAction method handle.

@Override
public void handle(Request request, Response response) throws Exception {
    JsonWriter json = response.newJsonWriter();
    json.beginObject();
    json.name("types");
    json.beginArray();
    for (Metric.ValueType metricType : Metric.ValueType.values()) {
        json.value(metricType.name());
    }
    json.endArray();
    json.endObject();
    json.close();
}
Also used : Metric(org.sonar.api.measures.Metric) JsonWriter(org.sonar.api.utils.text.JsonWriter)

Aggregations

JsonWriter (org.sonar.api.utils.text.JsonWriter)69 DbSession (org.sonar.db.DbSession)24 ComponentDto (org.sonar.db.component.ComponentDto)12 UserDto (org.sonar.db.user.UserDto)6 MetricDto (org.sonar.db.metric.MetricDto)5 OrganizationDto (org.sonar.db.organization.OrganizationDto)5 SearchOptions (org.sonar.server.es.SearchOptions)5 StringWriter (java.io.StringWriter)4 QualityGateDto (org.sonar.db.qualitygate.QualityGateDto)4 OutputStreamWriter (java.io.OutputStreamWriter)3 Language (org.sonar.api.resources.Language)3 Paging (org.sonar.api.utils.Paging)2 PluginInfo (org.sonar.core.platform.PluginInfo)2 CustomMeasureDto (org.sonar.db.measure.custom.CustomMeasureDto)2 QualityProfileDto (org.sonar.db.qualityprofile.QualityProfileDto)2 BadRequestException (org.sonar.server.exceptions.BadRequestException)2 NotFoundException (org.sonar.server.exceptions.NotFoundException)2 DatabaseVersion (org.sonar.server.platform.db.migration.version.DatabaseVersion)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 ImmutableMap (com.google.common.collect.ImmutableMap)1