001/*
002 *  Copyright 2020 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.newsletter.content;
017
018import java.util.Arrays;
019import java.util.Locale;
020
021import org.xml.sax.ContentHandler;
022import org.xml.sax.SAXException;
023
024import org.ametys.cms.repository.Content;
025import org.ametys.plugins.newsletter.NewsletterDAO;
026import org.ametys.plugins.repository.data.holder.ModelLessDataHolder;
027import org.ametys.runtime.model.View;
028import org.ametys.runtime.model.type.DataContext;
029
030/**
031 * Generates SAX events for Content, including category, automatic and sent for Newsletters.
032 * TODO NEWATTRIBUTEAPI_CONTENT: do not use type implementation but the ModelAwareDataHolder#getInternalValue when this API exist
033 */
034public class ContentSaxer extends org.ametys.web.content.ContentSaxer
035{
036    /** Avalon role. */
037    public static final String NEWSLETTER_CONTENT_SAXER_ROLE = ContentSaxer.class.getName();
038    
039    private static final String __CATEGORY_METADATA_NAME = "category";
040    private static final String __AUTOMATIC_METADATA_NAME = "automatic";
041    private static final String __SENT_METADATA_NAME = "sent";
042    
043    @Override
044    protected void saxBody(Content content, ContentHandler contentHandler, Locale locale, View view, String tagName, boolean saxWorkflowStep, boolean saxWorkflowInfo,
045            boolean saxLanguageInfo, String attributesTagName, boolean isEdition) throws SAXException
046    {
047        super.saxBody(content, contentHandler, locale, view, tagName, saxWorkflowStep, saxWorkflowInfo, saxLanguageInfo, attributesTagName, isEdition);
048        
049        if (Arrays.asList(content.getTypes()).contains(NewsletterDAO.__NEWSLETTER_CONTENT_TYPE))
050        {
051            saxMetadata(content, contentHandler);
052        }
053    }
054    
055    /**
056     * Generates SAX events for the newsletter's metadata.
057     * @param content the newsletter
058     * @param contentHandler the ContentHandler receiving SAX events.
059     * @throws SAXException if an error occurs during the SAX events generation.
060     */
061    protected void saxMetadata(Content content, ContentHandler contentHandler) throws SAXException
062    {
063        ModelLessDataHolder internalDataHolder = content.getInternalDataHolder();
064        DataContext context = DataContext.newInstance().withEmptyValues(false);
065
066        internalDataHolder.dataToSAX(contentHandler, __CATEGORY_METADATA_NAME, context);
067        internalDataHolder.dataToSAX(contentHandler, __AUTOMATIC_METADATA_NAME, context);
068        internalDataHolder.dataToSAX(contentHandler, __SENT_METADATA_NAME, context);
069    }
070}