ChildProjectResource: Use composition instead of inheritance

It was confusing that the project of the ChildProjectResource was
actually the parent project, due to using inheritance. This was
causing us to accidentally put the parent project name in the
ResourceNotFoundException when parsing a resource.

Change-Id: I94fe6522f1b11e81b677eb4cfda293ec991ff39f
This commit is contained in:
Dave Borowitz
2015-03-13 10:46:00 -07:00
parent 2ab016bf9a
commit b25a2d41da
2 changed files with 13 additions and 7 deletions

View File

@@ -15,28 +15,34 @@
package com.google.gerrit.server.project;
import com.google.common.collect.Iterables;
import com.google.gerrit.extensions.restapi.RestResource;
import com.google.gerrit.extensions.restapi.RestView;
import com.google.inject.TypeLiteral;
public class ChildProjectResource extends ProjectResource {
public class ChildProjectResource implements RestResource {
public static final TypeLiteral<RestView<ChildProjectResource>> CHILD_PROJECT_KIND =
new TypeLiteral<RestView<ChildProjectResource>>() {};
private final ProjectResource parent;
private final ProjectControl child;
public ChildProjectResource(ProjectResource project, ProjectControl child) {
super(project);
public ChildProjectResource(ProjectResource parent, ProjectControl child) {
this.parent = parent;
this.child = child;
}
public ProjectResource getParent() {
return parent;
}
public ProjectControl getChild() {
return child;
}
public boolean isDirectChild() {
ProjectState parent =
ProjectState firstParent =
Iterables.getFirst(child.getProjectState().parents(), null);
return parent != null
&& getNameKey().equals(parent.getProject().getNameKey());
return firstParent != null
&& parent.getNameKey().equals(firstParent.getProject().getNameKey());
}
}

View File

@@ -38,6 +38,6 @@ public class GetChildProject implements RestReadView<ChildProjectResource> {
if (recursive || rsrc.isDirectChild()) {
return json.format(rsrc.getChild().getProject());
}
throw new ResourceNotFoundException(rsrc.getName());
throw new ResourceNotFoundException(rsrc.getChild().getProject().getName());
}
}