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;
019
020import org.apache.avalon.framework.service.ServiceException;
021import org.apache.avalon.framework.service.ServiceManager;
022import org.apache.xpath.XPathAPI;
023import org.w3c.dom.Element;
024import org.w3c.dom.Node;
025
026import org.ametys.cms.repository.ModifiableContent;
027import org.ametys.plugins.newsletter.NewsletterDAO;
028import org.ametys.plugins.repository.data.extractor.ValuesExtractor;
029import org.ametys.plugins.repository.data.extractor.xml.ModelLessXMLValuesExtractor;
030import org.ametys.plugins.repository.data.extractor.xml.XMLValuesExtractorAdditionalDataGetter;
031import org.ametys.plugins.repository.data.holder.ModifiableModelLessDataHolder;
032import org.ametys.plugins.repository.data.type.ModelItemTypeExtensionPoint;
033
034/**
035 * Generates SAX events for Content, including category, automatic and sent for Newsletters.
036 * TODO NEWATTRIBUTEAPI_CONTENT: do not use type implementation but the ModelAwareDataHolder#getInternalValue when this API exist
037 */
038public class ContentExtractor extends org.ametys.web.content.ContentExtractor
039{
040    /** Avalon role. */
041    public static final String NEWSLETTER_CONTENT_EXTACTOR_ROLE = ContentExtractor.class.getName();
042    
043    private static final String __CATEGORY_METADATA_NAME = "category";
044    private static final String __AUTOMATIC_METADATA_NAME = "automatic";
045    private static final String __SENT_METADATA_NAME = "sent";
046    
047    private ModelItemTypeExtensionPoint _metadataTypesExtensionPoints;
048    
049    @Override
050    public void service(ServiceManager manager) throws ServiceException
051    {
052        super.service(manager);
053        _metadataTypesExtensionPoints = (ModelItemTypeExtensionPoint) manager.lookup(ModelItemTypeExtensionPoint.ROLE_MODEL_LESS_BASIC);
054    }
055    
056    @Override
057    public void fillContent(ModifiableContent content, Node node, XMLValuesExtractorAdditionalDataGetter additionalDataGetter) throws Exception
058    {
059        super.fillContent(content, node, additionalDataGetter);
060        
061        if (Arrays.asList(content.getTypes()).contains(NewsletterDAO.__NEWSLETTER_CONTENT_TYPE))
062        {
063            Element contentElement = (Element) XPathAPI.selectSingleNode(node, "content");
064            fillMetadata(content, contentElement, additionalDataGetter);
065        }
066    }
067    
068    /**
069     * Fills the given newsletter with the metadata from the provided {@link Element}
070     * @param content The newsletter to fill
071     * @param contentElement the element to read to get the metadata
072     * @param additionalDataGetter The object that will retrieve potential additional data for the content's metadata
073     * @throws Exception if an error occurs
074     */
075    protected void fillMetadata(ModifiableContent content, Element contentElement, XMLValuesExtractorAdditionalDataGetter additionalDataGetter) throws Exception
076    {
077        ModifiableModelLessDataHolder internalDataHolder = content.getInternalDataHolder();
078        ValuesExtractor valuesExtractor = new ModelLessXMLValuesExtractor(contentElement, additionalDataGetter, _metadataTypesExtensionPoints);
079        
080        Object category = valuesExtractor.extractValue(__CATEGORY_METADATA_NAME);
081        internalDataHolder.setValue(__CATEGORY_METADATA_NAME, category);
082        
083        Object automatic = valuesExtractor.extractValue(__AUTOMATIC_METADATA_NAME);
084        internalDataHolder.setValue(__AUTOMATIC_METADATA_NAME, automatic);
085        
086        Object sent = valuesExtractor.extractValue(__SENT_METADATA_NAME);
087        internalDataHolder.setValue(__SENT_METADATA_NAME, sent);
088    }
089}