001/*
002 *  Copyright 2018 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.odf.services;
017
018import org.apache.avalon.framework.service.ServiceException;
019import org.apache.avalon.framework.service.ServiceManager;
020import org.apache.cocoon.ProcessingException;
021import org.apache.cocoon.environment.ObjectModelHelper;
022import org.apache.cocoon.environment.Request;
023import org.apache.cocoon.generation.ServiceableGenerator;
024import org.apache.cocoon.xml.XMLUtils;
025import org.apache.commons.lang.ArrayUtils;
026import org.apache.commons.lang3.StringUtils;
027import org.xml.sax.SAXException;
028
029import org.ametys.cms.CmsConstants;
030import org.ametys.cms.contenttype.ContentTypesHelper;
031import org.ametys.cms.repository.DefaultContent;
032import org.ametys.plugins.repository.AmetysObjectResolver;
033import org.ametys.plugins.repository.AmetysRepositoryException;
034import org.ametys.runtime.model.ModelHelper;
035
036/**
037 * Class to parse an attribute from an ODF content in Live version.
038 */
039public class ODFContentFieldGenerator extends ServiceableGenerator
040{
041    /** The source resolver */
042    protected AmetysObjectResolver _resolver;
043    
044    /** Content type helper */
045    protected ContentTypesHelper _contentTypesHelper;
046    
047    @Override
048    public void service(ServiceManager serviceManager) throws ServiceException
049    {
050        super.service(serviceManager);
051        _resolver = (AmetysObjectResolver) serviceManager.lookup(AmetysObjectResolver.ROLE);
052        _contentTypesHelper = (ContentTypesHelper) serviceManager.lookup(ContentTypesHelper.ROLE);
053    }
054
055    @Override
056    public void generate() throws ProcessingException
057    {
058        Request request = ObjectModelHelper.getRequest(objectModel);
059        String contentId = request.getParameter("contentId");
060        if (StringUtils.isEmpty(contentId))
061        {
062            throw new ProcessingException("The content ID is empty.");
063        }
064
065        try
066        {
067            DefaultContent content = _resolver.resolveById(contentId);
068
069            // Is this an ODF content ?
070            if (!_contentTypesHelper.isInstanceOf(content, "org.ametys.plugins.odf.Content.odfContent"))
071            {
072                throw new ProcessingException(String.format("The content with the ID '%s' is not an ODF content.", contentId));
073            }
074
075            // The content Have a Live version ?
076            String[] labels = content.getAllLabels();
077            if (!ArrayUtils.contains(labels, CmsConstants.LIVE_LABEL))
078            {
079                throw new ProcessingException(String.format("The content with the ID '%s' hasn't a Live version.", contentId));
080            }
081            
082            // Get the Live version to the content
083            content.switchToLabel(CmsConstants.LIVE_LABEL);
084            
085            // Do we have an attribute name ?
086            String attributeName = request.getParameter("metadata");
087            if (StringUtils.isEmpty(attributeName))
088            {
089                throw new ProcessingException(String.format("No attribute has been transmitted for the content '%s'.", contentId));
090            }
091            
092            // The attribute exists ?
093            if (!ModelHelper.hasModelItem(attributeName, content.getModel()))
094            {
095                throw new ProcessingException(String.format("There is no attribute named '%s' for the content '%s'.", attributeName, contentId));
096            }
097            
098            
099            // Generate SAX events for the attribute
100            contentHandler.startDocument();
101            XMLUtils.startElement(contentHandler, "metadata");
102            
103            content.dataToSAX(contentHandler, attributeName);
104            
105            XMLUtils.endElement(contentHandler, "metadata");
106            contentHandler.endDocument();
107        }
108        catch (AmetysRepositoryException e)
109        {
110            throw new ProcessingException(String.format("Unable to get the content with the ID '%s'.", contentId), e);
111        }
112        catch (SAXException e)
113        {
114            throw new ProcessingException(String.format("Error while saxing the content '%s' on the 'export' metadata set.", contentId), e);
115        }
116    }
117}