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