Refactor: Have only one implementation to resolve parent projects

Often the parent project for a project needs to be resolved. The
parent project can be retrieved from a project by invoking
Project#getParent(). The problem is that this method may return null
for child projects of the wild project. This special case has to be
handled by every caller (if getParent() returns null and it's not the
wild project, the wild project is the parent). Similar code to handle
this situation is repeated in several places.

This change makes one implementation to resolve the parent project
reusable and removes duplicate code.

Change-Id: Id3d8f19c0ea6fc168c2f5dbc15f9e21bc2246d94
Signed-off-by: Edwin Kempin <edwin.kempin@sap.com>
This commit is contained in:
Edwin Kempin
2011-12-28 14:36:06 +01:00
parent 17abbef1b7
commit 128381f984
5 changed files with 32 additions and 41 deletions

View File

@@ -184,17 +184,12 @@ class ProjectAccessFactory extends Handler<ProjectAccess> {
detail.setRevision(config.getRevision().name()); detail.setRevision(config.getRevision().name());
} }
detail.setInheritsFrom(config.getProject().getParent(allProjectsName));
if (projectName.equals(allProjectsName)) { if (projectName.equals(allProjectsName)) {
if (pc.isOwner()) { if (pc.isOwner()) {
ownerOf.add(AccessSection.GLOBAL_CAPABILITIES); ownerOf.add(AccessSection.GLOBAL_CAPABILITIES);
} }
detail.setInheritsFrom(null);
} else if (config.getProject().getParent() != null) {
detail.setInheritsFrom(config.getProject().getParent());
} else {
detail.setInheritsFrom(allProjectsName);
} }
detail.setLocal(local); detail.setLocal(local);

View File

@@ -184,10 +184,36 @@ public final class Project {
state = update.state; state = update.state;
} }
/**
* Returns the name key of the parent project.
*
* @return name key of the parent project, <code>null</code> if this project
* is the wild project, <code>null</code> or the name key of the wild
* project if this project is a direct child of the wild project
*/
public Project.NameKey getParent() { public Project.NameKey getParent() {
return parent; return parent;
} }
/**
* Returns the name key of the parent project.
*
* @param allProjectsName name key of the wild project
* @return name key of the parent project, <code>null</code> if this project
* is the wild project
*/
public Project.NameKey getParent(final Project.NameKey allProjectsName) {
if (parent != null) {
return parent;
}
if (name.equals(allProjectsName)) {
return null;
}
return allProjectsName;
}
public String getParentName() { public String getParentName() {
return parent != null ? parent.get() : null; return parent != null ? parent.get() : null;
} }

View File

@@ -269,10 +269,6 @@ public class ProjectState {
if (isAllProjects) { if (isAllProjects) {
return null; return null;
} }
Project.NameKey parentName = getProject().getParent(); return projectCache.get(getProject().getParent(allProjectsName));
if (parentName == null) {
parentName = allProjectsName;
}
return projectCache.get(parentName);
} }
} }

View File

@@ -224,7 +224,7 @@ final class AdminSetParent extends BaseCommand {
// If we can't get it from the cache, pretend it's not present. // If we can't get it from the cache, pretend it's not present.
break; break;
} }
p = getParentName(e.getProject()); p = e.getProject().getParent(allProjectsName);
} }
return parents; return parents;
} }
@@ -238,28 +238,10 @@ final class AdminSetParent extends BaseCommand {
continue; continue;
} }
if (parentName.equals(getParentName(e.getProject()))) { if (parentName.equals(e.getProject().getParent(projectName))) {
childProjects.add(e.getProject()); childProjects.add(e.getProject());
} }
} }
return childProjects; return childProjects;
} }
/**
* Returns the project parent name.
*
* @return Project parent name, <code>null</code> for the 'All-Projects' root
* project
*/
private Project.NameKey getParentName(final Project project) {
if (project.getParent() != null) {
return project.getParent();
}
if (project.getNameKey().equals(allProjectsName)) {
return null;
}
return allProjectsName;
}
} }

View File

@@ -51,15 +51,7 @@ public class ProjectNode implements TreeNode, Comparable<ProjectNode> {
* project * project
*/ */
public Project.NameKey getParentName() { public Project.NameKey getParentName() {
if (project.getParent() != null) { return project.getParent(allProjectsName);
return project.getParent();
}
if (project.getNameKey().equals(allProjectsName)) {
return null;
}
return allProjectsName;
} }
public boolean isAllProjects() { public boolean isAllProjects() {