001/*
002 *  Copyright 2018 Anyware Services
003 *
004 *  Licensed under the Apache License, Version 2.0 (the "License");
005 *  you may not use this file except in compliance with the License.
006 *  You may obtain a copy of the License at
007 *
008 *      http://www.apache.org/licenses/LICENSE-2.0
009 *
010 *  Unless required by applicable law or agreed to in writing, software
011 *  distributed under the License is distributed on an "AS IS" BASIS,
012 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 *  See the License for the specific language governing permissions and
014 *  limitations under the License.
015 */
016package org.ametys.plugins.extraction.execution;
017
018import java.io.IOException;
019import java.nio.file.Files;
020import java.nio.file.Path;
021import java.nio.file.Paths;
022import java.util.ArrayList;
023import java.util.HashMap;
024import java.util.List;
025import java.util.Map;
026
027import org.apache.commons.io.FileUtils;
028
029import org.ametys.core.ui.Callable;
030import org.ametys.core.ui.StaticClientSideElement;
031import org.ametys.runtime.util.AmetysHomeHelper;
032
033/**
034 * This client site element manages a button to delete extraction result folders and files
035 */
036public class DeleteExtractionResultFilesClientSideElement extends StaticClientSideElement
037{
038    /**
039     * Deletes multiple result folders and result files.
040     * @param paths The paths of the result folders and files to delete
041     * @return The result map
042     */
043    @Callable(right = "Extraction_Rights_EditExtraction")
044    public Map<String, Object> deleteResults(List<String> paths)
045    {
046        Map<String, Object> result = new HashMap<>();
047        List<String> deleted = new ArrayList<>();
048        List<String> notDeleted = new ArrayList<>();
049        List<String> doNotExist = new ArrayList<>();
050        
051        Path rootResults = AmetysHomeHelper.getAmetysHomeData().toPath().resolve(ExtractionExecutor.EXTRACTION_DIR_NAME);
052        for (String path : paths)
053        {
054            _tryToDelete(path, rootResults, deleted, notDeleted, doNotExist);
055        }
056        
057        result.put("deleted", deleted);
058        result.put("errors", notDeleted);
059        result.put("not-exist", doNotExist);
060        return result;
061    }
062    
063    private static void _tryToDelete(String path, Path rootResults, List<String> deleted, List<String> notDeleted, List<String> doNotExist)
064    {
065        Path fileOrFolder = rootResults.resolve(Paths.get(path));
066        if (Files.exists(fileOrFolder))
067        {
068            try
069            {
070                _delete(fileOrFolder);
071                deleted.add(path);
072            }
073            catch (IOException e)
074            {
075                notDeleted.add(path);
076            }
077        }
078        else
079        {
080            doNotExist.add(path);
081        }
082    }
083    
084    private static void _delete(Path fileOrFolder) throws IOException
085    {
086        if (Files.isDirectory(fileOrFolder))
087        {
088            FileUtils.deleteDirectory(fileOrFolder.toFile());
089        }
090        else
091        {
092            Files.delete(fileOrFolder);
093        }
094    }
095}
096