LP1825896 Migrate values to new data directory
authorBill Erickson <berickxx@gmail.com>
Wed, 24 Apr 2019 23:05:20 +0000 (19:05 -0400)
committerJason Boyer <JBoyer@equinoxinitiative.org>
Fri, 13 Dec 2019 17:59:25 +0000 (12:59 -0500)
When the currently selected data (AKA profile) directory differs from
the default, migrate data from the default directory to the newly
configured directory.

This migration only occurs on the first instance of using the new
directory, specifically when Hatch determines the new directory does not
yet exist.

Signed-off-by: Bill Erickson <berickxx@gmail.com>
Signed-off-by: Jason Boyer <JBoyer@equinoxinitiative.org>
src/org/evergreen_ils/hatch/FileIO.java
src/org/evergreen_ils/hatch/RequestHandler.java

index 355c242..77b0735 100644 (file)
@@ -284,4 +284,49 @@ public class FileIO {
 
         return (String[]) nameList.toArray(new String[0]);
     }
+
+
+    // Migrate stored values from an old profile directory to a new one.
+    // If the destination path does not exists for the origin,
+    // create the path and copy data from the old profile dir to 
+    // the new profile dir. The source directory is unmodified.
+    public void migrateProfileData(String newDir, String oldDir, String origin) {
+        origin = cleanFileName(origin);
+
+        // basePath directory
+        File baseDir = new File(newDir);
+        if (!baseDir.exists()) {
+            if (!baseDir.mkdir()) {
+                logger.warning("Unable to create baseDirectory: " + baseDir.getPath());
+            }
+        }
+
+        File originDir = new File(newDir, origin);
+        if (originDir.exists()) {
+            // New dir is already built.  No need to migrate.
+            return;
+        }
+
+        File oldOriginDir = new File(oldDir, origin);
+
+        if (oldOriginDir.exists()) {
+            logger.info("Migrating data from " + 
+                oldDir + " to " + newDir + " for origin " + origin);
+
+            FileIO oldFiles = new FileIO(oldDir, origin);
+            for (String key: oldFiles.keys()) {
+                logger.info("Migrating key " + key);
+                String val = oldFiles.get(key);
+                this.set(key, val);
+            }
+
+        } else {
+            // create a new directory
+            if (!originDir.mkdir()) {
+                logger.warning(
+                    "Error creating data directory: " + originDir.getPath());
+            }
+        }
+    }
 }
+
index 400e02c..e2569cc 100644 (file)
@@ -32,29 +32,33 @@ public class RequestHandler extends Thread {
 
     /** Root directory for all FileIO operations */
     private static String profileDirectory = null;
+    private static String defaultDirectory = null;
+    private static boolean migrateProfileData = false;
 
     private void configure() {
 
+        if (profileDirectory != null) {
+            return; // Already loaded
+        }
+
+        String home = System.getProperty("user.home");
+        defaultDirectory = new File(home, ".evergreen").getPath();
+        profileDirectory = Hatch.getProp("data.directory");
+
         // Find the profile directory.
         // The profile directory + origin string represent the base 
         // directory for all file I/O for this session.
         if (profileDirectory == null) {
+            profileDirectory = defaultDirectory;
+        } else {
+            migrateProfileData = true;
+        }
 
-            // first see if a value is set in the properties file.
-            profileDirectory = Hatch.getProp("data.directory");
-
-            if (profileDirectory == null) {
-                // otherwise set the directory to the users's home
-                // directory + .evergreen
-                String home = System.getProperty("user.home");
-                profileDirectory = new File(home, ".evergreen").getPath();
+        logger.info("Using data directory: " + profileDirectory);
 
-                if (profileDirectory == null) {
-                    logger.warning("Unable to set profile directory");
-                }
-            }
-
-            logger.info("Using data directory: " + profileDirectory);
+        if (profileDirectory == null) {
+            logger.warning("Unable to set profile directory");
+            return;
         }
     }
 
@@ -83,6 +87,10 @@ public class RequestHandler extends Thread {
         String content = null;
         FileIO fileIO = new FileIO(profileDirectory, origin);
 
+        if (migrateProfileData) {
+            fileIO.migrateProfileData(profileDirectory, defaultDirectory, origin);
+        }
+
         switch (action) {
 
             case "printers":