Functionality which didn't need to be moved to FileProjectManager as it wasn't file system specific has been moved back to ProjectManager.

Some additional source code documentation added.

git-svn-id: http://google-refine.googlecode.com/svn/trunk@991 7d457c2a-affb-35e4-300a-418c747d4874
This commit is contained in:
Iain Sproat 2010-06-18 19:16:30 +00:00
parent 367c86fdd4
commit c72b4571a5
2 changed files with 75 additions and 30 deletions

View File

@ -49,6 +49,11 @@ public abstract class ProjectManager {
return _interProjectModel;
}
/**
* Registers the project in the memory of the current session
* @param project
* @param projectMetadata
*/
public void registerProject(Project project, ProjectMetadata projectMetadata) {
synchronized (this) {
_projects.put(project.id, project);
@ -80,12 +85,21 @@ public abstract class ProjectManager {
*/
public abstract void exportProject(long projectId, TarOutputStream tos) throws IOException;
/**
*
* @param id
*/
public abstract void ensureProjectSaved(long id);
public ProjectMetadata getProjectMetadata(long id) {
return _projectsMetadata.get(id);
}
/**
*
* @param name
* @return
*/
public ProjectMetadata getProjectMetadata(String name) {
for (ProjectMetadata pm : _projectsMetadata.values()) {
if (pm.getName().equals(name)) {
@ -95,6 +109,13 @@ public abstract class ProjectManager {
return null;
}
/**
* Tries to find the project id when given a project name
* @param name
* The name of the project
* @return
* The id of the project, or -1 if it cannot be found
*/
public long getProjectID(String name) {
for (Entry<Long, ProjectMetadata> entry : _projectsMetadata.entrySet()) {
if (entry.getValue().getName().equals(name)) {
@ -109,8 +130,33 @@ public abstract class ProjectManager {
return _projectsMetadata;
}
public abstract Project getProject(long parseLong);
/**
* Gets the required project from the data store
* @param id
* the id of the project
* @return
* the project with the matching id, or null if it can't be found
*/
public Project getProject(long id) {
synchronized (this) {
if (_projects.containsKey(id)) {
return _projects.get(id);
} else {
Project project = loadProject(id);
_projects.put(id, project);
return project;
}
}
}
protected abstract Project loadProject(long id);
/**
* Sets the flag for long running operations
* @param busy
*/
public void setBusy(boolean busy) {
synchronized (this) {
if (busy) {
@ -120,7 +166,7 @@ public abstract class ProjectManager {
}
}
}
public PreferenceStore getPreferenceStore() {
return _preferenceStore;
}
@ -141,6 +187,10 @@ public abstract class ProjectManager {
*/
public abstract void save(boolean b);
/**
* Remove the project from the data store
* @param project
*/
public void deleteProject(Project project) {
deleteProject(project.id);
}
@ -149,14 +199,23 @@ public abstract class ProjectManager {
* Remove project from data store
* @param projectID
*/
public abstract void deleteProject(long projectID) ;
public abstract void deleteProject(long projectID);
protected void removeProject(long projectID){
if (_projectsMetadata.containsKey(projectID)) {
_projectsMetadata.remove(projectID);
}
if (_projects.containsKey(projectID)) {
_projects.remove(projectID);
}
}
/**
* The history entry manager deals with changes
* @return manager for handling history
*/
public abstract HistoryEntryManager getHistoryEntryManager();
static protected void preparePreferenceStore(PreferenceStore ps) {
ps.put("expressions", new TopList(s_expressionHistoryMax));
}

View File

@ -56,12 +56,12 @@ public class FileProjectManager extends ProjectManager {
_projectsMetadata = new HashMap<Long, ProjectMetadata>();
_preferenceStore = new PreferenceStore();
_projects = new HashMap<Long, Project>();
preparePreferenceStore(_preferenceStore);
load();
}
public File getWorkspaceDir() {
return _workspaceDir;
}
@ -209,18 +209,9 @@ public class FileProjectManager extends ProjectManager {
}
}
public Project getProject(long id) {
synchronized (this) {
if (_projects.containsKey(id)) {
return _projects.get(id);
} else {
Project project = ProjectUtilities.load(getProjectDir(id), id);
_projects.put(id, project);
return project;
}
}
@Override
public Project loadProject(long id) {
return ProjectUtilities.load(getProjectDir(id), id);
}
public void save(boolean allModified) {
@ -284,9 +275,9 @@ public class FileProjectManager extends ProjectManager {
jsonWriter.endArray();
writer.write('\n');
jsonWriter.key("preferences");
jsonWriter.key("preferences");
_preferenceStore.write(jsonWriter, new Properties());
jsonWriter.endObject();
} finally {
writer.close();
@ -373,12 +364,7 @@ public class FileProjectManager extends ProjectManager {
public void deleteProject(long projectID) {
synchronized (this) {
if (_projectsMetadata.containsKey(projectID)) {
_projectsMetadata.remove(projectID);
}
if (_projects.containsKey(projectID)) {
_projects.remove(projectID);
}
removeProject(projectID);
File dir = getProjectDir(projectID);
if (dir.exists()) {
@ -430,16 +416,16 @@ public class FileProjectManager extends ProjectManager {
_projectsMetadata.put(id, metadata);
}
if (obj.has("preferences") && !obj.isNull("preferences")) {
_preferenceStore.load(obj.getJSONObject("preferences"));
}
if (obj.has("expressions") && !obj.isNull("expressions")) {
((TopList) _preferenceStore.get("expressions"))
.load(obj.getJSONArray("expressions"));
}
found = true;
} catch (JSONException e) {
logger.warn("Error reading file", e);