package edu.chl.proton.model;

import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.text.SimpleDateFormat;


/**
 * @author Stina Werme
 * Created by stinawerme on 01/05/17.
 */
public class FileUtility {

    private java.io.File file;
    private boolean isSaved;


    public FileUtility(String name, String path) {
        this.file = new File(name);
        this.setName(name);
        this.setPath(path);
    }
    public FileUtility(String path) {
        this.file = new File(path);
        this.setPath(path);
    }

    protected String getName() {
        return this.file.getName();
    }

    protected void setName(String name) {
        java.io.File file = new java.io.File(this.file.getParentFile(), name);
        this.file.renameTo(file);
    }

    protected String getPath() {
        return this.file.getPath();
    }

    protected void setPath(String path) {
        java.io.File file = new java.io.File(path);
        this.file.renameTo(file);
    }

    protected java.io.File getFile() {
        return this.file;
    }

    protected void setIsSaved(boolean state) {
        isSaved = state;
    }

    protected boolean isSaved() {
        return isSaved;
    }

    protected void save(List<String> text) throws IOException {
        try {

            java.io.File file = new java.io.File(this.getPath());
            BufferedWriter out = new BufferedWriter(new FileWriter(file));
            System.out.println("hej");
            for(String line : text) {
                out.write(line);
                out.newLine();
            }
            out.close();
            setIsSaved(true);
        } catch (IOException e) {

        }
    }

    protected String getDateForlastEdited() {

        java.io.File file = new java.io.File(getName());

        SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");

        return sdf.format(file.lastModified());
    }

    private java.io.File getLastEditedFile(String dirPath) {
        java.io.File dir = new java.io.File(dirPath);
        java.io.File[] files = dir.listFiles();
        if (files == null || files.length == 0) {
            return null;
        }

        java.io.File lastModifiedFile = files[0];
        for (int i = 1; i < files.length; i++) {
            if (lastModifiedFile.lastModified() < files[i].lastModified()) {
                lastModifiedFile = files[i];
            }
        }
        return lastModifiedFile;
    }

    protected void remove() {
        this.getFile().delete();
    }

    // Aqcuires the text from the file we opened.
    protected List<String> aqcuireText(){

        // This will reference one line at a time
        String line;
        List<String> lines = new ArrayList<>();

        try {
            // FileReader reads text files in the default encoding.
            FileReader fileReader =
                    new FileReader(getName());

            // Always wrap FileReader in BufferedReader.
            BufferedReader bufferedReader =
                    new BufferedReader(fileReader);

            while((line = bufferedReader.readLine()) != null) {
                lines.add(line);
            }

            // Close file.
            bufferedReader.close();
        }
        catch(FileNotFoundException ex) {
            System.out.println(
                    "Unable to open file '" +
                            getName() + "'");
        }
        catch(IOException ex) {
            System.out.println(
                    "Error reading file '"
                            + getName() + "'");
        }

        return lines;
    }

}


@@ -669,32 +669,24 @@
 ) %7B%0A        
-java.io.
 File file = 
@@ -685,32 +685,24 @@
  file = new 
-java.io.
 File(this.fi
@@ -891,32 +891,24 @@
 ) %7B%0A        
-java.io.
 File file = 
@@ -907,32 +907,24 @@
  file = new 
-java.io.
 File(path);%0A
@@ -978,24 +978,16 @@
 otected 
-java.io.
 File get
@@ -1099,32 +1099,37 @@
 = state;%0A    %7D%0A%0A
+    %0A
     protected bo
@@ -1169,32 +1169,111 @@
 isSaved;%0A    %7D%0A%0A
+    /**%0A     * Save file%0A     * @param text%0A     * @throws IOException%0A     */%0A
     protected vo
@@ -1343,32 +1343,24 @@
             
-java.io.
 File file = 
@@ -1359,32 +1359,24 @@
  file = new 
-java.io.
 File(this.ge
@@ -1714,32 +1714,120 @@
        %7D%0A    %7D%0A%0A
+    /**%0A     * Returns date and time for last time edited%0A     * @return String%0A     */%0A
     protected St
@@ -1861,32 +1861,24 @@
  %7B%0A%0A        
-java.io.
 File file = 
@@ -1877,32 +1877,24 @@
  file = new 
-java.io.
 File(getName
@@ -2031,32 +2031,118 @@
 %0A    %7D%0A%0A    
+/**%0A     * Get last edited file%0A     * @param dirPath%0A     * @return File%0A     */%0A    
 private 
 java.io.File
@@ -2125,32 +2125,24 @@
     private 
-java.io.
 File getLast
@@ -2174,32 +2174,24 @@
 ) %7B%0A        
-java.io.
 File dir = n
@@ -2193,24 +2193,16 @@
 r = new 
-java.io.
 File(dir
@@ -2212,32 +2212,24 @@
 h);%0A        
-java.io.
 File%5B%5D files
@@ -2342,24 +2342,16 @@
         
-java.io.
 File las
@@ -2686,17 +2686,25 @@
 %7D%0A%0A    /
-/
+**%0A     *
  Aqcuire
@@ -2727,23 +2727,59 @@
 the 
-file we opened.
+opened file.%0A     * @return list of Strings%0A     */
 %0A   

Add comments