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;
031
032/**
033 * Component for operations on extraction folders
034 */
035public class FoldersClientSideElement extends StaticClientSideElement
036{
037    private FileHelper _fileHelper;
038    private SourceResolver _srcResolver;
039    
040    @Override
041    public void service(ServiceManager serviceManager) throws ServiceException
042    {
043        super.service(serviceManager);
044        _fileHelper = (FileHelper) serviceManager.lookup(FileHelper.ROLE);
045        _srcResolver = (org.apache.excalibur.source.SourceResolver) serviceManager.lookup(org.apache.excalibur.source.SourceResolver.ROLE);        
046    }
047
048    /**
049     * Add a new folder
050     * @param parentRelPath the relative parent file's path from parameters files root directory
051     * @param name The name of folder to create
052     * @return a map containing the name of the created folder, its path and the path of its parent
053     * @throws IOException If an error occurred while creating folder
054     */
055    @Callable (right = ExtractionConstants.MODIFY_EXTRACTION_RIGHT_ID)
056    public Map<String, Object> addFolder(String parentRelPath, String name) throws IOException 
057    {
058        String nonNullParentRelPath = StringUtils.defaultString(parentRelPath);
059        
060        FileSource rootDir = (FileSource) _srcResolver.resolveURI(ExtractionConstants.DEFINITIONS_DIR);
061        if (!rootDir.exists())
062        {
063            rootDir.getFile().mkdirs();
064        }
065        
066        String parentURI = ExtractionConstants.DEFINITIONS_DIR + (StringUtils.isNotEmpty(nonNullParentRelPath) ? "/" + nonNullParentRelPath : "");
067        
068        Map<String, Object> result = _fileHelper.addFolder(parentURI, name, true);
069        
070        if (result.containsKey("uri"))
071        {
072            String folderUri = (String) result.get("uri");
073            String path = folderUri.substring(rootDir.getURI().length());
074            result.put("path", path.endsWith("/") ? path.substring(0, path.length() - 1) : path);
075            result.put("parentPath", nonNullParentRelPath.endsWith("/") ? nonNullParentRelPath.substring(0, nonNullParentRelPath.length() - 1) : nonNullParentRelPath);
076        }
077
078        return result;
079    }
080    
081    /**
082    * Remove a folder or a file
083    * @param relPath the relative file's path from parameters files root directory
084    * @return the result map
085    * @throws IOException If an error occurs while removing the folder
086    */
087    @Callable (right = ExtractionConstants.MODIFY_EXTRACTION_RIGHT_ID)
088    public Map<String, Object> deleteFile(String relPath) throws IOException
089    {
090        String fileUri = ExtractionConstants.DEFINITIONS_DIR + (relPath.length() > 0 ? "/" + relPath : "");
091        return _fileHelper.deleteFile(fileUri);
092    }
093    
094    /**
095    * Rename a file or a folder 
096    * @param relPath the relative file's path from parameters files root directory
097    * @param name the new name of the file/folder
098    * @return the result map
099    * @throws IOException if an error occurs while renaming the file/folder
100    */
101    @Callable (right = ExtractionConstants.MODIFY_EXTRACTION_RIGHT_ID)
102    public Map<String, Object> renameFile(String relPath, String name) throws IOException 
103    {
104        FileSource rootDir = (FileSource) _srcResolver.resolveURI(ExtractionConstants.DEFINITIONS_DIR);
105        
106        String fileUri = ExtractionConstants.DEFINITIONS_DIR + (relPath.length() > 0 ? "/" + relPath : "");
107        Map<String, Object> result = _fileHelper.renameFile(fileUri, name);
108        
109        if (result.containsKey("uri"))
110        {
111            String newURI = (String) result.get("uri");
112            String path = newURI.substring(rootDir.getURI().length());
113            result.put("path", path);
114        }
115        return result;
116    }
117}