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.repository.workspace;
017
018import java.io.IOException;
019import java.util.Iterator;
020import java.util.Map;
021
022import org.apache.avalon.framework.parameters.Parameters;
023import org.apache.avalon.framework.service.ServiceException;
024import org.apache.avalon.framework.service.ServiceManager;
025import org.apache.avalon.framework.service.Serviceable;
026import org.apache.cocoon.ProcessingException;
027import org.apache.cocoon.environment.SourceResolver;
028import org.apache.cocoon.generation.AbstractGenerator;
029import org.apache.cocoon.xml.AttributesImpl;
030import org.apache.cocoon.xml.XMLUtils;
031import org.apache.commons.lang3.StringUtils;
032import org.xml.sax.SAXException;
033import org.ametys.plugins.repository.AmetysObject;
034import org.ametys.plugins.repository.AmetysObjectIterable;
035import org.ametys.plugins.repository.AmetysObjectResolver;
036
037/**
038 * Generate a list of path from an XPath query
039 */
040public class QueryLogicResultGenerator extends AbstractGenerator implements Serviceable
041{
042    private AmetysObjectResolver _resolver;
043    
044    public void service(ServiceManager manager) throws ServiceException
045    {
046        _resolver = (AmetysObjectResolver) manager.lookup(AmetysObjectResolver.ROLE);
047    }
048    
049    @Override
050    public void setup(SourceResolver res, Map objModel, String src, Parameters params) throws ProcessingException, SAXException, IOException
051    {
052        super.setup(res, objModel, src, params);
053    }
054
055    public void generate() throws IOException, SAXException, ProcessingException
056    {
057        
058        if (getLogger().isInfoEnabled())
059        {
060            getLogger().info("Trying to generate results for a XPath query (logic view)");
061        }
062        
063        contentHandler.startDocument();
064        
065        XMLUtils.startElement(contentHandler, "resultList");
066        
067        String query = parameters.getParameter("query", "");
068        
069        if (StringUtils.isNotBlank(query))
070        {
071            try
072            {
073                _populateQueryResult(query);
074            }
075            catch (Exception e)
076            {
077                getLogger().error("Unable to execute logic query '" + query + "'", e);
078                XMLUtils.createElement(contentHandler, "error", e.toString());
079            }
080        }
081        
082        XMLUtils.endElement(contentHandler, "resultList");
083        
084        contentHandler.endDocument();
085    }
086
087
088    private void _populateQueryResult(String query) throws SAXException
089    {
090        AmetysObjectIterable<AmetysObject> itResults = _resolver.query(query);
091        Iterator<AmetysObject> it = itResults.iterator();
092        
093        while (it.hasNext())
094        {
095            AmetysObject ao = it.next();
096            
097            AttributesImpl attr = new AttributesImpl();
098            attr.addCDATAAttribute("id", ao.getId());
099            attr.addCDATAAttribute("path", ao.getPath());
100            attr.addCDATAAttribute("name", ao.getName());
101            XMLUtils.createElement(contentHandler, "result", attr);
102        }
103    }
104}