diff --git a/services/git-bridge/src/uk/ac/ic/wlgitbridge/application/WLGitBridgeServer.java b/services/git-bridge/src/uk/ac/ic/wlgitbridge/application/WLGitBridgeServer.java index e0b3354b5e..ba2c06ac5a 100644 --- a/services/git-bridge/src/uk/ac/ic/wlgitbridge/application/WLGitBridgeServer.java +++ b/services/git-bridge/src/uk/ac/ic/wlgitbridge/application/WLGitBridgeServer.java @@ -7,7 +7,7 @@ import org.eclipse.jetty.util.log.Log; import uk.ac.ic.wlgitbridge.application.jetty.NullLogger; import uk.ac.ic.wlgitbridge.git.WLGitServlet; import uk.ac.ic.wlgitbridge.git.exception.InvalidRootDirectoryPathException; -import uk.ac.ic.wlgitbridge.writelatex.api.DummySnapshotDBAPI; +import uk.ac.ic.wlgitbridge.writelatex.model.WLDataModel; import javax.servlet.ServletException; import java.net.BindException; @@ -65,7 +65,7 @@ public class WLGitBridgeServer { servletContextHandler.setContextPath("/"); servletContextHandler.addServlet( new ServletHolder( - new WLGitServlet(servletContextHandler, new DummySnapshotDBAPI(), rootGitDirectoryPath)), + new WLGitServlet(servletContextHandler, new WLDataModel(), rootGitDirectoryPath)), "/*" ); jettyServer.setHandler(servletContextHandler); diff --git a/services/git-bridge/src/uk/ac/ic/wlgitbridge/bridge/RepositorySource.java b/services/git-bridge/src/uk/ac/ic/wlgitbridge/bridge/RepositorySource.java index 039991fad9..b058339bf8 100644 --- a/services/git-bridge/src/uk/ac/ic/wlgitbridge/bridge/RepositorySource.java +++ b/services/git-bridge/src/uk/ac/ic/wlgitbridge/bridge/RepositorySource.java @@ -2,6 +2,7 @@ package uk.ac.ic.wlgitbridge.bridge; import org.eclipse.jgit.errors.RepositoryNotFoundException; import org.eclipse.jgit.lib.Repository; +import org.eclipse.jgit.transport.resolver.ServiceNotEnabledException; import java.io.File; @@ -10,6 +11,6 @@ import java.io.File; */ public interface RepositorySource { - public Repository getRepositoryWithNameAtRootDirectory(String name, File rootDirectory) throws RepositoryNotFoundException; + public Repository getRepositoryWithNameAtRootDirectory(String name, File rootDirectory) throws RepositoryNotFoundException, ServiceNotEnabledException; } diff --git a/services/git-bridge/src/uk/ac/ic/wlgitbridge/bridge/WLBridgedProject.java b/services/git-bridge/src/uk/ac/ic/wlgitbridge/bridge/WLBridgedProject.java index 9f14e1213a..1dbaf930f6 100644 --- a/services/git-bridge/src/uk/ac/ic/wlgitbridge/bridge/WLBridgedProject.java +++ b/services/git-bridge/src/uk/ac/ic/wlgitbridge/bridge/WLBridgedProject.java @@ -1,7 +1,11 @@ package uk.ac.ic.wlgitbridge.bridge; +import org.eclipse.jgit.api.AddCommand; +import org.eclipse.jgit.api.CommitCommand; +import org.eclipse.jgit.api.Git; import org.eclipse.jgit.errors.RepositoryNotFoundException; import org.eclipse.jgit.lib.Repository; +import org.eclipse.jgit.transport.resolver.ServiceNotEnabledException; import uk.ac.ic.wlgitbridge.writelatex.api.SnapshotDBAPI; import uk.ac.ic.wlgitbridge.writelatex.model.Snapshot; @@ -26,7 +30,7 @@ public class WLBridgedProject { this.snapshotDBAPI = snapshotDBAPI; } - public void buildRepository() throws RepositoryNotFoundException { + public void buildRepository() throws RepositoryNotFoundException, ServiceNotEnabledException { if (repository.getObjectDatabase().exists()) { updateRepositoryFromSnapshots(repository); } else { @@ -34,11 +38,26 @@ public class WLBridgedProject { } } - private void updateRepositoryFromSnapshots(Repository repository) { - List snapshotsToAdd = snapshotDBAPI.getSnapshotsToAddToRepository(repository); + private void updateRepositoryFromSnapshots(Repository repository) throws ServiceNotEnabledException { + try { + List snapshotsToAdd = snapshotDBAPI.getSnapshotsToAddToProject(name); + for (Snapshot snapshot : snapshotsToAdd) { + snapshot.getData().writeAll(repositoryDirectory.getAbsolutePath()); + Git git = new Git(repository); + AddCommand add = git.add(); + add.addFilepattern("."); + add.call(); + CommitCommand commit = git.commit(); + commit.setMessage("Commit"); + commit.call(); + } + } catch (Throwable throwable) { + throwable.printStackTrace(); + throw new ServiceNotEnabledException(); + } } - private void buildRepositoryFromScratch(Repository repository) throws RepositoryNotFoundException { + private void buildRepositoryFromScratch(Repository repository) throws RepositoryNotFoundException, ServiceNotEnabledException { if (!snapshotDBAPI.repositoryExists(name)) { throw new RepositoryNotFoundException(name); } diff --git a/services/git-bridge/src/uk/ac/ic/wlgitbridge/writelatex/SnapshotRepositoryBuilder.java b/services/git-bridge/src/uk/ac/ic/wlgitbridge/writelatex/SnapshotRepositoryBuilder.java index 80a862cda3..6eb90f5582 100644 --- a/services/git-bridge/src/uk/ac/ic/wlgitbridge/writelatex/SnapshotRepositoryBuilder.java +++ b/services/git-bridge/src/uk/ac/ic/wlgitbridge/writelatex/SnapshotRepositoryBuilder.java @@ -3,6 +3,7 @@ package uk.ac.ic.wlgitbridge.writelatex; import org.eclipse.jgit.errors.RepositoryNotFoundException; import org.eclipse.jgit.lib.Repository; import org.eclipse.jgit.storage.file.FileRepositoryBuilder; +import org.eclipse.jgit.transport.resolver.ServiceNotEnabledException; import uk.ac.ic.wlgitbridge.bridge.RepositorySource; import uk.ac.ic.wlgitbridge.bridge.WLBridgedProject; import uk.ac.ic.wlgitbridge.writelatex.api.SnapshotDBAPI; @@ -22,7 +23,7 @@ public class SnapshotRepositoryBuilder implements RepositorySource { } @Override - public Repository getRepositoryWithNameAtRootDirectory(String name, File rootDirectory) throws RepositoryNotFoundException { + public Repository getRepositoryWithNameAtRootDirectory(String name, File rootDirectory) throws RepositoryNotFoundException, ServiceNotEnabledException { File repositoryDirectory = new File(rootDirectory.getAbsolutePath(), name); Repository repository = null; diff --git a/services/git-bridge/src/uk/ac/ic/wlgitbridge/writelatex/api/DummySnapshotDBAPI.java b/services/git-bridge/src/uk/ac/ic/wlgitbridge/writelatex/api/DummySnapshotDBAPI.java deleted file mode 100644 index aac4f4834d..0000000000 --- a/services/git-bridge/src/uk/ac/ic/wlgitbridge/writelatex/api/DummySnapshotDBAPI.java +++ /dev/null @@ -1,37 +0,0 @@ -package uk.ac.ic.wlgitbridge.writelatex.api; - -import org.eclipse.jgit.lib.Repository; -import uk.ac.ic.wlgitbridge.writelatex.model.Snapshot; - -import java.util.HashMap; -import java.util.LinkedList; -import java.util.List; -import java.util.Map; - -/** - * Created by Winston on 03/11/14. - */ -public class DummySnapshotDBAPI implements SnapshotDBAPI { - - private Map> projects; - - public DummySnapshotDBAPI() { - projects = new HashMap>(); - - } - - private void initTestData() { - - } - - @Override - public boolean repositoryExists(String name) { - return projects.containsKey(name); - } - - @Override - public List getSnapshotsToAddToRepository(Repository repository) { - return new LinkedList(); - } - -} diff --git a/services/git-bridge/src/uk/ac/ic/wlgitbridge/writelatex/api/SnapshotDBAPI.java b/services/git-bridge/src/uk/ac/ic/wlgitbridge/writelatex/api/SnapshotDBAPI.java index 9b4fb3b94b..f508728752 100644 --- a/services/git-bridge/src/uk/ac/ic/wlgitbridge/writelatex/api/SnapshotDBAPI.java +++ b/services/git-bridge/src/uk/ac/ic/wlgitbridge/writelatex/api/SnapshotDBAPI.java @@ -11,6 +11,6 @@ import java.util.List; public interface SnapshotDBAPI { public boolean repositoryExists(String name); - public List getSnapshotsToAddToRepository(Repository repository); + public List getSnapshotsToAddToProject(String name) throws Throwable; } diff --git a/services/git-bridge/src/uk/ac/ic/wlgitbridge/writelatex/api/request/getdoc/SnapshotGetDocResult.java b/services/git-bridge/src/uk/ac/ic/wlgitbridge/writelatex/api/request/getdoc/SnapshotGetDocResult.java index fab3c816b0..f8dea2b946 100644 --- a/services/git-bridge/src/uk/ac/ic/wlgitbridge/writelatex/api/request/getdoc/SnapshotGetDocResult.java +++ b/services/git-bridge/src/uk/ac/ic/wlgitbridge/writelatex/api/request/getdoc/SnapshotGetDocResult.java @@ -19,4 +19,9 @@ public class SnapshotGetDocResult extends Result { public void fromJSON(JsonElement json) { versionID = json.getAsJsonObject().get("latestVerId").getAsInt(); } + + public int getVersionID() { + return versionID; + } + } diff --git a/services/git-bridge/src/uk/ac/ic/wlgitbridge/writelatex/api/request/getforversion/SnapshotData.java b/services/git-bridge/src/uk/ac/ic/wlgitbridge/writelatex/api/request/getforversion/SnapshotData.java index 5ad2e1bd25..7d51fb1713 100644 --- a/services/git-bridge/src/uk/ac/ic/wlgitbridge/writelatex/api/request/getforversion/SnapshotData.java +++ b/services/git-bridge/src/uk/ac/ic/wlgitbridge/writelatex/api/request/getforversion/SnapshotData.java @@ -7,6 +7,7 @@ import uk.ac.ic.wlgitbridge.writelatex.api.request.base.JSONSource; import java.io.IOException; import java.util.LinkedList; import java.util.List; +import java.util.concurrent.ExecutionException; /** * Created by Winston on 06/11/14. @@ -42,6 +43,16 @@ public class SnapshotData implements JSONSource { for (JsonElement json : jsonArray) { atts.add(new WLAttachment(json)); } + System.out.println(atts); + } + + public void writeAll(String repoDir) throws InterruptedException, ExecutionException, IOException { + for (WLFile src : srcs) { + src.writeToDisk(repoDir); + } + for (WLFile att : atts) { + att.writeToDisk(repoDir); + } } } diff --git a/services/git-bridge/src/uk/ac/ic/wlgitbridge/writelatex/api/request/getforversion/SnapshotGetForVersionRequest.java b/services/git-bridge/src/uk/ac/ic/wlgitbridge/writelatex/api/request/getforversion/SnapshotGetForVersionRequest.java index f4af37210d..9ac7eb0ea8 100644 --- a/services/git-bridge/src/uk/ac/ic/wlgitbridge/writelatex/api/request/getforversion/SnapshotGetForVersionRequest.java +++ b/services/git-bridge/src/uk/ac/ic/wlgitbridge/writelatex/api/request/getforversion/SnapshotGetForVersionRequest.java @@ -10,8 +10,11 @@ public class SnapshotGetForVersionRequest extends SnapshotAPIRequest(); + for (JsonElement elem : json.getAsJsonArray()) { + savedVers.add(new Gson().fromJson(elem.getAsJsonObject(), SnapshotInfo.class)); + } } public List getSavedVers() { diff --git a/services/git-bridge/src/uk/ac/ic/wlgitbridge/writelatex/model/Snapshot.java b/services/git-bridge/src/uk/ac/ic/wlgitbridge/writelatex/model/Snapshot.java index b991f0d4e1..f7c027512b 100644 --- a/services/git-bridge/src/uk/ac/ic/wlgitbridge/writelatex/model/Snapshot.java +++ b/services/git-bridge/src/uk/ac/ic/wlgitbridge/writelatex/model/Snapshot.java @@ -1,6 +1,8 @@ package uk.ac.ic.wlgitbridge.writelatex.model; import com.google.gson.JsonElement; +import uk.ac.ic.wlgitbridge.writelatex.api.request.getforversion.SnapshotData; +import uk.ac.ic.wlgitbridge.writelatex.api.request.getsavedvers.WLUser; /** * Created by Winston on 03/11/14. @@ -8,9 +10,14 @@ import com.google.gson.JsonElement; public class Snapshot implements JSONModel { private int versionID; + private String comment; + private WLUser user; + private SnapshotData data; - public Snapshot() { + public Snapshot(int versionID, SnapshotData data) { + this.comment = comment; + this.data = data; } @Override @@ -18,4 +25,15 @@ public class Snapshot implements JSONModel { } + public void writeToDisk() { + + } + + public SnapshotData getData() { + return data; + } + + public String getComment() { + return comment; + } } diff --git a/services/git-bridge/src/uk/ac/ic/wlgitbridge/writelatex/model/WLDataModel.java b/services/git-bridge/src/uk/ac/ic/wlgitbridge/writelatex/model/WLDataModel.java index 8a1d827880..c59f700707 100644 --- a/services/git-bridge/src/uk/ac/ic/wlgitbridge/writelatex/model/WLDataModel.java +++ b/services/git-bridge/src/uk/ac/ic/wlgitbridge/writelatex/model/WLDataModel.java @@ -1,13 +1,18 @@ package uk.ac.ic.wlgitbridge.writelatex.model; +import org.eclipse.jgit.lib.Repository; +import uk.ac.ic.wlgitbridge.writelatex.api.SnapshotDBAPI; + import java.io.IOException; +import java.util.HashMap; +import java.util.List; import java.util.Map; import java.util.concurrent.ExecutionException; /** * Created by Winston on 06/11/14. */ -public class WLDataModel { +public class WLDataModel implements SnapshotDBAPI { private final Map projects; @@ -15,8 +20,32 @@ public class WLDataModel { this.projects = projects; } - public void updateProjectWithName(String name) throws InterruptedException, ExecutionException, IOException { + public WLDataModel() { + projects = new HashMap(); + } + + public void updateProjectWithName(String name) throws Throwable { + if (!projects.containsKey(name)) { + projects.put(name, new WLProject(name)); + } + System.out.println(projects); + System.out.println(projects.get(name)); + System.out.println(name); projects.get(name).update(); } + @Override + public boolean repositoryExists(String name) { + if (!projects.containsKey(name)) { + projects.put(name, new WLProject(name)); + } + return projects.containsKey(name); + } + + @Override + public List getSnapshotsToAddToProject(String name) throws Throwable { + updateProjectWithName(name); + return projects.get(name).getSnapshotsToAdd(); + } + } diff --git a/services/git-bridge/src/uk/ac/ic/wlgitbridge/writelatex/model/WLProject.java b/services/git-bridge/src/uk/ac/ic/wlgitbridge/writelatex/model/WLProject.java index 1113cdd1d6..3114a74d36 100644 --- a/services/git-bridge/src/uk/ac/ic/wlgitbridge/writelatex/model/WLProject.java +++ b/services/git-bridge/src/uk/ac/ic/wlgitbridge/writelatex/model/WLProject.java @@ -3,8 +3,12 @@ package uk.ac.ic.wlgitbridge.writelatex.model; import com.google.gson.JsonElement; import uk.ac.ic.wlgitbridge.writelatex.api.request.base.Request; import uk.ac.ic.wlgitbridge.writelatex.api.request.getdoc.SnapshotGetDocRequest; +import uk.ac.ic.wlgitbridge.writelatex.api.request.getdoc.SnapshotGetDocResult; +import uk.ac.ic.wlgitbridge.writelatex.api.request.getforversion.SnapshotData; import uk.ac.ic.wlgitbridge.writelatex.api.request.getforversion.SnapshotGetForVersionRequest; +import uk.ac.ic.wlgitbridge.writelatex.api.request.getforversion.SnapshotGetForVersionResult; import uk.ac.ic.wlgitbridge.writelatex.api.request.getsavedvers.SnapshotGetSavedVersRequest; +import uk.ac.ic.wlgitbridge.writelatex.api.request.getsavedvers.SnapshotInfo; import java.io.IOException; import java.util.*; @@ -20,6 +24,9 @@ public class WLProject implements JSONModel { private final Map snapshots; private final SortedSet versions; private int latestVersionID; + private List snapshotsToAdd; + private SortedSet idsToUpdate; + private HashMap msg; public WLProject(String name) { this.name = name; @@ -33,18 +40,25 @@ public class WLProject implements JSONModel { } - public void update() throws InterruptedException, ExecutionException, IOException { + public void update() throws Throwable { getNew(); } - private boolean getNew() throws InterruptedException, ExecutionException, IOException { - Request getDoc = new SnapshotGetDocRequest(name); - Request getSavedVers = new SnapshotGetSavedVersRequest(name); + private boolean getNew() throws Throwable { + SnapshotGetDocRequest getDoc = new SnapshotGetDocRequest(name); + SnapshotGetSavedVersRequest getSavedVers = new SnapshotGetSavedVersRequest(name); getDoc.request(); getSavedVers.request(); - List ids = new LinkedList(); + List fetchedIDs = new LinkedList(); + fetchedIDs.add(getDoc.getResult().getVersionID()); + + for (SnapshotInfo snapshotInfo : getSavedVers.getResult().getSavedVers()) { + msg = new HashMap(); + msg.put(snapshotInfo.getVersionId(), snapshotInfo); + fetchedIDs.add(snapshotInfo.getVersionId()); + } boolean result = false; @@ -52,10 +66,10 @@ public class WLProject implements JSONModel { // ids.addAll(getLatestVersionIDs(getSavedVers.getResult())); - List idsToUpdate = new LinkedList(); + idsToUpdate = new TreeSet(); boolean hasNew = false; - for (Integer id : ids) { + for (Integer id : fetchedIDs) { boolean contains = versions.contains(id); result = result || contains; if (!contains) { @@ -68,14 +82,27 @@ public class WLProject implements JSONModel { return result; } - private void updateIDs(List idsToUpdate) { - List requests = new LinkedList(); + private void updateIDs(SortedSet idsToUpdate) throws Throwable { + List requests = new LinkedList(); for (int id : idsToUpdate) { SnapshotGetForVersionRequest request = new SnapshotGetForVersionRequest(name, id); requests.add(request); request.request(); } - + for (SnapshotGetForVersionRequest request : requests) { + SnapshotGetForVersionResult result = request.getResult(); + SnapshotData data = result.getSnapshotData(); + Snapshot snapshot = new Snapshot(request.getVersionID(), data); + snapshots.put(request.getVersionID(), snapshot); + } + snapshotsToAdd = new LinkedList(); + for (int id : idsToUpdate) { + snapshotsToAdd.add(snapshots.get(id)); + } + } + + public List getSnapshotsToAdd() { + return snapshotsToAdd; } } diff --git a/services/git-bridge/test/uk/ac/ic/wlgitbridge/test/writelatex/api/SnapshotGetForVersionRequestTests.java b/services/git-bridge/test/uk/ac/ic/wlgitbridge/test/writelatex/api/SnapshotGetForVersionRequestTests.java deleted file mode 100644 index c35717c7e5..0000000000 --- a/services/git-bridge/test/uk/ac/ic/wlgitbridge/test/writelatex/api/SnapshotGetForVersionRequestTests.java +++ /dev/null @@ -1,15 +0,0 @@ -package uk.ac.ic.wlgitbridge.test.writelatex.api; - -import org.junit.Test; - -/** - * Created by Winston on 06/11/14. - */ -public class SnapshotGetForVersionRequestTests { - - @Test - public void nothingToTest() { - - } - -} diff --git a/services/git-bridge/test/uk/ac/ic/wlgitbridge/test/writelatex/api/SnapshotAPIRequestTests.java b/services/git-bridge/test/uk/ac/ic/wlgitbridge/test/writelatex/api/request/base/SnapshotAPIRequestTests.java similarity index 72% rename from services/git-bridge/test/uk/ac/ic/wlgitbridge/test/writelatex/api/SnapshotAPIRequestTests.java rename to services/git-bridge/test/uk/ac/ic/wlgitbridge/test/writelatex/api/request/base/SnapshotAPIRequestTests.java index f6c74b9fcc..3e696479f3 100644 --- a/services/git-bridge/test/uk/ac/ic/wlgitbridge/test/writelatex/api/SnapshotAPIRequestTests.java +++ b/services/git-bridge/test/uk/ac/ic/wlgitbridge/test/writelatex/api/request/base/SnapshotAPIRequestTests.java @@ -1,4 +1,4 @@ -package uk.ac.ic.wlgitbridge.test.writelatex.api; +package uk.ac.ic.wlgitbridge.test.writelatex.api.request.base; import org.junit.Test; diff --git a/services/git-bridge/test/uk/ac/ic/wlgitbridge/test/writelatex/api/SnapshotGetDocRequestTests.java b/services/git-bridge/test/uk/ac/ic/wlgitbridge/test/writelatex/api/request/getdoc/SnapshotGetDocRequestTests.java similarity index 71% rename from services/git-bridge/test/uk/ac/ic/wlgitbridge/test/writelatex/api/SnapshotGetDocRequestTests.java rename to services/git-bridge/test/uk/ac/ic/wlgitbridge/test/writelatex/api/request/getdoc/SnapshotGetDocRequestTests.java index 1180efea74..3e9815c3a1 100644 --- a/services/git-bridge/test/uk/ac/ic/wlgitbridge/test/writelatex/api/SnapshotGetDocRequestTests.java +++ b/services/git-bridge/test/uk/ac/ic/wlgitbridge/test/writelatex/api/request/getdoc/SnapshotGetDocRequestTests.java @@ -1,4 +1,4 @@ -package uk.ac.ic.wlgitbridge.test.writelatex.api; +package uk.ac.ic.wlgitbridge.test.writelatex.api.request.getdoc; import org.junit.Test; diff --git a/services/git-bridge/test/uk/ac/ic/wlgitbridge/test/writelatex/api/request/getforversion/SnapshotGetForVersionRequestTests.java b/services/git-bridge/test/uk/ac/ic/wlgitbridge/test/writelatex/api/request/getforversion/SnapshotGetForVersionRequestTests.java new file mode 100644 index 0000000000..ca399cca1c --- /dev/null +++ b/services/git-bridge/test/uk/ac/ic/wlgitbridge/test/writelatex/api/request/getforversion/SnapshotGetForVersionRequestTests.java @@ -0,0 +1,25 @@ +package uk.ac.ic.wlgitbridge.test.writelatex.api.request.getforversion; + +import org.junit.Test; +import uk.ac.ic.wlgitbridge.writelatex.api.request.getforversion.SnapshotGetForVersionRequest; +import uk.ac.ic.wlgitbridge.writelatex.api.request.getforversion.SnapshotGetForVersionResult; + +/** + * Created by Winston on 06/11/14. + */ +public class SnapshotGetForVersionRequestTests { + + @Test + public void nothingToTest() { + SnapshotGetForVersionRequest request = new SnapshotGetForVersionRequest("1826rqgsdb", 76); + request.request(); + try { + SnapshotGetForVersionResult result = request.getResult(); + } catch (Throwable throwable) { + throwable.printStackTrace(); + } + + + } + +} diff --git a/services/git-bridge/test/uk/ac/ic/wlgitbridge/test/writelatex/model/WLProjectTests.java b/services/git-bridge/test/uk/ac/ic/wlgitbridge/test/writelatex/model/WLProjectTests.java index 87ba771d33..5105462b8d 100644 --- a/services/git-bridge/test/uk/ac/ic/wlgitbridge/test/writelatex/model/WLProjectTests.java +++ b/services/git-bridge/test/uk/ac/ic/wlgitbridge/test/writelatex/model/WLProjectTests.java @@ -22,6 +22,8 @@ public class WLProjectTests { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); + } catch (Throwable throwable) { + throwable.printStackTrace(); } }