001/*
002 *  Copyright 2017 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.calendar.events;
017
018import java.io.IOException;
019import java.util.ArrayList;
020import java.util.Collection;
021import java.util.Date;
022import java.util.List;
023
024import org.apache.avalon.framework.service.ServiceException;
025import org.apache.avalon.framework.service.ServiceManager;
026import org.apache.cocoon.components.source.impl.SitemapSource;
027import org.apache.cocoon.generation.ServiceableGenerator;
028import org.apache.cocoon.xml.AttributesImpl;
029import org.apache.cocoon.xml.XMLUtils;
030import org.apache.excalibur.source.SourceResolver;
031import org.joda.time.DateTime;
032import org.joda.time.format.ISODateTimeFormat;
033import org.xml.sax.ContentHandler;
034import org.xml.sax.SAXException;
035
036import org.ametys.cms.repository.Content;
037import org.ametys.core.util.IgnoreRootHandler;
038import org.ametys.runtime.i18n.I18nizableText;
039import org.ametys.web.filter.ContentFilterHelper;
040import org.ametys.web.filter.WebContentFilter;
041import org.ametys.web.repository.content.WebContent;
042import org.ametys.web.repository.page.Page;
043
044/**
045 * Abstract generator provinding methods to sax an event content
046 */
047public abstract class AbstractEventGenerator extends ServiceableGenerator
048{
049    /** The source resolver. */
050    protected SourceResolver _resolver;
051    
052    /** The content filter helper. */
053    protected ContentFilterHelper _filterHelper;
054    
055    @Override
056    public void service(ServiceManager serviceManager) throws ServiceException
057    {
058        super.service(serviceManager);
059        _resolver = (SourceResolver) serviceManager.lookup(SourceResolver.ROLE);
060        _filterHelper = (ContentFilterHelper) serviceManager.lookup(ContentFilterHelper.ROLE);
061    }
062    
063    /**
064     * SAX a content
065     * 
066     * @param handler The content handler to SAX into
067     * @param content The content.
068     * @param saxContentItSelf true to sax the content, false will only sax some meta
069     * @param filter The filter. Can be null if saxContentItSelf is false
070     * @param checkUserAccess True to check user access when saxing the content itself
071     * @throws SAXException If an error occurs while SAXing
072     * @throws IOException If an error occurs while retrieving content.
073     */
074    public void saxContent(ContentHandler handler, Content content, boolean saxContentItSelf, WebContentFilter filter, boolean checkUserAccess) throws SAXException, IOException
075    {
076        Date start = content.getMetadataHolder().getDate(EventsFilterHelper.START_DATE_META, null);
077        Date end = content.getMetadataHolder().getDate(EventsFilterHelper.END_DATE_META, null);
078
079        List<String> params = new ArrayList<>();
080        params.add(content.getTitle());
081
082        AttributesImpl attrs = new AttributesImpl();
083        if (start != null)
084        {
085            String startStr = ISODateTimeFormat.date().print(new DateTime(start));
086            params.add(startStr);
087            attrs.addCDATAAttribute("start", startStr);
088        }
089        if (end != null)
090        {
091            String endStr = ISODateTimeFormat.date().print(new DateTime(end));
092            params.add(endStr);
093            attrs.addCDATAAttribute("end", endStr);
094        }
095
096        XMLUtils.startElement(handler, "event", attrs);
097
098        String key = end == null ? "CALENDAR_SERVICE_AGENDA_EVENT_TITLE_SINGLE_DAY" : "CALENDAR_SERVICE_AGENDA_FROM_TO";
099        I18nizableText description = new I18nizableText(null, key, params);
100
101        description.toSAX(handler, "description");
102
103        if (saxContentItSelf)
104        {
105            // Link view.
106            XMLUtils.startElement(handler, "view");
107            _filterHelper.saxContent(handler, content, filter.getView(), checkUserAccess);
108            XMLUtils.endElement(handler, "view");
109        }
110
111        // XML full view.
112        saxXMLContent(handler, content, "main");
113
114        if (content instanceof WebContent)
115        {
116            WebContent webContent = (WebContent) content;
117
118            XMLUtils.startElement(handler, "pages");
119            Collection<Page> pages = webContent.getReferencingPages();
120            for (Page page : pages)
121            {
122                AttributesImpl atts = new AttributesImpl();
123                atts.addCDATAAttribute("id", page.getId());
124                atts.addCDATAAttribute("name", page.getName());
125                atts.addCDATAAttribute("lang", page.getSitemapName());
126                atts.addCDATAAttribute("site", page.getSiteName());
127                atts.addCDATAAttribute("path", page.getPathInSitemap());
128                atts.addCDATAAttribute("title", page.getTitle());
129                XMLUtils.createElement(handler, "page", atts);
130            }
131            XMLUtils.endElement(handler, "pages");
132        }
133        XMLUtils.endElement(handler, "event");
134    }
135    
136    /**
137     * SAX a content in XML mode.
138     * 
139     * @param handler The content handler to SAX into
140     * @param content The content to SAX
141     * @param metadataSetName The metadata set to use
142     * @throws SAXException If an error occurs while SAXing
143     * @throws IOException If an error occurs while retrieving content.
144     */
145    public void saxXMLContent(ContentHandler handler, Content content, String metadataSetName) throws SAXException, IOException
146    {
147        String uri = "cocoon://_content.xml?contentId=" + content.getId() + "&metadataSetName=" + metadataSetName;
148        SitemapSource src = null;
149
150        try
151        {
152            src = (SitemapSource) _resolver.resolveURI(uri);
153            src.toSAX(new IgnoreRootHandler(handler));
154        }
155        finally
156        {
157            _resolver.release(src);
158        }
159    }
160}