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    /** The request parameter name for content ids */
046    public static final String PARAMETER_CONTENTTYPE_IDS = "contenttype-ids";
047    /** The request parameter name for view name */
048    public static final String PARAMETER_VIEW_NAME = "viewName";
049    
050    private ContentTypeExtensionPoint _contentTypeEP;
051    private I18nUtils _i18nUtils;
052
053    @Override
054    public void service(ServiceManager serviceManager) throws ServiceException
055    {
056        super.service(serviceManager);
057        _contentTypeEP = (ContentTypeExtensionPoint) serviceManager.lookup(ContentTypeExtensionPoint.ROLE);
058        _i18nUtils = (I18nUtils) serviceManager.lookup(I18nUtils.ROLE);
059    }
060    
061    @Override
062    public void generate() throws IOException, SAXException, ProcessingException
063    {
064        Request request = ObjectModelHelper.getRequest(objectModel);
065        String[] contentTypeIds = request.getParameterValues(PARAMETER_CONTENTTYPE_IDS);
066        String viewName = request.getParameter(PARAMETER_VIEW_NAME);
067        Set<String> contentTypeSet = new HashSet<>();
068        if (contentTypeIds != null)
069        {
070            for (String contentTypeId : contentTypeIds)
071            {
072                if (StringUtils.isBlank(contentTypeId))
073                {
074                    throw new IllegalArgumentException("Missing or empty '" + PARAMETER_CONTENTTYPE_IDS + "' parameter to export content type");
075                }
076                contentTypeSet.add(contentTypeId);
077            }
078        }
079        else 
080        {
081            contentTypeSet = _contentTypeEP.getExtensionsIds();
082        }
083        
084        contentHandler.startDocument();
085        contentHandler.startPrefixMapping("zip", ZipArchiveSerializer.ZIP_NAMESPACE);
086        
087        XMLUtils.startElement(contentHandler, ZipArchiveSerializer.ZIP_NAMESPACE, "archive");
088        
089        for (String id : contentTypeSet)
090        {
091            ContentType contentType = _contentTypeEP.getExtension(id);
092            AttributesImpl attrs = new AttributesImpl();
093            
094            String translatedLabel = _i18nUtils.translate(contentType.getLabel());
095            String fileName = (translatedLabel == null ? contentType.getLabel() : translatedLabel) + (StringUtils.isEmpty(viewName) ? "" : "-" + viewName) + ".xls";
096            attrs.addCDATAAttribute("name", fileName);
097            
098            String src = "cocoon:/export/content-type.xls?contenttype-id=" + URIUtils.encodeParameter(contentType.getId());
099            if (StringUtils.isNotEmpty(viewName))
100            {
101                src += "&viewName=" + URIUtils.encodeParameter(viewName);
102            }
103            attrs.addCDATAAttribute("src", src);
104            
105            XMLUtils.startElement(contentHandler, ZipArchiveSerializer.ZIP_NAMESPACE, "entry", attrs);
106            XMLUtils.endElement(contentHandler, ZipArchiveSerializer.ZIP_NAMESPACE, "entry");
107        }
108        
109        XMLUtils.endElement(contentHandler, ZipArchiveSerializer.ZIP_NAMESPACE, "archive");
110        
111        contentHandler.endPrefixMapping("zip");
112        contentHandler.endDocument();
113    }
114
115}