package uk.ac.ic.wlgitbridge.writelatex; import uk.ac.ic.wlgitbridge.writelatex.api.request.exception.FailedConnectionException; import uk.ac.ic.wlgitbridge.writelatex.api.request.getdoc.SnapshotGetDocRequest; import uk.ac.ic.wlgitbridge.writelatex.api.request.getdoc.exception.InvalidProjectException; 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 uk.ac.ic.wlgitbridge.writelatex.model.Snapshot; import java.util.*; /** * Created by Winston on 07/11/14. */ public class SnapshotFetcher { private final String projectName; private final Map snapshots; private final SortedSet versions; public SnapshotFetcher(String projectName, Map snapshots) { this.projectName = projectName; this.snapshots = snapshots; versions = new TreeSet(); } public SortedSet fetchNewSnapshots() throws FailedConnectionException, InvalidProjectException { SortedSet newSnapshots = new TreeSet(); while (getNew(newSnapshots)); System.out.println("Snapshots fetched: " + newSnapshots); return newSnapshots; } public Snapshot getLatestSnapshot() { return snapshots.get(versions.last()); } private boolean getNew(SortedSet newSnapshots) throws FailedConnectionException, InvalidProjectException { SnapshotGetDocRequest getDoc = new SnapshotGetDocRequest(projectName); SnapshotGetSavedVersRequest getSavedVers = new SnapshotGetSavedVersRequest(projectName); getDoc.request(); getSavedVers.request(); Set fetchedIDs = new HashSet(); Map fetchedSnapshotInfos = new HashMap(); int latestVersionID = putLatestDoc(getDoc, fetchedIDs, fetchedSnapshotInfos); putSavedVers(getSavedVers, fetchedIDs, fetchedSnapshotInfos); List idsToUpdate = getIDsToUpdate(fetchedIDs); versions.addAll(fetchedIDs); versions.add(latestVersionID); return updateIDs(idsToUpdate, fetchedSnapshotInfos, newSnapshots); } private void putFetchedResult(SnapshotInfo snapshotInfo, Set ids, Map snapshotInfos) { int versionID = snapshotInfo.getVersionId(); snapshotInfos.put(versionID, snapshotInfo); ids.add(versionID); } private int putLatestDoc(SnapshotGetDocRequest getDoc, Set fetchedIDs, Map fetchedSnapshotInfos) throws FailedConnectionException, InvalidProjectException { int latestVersionID = getDoc.getResult().getVersionID(); putFetchedResult(new SnapshotInfo(latestVersionID), fetchedIDs, fetchedSnapshotInfos); return latestVersionID; } private void putSavedVers(SnapshotGetSavedVersRequest getSavedVers, Set fetchedIDs, Map fetchedSnapshotInfos) throws FailedConnectionException { for (SnapshotInfo snapshotInfo : getSavedVers.getResult().getSavedVers()) { putFetchedResult(snapshotInfo, fetchedIDs, fetchedSnapshotInfos); } } private List getIDsToUpdate(Set fetchedIDs) { List idsToUpdate = new LinkedList(); for (Integer id : fetchedIDs) { if (!versions.contains(id)) { idsToUpdate.add(id); } } return idsToUpdate; } private boolean updateIDs(List idsToUpdate, Map fetchedSnapshotInfos, SortedSet newSnapshots) throws FailedConnectionException { if (idsToUpdate.isEmpty()) { return false; } fetchVersions(idsToUpdate, fetchedSnapshotInfos, newSnapshots); return true; } private void fetchVersions(List idsToUpdate, Map fetchedSnapshotInfos, SortedSet newSnapshots) throws FailedConnectionException { List requests = createFiredRequests(idsToUpdate); processResults(fetchedSnapshotInfos, newSnapshots, requests); } private List createFiredRequests(List idsToUpdate) { List requests = new LinkedList(); for (int id : idsToUpdate) { SnapshotGetForVersionRequest request = new SnapshotGetForVersionRequest(projectName, id); requests.add(request); request.request(); } return requests; } private void processResults(Map fetchedSnapshotInfos, SortedSet newSnapshots, List requests) throws FailedConnectionException { for (SnapshotGetForVersionRequest request : requests) { processResult(fetchedSnapshotInfos, newSnapshots, request); } } private void processResult(Map fetchedSnapshotInfos, SortedSet newSnapshots, SnapshotGetForVersionRequest request) throws FailedConnectionException { SnapshotGetForVersionResult result = request.getResult(); SnapshotData data = result.getSnapshotData(); Snapshot snapshot = new Snapshot(fetchedSnapshotInfos.get(request.getVersionID()), data); snapshots.put(request.getVersionID(), snapshot); newSnapshots.add(snapshot); } }