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.cms.content;
017
018import java.io.IOException;
019
020import org.apache.cocoon.ProcessingException;
021import org.apache.cocoon.generation.Generator;
022import org.apache.cocoon.xml.AttributesImpl;
023import org.apache.cocoon.xml.XMLUtils;
024import org.apache.commons.lang.StringUtils;
025import org.apache.commons.lang3.ArrayUtils;
026import org.xml.sax.SAXException;
027
028import org.ametys.cms.contenttype.MetadataSet;
029
030/**
031 * {@link Generator} for rendering the structure of a metadata set (by the content type)
032 */
033public class MetadataSetDefByContentTypeGenerator extends MetadataSetDefGenerator
034{
035    @Override
036    public void generate() throws IOException, SAXException, ProcessingException
037    {
038        boolean isEdition = parameters.getParameterAsBoolean("isEdition", false);
039        String viewName = parameters.getParameter("viewName", "main");
040        String contentTypeId = parameters.getParameter("contentTypeId", null);
041        
042        contentHandler.startDocument();
043        
044        AttributesImpl attrs = new AttributesImpl();
045        attrs.addCDATAAttribute("metadataSetName", viewName);
046        attrs.addCDATAAttribute("isEditionMetadataSet", Boolean.toString(isEdition));
047        attrs.addCDATAAttribute("id", contentTypeId);
048        XMLUtils.startElement(contentHandler, "metadataSet", attrs);
049        _saxContentTypeMetadataSet(contentTypeId, viewName, isEdition);
050        XMLUtils.endElement(contentHandler, "metadataSet");
051
052        contentHandler.endDocument();
053    }
054    
055    /**
056     * SAX metadata set structure
057     * @param contentTypeId the content type id.
058     * @param viewName The name of the metadata set to sax
059     * @param isEdition True to use the edit metadata set
060     * @throws SAXException if an error occurs while SAXing.
061     * @throws IOException if an error occurs.
062     * @throws ProcessingException if an error occurs.
063     */
064    protected void _saxContentTypeMetadataSet(String contentTypeId, String viewName, boolean isEdition) throws SAXException, ProcessingException, IOException
065    {
066        String[] contentTypeTab = new String[] {contentTypeId};
067        String[] mixinTab = ArrayUtils.EMPTY_STRING_ARRAY;
068        
069        MetadataSet metadataSet = null;
070        
071        if (isEdition)
072        {
073            metadataSet = _contentTypesHelper.getMetadataSetForEdition(viewName, contentTypeTab, mixinTab);
074        }
075        else
076        {
077            metadataSet = _contentTypesHelper.getMetadataSetForView(viewName, contentTypeTab, mixinTab);
078        }
079        
080        if (metadataSet == null)
081        {
082            throw new ProcessingException(String.format("Unknown metadata set '%s' of type '%s' for content type '%s'",
083                                          viewName, isEdition ? "edition" : "view", StringUtils.join(contentTypeTab, ',')));
084        }
085        
086        _saxMetadataSetElement(null, contentTypeTab, mixinTab, null, metadataSet);
087    }
088}