Skip to content

Commit

Permalink
fix quality flaws
Browse files Browse the repository at this point in the history
  • Loading branch information
teryk committed Jun 26, 2015
1 parent 11e082f commit 3b86b3c
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 50 deletions.
Expand Up @@ -33,7 +33,6 @@
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;
Expand All @@ -42,6 +41,7 @@
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";
Expand Down Expand Up @@ -113,7 +113,7 @@ public void handle(Request request, Response response) throws Exception {
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);
Expand Down Expand Up @@ -163,26 +163,4 @@ private MetricDto searchMetric(DbSession dbSession, Request request) {

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;
}
}
Expand Up @@ -31,12 +31,11 @@
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";
Expand Down Expand Up @@ -76,7 +75,7 @@ public void handle(Request request, Response response) throws Exception {
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);

Expand All @@ -86,7 +85,7 @@ public void handle(Request request, Response response) throws Exception {
}
}

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();
Expand All @@ -96,26 +95,4 @@ private void writeResponse(JsonWriter json, List<MetricDto> metrics) {
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;
}
}
@@ -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;
}

}

0 comments on commit 3b86b3c

Please sign in to comment.