Skip to content

Commit

Permalink
Fix quality flaws
Browse files Browse the repository at this point in the history
  • Loading branch information
julienlancelot committed Jul 15, 2015
1 parent ced0540 commit 2b803cf
Show file tree
Hide file tree
Showing 9 changed files with 13 additions and 26 deletions.
Expand Up @@ -37,7 +37,7 @@ public ComponentFinder(DbClient dbClient) {
this.dbClient = dbClient; this.dbClient = dbClient;
} }


public ComponentDto getByKeyOrUuid(DbSession dbSession, @Nullable String componentUuid, @Nullable String componentKey) { public ComponentDto getByUuidOrKey(DbSession dbSession, @Nullable String componentUuid, @Nullable String componentKey) {
checkArgument(componentUuid != null ^ componentKey != null, "The component key or the component id must be provided, not both."); checkArgument(componentUuid != null ^ componentKey != null, "The component key or the component id must be provided, not both.");


if (componentUuid != null) { if (componentUuid != null) {
Expand Down
Expand Up @@ -97,7 +97,6 @@ public ComponentDto getNonNullByUuid(String uuid) {
} }
} }


@CheckForNull
public Optional<ComponentDto> getByUuid(String uuid) { public Optional<ComponentDto> getByUuid(String uuid) {
DbSession session = dbClient.openSession(false); DbSession session = dbClient.openSession(false);
try { try {
Expand Down
Expand Up @@ -85,7 +85,7 @@ public void handle(Request request, Response response) {


DbSession session = dbClient.openSession(false); DbSession session = dbClient.openSession(false);
try { try {
ComponentDto component = componentFinder.getByKeyOrUuid(session, fileUuid, fileKey); ComponentDto component = componentFinder.getByUuidOrKey(session, fileUuid, fileKey);
String componentKey = component.key(); String componentKey = component.key();
userSession.checkComponentPermission(UserRole.CODEVIEWER, componentKey); userSession.checkComponentPermission(UserRole.CODEVIEWER, componentKey);
JsonWriter json = response.newJsonWriter().beginObject(); JsonWriter json = response.newJsonWriter().beginObject();
Expand Down
Expand Up @@ -115,7 +115,7 @@ public void handle(Request request, Response response) throws Exception {
long now = system.now(); long now = system.now();


try { try {
ComponentDto component = componentFinder.getByKeyOrUuid(dbSession, request.param(CreateAction.PARAM_PROJECT_ID), request.param(CreateAction.PARAM_PROJECT_KEY)); ComponentDto component = componentFinder.getByUuidOrKey(dbSession, request.param(CreateAction.PARAM_PROJECT_ID), request.param(CreateAction.PARAM_PROJECT_KEY));
MetricDto metric = searchMetric(dbSession, request); MetricDto metric = searchMetric(dbSession, request);
checkPermissions(userSession, component); checkPermissions(userSession, component);
checkIsProjectOrModule(component); checkIsProjectOrModule(component);
Expand Down
Expand Up @@ -77,7 +77,7 @@ public void handle(Request request, Response response) throws Exception {
DbSession dbSession = dbClient.openSession(false); DbSession dbSession = dbClient.openSession(false);


try { try {
ComponentDto project = componentFinder.getByKeyOrUuid(dbSession, request.param(CreateAction.PARAM_PROJECT_ID), request.param(CreateAction.PARAM_PROJECT_KEY)); ComponentDto project = componentFinder.getByUuidOrKey(dbSession, request.param(CreateAction.PARAM_PROJECT_ID), request.param(CreateAction.PARAM_PROJECT_KEY));
checkPermissions(userSession, project); checkPermissions(userSession, project);
List<MetricDto> metrics = searchMetrics(dbSession, project); List<MetricDto> metrics = searchMetrics(dbSession, project);


Expand Down
Expand Up @@ -100,7 +100,7 @@ public void handle(Request request, Response response) throws Exception {


DbSession dbSession = dbClient.openSession(false); DbSession dbSession = dbClient.openSession(false);
try { try {
ComponentDto project = componentFinder.getByKeyOrUuid(dbSession, projectUuid, projectKey); ComponentDto project = componentFinder.getByUuidOrKey(dbSession, projectUuid, projectKey);
checkPermissions(userSession, project); checkPermissions(userSession, project);
Long lastAnalysisDateMs = searchLastSnapshot(dbSession, project); Long lastAnalysisDateMs = searchLastSnapshot(dbSession, project);
List<CustomMeasureDto> customMeasures = searchCustomMeasures(dbSession, project, searchOptions); List<CustomMeasureDto> customMeasures = searchCustomMeasures(dbSession, project, searchOptions);
Expand Down
Expand Up @@ -20,7 +20,6 @@


package org.sonar.server.project.ws; package org.sonar.server.project.ws;


import com.google.common.base.Optional;
import java.util.Arrays; import java.util.Arrays;
import javax.annotation.Nullable; import javax.annotation.Nullable;
import org.sonar.api.server.ws.Request; import org.sonar.api.server.ws.Request;
Expand All @@ -33,7 +32,7 @@
import org.sonar.db.MyBatis; import org.sonar.db.MyBatis;
import org.sonar.db.component.ComponentDto; import org.sonar.db.component.ComponentDto;
import org.sonar.server.component.ComponentCleanerService; import org.sonar.server.component.ComponentCleanerService;
import org.sonar.server.exceptions.NotFoundException; import org.sonar.server.component.ComponentFinder;
import org.sonar.server.user.UserSession; import org.sonar.server.user.UserSession;


public class DeleteAction implements ProjectsWsAction { public class DeleteAction implements ProjectsWsAction {
Expand All @@ -43,11 +42,13 @@ public class DeleteAction implements ProjectsWsAction {
public static final String PARAM_KEY = "key"; public static final String PARAM_KEY = "key";


private final ComponentCleanerService componentCleanerService; private final ComponentCleanerService componentCleanerService;
private final ComponentFinder componentFinder;
private final DbClient dbClient; private final DbClient dbClient;
private final UserSession userSession; private final UserSession userSession;


public DeleteAction(ComponentCleanerService componentCleanerService, DbClient dbClient, UserSession userSession) { public DeleteAction(ComponentCleanerService componentCleanerService, ComponentFinder componentFinder, DbClient dbClient, UserSession userSession) {
this.componentCleanerService = componentCleanerService; this.componentCleanerService = componentCleanerService;
this.componentFinder = componentFinder;
this.dbClient = dbClient; this.dbClient = dbClient;
this.userSession = userSession; this.userSession = userSession;
} }
Expand Down Expand Up @@ -80,7 +81,7 @@ public void handle(Request request, Response response) throws Exception {


DbSession dbSession = dbClient.openSession(false); DbSession dbSession = dbClient.openSession(false);
try { try {
ComponentDto project = getProject(dbSession, uuid, key); ComponentDto project = componentFinder.getByUuidOrKey(dbSession, uuid, key);
componentCleanerService.delete(dbSession, Arrays.asList(project)); componentCleanerService.delete(dbSession, Arrays.asList(project));
} finally { } finally {
MyBatis.closeQuietly(dbSession); MyBatis.closeQuietly(dbSession);
Expand All @@ -103,20 +104,4 @@ private boolean missPermissionsBasedOnUuid(@Nullable String uuid) {
return uuid != null && !userSession.hasProjectPermissionByUuid(UserRole.ADMIN, uuid) && !userSession.hasGlobalPermission(GlobalPermissions.SYSTEM_ADMIN); return uuid != null && !userSession.hasProjectPermissionByUuid(UserRole.ADMIN, uuid) && !userSession.hasGlobalPermission(GlobalPermissions.SYSTEM_ADMIN);
} }


private ComponentDto getProject(DbSession session, @Nullable String uuid, @Nullable String key) {
if (key == null && uuid != null) {
Optional<ComponentDto> componentDto = dbClient.componentDao().selectByUuid(session, uuid);
if (!componentDto.isPresent()) {
throw new NotFoundException(String.format("Component with uuid '%s' not found", uuid));
}
return componentDto.get();
} else if (uuid == null && key != null) {
Optional<ComponentDto> componentDto = dbClient.componentDao().selectByKey(session, key);
if (!componentDto.isPresent()) {
throw new NotFoundException(String.format("Component with key '%s' not found", key));
}
return componentDto.get();
}
throw new IllegalArgumentException("Id or key must be provided");
}
} }
Expand Up @@ -124,6 +124,7 @@ public void setUp() {
new TestIndexer(dbClient, es.client()), new TestIndexer(dbClient, es.client()),
mockResourceTypes, mockResourceTypes,
new ComponentFinder(dbClient)), new ComponentFinder(dbClient)),
new ComponentFinder(dbClient),
dbClient, dbClient,
userSessionRule))); userSessionRule)));
userSessionRule.login("login").setGlobalPermissions(UserRole.ADMIN); userSessionRule.login("login").setGlobalPermissions(UserRole.ADMIN);
Expand Down
Expand Up @@ -28,11 +28,13 @@
@Deprecated @Deprecated
public class PropertyDto extends org.sonar.db.property.PropertyDto { public class PropertyDto extends org.sonar.db.property.PropertyDto {


@Override
public PropertyDto setKey(String key) { public PropertyDto setKey(String key) {
super.setKey(key); super.setKey(key);
return this; return this;
} }


@Override
public PropertyDto setValue(String value) { public PropertyDto setValue(String value) {
super.setValue(value); super.setValue(value);
return this; return this;
Expand Down

0 comments on commit 2b803cf

Please sign in to comment.