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.plugins.extraction.ExtractionConstants;
032import org.ametys.runtime.util.AmetysHomeHelper;
033
034/**
035 * This client site element manages a button to delete extraction result folders and files
036 */
037public class DeleteExtractionResultFilesClientSideElement extends StaticClientSideElement
038{
039    /**
040     * Deletes multiple result folders and result files.
041     * @param paths The paths of the result folders and files to delete
042     * @return The result map
043     */
044    @Callable(right = ExtractionConstants.MODIFY_EXTRACTION_RIGHT_ID)
045    public Map<String, Object> deleteResults(List<String> paths)
046    {
047        Map<String, Object> result = new HashMap<>();
048        List<String> deleted = new ArrayList<>();
049        List<String> notDeleted = new ArrayList<>();
050        List<String> doNotExist = new ArrayList<>();
051        
052        Path rootResults = AmetysHomeHelper.getAmetysHomeData().toPath().resolve(ExtractionConstants.RESULT_EXTRACTION_DIR_NAME);
053        for (String path : paths)
054        {
055            _tryToDelete(path, rootResults, deleted, notDeleted, doNotExist);
056        }
057        
058        result.put("deleted", deleted);
059        result.put("errors", notDeleted);
060        result.put("not-exist", doNotExist);
061        return result;
062    }
063    
064    private static void _tryToDelete(String path, Path rootResults, List<String> deleted, List<String> notDeleted, List<String> doNotExist)
065    {
066        Path fileOrFolder = rootResults.resolve(Paths.get(path));
067        if (Files.exists(fileOrFolder))
068        {
069            try
070            {
071                _delete(fileOrFolder);
072                deleted.add(path);
073            }
074            catch (IOException e)
075            {
076                notDeleted.add(path);
077            }
078        }
079        else
080        {
081            doNotExist.add(path);
082        }
083    }
084    
085    private static void _delete(Path fileOrFolder) throws IOException
086    {
087        if (Files.isDirectory(fileOrFolder))
088        {
089            FileUtils.deleteDirectory(fileOrFolder.toFile());
090        }
091        else
092        {
093            Files.delete(fileOrFolder);
094        }
095    }
096}
097