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.Optional; 021 022import javax.xml.transform.TransformerException; 023 024import org.apache.avalon.framework.service.ServiceException; 025import org.apache.avalon.framework.service.ServiceManager; 026import org.apache.xpath.XPathAPI; 027import org.w3c.dom.Element; 028import org.w3c.dom.Node; 029 030import org.ametys.cms.repository.ContentAttributeTypeExtensionPoint; 031import org.ametys.cms.repository.ModifiableContent; 032import org.ametys.core.util.dom.DOMUtils; 033import org.ametys.plugins.newsletter.NewsletterDAO; 034import org.ametys.plugins.repository.data.extractor.xml.XMLValuesExtractorAdditionalDataGetter; 035import org.ametys.plugins.repository.data.repositorydata.ModifiableRepositoryData; 036import org.ametys.plugins.repository.data.repositorydata.impl.JCRRepositoryData; 037import org.ametys.plugins.repository.data.type.RepositoryElementType; 038import org.ametys.plugins.repository.jcr.JCRAmetysObject; 039import org.ametys.runtime.model.type.ModelItemTypeConstants; 040 041/** 042 * Generates SAX events for Content, including category, automatic and sent for Newsletters. 043 * TODO NEWATTRIBUTEAPI_CONTENT: do not use type implementation but the ModelAwareDataHolder#getInternalValue when this API exist 044 */ 045public class ContentExtractor extends org.ametys.web.content.ContentExtractor 046{ 047 /** Avalon role. */ 048 public static final String NEWSLETTER_CONTENT_EXTACTOR_ROLE = ContentExtractor.class.getName(); 049 050 private static final String __CATEGORY_METADATA_NAME = "category"; 051 private static final String __CATEGORY_TAG_NAME = "category"; 052 private static final String __AUTOMATIC_METADATA_NAME = "automatic"; 053 private static final String __AUTOMATIC_TAG_NAME = "automatic"; 054 private static final String __SENT_METADATA_NAME = "sent"; 055 private static final String __SENT_TAG_NAME = "sent"; 056 057 // Content attribute types extension point 058 private ContentAttributeTypeExtensionPoint _contentAttributeTypeExtensionPoint; 059 060 @Override 061 public void service(ServiceManager manager) throws ServiceException 062 { 063 super.service(manager); 064 _contentAttributeTypeExtensionPoint = (ContentAttributeTypeExtensionPoint) manager.lookup(ContentAttributeTypeExtensionPoint.ROLE); 065 } 066 067 @Override 068 public void fillContent(ModifiableContent content, Node node, XMLValuesExtractorAdditionalDataGetter additionalDataGetter) throws Exception 069 { 070 super.fillContent(content, node, additionalDataGetter); 071 072 if (Arrays.asList(content.getTypes()).contains(NewsletterDAO.__NEWSLETTER_CONTENT_TYPE)) 073 { 074 Element contentElement = (Element) XPathAPI.selectSingleNode(node, "content"); 075 fillCategory(content, contentElement); 076 fillAutomatic(content, contentElement); 077 fillSent(content, contentElement); 078 } 079 } 080 081 /** 082 * Fills the given newsletter with the category from the provided {@link Element} 083 * @param content The newsletter to fill 084 * @param contentElement the element to read to get the category 085 * @throws TransformerException if an error occurs 086 * @throws IOException if an error occurs while reading the XML value 087 */ 088 @SuppressWarnings("unchecked") 089 protected void fillCategory(ModifiableContent content, Element contentElement) throws TransformerException, IOException 090 { 091 if (content instanceof JCRAmetysObject) 092 { 093 ModifiableRepositoryData contentRepositoryData = new JCRRepositoryData(((JCRAmetysObject) content).getNode()); 094 RepositoryElementType<String> stringElementType = (RepositoryElementType<String>) _contentAttributeTypeExtensionPoint.getExtension(ModelItemTypeConstants.STRING_TYPE_ID); 095 096 if (DOMUtils.hasChildElement(contentElement, __CATEGORY_TAG_NAME)) 097 { 098 Object category = stringElementType.valueFromXML(contentElement, __CATEGORY_TAG_NAME, Optional.empty()); 099 stringElementType.write(contentRepositoryData, __CATEGORY_METADATA_NAME, category); 100 } 101 } 102 } 103 104 /** 105 * Fills the given newsletter with the automatic status from the provided {@link Element} 106 * @param content The newsletter to fill 107 * @param contentElement the element to read to get the automatic status 108 * @throws TransformerException if an error occurs 109 * @throws IOException if an error occurs while reading the XML value 110 */ 111 @SuppressWarnings("unchecked") 112 protected void fillAutomatic(ModifiableContent content, Element contentElement) throws TransformerException, IOException 113 { 114 if (content instanceof JCRAmetysObject) 115 { 116 ModifiableRepositoryData contentRepositoryData = new JCRRepositoryData(((JCRAmetysObject) content).getNode()); 117 RepositoryElementType<Boolean> booleanElementType = (RepositoryElementType<Boolean>) _contentAttributeTypeExtensionPoint.getExtension(ModelItemTypeConstants.BOOLEAN_TYPE_ID); 118 119 if (DOMUtils.hasChildElement(contentElement, __AUTOMATIC_TAG_NAME)) 120 { 121 Object automatic = booleanElementType.valueFromXML(contentElement, __AUTOMATIC_TAG_NAME, Optional.empty()); 122 booleanElementType.write(contentRepositoryData, __AUTOMATIC_METADATA_NAME, automatic); 123 } 124 } 125 } 126 127 /** 128 * Fills the given newsletter with the sent status from the provided {@link Element} 129 * @param content The newsletter to fill 130 * @param contentElement the element to read to get the sent status 131 * @throws TransformerException if an error occurs 132 * @throws IOException if an error occurs while reading the XML value 133 */ 134 @SuppressWarnings("unchecked") 135 protected void fillSent(ModifiableContent content, Element contentElement) throws TransformerException, IOException 136 { 137 if (content instanceof JCRAmetysObject) 138 { 139 ModifiableRepositoryData contentRepositoryData = new JCRRepositoryData(((JCRAmetysObject) content).getNode()); 140 RepositoryElementType<Boolean> booleanElementType = (RepositoryElementType<Boolean>) _contentAttributeTypeExtensionPoint.getExtension(ModelItemTypeConstants.BOOLEAN_TYPE_ID); 141 142 if (DOMUtils.hasChildElement(contentElement, __SENT_TAG_NAME)) 143 { 144 Object sent = booleanElementType.valueFromXML(contentElement, __SENT_TAG_NAME, Optional.empty()); 145 booleanElementType.write(contentRepositoryData, __SENT_METADATA_NAME, sent); 146 } 147 } 148 } 149}