package uk.ac.ebi.spot.goci.curation.service;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import uk.ac.ebi.spot.goci.curation.model.StudyFileSummary;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by emma on 15/04/2016.
 *
 * @author emma
 *         <p>
 *         Service to handle upload and download of study files
 */
@Service
public class StudyFileService {

    @Value("${study.directory}")
    private File studyDirRoot;

    private Logger log = LoggerFactory.getLogger(getClass());

    protected Logger getLog() {
        return log;
    }

    public synchronized void createStudyDir(Long studyId) {

        if (getStudyDirRoot() == null) {
            getLog().error("Cannot find root dir for study with ID" + studyId);
            throw new IllegalStateException("Cannot find root directory");
        }

        if (!getStudyDirRoot().exists()) {
            getLog().debug("Making root directory '" + getStudyDirRoot().getAbsolutePath() + "'...");
            getStudyDirRoot().mkdirs();
            getLog().debug("Directory created!");
        }

        String uploadDirName = getStudyDirRoot() + File.separator + studyId;

        // Create a subdir based on the study id
        File uploadDir = new File(uploadDirName);
        boolean success = uploadDir.mkdir();
        if (!success) {
            getLog().error("Could not create directory for a study");
        }
    }


    public List<StudyFileSummary> getStudyFiles() {
        List<StudyFileSummary> files = new ArrayList<>();
        return files;
    }

    public File getStudyDirRoot() {
        return studyDirRoot;
    }
}

@@ -1126,24 +1126,42 @@
 %0A           
+ boolean success =
  getStudyDir
@@ -1173,24 +1173,55 @@
 ).mkdirs();%0A
+            if (success) %7B%0A    
             
@@ -1258,34 +1258,37 @@
 ted!%22);%0A        
+    
 %7D
-%0A
 %0A        String 
@@ -1284,126 +1284,105 @@
     
-String uploadDirName = getStudyDirRoot() + File.separator + studyId;%0A%0A        // Create a subdir based on the
+    else %7B%0A                getLog().error(%22Directory not created for study with ID: %22 +
  study
- id
+Id);
 %0A   
@@ -1390,49 +1390,24 @@
     
-File uploadDir = new File(uploadDirName);
+    %7D%0A        %7D%0A
 %0A   
@@ -1433,17 +1433,32 @@
 s = 
-uploadDir
+getStudyDirPath(studyId)
 .mkd
@@ -1623,16 +1623,28 @@
 dyFiles(
+Long studyId
 ) %7B%0A    
@@ -1701,100 +1701,823 @@
 ();%0A
-        return files;%0A    %7D%0A%0A    public File getStudyDirRoot() %7B%0A        return studyDirRoot
+%0A        File pathToStudyDir = getStudyDirPath(studyId);%0A%0A        if (pathToStudyDir.exists()) %7B%0A            String%5B%5D fileNames = pathToStudyDir.list();%0A%0A            for (String filename : fileNames) %7B%0A                StudyFileSummary studyFileSummary = new StudyFileSummary(filename);%0A                files.add(studyFileSummary);%0A            %7D%0A        %7D%0A        else %7B%0A            getLog().error(%22No study directory found for study with ID: %22 + studyId);%0A        %7D%0A        return files;%0A    %7D%0A%0A    public File getStudyDirRoot() %7B%0A        return studyDirRoot;%0A    %7D%0A%0A    /**%0A     * Create a subdir based on the study id%0A     *%0A     * @param id Study ID%0A     */%0A    private File getStudyDirPath(Long id) %7B%0A        String uploadDirName = getStudyDirRoot() + File.separator + id;%0A        return new File(uploadDirName)
 ;%0A  

Add method to get list of files in dir