001/*
002 *  Copyright 2011 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.web.explorer;
017
018import java.io.IOException;
019import java.util.Comparator;
020
021import org.apache.avalon.framework.service.ServiceException;
022import org.apache.avalon.framework.service.ServiceManager;
023import org.apache.cocoon.ProcessingException;
024import org.apache.cocoon.environment.ObjectModelHelper;
025import org.apache.cocoon.environment.Request;
026import org.apache.cocoon.generation.ServiceableGenerator;
027import org.apache.cocoon.xml.XMLUtils;
028import org.xml.sax.SAXException;
029
030import org.ametys.core.util.LambdaUtils;
031import org.ametys.core.util.StringUtils.AlphanumComparator;
032import org.ametys.plugins.explorer.resources.Resource;
033import org.ametys.plugins.explorer.resources.ResourceCollection;
034import org.ametys.plugins.repository.AmetysObject;
035import org.ametys.plugins.repository.AmetysObjectIterable;
036import org.ametys.plugins.repository.AmetysObjectResolver;
037
038/**
039 * Generates the images' id of a folder
040 *
041 */
042public class GetImagesGenerator extends ServiceableGenerator
043{
044    private AmetysObjectResolver _resolver;
045    
046    @Override
047    public void service(ServiceManager smanager) throws ServiceException
048    {
049        super.service(smanager);
050        
051        _resolver = (AmetysObjectResolver) smanager.lookup(AmetysObjectResolver.ROLE);
052    }
053    
054    @Override
055    public void generate() throws IOException, SAXException, ProcessingException
056    {
057        Request request = ObjectModelHelper.getRequest(objectModel);
058        String id = request.getParameter("id");
059        
060        contentHandler.startDocument();
061        XMLUtils.startElement(contentHandler, "images");
062        
063        ResourceCollection collection = _resolver.resolveById(id);
064        AmetysObjectIterable<AmetysObject> children = collection.getChildren();
065        
066        children
067            .stream()
068            .filter(Resource.class::isInstance)
069            .map(Resource.class::cast)
070            .filter(this::_isImage)
071            .sorted(Comparator.comparing(Resource::getName, new AlphanumComparator()))
072            .forEach(LambdaUtils.wrapConsumer(r -> {
073                XMLUtils.createElement(contentHandler, "file", r.getId());
074            }));
075        
076        XMLUtils.endElement(contentHandler, "images");
077        contentHandler.endDocument();
078    }
079    
080    private boolean _isImage (Resource resource)
081    {
082        String filename = resource.getName().toLowerCase();
083        
084        return filename.endsWith(".jpeg") || filename.endsWith(".jpg") || filename.endsWith(".gif") || filename.endsWith(".png");
085    }
086
087}