001/*
002 *  Copyright 2013 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.explorer.clientsideelement;
017
018import java.util.ArrayList;
019import java.util.HashMap;
020import java.util.List;
021import java.util.Map;
022
023import org.apache.avalon.framework.service.ServiceException;
024import org.apache.avalon.framework.service.ServiceManager;
025
026import org.ametys.core.ui.Callable;
027import org.ametys.core.ui.StaticClientSideElement;
028import org.ametys.plugins.explorer.resources.Resource;
029import org.ametys.plugins.explorer.resources.ResourceCollection;
030import org.ametys.plugins.repository.AmetysObject;
031import org.ametys.plugins.repository.AmetysObjectResolver;
032
033/**
034 * This element creates a button which stores id of images contained in selection
035 */
036public class ImagesContainerClientSideElement extends StaticClientSideElement
037{
038    /** Ametys Repository */
039    protected AmetysObjectResolver _resolver;
040    
041    @Override
042    public void service(ServiceManager smanager) throws ServiceException
043    {
044        super.service(smanager);
045        _resolver = (AmetysObjectResolver) smanager.lookup(AmetysObjectResolver.ROLE);
046    }
047    
048    /**
049     * Get the list of direct child images of a ResourceCollection 
050     * @param id The id of the ResourceCollection or a child Resource
051     * @return The list of images id
052     */
053    @Callable
054    public Map<String, Object> getImages (String id)
055    {
056        Map<String, Object> results = new HashMap<>();
057        
058        AmetysObject ao = _resolver.resolveById(id);
059        
060        List<String> images = new ArrayList<>();
061        
062        ResourceCollection collection = null;
063        if (ao instanceof ResourceCollection)
064        {
065            collection = (ResourceCollection) ao;
066        }
067        else if (ao instanceof Resource)
068        {
069            collection = ((Resource) ao).getParent();
070        }
071        
072        if (collection != null)
073        {
074            for (AmetysObject child : collection.getChildren())
075            {
076                if (child instanceof Resource)
077                {
078                    Resource resource = (Resource) child;
079                    if (_isImage(resource))
080                    {
081                        images.add (resource.getId());
082                    }
083                }
084            }
085        }
086        
087        results.put("images", images);
088        return results;
089    }
090    
091    private boolean _isImage (Resource resource)
092    {
093        String filename = resource.getName().toLowerCase();
094        return filename.endsWith(".jpeg") || filename.endsWith(".jpg") || filename.endsWith(".gif") || filename.endsWith(".png");
095    }
096
097}