package jfdi.storage;

import java.nio.file.Path;
import java.util.ArrayList;

import jfdi.storage.exceptions.FilesReplacedException;
import jfdi.storage.exceptions.FilePathPair;

/**
 * This class manages all the databases in the Storage component.
 *
 * @author Thng Kai Yuan
 */
public class DatabaseManager {

    /*
     * Public APIs
     */

    /**
     * This method sets the file path of each database accordingly, using
     * storageFolderPath as the root directory of all data.
     *
     * @param storageFolderPath
     *            the root directory where all data will be stored
     */
    public static void setAllFilePaths(String storageFolderPath) {
        for (IDatabase database : Constants.DATABASES) {
            database.setFilePath(storageFolderPath);
        }
    }

    /**
     * This method persists all databases to disk.
     */
    public static void persistAll() {
        for (IDatabase database : Constants.DATABASES) {
            database.persist();
        }
    }

    /**
     * This method loads/refreshes all databases based on data contained within
     * the data file defined by the file path of each record.
     *
     * @throws FilesReplacedException
     *             if unrecognized files were replaced (with backups made)
     */
    public static void loadAllDatabases() throws FilesReplacedException {
        ArrayList<FilePathPair> replacedFiles = new ArrayList<FilePathPair>();
        FilePathPair filePathPair = null;

        for (IDatabase database : Constants.DATABASES) {
            filePathPair = database.load();
            if (filePathPair != null) {
                replacedFiles.add(filePathPair);
            }
        }

        if (!replacedFiles.isEmpty()) {
            throw new FilesReplacedException(replacedFiles);
        }
    }

    /**
     * @return an ArrayList of Paths where the existing data for each database is
     *         stored.
     */
    public static ArrayList<Path> getAllFilePaths() {
        ArrayList<Path> filePaths = new ArrayList<Path>();

        for (IDatabase database : Constants.DATABASES) {
            Path filePath = database.getFilePath();
            filePaths.add(filePath);
        }

        return filePaths;
    }

}

@@ -70,16 +70,67 @@
 rayList;
+%0Aimport java.util.Arrays;%0Aimport java.util.Objects;
 %0A%0Aimport
@@ -960,34 +960,22 @@
     
-for (IDatabase database : 
+Arrays.stream(
 Cons
@@ -994,53 +994,38 @@
 SES)
- %7B%0A            d
+.forEach(ID
 atabase
-.
+::
 persist
-(
 );%0A
-        %7D%0A
     
@@ -1272,36 +1272,13 @@
 ere 
-replaced (with backups made)
+moved
 %0A   
@@ -1388,22 +1388,19 @@
 thPair%3E 
-replac
+mov
 edFiles 
@@ -1444,77 +1444,22 @@
     
-FilePathPair filePathPair = null;%0A%0A        for (IDatabase database : 
+Arrays.stream(
 Cons
@@ -1466,34 +1466,32 @@
 tants.DATABASES)
- %7B
 %0A            fil
@@ -1491,39 +1491,29 @@
     
-filePathPair = d
+.map(ID
 atabase
-.
+::
 load
-();
+)
 %0A   
@@ -1525,35 +1525,33 @@
     
-if (filePathPair != n
+.filter(Objects::nonN
 ull)
- %7B
 %0A   
@@ -1563,68 +1563,34 @@
     
-    replac
+.forEach(mov
 edFiles
-.add(filePathPair);%0A            %7D%0A        %7D
+::add);
 %0A%0A  
@@ -1600,22 +1600,19 @@
    if (!
-replac
+mov
 edFiles.
@@ -1673,14 +1673,11 @@
 ion(
-replac
+mov
 edFi
@@ -1703,24 +1703,99 @@
  %7D%0A%0A    /**%0A
+     * This method returns an ArrayList of Paths to each data file.%0A     *%0A
      * @retu
@@ -1864,11 +1864,8 @@
 base
- is
 %0A   
@@ -1880,15 +1880,17 @@
     
+is 
 stored
-.
 %0A   
@@ -2007,25 +2007,24 @@
 ath%3E();%0A
-%0A
         
 for (IDa
@@ -2019,34 +2019,22 @@
     
-for (IDatabase database : 
+Arrays.stream(
 Cons
@@ -2041,34 +2041,32 @@
 tants.DATABASES)
- %7B
 %0A            Pat
@@ -2066,33 +2066,24 @@
     
-Path filePath = d
+.map(ID
 atabase
-.
+::
 getF
@@ -2089,19 +2089,17 @@
 FilePath
-();
+)
 %0A       
@@ -2107,42 +2107,33 @@
     
-filePaths.add(filePath);%0A        %7D
+.forEach(filePaths::add);
 %0A%0A  

Refactor DatabaseManager