Преглед изворни кода

Now saves the versions already held.

Winston Li пре 12 година
родитељ
комит
0d7f5e4430

+ 3 - 7
services/git-bridge/src/uk/ac/ic/wlgitbridge/bridge/WLBridgedProject.java

@@ -42,14 +42,10 @@ public class WLBridgedProject {
         try {
             List<Snapshot> snapshotsToAdd = snapshotDBAPI.getSnapshotsToAddToProject(name);
             for (Snapshot snapshot : snapshotsToAdd) {
-                snapshot.getData().writeAll(repositoryDirectory.getAbsolutePath());
+                snapshot.writeToDisk(repositoryDirectory.getAbsolutePath());
                 Git git = new Git(repository);
-                AddCommand add = git.add();
-                add.addFilepattern(".");
-                add.call();
-                CommitCommand commit = git.commit();
-                commit.setMessage("Commit");
-                commit.call();
+                git.add().addFilepattern(".").call();
+                git.commit().setAuthor(snapshot.getUserName(), snapshot.getUserEmail()).setMessage(snapshot.getComment()).call();
             }
         } catch (Throwable throwable) {
             throwable.printStackTrace();

+ 0 - 22
services/git-bridge/src/uk/ac/ic/wlgitbridge/writelatex/api/DummySnapshotAPI.java

@@ -1,22 +0,0 @@
-package uk.ac.ic.wlgitbridge.writelatex.api;
-
-import uk.ac.ic.wlgitbridge.writelatex.model.Snapshot;
-
-import java.util.List;
-
-/**
- * Created by Winston on 03/11/14.
- */
-public class DummySnapshotAPI implements SnapshotAPI {
-
-    @Override
-    public List<Snapshot> getSnapshots() {
-        return null;
-    }
-
-    @Override
-    public void putSnapshots(List<Snapshot> snapshots) {
-
-    }
-
-}

+ 7 - 2
services/git-bridge/src/uk/ac/ic/wlgitbridge/writelatex/api/request/getforversion/SnapshotData.java

@@ -36,14 +36,12 @@ public class SnapshotData implements JSONSource {
         for (JsonElement json : jsonArray) {
             srcs.add(new WLFile(json));
         }
-        System.out.println(srcs);
     }
 
     private void populateAtts(JsonArray jsonArray) {
         for (JsonElement json : jsonArray) {
             atts.add(new WLAttachment(json));
         }
-        System.out.println(atts);
     }
 
     public void writeAll(String repoDir) throws InterruptedException, ExecutionException, IOException {
@@ -55,4 +53,11 @@ public class SnapshotData implements JSONSource {
         }
     }
 
+    public List<WLFile> getSrcs() {
+        return srcs;
+    }
+
+    public List<WLFile> getAtts() {
+        return atts;
+    }
 }

+ 0 - 23
services/git-bridge/src/uk/ac/ic/wlgitbridge/writelatex/api/request/getforversion/WLFile.java

@@ -44,7 +44,6 @@ public class WLFile implements JSONSource {
     }
 
     public void writeToDisk(String repoDir) throws IOException, ExecutionException, InterruptedException {
-        System.out.println("write to " + repoDir);
         File file = new File(repoDir, path);
         file.getParentFile().mkdirs();
         file.createNewFile();
@@ -53,26 +52,4 @@ public class WLFile implements JSONSource {
         out.close();
     }
 
-//    @Override
-//    public String toString() {
-//        try {
-//            File file = new File("/Users/Roxy/git-test-files/" + path);
-//            file.getParentFile().mkdirs();
-//            file.createNewFile();
-//            OutputStream out = new FileOutputStream(file);
-//            out.write(getContents());
-//            out.close();
-//            return "{ path: " + path + ", contents: " + getContents().toString() + " }";
-//        } catch (ExecutionException e) {
-//            e.printStackTrace();
-//        } catch (InterruptedException e) {
-//            e.printStackTrace();
-//        } catch (FileNotFoundException e) {
-//            e.printStackTrace();
-//        } catch (IOException e) {
-//            e.printStackTrace();
-//        }
-//        return "Exception";
-//    }
-
 }

+ 5 - 0
services/git-bridge/src/uk/ac/ic/wlgitbridge/writelatex/api/request/getsavedvers/SnapshotInfo.java

@@ -13,6 +13,11 @@ public class SnapshotInfo {
     private WLUser user;
     private String createdAt;
 
+    public SnapshotInfo(int versionID) {
+        comment = "Update on WriteLatex.com.";
+        user = new WLUser();
+    }
+
     public int getVersionId() {
         return versionId;
     }

+ 5 - 0
services/git-bridge/src/uk/ac/ic/wlgitbridge/writelatex/api/request/getsavedvers/WLUser.java

@@ -11,6 +11,11 @@ public class WLUser {
     private String name;
     private String email;
 
+    public WLUser() {
+        name = "Anonymous";
+        email = "anonymous@writelatex.com";
+    }
+
     public String getName() {
         return name;
     }

+ 39 - 16
services/git-bridge/src/uk/ac/ic/wlgitbridge/writelatex/model/Snapshot.java

@@ -2,38 +2,61 @@ 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.getforversion.WLFile;
+import uk.ac.ic.wlgitbridge.writelatex.api.request.getsavedvers.SnapshotInfo;
 import uk.ac.ic.wlgitbridge.writelatex.api.request.getsavedvers.WLUser;
 
+import java.io.IOException;
+import java.util.List;
+import java.util.concurrent.ExecutionException;
+
 /**
  * Created by Winston on 03/11/14.
  */
-public class Snapshot implements JSONModel {
-
-    private int versionID;
-    private String comment;
-    private WLUser user;
-    private SnapshotData data;
+public class Snapshot {
 
-    public Snapshot(int versionID, SnapshotData data) {
-        this.comment = comment;
+    private final int versionID;
+    private final String comment;
+    private final String userName;
+    private final String userEmail;
 
-        this.data = data;
-    }
+    private final List<WLFile> srcs;
+    private final List<WLFile> atts;
 
-    @Override
-    public void updateFromJSON(JsonElement json) {
+    public Snapshot(SnapshotInfo info, SnapshotData data) {
+        versionID = info.getVersionId();
+        comment = info.getComment();
+        WLUser user = info.getUser();
+        userName = user.getName();
+        userEmail = user.getEmail();
 
+        srcs = data.getSrcs();
+        atts = data.getAtts();
     }
 
-    public void writeToDisk() {
-
+    public void writeToDisk(String basePath) throws InterruptedException, ExecutionException, IOException {
+        for (WLFile file : srcs) {
+            file.writeToDisk(basePath);
+        }
+        for (WLFile file : atts) {
+            file.writeToDisk(basePath);
+        }
     }
 
-    public SnapshotData getData() {
-        return data;
+    public int getVersionID() {
+        return versionID;
     }
 
     public String getComment() {
         return comment;
     }
+
+    public String getUserName() {
+        return userName;
+    }
+
+    public String getUserEmail() {
+        return userEmail;
+    }
+
 }

+ 1 - 4
services/git-bridge/src/uk/ac/ic/wlgitbridge/writelatex/model/WLDataModel.java

@@ -24,13 +24,10 @@ public class WLDataModel implements SnapshotDBAPI {
         projects = new HashMap<String, WLProject>();
     }
 
-    public void updateProjectWithName(String name) throws Throwable {
+    private 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();
     }
 

+ 18 - 24
services/git-bridge/src/uk/ac/ic/wlgitbridge/writelatex/model/WLProject.java

@@ -17,27 +17,17 @@ import java.util.concurrent.ExecutionException;
 /**
  * Created by Winston on 06/11/14.
  */
-public class WLProject implements JSONModel {
+public class WLProject {
 
     private final String name;
-    public static final int VERSION_ID_INVALID = -1;
     private final Map<Integer, Snapshot> snapshots;
     private final SortedSet<Integer> versions;
-    private int latestVersionID;
     private List<Snapshot> snapshotsToAdd;
-    private SortedSet<Integer> idsToUpdate;
-    private HashMap<Integer, SnapshotInfo> msg;
 
     public WLProject(String name) {
         this.name = name;
         snapshots = new HashMap<Integer, Snapshot>();
         versions = new TreeSet<Integer>();
-        latestVersionID = VERSION_ID_INVALID;
-    }
-
-    @Override
-    public void updateFromJSON(JsonElement json) {
-
     }
 
     public void update() throws Throwable {
@@ -51,22 +41,22 @@ public class WLProject implements JSONModel {
         getDoc.request();
         getSavedVers.request();
 
-        List<Integer> fetchedIDs = new LinkedList<Integer>();
-        fetchedIDs.add(getDoc.getResult().getVersionID());
+        Set<Integer> fetchedIDs = new HashSet<Integer>();
+        Map<Integer, SnapshotInfo> fetchedSnapshotInfos = new HashMap<Integer, SnapshotInfo>();
+
+        int latestVersionID = getDoc.getResult().getVersionID();
+        fetchedSnapshotInfos.put(latestVersionID, new SnapshotInfo(latestVersionID));
+        fetchedIDs.add(latestVersionID);
 
         for (SnapshotInfo snapshotInfo : getSavedVers.getResult().getSavedVers()) {
-            msg = new HashMap<Integer, SnapshotInfo>();
-            msg.put(snapshotInfo.getVersionId(), snapshotInfo);
-            fetchedIDs.add(snapshotInfo.getVersionId());
+            int versionId = snapshotInfo.getVersionId();
+            fetchedSnapshotInfos.put(versionId, snapshotInfo);
+            fetchedIDs.add(versionId);
         }
 
         boolean result = false;
 
-//        ids.add(getLatestVersionID(getDoc.getResult()));
-
-//        ids.addAll(getLatestVersionIDs(getSavedVers.getResult()));
-
-        idsToUpdate = new TreeSet<Integer>();
+        List<Integer> idsToUpdate = new LinkedList<Integer>();
 
         boolean hasNew = false;
         for (Integer id : fetchedIDs) {
@@ -77,12 +67,16 @@ public class WLProject implements JSONModel {
             }
         }
 
-        updateIDs(idsToUpdate);
+        versions.addAll(fetchedIDs);
+        versions.add(latestVersionID);
+
+        updateIDs(idsToUpdate, fetchedSnapshotInfos);
 
         return result;
     }
 
-    private void updateIDs(SortedSet<Integer> idsToUpdate) throws Throwable {
+    private void updateIDs(List<Integer> idsToUpdate, Map<Integer, SnapshotInfo> fetchedSnapshotInfos) throws Throwable {
+        System.out.println(idsToUpdate);
         List<SnapshotGetForVersionRequest> requests = new LinkedList<SnapshotGetForVersionRequest>();
         for (int id : idsToUpdate) {
             SnapshotGetForVersionRequest request = new SnapshotGetForVersionRequest(name, id);
@@ -92,7 +86,7 @@ public class WLProject implements JSONModel {
         for (SnapshotGetForVersionRequest request : requests) {
             SnapshotGetForVersionResult result = request.getResult();
             SnapshotData data = result.getSnapshotData();
-            Snapshot snapshot = new Snapshot(request.getVersionID(), data);
+            Snapshot snapshot = new Snapshot(fetchedSnapshotInfos.get(request.getVersionID()), data);
             snapshots.put(request.getVersionID(), snapshot);
         }
         snapshotsToAdd = new LinkedList<Snapshot>();