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    // FIXME no read access is checked as resource tree does not filter resources on read access
054    @Callable (rights = Callable.NO_CHECK_REQUIRED) 
055    public Map<String, Object> getImages (String id)
056    {
057        Map<String, Object> results = new HashMap<>();
058        
059        AmetysObject ao = _resolver.resolveById(id);
060        
061        List<String> images = new ArrayList<>();
062        
063        ResourceCollection collection = null;
064        if (ao instanceof ResourceCollection)
065        {
066            collection = (ResourceCollection) ao;
067        }
068        else if (ao instanceof Resource)
069        {
070            collection = ao.getParent();
071        }
072        
073        if (collection != null)
074        {
075            for (AmetysObject child : collection.getChildren())
076            {
077                if (child instanceof Resource)
078                {
079                    Resource resource = (Resource) child;
080                    if (_isImage(resource))
081                    {
082                        images.add (resource.getId());
083                    }
084                }
085            }
086        }
087        
088        results.put("images", images);
089        return results;
090    }
091    
092    private boolean _isImage (Resource resource)
093    {
094        String filename = resource.getName().toLowerCase();
095        return filename.endsWith(".jpeg") || filename.endsWith(".jpg") || filename.endsWith(".gif") || filename.endsWith(".png");
096    }
097
098}