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.io.IOException;
019import java.util.Arrays;
020import java.util.Locale;
021import java.util.Optional;
022
023import javax.xml.transform.TransformerException;
024
025import org.apache.avalon.framework.service.ServiceException;
026import org.apache.avalon.framework.service.ServiceManager;
027import org.apache.xpath.XPathAPI;
028import org.w3c.dom.Element;
029import org.w3c.dom.Node;
030import org.xml.sax.ContentHandler;
031import org.xml.sax.SAXException;
032
033import org.ametys.cms.repository.Content;
034import org.ametys.cms.repository.ContentAttributeTypeExtensionPoint;
035import org.ametys.cms.repository.ModifiableContent;
036import org.ametys.core.util.dom.DOMUtils;
037import org.ametys.plugins.newsletter.NewsletterDAO;
038import org.ametys.plugins.repository.data.extractor.xml.XMLValuesExtractorAdditionalDataGetter;
039import org.ametys.plugins.repository.data.repositorydata.ModifiableRepositoryData;
040import org.ametys.plugins.repository.data.repositorydata.RepositoryData;
041import org.ametys.plugins.repository.data.repositorydata.impl.JCRRepositoryData;
042import org.ametys.plugins.repository.data.type.RepositoryElementType;
043import org.ametys.plugins.repository.jcr.JCRAmetysObject;
044import org.ametys.runtime.model.View;
045import org.ametys.runtime.model.type.ModelItemTypeConstants;
046
047/**
048 * Generates SAX events for Content, including category, automatic and sent for Newsletters.
049 * TODO NEWATTRIBUTEAPI_CONTENT: do no use type implementation but the ModelAwareDataHolder#getInternalValue when this API exist
050 */
051public class ContentSaxer extends org.ametys.web.content.ContentSaxer
052{
053    private static final String __CATEGORY_METADATA_NAME = "category";
054    private static final String __CATEGORY_TAG_NAME = "category";
055    private static final String __AUTOMATIC_METADATA_NAME = "automatic";
056    private static final String __AUTOMATIC_TAG_NAME = "automatic";
057    private static final String __SENT_METADATA_NAME = "sent";
058    private static final String __SENT_TAG_NAME = "sent";
059    
060    // Content attribute types extension point
061    private ContentAttributeTypeExtensionPoint _contentAttributeTypeExtensionPoint;
062    
063    @Override
064    public void service(ServiceManager manager) throws ServiceException
065    {
066        super.service(manager);
067        _contentAttributeTypeExtensionPoint = (ContentAttributeTypeExtensionPoint) manager.lookup(ContentAttributeTypeExtensionPoint.ROLE);
068    }
069    
070    @Override
071    protected void saxBody(Content content, ContentHandler contentHandler, Locale locale, View view, String tagName, boolean saxWorkflowStep, boolean saxWorkflowInfo,
072            boolean saxLanguageInfo, String attributesTagName) throws SAXException
073    {
074        super.saxBody(content, contentHandler, locale, view, tagName, saxWorkflowStep, saxWorkflowInfo, saxLanguageInfo, attributesTagName);
075        
076        if (Arrays.asList(content.getTypes()).contains(NewsletterDAO.__NEWSLETTER_CONTENT_TYPE))
077        {
078            saxCategory(content, contentHandler);
079            saxAutomatic(content, contentHandler);
080            saxSent(content, contentHandler);
081        }
082    }
083    
084    /**
085     * Generates SAX events for the newsletter's category.
086     * @param content the newsletter
087     * @param contentHandler the ContentHandler receiving SAX events.
088     * @throws SAXException if an error occurs during the SAX events generation.
089     */
090    @SuppressWarnings("unchecked")
091    protected void saxCategory(Content content, ContentHandler contentHandler) throws SAXException
092    {
093        try
094        {
095            if (content instanceof JCRAmetysObject)
096            {
097                RepositoryData contentRepositoryData = new JCRRepositoryData(((JCRAmetysObject) content).getNode());
098                RepositoryElementType<String> stringElementType = (RepositoryElementType<String>) _contentAttributeTypeExtensionPoint.getExtension(ModelItemTypeConstants.STRING_TYPE_ID);
099                
100                if (stringElementType.hasNonEmptyValue(contentRepositoryData, __CATEGORY_METADATA_NAME))
101                {
102                    Object category = stringElementType.read(contentRepositoryData, __CATEGORY_METADATA_NAME);
103                    stringElementType.valueToSAX(contentHandler, __CATEGORY_TAG_NAME, category, null);
104                }
105            }
106        }
107        catch (IOException e)
108        {
109            throw new RuntimeException(e);
110        }
111    }
112    
113    /**
114     * Generates SAX events for the newsletter's automatic status.
115     * @param content the newsletter
116     * @param contentHandler the ContentHandler receiving SAX events.
117     * @throws SAXException if an error occurs during the SAX events generation.
118     */
119    @SuppressWarnings("unchecked")
120    protected void saxAutomatic(Content content, ContentHandler contentHandler) throws SAXException
121    {
122        try
123        {
124            if (content instanceof JCRAmetysObject)
125            {
126                RepositoryData contentRepositoryData = new JCRRepositoryData(((JCRAmetysObject) content).getNode());
127                RepositoryElementType<String> stringElementType = (RepositoryElementType<String>) _contentAttributeTypeExtensionPoint.getExtension(ModelItemTypeConstants.STRING_TYPE_ID);
128                RepositoryElementType<Boolean> booleanElementType = (RepositoryElementType<Boolean>) _contentAttributeTypeExtensionPoint.getExtension(ModelItemTypeConstants.BOOLEAN_TYPE_ID);
129                
130                // The boolean is sometimes stored as a String :/
131                if (contentRepositoryData.hasValue(__AUTOMATIC_METADATA_NAME))
132                {
133                    RepositoryElementType type = booleanElementType.isCompatible(contentRepositoryData, __AUTOMATIC_METADATA_NAME) ? booleanElementType : stringElementType;
134                    
135                    if (type.hasNonEmptyValue(contentRepositoryData, __AUTOMATIC_METADATA_NAME))
136                    {
137                        Object automatic = type.read(contentRepositoryData, __AUTOMATIC_METADATA_NAME);
138                        if (automatic instanceof String)
139                        {
140                            automatic = Boolean.valueOf((String) automatic);
141                        }
142                        booleanElementType.valueToSAX(contentHandler, __AUTOMATIC_TAG_NAME, automatic, null);
143                    }
144                }
145            }
146        }
147        catch (IOException e)
148        {
149            throw new RuntimeException(e);
150        }
151    }
152    
153    /**
154     * Generates SAX events for the newsletter's sent status.
155     * @param content the newsletter
156     * @param contentHandler the ContentHandler receiving SAX events.
157     * @throws SAXException if an error occurs during the SAX events generation.
158     */
159    @SuppressWarnings("unchecked")
160    protected void saxSent(Content content, ContentHandler contentHandler) throws SAXException
161    {
162        try
163        {
164            if (content instanceof JCRAmetysObject)
165            {
166                RepositoryData contentRepositoryData = new JCRRepositoryData(((JCRAmetysObject) content).getNode());
167                RepositoryElementType<Boolean> booleanElementType = (RepositoryElementType<Boolean>) _contentAttributeTypeExtensionPoint.getExtension(ModelItemTypeConstants.BOOLEAN_TYPE_ID);
168                
169                if (booleanElementType.hasNonEmptyValue(contentRepositoryData, __SENT_METADATA_NAME))
170                {
171                    Object sent = booleanElementType.read(contentRepositoryData, __SENT_METADATA_NAME);
172                    booleanElementType.valueToSAX(contentHandler, __SENT_TAG_NAME, sent, null);
173                }
174            }
175        }
176        catch (IOException e)
177        {
178            throw new RuntimeException(e);
179        }
180    }
181    
182    @Override
183    public void fillContent(ModifiableContent content, Node node, XMLValuesExtractorAdditionalDataGetter additionalDataGetter) throws Exception
184    {
185        super.fillContent(content, node, additionalDataGetter);
186        
187        if (Arrays.asList(content.getTypes()).contains(NewsletterDAO.__NEWSLETTER_CONTENT_TYPE))
188        {
189            Element contentElement = (Element) XPathAPI.selectSingleNode(node, "content");
190            fillCategory(content, contentElement);
191            fillAutomatic(content, contentElement);
192            fillSent(content, contentElement);
193        }
194    }
195    
196    /**
197     * Fills the given newsletter with the category from the provided {@link Element}
198     * @param content The newsletter to fill
199     * @param contentElement the element to read to get the category
200     * @throws TransformerException if an error occurs
201     * @throws IOException if an error occurs while reading the XML value
202     */
203    @SuppressWarnings("unchecked")
204    protected void fillCategory(ModifiableContent content, Element contentElement) throws TransformerException, IOException
205    {
206        if (content instanceof JCRAmetysObject)
207        {
208            ModifiableRepositoryData contentRepositoryData = new JCRRepositoryData(((JCRAmetysObject) content).getNode());
209            RepositoryElementType<String> stringElementType = (RepositoryElementType<String>) _contentAttributeTypeExtensionPoint.getExtension(ModelItemTypeConstants.STRING_TYPE_ID);
210            
211            if (DOMUtils.hasChildElement(contentElement, __CATEGORY_TAG_NAME))
212            {
213                Object category = stringElementType.valueFromXML(contentElement, __CATEGORY_TAG_NAME, Optional.empty());
214                stringElementType.write(contentRepositoryData, __CATEGORY_METADATA_NAME, category);
215            }
216        }
217    }
218    
219    /**
220     * Fills the given newsletter with the automatic status from the provided {@link Element}
221     * @param content The newsletter to fill
222     * @param contentElement the element to read to get the automatic status
223     * @throws TransformerException if an error occurs
224     * @throws IOException if an error occurs while reading the XML value
225     */
226    @SuppressWarnings("unchecked")
227    protected void fillAutomatic(ModifiableContent content, Element contentElement) throws TransformerException, IOException
228    {
229        if (content instanceof JCRAmetysObject)
230        {
231            ModifiableRepositoryData contentRepositoryData = new JCRRepositoryData(((JCRAmetysObject) content).getNode());
232            RepositoryElementType<Boolean> booleanElementType = (RepositoryElementType<Boolean>) _contentAttributeTypeExtensionPoint.getExtension(ModelItemTypeConstants.BOOLEAN_TYPE_ID);
233            
234            if (DOMUtils.hasChildElement(contentElement, __AUTOMATIC_TAG_NAME))
235            {
236                Object automatic = booleanElementType.valueFromXML(contentElement, __AUTOMATIC_TAG_NAME, Optional.empty());
237                booleanElementType.write(contentRepositoryData, __AUTOMATIC_METADATA_NAME, automatic);
238            }
239        }
240    }
241    
242    /**
243     * Fills the given newsletter with the sent status from the provided {@link Element}
244     * @param content The newsletter to fill
245     * @param contentElement the element to read to get the sent status
246     * @throws TransformerException if an error occurs
247     * @throws IOException if an error occurs while reading the XML value
248     */
249    @SuppressWarnings("unchecked")
250    protected void fillSent(ModifiableContent content, Element contentElement) throws TransformerException, IOException
251    {
252        if (content instanceof JCRAmetysObject)
253        {
254            ModifiableRepositoryData contentRepositoryData = new JCRRepositoryData(((JCRAmetysObject) content).getNode());
255            RepositoryElementType<Boolean> booleanElementType = (RepositoryElementType<Boolean>) _contentAttributeTypeExtensionPoint.getExtension(ModelItemTypeConstants.BOOLEAN_TYPE_ID);
256            
257            if (DOMUtils.hasChildElement(contentElement, __SENT_TAG_NAME))
258            {
259                Object sent = booleanElementType.valueFromXML(contentElement, __SENT_TAG_NAME, Optional.empty());
260                booleanElementType.write(contentRepositoryData, __SENT_METADATA_NAME, sent);
261            }
262        }
263    }
264}