Loading WLProjectStore from database.

This commit is contained in:
Winston Li 2014-11-19 19:01:18 +00:00
parent 7d47514810
commit f103c7e3b5
4 changed files with 33 additions and 16 deletions

View file

@ -3,6 +3,8 @@ package uk.ac.ic.wlgitbridge.writelatex.model;
import uk.ac.ic.wlgitbridge.writelatex.SnapshotFetcher;
import uk.ac.ic.wlgitbridge.writelatex.api.request.exception.FailedConnectionException;
import uk.ac.ic.wlgitbridge.writelatex.api.request.getdoc.exception.InvalidProjectException;
import uk.ac.ic.wlgitbridge.writelatex.model.db.PersistentStoreAPI;
import uk.ac.ic.wlgitbridge.writelatex.model.db.PersistentStoreSource;
import java.util.HashMap;
import java.util.Map;
@ -11,7 +13,7 @@ import java.util.SortedSet;
/**
* Created by Winston on 06/11/14.
*/
public class WLProject {
public class WLProject implements PersistentStoreSource {
private final String name;
private final Map<Integer, Snapshot> snapshots;
@ -25,6 +27,11 @@ public class WLProject {
snapshotFetcher = new SnapshotFetcher(name, snapshots);
}
public WLProject(String projectName, PersistentStoreAPI database) {
this(projectName);
initFromPersistentStore(database);
}
public SortedSet<Snapshot> fetchNewSnapshots() throws FailedConnectionException, InvalidProjectException {
SortedSet<Snapshot> newSnapshots = snapshotFetcher.fetchNewSnapshots();
latestSnapshotID = snapshotFetcher.getLatestSnapshot().getVersionID();
@ -45,4 +52,8 @@ public class WLProject {
latestSnapshotID = versionID;
}
@Override
public void initFromPersistentStore(PersistentStoreAPI persistentStore) {
}
}

View file

@ -1,7 +1,7 @@
package uk.ac.ic.wlgitbridge.writelatex.model;
import uk.ac.ic.wlgitbridge.writelatex.model.db.PersistentStoreAPI;
import uk.ac.ic.wlgitbridge.writelatex.model.db.WLDatabaseSource;
import uk.ac.ic.wlgitbridge.writelatex.model.db.PersistentStoreSource;
import java.util.ArrayList;
import java.util.HashMap;
@ -11,8 +11,9 @@ import java.util.Map;
/**
* Created by Winston on 17/11/14.
*/
public class WLProjectStore implements WLDatabaseSource {
public class WLProjectStore implements PersistentStoreSource {
private PersistentStoreAPI persistentStore;
private final Map<String, WLProject> projects;
public WLProjectStore() {
@ -21,7 +22,7 @@ public class WLProjectStore implements WLDatabaseSource {
public WLProjectStore(PersistentStoreAPI persistentStore) {
this();
initFromDatabase(persistentStore);
initFromPersistentStore(persistentStore);
}
public WLProject getProjectWithName(String name) {
@ -31,6 +32,7 @@ public class WLProjectStore implements WLDatabaseSource {
} else {
project = new WLProject(name);
projects.put(name, project);
persistentStore.addProject(name);
}
return project;
}
@ -40,7 +42,11 @@ public class WLProjectStore implements WLDatabaseSource {
}
@Override
public void initFromDatabase(PersistentStoreAPI database) {
public void initFromPersistentStore(PersistentStoreAPI persistentStore) {
this.persistentStore = persistentStore;
for (String projectName : persistentStore.getProjectNames()) {
projects.put(projectName, new WLProject(projectName, persistentStore));
}
}
}

View file

@ -0,0 +1,10 @@
package uk.ac.ic.wlgitbridge.writelatex.model.db;
/**
* Created by Winston on 19/11/14.
*/
public interface PersistentStoreSource {
public void initFromPersistentStore(PersistentStoreAPI persistentStore);
}

View file

@ -1,10 +0,0 @@
package uk.ac.ic.wlgitbridge.writelatex.model.db;
/**
* Created by Winston on 19/11/14.
*/
public interface WLDatabaseSource {
public void initFromDatabase(PersistentStoreAPI database);
}