001/*
002 *  Copyright 2019 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.edition;
017
018import java.io.IOException;
019import java.util.Map;
020
021import org.apache.avalon.framework.service.ServiceException;
022import org.apache.avalon.framework.service.ServiceManager;
023import org.apache.commons.lang3.StringUtils;
024import org.apache.excalibur.source.SourceResolver;
025import org.apache.excalibur.source.impl.FileSource;
026
027import org.ametys.core.file.FileHelper;
028import org.ametys.core.ui.Callable;
029import org.ametys.core.ui.StaticClientSideElement;
030import org.ametys.plugins.extraction.ExtractionConstants;
031import org.ametys.plugins.extraction.execution.ExtractionDAO;
032import org.ametys.plugins.extraction.rights.ExtractionAccessController;
033
034/**
035 * Component for operations on extraction folders
036 */
037public class FoldersClientSideElement extends StaticClientSideElement
038{
039    private FileHelper _fileHelper;
040    private SourceResolver _srcResolver;
041    private ExtractionDAO _extractionDAO;
042    
043    @Override
044    public void service(ServiceManager serviceManager) throws ServiceException
045    {
046        super.service(serviceManager);
047        _fileHelper = (FileHelper) serviceManager.lookup(FileHelper.ROLE);
048        _srcResolver = (org.apache.excalibur.source.SourceResolver) serviceManager.lookup(org.apache.excalibur.source.SourceResolver.ROLE);        
049        _extractionDAO = (ExtractionDAO) serviceManager.lookup(ExtractionDAO.ROLE);
050    }
051
052    /**
053     * Add a new folder
054     * @param parentRelPath the relative parent file's path from parameters files root directory
055     * @param name The name of folder to create
056     * @return a map containing the name of the created folder, its path and the path of its parent
057     * @throws IOException If an error occurred while creating folder
058     */
059    @Callable (rights = ExtractionConstants.MODIFY_EXTRACTION_RIGHT_ID)
060    public Map<String, Object> addFolder(String parentRelPath, String name) throws IOException 
061    {
062        String nonNullParentRelPath = StringUtils.defaultString(parentRelPath);
063        
064        FileSource rootDir = (FileSource) _srcResolver.resolveURI(ExtractionConstants.DEFINITIONS_DIR);
065        if (!rootDir.exists())
066        {
067            rootDir.getFile().mkdirs();
068        }
069        
070        String parentURI = ExtractionConstants.DEFINITIONS_DIR + (StringUtils.isNotEmpty(nonNullParentRelPath) ? "/" + nonNullParentRelPath : "");
071        
072        Map<String, Object> result = _fileHelper.addFolder(parentURI, name, true);
073        
074        if (result.containsKey("uri"))
075        {
076            String folderUri = (String) result.get("uri");
077            // Get only the part after the root folder to get the relative path 
078            String path = StringUtils.substringAfter(folderUri, rootDir.getURI());
079            result.put("path", ExtractionDAO.trimLastFileSeparator(path));
080            result.put("parentPath", ExtractionDAO.trimLastFileSeparator(nonNullParentRelPath));
081        }
082
083        return result;
084    }
085    
086    /**
087    * Remove a folder or a file
088    * @param relPath the relative file's path from parameters files root directory
089    * @return the result map
090    * @throws IOException If an error occurs while removing the folder
091    */
092    @Callable (rights = ExtractionConstants.MODIFY_EXTRACTION_RIGHT_ID)
093    public Map<String, Object> deleteFile(String relPath) throws IOException
094    {
095        String fileUri = ExtractionConstants.DEFINITIONS_DIR + (relPath.length() > 0 ? "/" + relPath : "");
096        String context = ExtractionAccessController.ROOT_CONTEXT + "/" + relPath;
097        FileSource folderToDelete = (FileSource) _srcResolver.resolveURI(fileUri);
098        _extractionDAO.deleteRightsRecursively(context, folderToDelete);
099        return _fileHelper.deleteFile(fileUri);
100    }
101    
102    /**
103    * Rename a file or a folder 
104    * @param relPath the relative file's path from parameters files root directory
105    * @param name the new name of the file/folder
106    * @return the result map
107    * @throws IOException if an error occurs while renaming the file/folder
108    */
109    @Callable (rights = ExtractionConstants.MODIFY_EXTRACTION_RIGHT_ID)
110    public Map<String, Object> renameFile(String relPath, String name) throws IOException 
111    {
112        String fileUri = ExtractionConstants.DEFINITIONS_DIR + relPath;
113        FileSource file = (FileSource) _srcResolver.resolveURI(fileUri);
114        String relativeParentPath = StringUtils.removeEnd(relPath, file.getName());
115        String relativeNewFilePath = relativeParentPath + name;
116        return _extractionDAO.moveOrRenameExtractionDefinitionFile(relPath, relativeNewFilePath);
117    }
118}