001/*
002 *  Copyright 2015 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.cms.search.cocoon;
017
018import java.io.IOException;
019import java.util.Map;
020
021import org.apache.avalon.framework.service.ServiceException;
022import org.apache.avalon.framework.service.ServiceManager;
023import org.apache.cocoon.ProcessingException;
024import org.apache.cocoon.generation.ServiceableGenerator;
025import org.apache.cocoon.xml.AttributesImpl;
026import org.apache.cocoon.xml.XMLUtils;
027import org.xml.sax.SAXException;
028
029import org.ametys.cms.search.ui.model.SearchUIColumn;
030import org.ametys.cms.search.ui.model.SearchUIModel;
031import org.ametys.cms.search.ui.model.SearchUIModelExtensionPoint;
032import org.ametys.core.util.ServerCommHelper;
033
034/**
035 * SAX the columns of a search tool model.
036 */
037public class ModelColumnsGenerator extends ServiceableGenerator
038{
039    private SearchUIModelExtensionPoint _searchModelManager;
040    private ServerCommHelper _serverCommHelper;
041    
042    @Override
043    public void service(ServiceManager smanager) throws ServiceException
044    {
045        super.service(smanager);
046        _searchModelManager = (SearchUIModelExtensionPoint) smanager.lookup(SearchUIModelExtensionPoint.ROLE);
047        _serverCommHelper = (ServerCommHelper) manager.lookup(ServerCommHelper.ROLE);
048    }
049    
050    @Override
051    public void generate() throws IOException, SAXException, ProcessingException
052    {
053        Map<String, Object> jsParameters = _serverCommHelper.getJsParameters();
054        
055        String modelId = (String) jsParameters.get("model");
056        SearchUIModel model = _searchModelManager.getExtension(modelId);
057        
058        @SuppressWarnings("unchecked")
059        Map<String, Object> contextualParameters = (Map<String, Object>) jsParameters.get("contextualParameters");
060        
061        contentHandler.startDocument();
062        
063        AttributesImpl attr = new AttributesImpl();
064        attr.addCDATAAttribute("model", modelId);
065        XMLUtils.startElement(contentHandler, "columns", attr);
066        
067        for (SearchUIColumn column : model.getResultFields(contextualParameters).values())
068        {
069            AttributesImpl attrs = new AttributesImpl();
070            attrs.addCDATAAttribute("id", column.getId());
071            // TODO Restore?
072//            attrs.addCDATAAttribute("mapping", column.getMapping());
073            attrs.addCDATAAttribute("type", column.getType().name());
074            
075            XMLUtils.startElement(contentHandler, "column", attrs);
076            column.getLabel().toSAX(contentHandler);
077            XMLUtils.endElement(contentHandler, "column");
078        }
079        
080        XMLUtils.endElement(contentHandler, "columns");
081        contentHandler.endDocument();
082    }
083
084}