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.cms.search.cocoon;
017
018import java.io.IOException;
019import java.util.Collection;
020import java.util.Locale;
021import java.util.Map;
022
023import org.apache.avalon.framework.context.Context;
024import org.apache.avalon.framework.context.ContextException;
025import org.apache.avalon.framework.context.Contextualizable;
026import org.apache.avalon.framework.service.ServiceException;
027import org.apache.avalon.framework.service.ServiceManager;
028import org.apache.cocoon.ProcessingException;
029import org.apache.cocoon.components.ContextHelper;
030import org.apache.cocoon.environment.Request;
031import org.apache.cocoon.generation.ServiceableGenerator;
032import org.apache.cocoon.i18n.I18nUtils;
033import org.apache.cocoon.xml.AttributesImpl;
034import org.apache.cocoon.xml.XMLUtils;
035import org.apache.commons.lang3.StringUtils;
036import org.xml.sax.SAXException;
037
038import org.ametys.cms.repository.Content;
039import org.ametys.cms.search.model.ResultField;
040import org.ametys.cms.search.ui.model.SearchUIColumn;
041
042/**
043 * Generate contents returned by the {@link SearchAction}.
044 */
045public class ContentDocGenerator extends ServiceableGenerator implements Contextualizable
046{
047    /** Context */
048    protected Context _context;
049    /** Helper for saxing result */
050    protected ContentResultSetHelper _contentRSH;
051
052    @Override
053    public void service(ServiceManager smanager) throws ServiceException
054    {
055        super.service(smanager);
056
057        _contentRSH = (ContentResultSetHelper) smanager.lookup(ContentResultSetHelper.ROLE);
058    }
059    @SuppressWarnings("unchecked")
060    public void generate() throws IOException, SAXException, ProcessingException
061    {
062        Request request = ContextHelper.getRequest(_context);
063        Content content = (Content) request.getAttribute(Content.class.getName());
064        
065        Collection<? extends ResultField> resultFields = (Collection< ? extends ResultField>) request.getAttribute(ResultField.class.getName());
066
067        contentHandler.startDocument();
068        
069        XMLUtils.startElement(contentHandler, "content");
070        
071        Map<String, SearchUIColumn> uiResultFields = (Map<String, SearchUIColumn>) request.getAttribute("uiResultFields");
072        _saxColumns(uiResultFields);
073        
074        Locale defaultLocale = getDefaultLocale(request);
075        
076        AttributesImpl attrs = new AttributesImpl();
077        attrs.addCDATAAttribute("id", content.getId());
078        attrs.addCDATAAttribute("name", content.getName());
079        attrs.addCDATAAttribute("title", content.getTitle(defaultLocale));
080        if (content.getLanguage() != null)
081        {
082            attrs.addCDATAAttribute("language", content.getLanguage());
083        }
084        XMLUtils.startElement(contentHandler, "content", attrs);
085        _contentRSH.saxResultFields(contentHandler, content, resultFields, defaultLocale);
086        XMLUtils.endElement(contentHandler, "content");
087        
088        XMLUtils.endElement(contentHandler, "content");
089        contentHandler.endDocument();
090    }
091
092    public void contextualize(Context context) throws ContextException
093    {
094        _context = context;
095    }
096    
097    /**
098     * Get the default locale to use to sax localized values
099     * @param request the request
100     * @return the default locale
101     */
102    protected Locale getDefaultLocale(Request request)
103    {
104        String lang = parameters.getParameter("lang", request.getParameter("lang"));
105        return StringUtils.isNotEmpty(lang) ? new Locale(lang) : I18nUtils.findLocale(objectModel, "locale", null, Locale.getDefault(), true);
106    }
107    
108    private void _saxColumns(Map<String, SearchUIColumn> resultFields) throws SAXException
109    {
110        XMLUtils.startElement(contentHandler, "fields");
111        for (SearchUIColumn column : resultFields.values())
112        {
113            AttributesImpl attrs = new AttributesImpl();
114            // cf org.ametys.cms.search.ui.model.SearchUIModelHelper.getColumnInfo(SearchUIColumn)
115            attrs.addCDATAAttribute("id", column.getId().replace('.', '/'));
116            
117            if (column.getType() != null)
118            {
119                attrs.addCDATAAttribute("type", column.getType().toString().toLowerCase());
120            }
121            XMLUtils.startElement(contentHandler, "field", attrs);
122            column.getLabel().toSAX(contentHandler);
123            XMLUtils.endElement(contentHandler, "field");
124        }
125        XMLUtils.endElement(contentHandler, "fields");
126    }
127}