001/*
002 *  Copyright 2017 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.contenttypeseditor;
017
018import java.io.IOException;
019import java.util.HashSet;
020import java.util.Set;
021
022import org.apache.avalon.framework.service.ServiceException;
023import org.apache.avalon.framework.service.ServiceManager;
024import org.apache.cocoon.ProcessingException;
025import org.apache.cocoon.environment.ObjectModelHelper;
026import org.apache.cocoon.environment.Request;
027import org.apache.cocoon.generation.ServiceableGenerator;
028import org.apache.cocoon.serialization.ZipArchiveSerializer;
029import org.apache.cocoon.xml.AttributesImpl;
030import org.apache.cocoon.xml.XMLUtils;
031import org.apache.commons.lang.StringUtils;
032import org.xml.sax.SAXException;
033
034import org.ametys.cms.contenttype.ContentType;
035import org.ametys.cms.contenttype.ContentTypeExtensionPoint;
036import org.ametys.core.util.I18nUtils;
037import org.ametys.core.util.URIUtils;
038
039/**
040 * Generates a ZIP archive for all content types
041 * 
042 */
043public class ZipExportContentTypesGenerator extends ServiceableGenerator
044{
045    
046    /** The request parameter name for content ids */
047    public static final String PARAMETER_CONTENTTYPE_IDS = "contenttype-ids";
048    
049    private ContentTypeExtensionPoint _contentTypeEP;
050    private I18nUtils _i18nUtils;
051
052    @Override
053    public void service(ServiceManager serviceManager) throws ServiceException
054    {
055        super.service(serviceManager);
056        _contentTypeEP = (ContentTypeExtensionPoint) serviceManager.lookup(ContentTypeExtensionPoint.ROLE);
057        _i18nUtils = (I18nUtils) serviceManager.lookup(I18nUtils.ROLE);
058    }
059    
060    @Override
061    public void generate() throws IOException, SAXException, ProcessingException
062    {
063        Request request = ObjectModelHelper.getRequest(objectModel);
064        String[] contentTypeIds = request.getParameterValues(PARAMETER_CONTENTTYPE_IDS);
065        Set<String> contentTypeSet = new HashSet<>();
066        if (contentTypeIds != null)
067        {
068            for (String contentTypeId : contentTypeIds)
069            {
070                if (StringUtils.isBlank(contentTypeId))
071                {
072                    throw new IllegalArgumentException("Missing or empty '" + PARAMETER_CONTENTTYPE_IDS + "' parameter to export content type");
073                }
074                contentTypeSet.add(contentTypeId);
075            }
076        }
077        else 
078        {
079            contentTypeSet = _contentTypeEP.getExtensionsIds();
080        }
081        
082        contentHandler.startDocument();
083        contentHandler.startPrefixMapping("zip", ZipArchiveSerializer.ZIP_NAMESPACE);
084        
085        XMLUtils.startElement(contentHandler, ZipArchiveSerializer.ZIP_NAMESPACE, "archive");
086        
087        for (String id : contentTypeSet)
088        {
089            ContentType contentType = _contentTypeEP.getExtension(id);
090            AttributesImpl attrs = new AttributesImpl();
091            String translatedLabel = _i18nUtils.translate(contentType.getLabel());
092            String name = translatedLabel == null ? contentType.getLabel() + ".xls" : translatedLabel + ".xls";
093            attrs.addCDATAAttribute("name",  name);
094            attrs.addCDATAAttribute("src", "cocoon:/export/content-type.xls?contenttype-id=" + URIUtils.encodeParameter(contentType.getId()));
095            XMLUtils.startElement(contentHandler, ZipArchiveSerializer.ZIP_NAMESPACE, "entry", attrs);
096            XMLUtils.endElement(contentHandler, ZipArchiveSerializer.ZIP_NAMESPACE, "entry");
097        }
098        
099        XMLUtils.endElement(contentHandler, ZipArchiveSerializer.ZIP_NAMESPACE, "archive");
100        
101        contentHandler.endPrefixMapping("zip");
102        contentHandler.endDocument();
103    }
104
105}