/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package org.zols.documents.service;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import org.zols.documents.domain.Document;
import org.zols.documents.domain.DocumentRepository;
import org.zols.documents.domain.Upload;

/**
 * DocumentService provides methods to access and add files to the file systems.
 *
 */
@Service
public class DocumentService {

    private static final Logger LOGGER = LoggerFactory
            .getLogger(DocumentService.class);

    @Autowired
    private DocumentRepositoryService documentRepositoryService;

    /**
     * Upload documents
     *
     * @param documentRepositoryName
     * @param upload documents to be uploaded
     * @param rootFolderPath source path of the document
     */
    public void upload(String documentRepositoryName, Upload upload, String rootFolderPath) {
        DocumentRepository documentRepository = documentRepositoryService.read(documentRepositoryName);
        String folderPath = documentRepository.getPath();
        if (rootFolderPath != null && rootFolderPath.trim().length() != 0) {
            folderPath = folderPath + File.separator + rootFolderPath;
        }
        List<MultipartFile> multipartFiles = upload.getFiles();
        if (null != multipartFiles && multipartFiles.size() > 0) {
            for (MultipartFile multipartFile : multipartFiles) {
                //Handle file content - multipartFile.getInputStream()
                byte[] bytes;
                try {
                    bytes = multipartFile.getBytes();

                    BufferedOutputStream stream
                            = new BufferedOutputStream(new FileOutputStream(new File(folderPath + File.separator + multipartFile.getOriginalFilename())));
                    stream.write(bytes);
                    stream.close();
                } catch (IOException ex) {
                    java.util.logging.Logger.getLogger(DocumentService.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
    }

    /**
     * Creates a directory in the given document path
     *
     * @param documentRepositoryName the path in which the directory will be
     * created
     * @param directoryName the name of the directory to be created
     */
    public void createDirectory(String documentRepositoryName, String directoryName) {
        createDirectory(documentRepositoryName, null, directoryName);
    }

    /**
     * Creates a directory in the given document path
     *
     * @param documentRepositoryName the path in which the directory will be
     * created
     * @param rootFolderPath folder where we need to create directory
     * @param directoryName the name of the directory to be created
     */
    public void createDirectory(String documentRepositoryName, String rootFolderPath, String directoryName) {
        DocumentRepository documentRepository = documentRepositoryService.read(documentRepositoryName);
        String folderPath = documentRepository.getPath();
        if (rootFolderPath != null && rootFolderPath.trim().length() != 0) {
            folderPath = folderPath + File.separator + rootFolderPath;
        }
        File newFolder = new File(folderPath + File.separator + directoryName);
        newFolder.mkdirs();
    }

    public void delete(String documentRepositoryName, String filePath) {
        DocumentRepository documentRepository = documentRepositoryService.read(documentRepositoryName);
        String path = documentRepository.getPath();
        if (filePath != null && filePath.trim().length() != 0) {
            path = path + File.separator + filePath;
        }
        LOGGER.info("Deleting a file form {}", path);
        File file = new File(path);
        file.delete();
    }

    /**
     * List all the files in the current directory
     *
     * @param documentRepositoryName type of storage
     * @param folderPath the directory to list the files
     * @return
     */
    public List<Document> list(String documentRepositoryName, String folderPath) {
        DocumentRepository documentRepository = documentRepositoryService.read(documentRepositoryName);
        String path = documentRepository.getPath();
        if (folderPath != null && folderPath.trim().length() != 0) {
            path = path + File.separator + folderPath;
        }

        LOGGER.info("Listing documents from file path {}", path);

        List<Document> documents = new ArrayList<>();
        Document document;
        for (File innerFile : new File(path).listFiles()) {
            document = new Document();
            document.setRepositoryName(documentRepositoryName);
            document.setFileName(innerFile.getName());
            document.setIsDir(innerFile.isDirectory());
            document.setPath(innerFile.getPath().replaceAll(path, ""));
            documents.add(document);
        }
        return documents;
    }

}

@@ -269,24 +269,62 @@
 va.io.File;%0A
+import java.io.FileNotFoundException;%0A
 import java.
@@ -4309,22 +4309,499 @@
     
-file.delete();
+try %7B%0A            delete(file);%0A        %7D catch (IOException ex) %7B%0A            java.util.logging.Logger.getLogger(DocumentService.class.getName()).log(Level.SEVERE, null, ex);%0A        %7D%0A    %7D%0A    %0A        private void delete(File f) throws IOException %7B%0A        if (f.isDirectory()) %7B%0A            for (File c : f.listFiles()) %7B%0A                delete(c);%0A            %7D%0A        %7D%0A        if (!f.delete()) %7B%0A            throw new FileNotFoundException(%22Failed to delete file: %22 + f);%0A        %7D
 %0A   

Delete File integrated