001/*
002 *  Copyright 2010 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.skineditor.skin;
017
018import java.util.ArrayList;
019import java.util.Map;
020
021import org.apache.cocoon.environment.Request;
022import org.apache.commons.lang3.ArrayUtils;
023import org.apache.excalibur.source.SourceException;
024import org.apache.excalibur.source.TraversableSource;
025
026import org.ametys.plugins.core.ui.parameter.files.AbstractGetFilesAction;
027
028/**
029 * Action to get child files and folder of a skin directory
030 *
031 */
032public class GetTempSkinResourcesAction extends AbstractGetFilesAction
033{
034    static final String[] __IGNORED_SOURCE = new String[] {".svn", ".cvs", ".lock", "CVS"};
035    
036    @Override
037    protected String getRootURI(Request request)
038    {
039        String skinName = request.getParameter("skinName");
040        return "ametys-home://skins/temp/" + skinName;
041    }
042    
043    @Override
044    protected boolean isIgnoredSource(TraversableSource source)
045    {
046        return ArrayUtils.contains(__IGNORED_SOURCE, source.getName());
047    }
048    
049    @Override
050    protected Map<String, Object> _collection2JsonObject(TraversableSource folder, TraversableSource root)
051    {
052        Map<String, Object> object = super._collection2JsonObject(folder, root);
053        object.put("skinName", root.getName());
054        
055        try
056        {
057            if (folder.getChildren().size() == 0)
058            {
059                object.put("nodes", new ArrayList<>());
060            }
061        }
062        catch (SourceException e)
063        {
064            getLogger().warn("Unable to list the children of the folder '" + folder.getURI() + "'.", e);
065        }
066        
067        return object;
068    }
069    
070    @Override
071    protected Map<String, Object> _resource2JsonObject(TraversableSource file, TraversableSource root)
072    {
073        Map<String, Object> object = super._resource2JsonObject(file, root);
074        object.put("skinName", root.getName());
075        return object;
076    }
077}