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.Locale;
020
021import org.apache.avalon.framework.context.Context;
022import org.apache.avalon.framework.context.ContextException;
023import org.apache.avalon.framework.context.Contextualizable;
024import org.apache.cocoon.ProcessingException;
025import org.apache.cocoon.components.ContextHelper;
026import org.apache.cocoon.environment.Request;
027import org.apache.cocoon.generation.ServiceableGenerator;
028import org.apache.cocoon.i18n.I18nUtils;
029import org.apache.cocoon.xml.AttributesImpl;
030import org.apache.cocoon.xml.XMLUtils;
031import org.apache.commons.lang3.LocaleUtils;
032import org.apache.commons.lang3.StringUtils;
033import org.xml.sax.SAXException;
034
035import org.ametys.cms.repository.Content;
036import org.ametys.runtime.model.ModelViewItem;
037import org.ametys.runtime.model.ViewHelper;
038import org.ametys.runtime.model.ViewItem;
039import org.ametys.runtime.model.ViewItemAccessor;
040import org.ametys.runtime.model.ViewItemContainer;
041import org.ametys.runtime.model.type.DataContext;
042
043/**
044 * Generate contents returned by the {@link SearchAction}.
045 */
046public class ContentDocGenerator extends ServiceableGenerator implements Contextualizable
047{
048    /** Context */
049    protected Context _context;
050
051    public void generate() throws IOException, SAXException, ProcessingException
052    {
053        Request request = ContextHelper.getRequest(_context);
054        Content content = (Content) request.getAttribute(Content.class.getName());
055        
056        ViewItemContainer resultItems = (ViewItemContainer) request.getAttribute("resultItems");
057
058        contentHandler.startDocument();
059        
060        XMLUtils.startElement(contentHandler, "content");
061        
062        XMLUtils.startElement(contentHandler, "fields");
063        ViewItemContainer uiResultItems = (ViewItemContainer) request.getAttribute("uiResultItems");
064        _saxColumns(uiResultItems);
065        XMLUtils.endElement(contentHandler, "fields");
066        
067        Locale defaultLocale = getDefaultLocale(request);
068        
069        AttributesImpl attrs = new AttributesImpl();
070        attrs.addCDATAAttribute("id", content.getId());
071        attrs.addCDATAAttribute("name", content.getName());
072        attrs.addCDATAAttribute("title", content.getTitle(defaultLocale));
073        if (content.getLanguage() != null)
074        {
075            attrs.addCDATAAttribute("language", content.getLanguage());
076        }
077        XMLUtils.startElement(contentHandler, "content", attrs);
078        
079        DataContext context = DataContext.newInstance()
080                                         .withLocale(defaultLocale)
081                                         .withEmptyValues(false);
082        content.dataToSAX(contentHandler, resultItems, context);
083        
084        XMLUtils.endElement(contentHandler, "content");
085        
086        XMLUtils.endElement(contentHandler, "content");
087        contentHandler.endDocument();
088    }
089
090    public void contextualize(Context context) throws ContextException
091    {
092        _context = context;
093    }
094    
095    /**
096     * Get the default locale to use to sax localized values
097     * @param request the request
098     * @return the default locale
099     */
100    protected Locale getDefaultLocale(Request request)
101    {
102        String lang = parameters.getParameter("lang", request.getParameter("lang"));
103        return StringUtils.isNotEmpty(lang) ? LocaleUtils.toLocale(lang) : I18nUtils.findLocale(objectModel, "locale", null, Locale.getDefault(), true);
104    }
105    
106    private void _saxColumns(ViewItemAccessor viewItemAccessor) throws SAXException
107    {
108        for (ViewItem viewItem : viewItemAccessor.getViewItems())
109        {
110            if (viewItem instanceof ViewItemAccessor itemAccessor && !itemAccessor.getViewItems().isEmpty())
111            {
112                _saxColumns(itemAccessor);
113            }
114            else if (viewItem instanceof ModelViewItem modelViewItem)
115            {
116                AttributesImpl attrs = new AttributesImpl();
117                attrs.addCDATAAttribute("id", ViewHelper.getModelViewItemPath(modelViewItem));
118                attrs.addCDATAAttribute("type", modelViewItem.getDefinition().getType().getId());
119                
120                XMLUtils.startElement(contentHandler, "field", attrs);
121                modelViewItem.getLabel().toSAX(contentHandler);
122                XMLUtils.endElement(contentHandler, "field");
123            }
124        }
125    }
126}