From 6ad4772e421f260f5db555d2dacd26153caa54f9 Mon Sep 17 00:00:00 2001
From: Eric Slenk <slenkeri@anr.msu.edu>
Date: Wed, 30 Nov 2016 18:57:50 -0500
Subject: [PATCH] Implement read-only methods of StructuresSnapshotService.

---
 build.gradle                                  |  2 +-
 .../StructuresSnapshotServiceTool.java        | 97 ++++++++++++++++---
 .../StructuresSnapshotServiceToolInfo.java    |  2 +-
 3 files changed, 83 insertions(+), 18 deletions(-)

diff --git a/build.gradle b/build.gradle
index e24bdf5..8a7d2b4 100644
--- a/build.gradle
+++ b/build.gradle
@@ -4,7 +4,7 @@ apply plugin: 'war'
 apply plugin: 'eclipse'
 
 sourceCompatibility = '1.8'
-version = '0.1.0-8'
+version = '0.1.0-9'
 
 
 repositories {
diff --git a/src/main/java/edu/msu/anr/osgi/structuralintegrity/viewtool/StructuresSnapshotServiceTool.java b/src/main/java/edu/msu/anr/osgi/structuralintegrity/viewtool/StructuresSnapshotServiceTool.java
index 4c83f9c..974ee9c 100644
--- a/src/main/java/edu/msu/anr/osgi/structuralintegrity/viewtool/StructuresSnapshotServiceTool.java
+++ b/src/main/java/edu/msu/anr/osgi/structuralintegrity/viewtool/StructuresSnapshotServiceTool.java
@@ -1,41 +1,106 @@
 package edu.msu.anr.osgi.structuralintegrity.viewtool;
 
+import java.io.File;
+import java.io.FileFilter;
+import java.io.IOException;
+import java.util.List;
+
 import com.dotcms.repackage.org.osgi.util.tracker.ServiceTracker;
-import edu.msu.anr.osgi.structuralintegrity.service.StructuresSnapshotService;
 import org.apache.velocity.tools.view.tools.ViewTool;
 
-import java.io.File;
+import edu.msu.anr.osgi.structuralintegrity.service.StructuresSnapshot;
+import edu.msu.anr.osgi.structuralintegrity.service.StructuresSnapshotService;
 
+/**
+ * A viewtool which provides access to the {@link StructuresSnapshotService} read-only methods.
+ */
 public class StructuresSnapshotServiceTool implements ViewTool {
 
-    private ServiceTracker<StructuresSnapshotService, StructuresSnapshotService> structuresSnapshotServiceTracker;
+    /** ServiceTracker for the {@link StructuresSnapshotService}. */
+    private final ServiceTracker<StructuresSnapshotService, StructuresSnapshotService> structuresSnapshotServiceTracker;
 
+    /**
+     * Constructs the StructuresSnapshotServiceTool.
+     * @param structuresSnapshotServiceTracker A ServiceTracker tracking the {@link StructuresSnapshotService}.
+     */
     public StructuresSnapshotServiceTool (ServiceTracker<StructuresSnapshotService, StructuresSnapshotService> structuresSnapshotServiceTracker) {
         this.structuresSnapshotServiceTracker = structuresSnapshotServiceTracker;
     }
 
+    /**
+     * Initializes the viewtool.
+     * @param initData Data used to initialize the viewtool.
+     */
 	@Override
 	public void init(Object initData) {
 	}
 
-	public String getHelloMessage() {
-		return "Hello dotCMS World";
-	}
+    /**
+     * Loads a saved snapshot from a snapshot file.
+     * @param snapshotFile File containing a saved snapshot.
+     * @return The snapshot saved in the given file.
+     * @throws IOException If the snapshot could not be loaded and deserialized from the given file.
+     * @throws ClassNotFoundException If the StructuresSnapshot class could not be loaded;
+     */
+    public StructuresSnapshot loadSnapshotFromFile(File snapshotFile) throws IOException, ClassNotFoundException {
+        return getService().loadSnapshotFromFile(snapshotFile);
+    }
 
-	public String getHelloMessage(String name) {
-		return "Hello " + name;
-	}
+    /**
+     * Loads a saved snapshot from a snapshot file.
+     * @param snapshotFilePath Path to a file containing a saved snapshot.
+     * @return The snapshot saved in the given file.
+     * @throws IOException if the snapshot could not be loaded and deserialized from the given file.
+     * @throws ClassNotFoundException if the StructuresSnapshot class could not be loaded;
+     */
+    public StructuresSnapshot loadSnapshotFromFile(String snapshotFilePath) throws IOException, ClassNotFoundException {
+        return getService().loadSnapshotFromFile(snapshotFilePath);
+    }
 
-	public File[] getSavedSnapshotFiles() throws Exception {
+    /**
+     * Gets all saved snapshot files.
+     * @return Array containing all files in the snapshots directory.
+     */
+    public File[] getSavedSnapshotFiles() {
         return getService().getSavedSnapshotFiles();
     }
 
-    private StructuresSnapshotService getService() throws Exception {
-        StructuresSnapshotService service = (StructuresSnapshotService) this.structuresSnapshotServiceTracker.getService();
-        if (service == null) {
-            throw new Exception("Unable to locate service edu.msu.anr.osgi.structuralintegrity.service.StructuresSnapshotService.");
-        }
-        return service;
+    /**
+     * Gets all saved snapshot files which meet the given filter criteria.
+     * @param filter Filter object.
+     * @return Array containing all files in the snapshots directory which meet the given filter criteria.
+     */
+    public File[] getSavedSnapshotFiles(FileFilter filter) {
+        return getService().getSavedSnapshotFiles(filter);
+    }
+
+    /**
+     * Gets all historic structures snapshots.
+     * @return All saved snapshot files.
+     * @throws IOException If the snapshot could not be loaded and deserialized from the given file.
+     * @throws ClassNotFoundException If the StructuresSnapshot class could not be loaded;
+     */
+    public List<StructuresSnapshot> getSavedSnapshots() throws IOException, ClassNotFoundException {
+        return getService().getSavedSnapshots();
+    }
+
+    /**
+     * Gets the most recent saved structures snapshot.
+     * @return The most recent saved structures snapshot.
+     * @throws IOException if there is an error reading the snapshot from disk.
+     * @throws ClassNotFoundException if the StructuresSnapshot class is not found.
+     * @throws IndexOutOfBoundsException if there are no snapshots on disk.
+     */
+    public StructuresSnapshot getPreviousSnapshot() throws IOException, ClassNotFoundException, IndexOutOfBoundsException {
+        return getService().getPreviousSnapshot();
+    }
+
+    /**
+     * Gets the {@link StructuresSnapshotService} from the ServiceTracker.
+     * @return The StructuresSnapshotService.
+     */
+    private StructuresSnapshotService getService() {
+        return this.structuresSnapshotServiceTracker.getService();
     }
 
 }
diff --git a/src/main/java/edu/msu/anr/osgi/structuralintegrity/viewtool/StructuresSnapshotServiceToolInfo.java b/src/main/java/edu/msu/anr/osgi/structuralintegrity/viewtool/StructuresSnapshotServiceToolInfo.java
index 11626ab..1ab1c67 100644
--- a/src/main/java/edu/msu/anr/osgi/structuralintegrity/viewtool/StructuresSnapshotServiceToolInfo.java
+++ b/src/main/java/edu/msu/anr/osgi/structuralintegrity/viewtool/StructuresSnapshotServiceToolInfo.java
@@ -8,7 +8,7 @@ import edu.msu.anr.osgi.structuralintegrity.service.StructuresSnapshotService;
 
 class StructuresSnapshotServiceToolInfo extends ServletToolInfo {
 
-    private ServiceTracker<StructuresSnapshotService, StructuresSnapshotService> structuresSnapshotServiceTracker;
+    private final ServiceTracker<StructuresSnapshotService, StructuresSnapshotService> structuresSnapshotServiceTracker;
 
     StructuresSnapshotServiceToolInfo(ServiceTracker<StructuresSnapshotService, StructuresSnapshotService> structuresSnapshotServiceTracker) {
         this.structuresSnapshotServiceTracker = structuresSnapshotServiceTracker;
-- 
GitLab