diff --git a/services/git-bridge/.idea/workspace.xml b/services/git-bridge/.idea/workspace.xml index 83b5ff2043..8e5229f326 100644 --- a/services/git-bridge/.idea/workspace.xml +++ b/services/git-bridge/.idea/workspace.xml @@ -7,20 +7,10 @@ - - - - - - - - - - - - + + + - @@ -43,21 +33,11 @@ - - + + - - - - - - - - - - - - + + @@ -66,28 +46,38 @@ - - + + - - + + - - + + - - + + - - + + + + + + + + + + + + @@ -97,7 +87,7 @@ - + @@ -113,18 +103,8 @@ - - - - - - - - - - - - + + @@ -133,8 +113,18 @@ - - + + + + + + + + + + + + @@ -156,8 +146,6 @@ @@ -241,8 +231,6 @@ - - @@ -923,6 +911,8 @@ + + @@ -968,7 +958,7 @@ - + + + + + + + + + + + + + + + + + + + + + + - - - - + + + - - + + + + @@ -1198,7 +1236,6 @@ - @@ -1215,9 +1252,10 @@ - + + @@ -1371,14 +1409,6 @@ - - - - - - - - @@ -1495,22 +1525,6 @@ - - - - - - - - - - - - - - - - @@ -1527,22 +1541,6 @@ - - - - - - - - - - - - - - - - @@ -1615,18 +1613,18 @@ - + - - + + - + - - + + @@ -1634,7 +1632,7 @@ - + @@ -1647,14 +1645,54 @@ + + + + + + + + - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 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 a2db04c118..8b3ae32889 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 @@ -2,12 +2,14 @@ package uk.ac.ic.wlgitbridge.writelatex.model; import com.google.gson.JsonElement; +import java.io.IOException; import java.util.Map; +import java.util.concurrent.ExecutionException; /** * Created by Winston on 06/11/14. */ -public class WLDataModel implements JSONModel { +public class WLDataModel { private final Map projects; @@ -15,8 +17,8 @@ public class WLDataModel implements JSONModel { this.projects = projects; } - @Override - public void updateFromJSON(JsonElement json) { - + public void updateProjectWithName(String name) throws InterruptedException, ExecutionException, IOException { + projects.get(name).update(); } + } 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 6371a3b778..142719b08c 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 @@ -1,21 +1,34 @@ package uk.ac.ic.wlgitbridge.writelatex.model; +import com.google.gson.Gson; +import com.google.gson.JsonArray; import com.google.gson.JsonElement; +import com.google.gson.JsonObject; +import com.sun.javafx.collections.transformation.SortedList; +import uk.ac.ic.wlgitbridge.writelatex.api.request.Request; +import uk.ac.ic.wlgitbridge.writelatex.api.request.SnapshotGetDocRequest; +import uk.ac.ic.wlgitbridge.writelatex.api.request.SnapshotGetForVersionRequest; +import uk.ac.ic.wlgitbridge.writelatex.api.request.SnapshotGetSavedVersRequest; -import java.util.HashMap; -import java.util.Map; +import java.io.IOException; +import java.util.*; +import java.util.concurrent.ExecutionException; /** * Created by Winston on 06/11/14. */ public class WLProject implements JSONModel { + private final String name; public static final int VERSION_ID_INVALID = -1; private final Map snapshots; + private final SortedSet versions; private int latestVersionID; - public WLProject() { + public WLProject(String name) { + this.name = name; snapshots = new HashMap(); + versions = new TreeSet(); latestVersionID = VERSION_ID_INVALID; } @@ -24,4 +37,69 @@ public class WLProject implements JSONModel { } + public void update() throws InterruptedException, ExecutionException, IOException { + getNew(); + } + + private boolean getNew() throws InterruptedException, ExecutionException, IOException { + Request getDoc = new SnapshotGetDocRequest(name); + Request getSavedVers = new SnapshotGetSavedVersRequest(name); + + getDoc.request(); + getSavedVers.request(); + + List ids = new LinkedList(); + + boolean result = false; + + ids.add(getLatestVersionID(getDoc.getResponse())); + + ids.addAll(getLatestVersionIDs(getSavedVers.getResponse())); + + List idsToUpdate = new LinkedList(); + + boolean hasNew = false; + for (Integer id : ids) { + boolean contains = versions.contains(id); + result = result || contains; + if (!contains) { + idsToUpdate.add(id); + } + } + + updateIDs(idsToUpdate); + + return result; + } + + private void updateIDs(List idsToUpdate) { + List requests = new LinkedList(); + for (int id : idsToUpdate) { + SnapshotGetForVersionRequest request = new SnapshotGetForVersionRequest(name, id); + requests.add(request); + request.request(); + } + + } + + private int getLatestVersionID(String response) { + Gson gson = new Gson(); + JsonObject responseObject = gson.fromJson(response, JsonObject.class); + return responseObject.get("latestVerId").getAsInt(); + } + + private Collection getLatestVersionIDs(String response) { + List ids = new LinkedList(); + Gson gson = new Gson(); + JsonArray responseArray = gson.fromJson(response, JsonArray.class); + for (JsonElement elem : responseArray) { + ids.add(getVersionID(elem.getAsJsonObject())); + } + return ids; + } + + private Integer getVersionID(JsonObject object) { + return object.get("versionId").getAsInt(); + } + } 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 cb9503a6df..87ba771d33 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 @@ -1,6 +1,10 @@ package uk.ac.ic.wlgitbridge.test.writelatex.model; import org.junit.Test; +import uk.ac.ic.wlgitbridge.writelatex.model.WLProject; + +import java.io.IOException; +import java.util.concurrent.ExecutionException; /** * Created by Winston on 06/11/14. @@ -9,7 +13,16 @@ public class WLProjectTests { @Test public void nothingToTest() { - + WLProject project = new WLProject("1826rqgsdb"); + try { + project.update(); + } catch (InterruptedException e) { + e.printStackTrace(); + } catch (ExecutionException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } } }