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; return _interProjectModel;
} }
/**
* Registers the project in the memory of the current session
* @param project
* @param projectMetadata
*/
public void registerProject(Project project, ProjectMetadata projectMetadata) { public void registerProject(Project project, ProjectMetadata projectMetadata) {
synchronized (this) { synchronized (this) {
_projects.put(project.id, project); _projects.put(project.id, project);
@ -80,12 +85,21 @@ public abstract class ProjectManager {
*/ */
public abstract void exportProject(long projectId, TarOutputStream tos) throws IOException; public abstract void exportProject(long projectId, TarOutputStream tos) throws IOException;
/**
*
* @param id
*/
public abstract void ensureProjectSaved(long id); public abstract void ensureProjectSaved(long id);
public ProjectMetadata getProjectMetadata(long id) { public ProjectMetadata getProjectMetadata(long id) {
return _projectsMetadata.get(id); return _projectsMetadata.get(id);
} }
/**
*
* @param name
* @return
*/
public ProjectMetadata getProjectMetadata(String name) { public ProjectMetadata getProjectMetadata(String name) {
for (ProjectMetadata pm : _projectsMetadata.values()) { for (ProjectMetadata pm : _projectsMetadata.values()) {
if (pm.getName().equals(name)) { if (pm.getName().equals(name)) {
@ -95,6 +109,13 @@ public abstract class ProjectManager {
return null; 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) { public long getProjectID(String name) {
for (Entry<Long, ProjectMetadata> entry : _projectsMetadata.entrySet()) { for (Entry<Long, ProjectMetadata> entry : _projectsMetadata.entrySet()) {
if (entry.getValue().getName().equals(name)) { if (entry.getValue().getName().equals(name)) {
@ -109,8 +130,33 @@ public abstract class ProjectManager {
return _projectsMetadata; 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) { public void setBusy(boolean busy) {
synchronized (this) { synchronized (this) {
if (busy) { if (busy) {
@ -120,7 +166,7 @@ public abstract class ProjectManager {
} }
} }
} }
public PreferenceStore getPreferenceStore() { public PreferenceStore getPreferenceStore() {
return _preferenceStore; return _preferenceStore;
} }
@ -141,6 +187,10 @@ public abstract class ProjectManager {
*/ */
public abstract void save(boolean b); public abstract void save(boolean b);
/**
* Remove the project from the data store
* @param project
*/
public void deleteProject(Project project) { public void deleteProject(Project project) {
deleteProject(project.id); deleteProject(project.id);
} }
@ -149,14 +199,23 @@ public abstract class ProjectManager {
* Remove project from data store * Remove project from data store
* @param projectID * @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 * The history entry manager deals with changes
* @return manager for handling history * @return manager for handling history
*/ */
public abstract HistoryEntryManager getHistoryEntryManager(); public abstract HistoryEntryManager getHistoryEntryManager();
static protected void preparePreferenceStore(PreferenceStore ps) { static protected void preparePreferenceStore(PreferenceStore ps) {
ps.put("expressions", new TopList(s_expressionHistoryMax)); ps.put("expressions", new TopList(s_expressionHistoryMax));
} }

View File

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