diff options
author | Teryk Bellahsene <teryk.bellahsene@sonarsource.com> | 2015-06-26 17:10:45 +0200 |
---|---|---|
committer | Teryk Bellahsene <teryk.bellahsene@sonarsource.com> | 2015-06-26 17:10:45 +0200 |
commit | 3b86b3cd173c9aeae215a80b7134d81a8c7a16c8 (patch) | |
tree | 728fe07df171f4d4510ee96588842c6cf0560d42 /server | |
parent | 11e082fab82637141908cef3801eb95ef8d13ba4 (diff) | |
download | sonarqube-3b86b3cd173c9aeae215a80b7134d81a8c7a16c8.tar.gz sonarqube-3b86b3cd173c9aeae215a80b7134d81a8c7a16c8.zip |
fix quality flaws
Diffstat (limited to 'server')
3 files changed, 63 insertions, 50 deletions
diff --git a/server/sonar-server/src/main/java/org/sonar/server/measure/custom/ws/CreateAction.java b/server/sonar-server/src/main/java/org/sonar/server/measure/custom/ws/CreateAction.java index 9ea34b0146c..e005fec722a 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/measure/custom/ws/CreateAction.java +++ b/server/sonar-server/src/main/java/org/sonar/server/measure/custom/ws/CreateAction.java @@ -33,7 +33,6 @@ import org.sonar.core.metric.db.MetricDto; import org.sonar.core.persistence.DbSession; import org.sonar.core.persistence.MyBatis; import org.sonar.server.db.DbClient; -import org.sonar.server.exceptions.NotFoundException; import org.sonar.server.exceptions.ServerException; import org.sonar.server.user.UserSession; import org.sonar.server.user.index.UserDoc; @@ -42,6 +41,7 @@ import org.sonar.server.user.index.UserIndex; import static com.google.common.base.Preconditions.checkArgument; import static org.sonar.server.measure.custom.ws.CustomMeasureValidator.checkPermissions; import static org.sonar.server.measure.custom.ws.CustomMeasureValueDescription.measureValueDescription; +import static org.sonar.server.measure.custom.ws.ProjectFinder.searchProject; public class CreateAction implements CustomMeasuresWsAction { public static final String ACTION = "create"; @@ -113,7 +113,7 @@ public class CreateAction implements CustomMeasuresWsAction { long now = system.now(); try { - ComponentDto component = searchProject(dbSession, request); + ComponentDto component = searchProject(dbSession, dbClient, request); MetricDto metric = searchMetric(dbSession, request); checkPermissions(userSession, component); checkIsProjectOrModule(component); @@ -163,26 +163,4 @@ public class CreateAction implements CustomMeasuresWsAction { return dbClient.metricDao().selectByKey(dbSession, metricKey); } - - private ComponentDto searchProject(DbSession dbSession, Request request) { - String projectUuid = request.param(PARAM_PROJECT_ID); - String projectKey = request.param(PARAM_PROJECT_KEY); - checkArgument(projectUuid != null ^ projectKey != null, "The project key or the project id must be provided, not both."); - - if (projectUuid != null) { - ComponentDto project = dbClient.componentDao().selectNullableByUuid(dbSession, projectUuid); - if (project == null) { - throw new NotFoundException(String.format("Project id '%s' not found", projectUuid)); - } - - return project; - } - - ComponentDto project = dbClient.componentDao().selectNullableByKey(dbSession, projectKey); - if (project == null) { - throw new NotFoundException(String.format("Project key '%s' not found", projectKey)); - } - - return project; - } } diff --git a/server/sonar-server/src/main/java/org/sonar/server/measure/custom/ws/MetricsAction.java b/server/sonar-server/src/main/java/org/sonar/server/measure/custom/ws/MetricsAction.java index 47e6424d1c4..ca379062add 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/measure/custom/ws/MetricsAction.java +++ b/server/sonar-server/src/main/java/org/sonar/server/measure/custom/ws/MetricsAction.java @@ -31,12 +31,11 @@ import org.sonar.core.metric.db.MetricDto; import org.sonar.core.persistence.DbSession; import org.sonar.core.persistence.MyBatis; import org.sonar.server.db.DbClient; -import org.sonar.server.exceptions.NotFoundException; import org.sonar.server.metric.ws.MetricJsonWriter; import org.sonar.server.user.UserSession; -import static com.google.common.base.Preconditions.checkArgument; import static org.sonar.server.measure.custom.ws.CustomMeasureValidator.checkPermissions; +import static org.sonar.server.measure.custom.ws.ProjectFinder.searchProject; public class MetricsAction implements CustomMeasuresWsAction { public static final String ACTION = "metrics"; @@ -76,7 +75,7 @@ public class MetricsAction implements CustomMeasuresWsAction { DbSession dbSession = dbClient.openSession(false); try { - ComponentDto project = searchProject(dbSession, request); + ComponentDto project = searchProject(dbSession, dbClient, request); checkPermissions(userSession, project); List<MetricDto> metrics = searchMetrics(dbSession, project); @@ -86,7 +85,7 @@ public class MetricsAction implements CustomMeasuresWsAction { } } - private void writeResponse(JsonWriter json, List<MetricDto> metrics) { + private static void writeResponse(JsonWriter json, List<MetricDto> metrics) { json.beginObject(); MetricJsonWriter.write(json, metrics, MetricJsonWriter.ALL_FIELDS); json.endObject(); @@ -96,26 +95,4 @@ public class MetricsAction implements CustomMeasuresWsAction { private List<MetricDto> searchMetrics(DbSession dbSession, ComponentDto project) { return dbClient.metricDao().selectAvailableCustomMetricsByComponentUuid(dbSession, project.uuid()); } - - private ComponentDto searchProject(DbSession dbSession, Request request) { - String projectUuid = request.param(PARAM_PROJECT_ID); - String projectKey = request.param(PARAM_PROJECT_KEY); - checkArgument(projectUuid != null ^ projectKey != null, "The project key or the project id must be provided, not both."); - - if (projectUuid != null) { - ComponentDto project = dbClient.componentDao().selectNullableByUuid(dbSession, projectUuid); - if (project == null) { - throw new NotFoundException(String.format("Project id '%s' not found", projectUuid)); - } - - return project; - } - - ComponentDto project = dbClient.componentDao().selectNullableByKey(dbSession, projectKey); - if (project == null) { - throw new NotFoundException(String.format("Project key '%s' not found", projectKey)); - } - - return project; - } } diff --git a/server/sonar-server/src/main/java/org/sonar/server/measure/custom/ws/ProjectFinder.java b/server/sonar-server/src/main/java/org/sonar/server/measure/custom/ws/ProjectFinder.java new file mode 100644 index 00000000000..1ea3320d235 --- /dev/null +++ b/server/sonar-server/src/main/java/org/sonar/server/measure/custom/ws/ProjectFinder.java @@ -0,0 +1,58 @@ +/* + * 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.measure.custom.ws; + +import org.sonar.api.server.ws.Request; +import org.sonar.core.component.ComponentDto; +import org.sonar.core.persistence.DbSession; +import org.sonar.server.db.DbClient; +import org.sonar.server.exceptions.NotFoundException; + +import static com.google.common.base.Preconditions.checkArgument; + +class ProjectFinder { + private ProjectFinder() { + // utility class + } + + static ComponentDto searchProject(DbSession dbSession, DbClient dbClient, Request request) { + String projectUuid = request.param(CreateAction.PARAM_PROJECT_ID); + String projectKey = request.param(CreateAction.PARAM_PROJECT_KEY); + checkArgument(projectUuid != null ^ projectKey != null, "The project key or the project id must be provided, not both."); + + if (projectUuid != null) { + ComponentDto project = dbClient.componentDao().selectNullableByUuid(dbSession, projectUuid); + if (project == null) { + throw new NotFoundException(String.format("Project id '%s' not found", projectUuid)); + } + + return project; + } + + ComponentDto project = dbClient.componentDao().selectNullableByKey(dbSession, projectKey); + if (project == null) { + throw new NotFoundException(String.format("Project key '%s' not found", projectKey)); + } + + return project; + } + +} |