001/*
002 *  Copyright 2014 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.contentio.in;
017
018import java.io.IOException;
019import java.util.Collection;
020import java.util.Map;
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.generation.ServiceableGenerator;
027import org.apache.cocoon.xml.AttributesImpl;
028import org.apache.cocoon.xml.XMLUtils;
029import org.apache.commons.lang3.StringUtils;
030import org.xml.sax.SAXException;
031
032import org.ametys.cms.content.ContentHelper;
033import org.ametys.cms.repository.Content;
034import org.ametys.plugins.repository.AmetysObjectResolver;
035
036/**
037 * Generates the report of a content import.
038 */
039public class ImportReportGenerator extends ServiceableGenerator
040{
041    
042    /** The AmetysObject resolver. */
043    protected AmetysObjectResolver _resolver;
044    private ContentHelper _contentHelper;
045    
046    @Override
047    public void service(ServiceManager serviceManager) throws ServiceException
048    {
049        super.service(serviceManager);
050        _resolver = (AmetysObjectResolver) serviceManager.lookup(AmetysObjectResolver.ROLE);
051        _contentHelper = (ContentHelper) serviceManager.lookup(ContentHelper.ROLE);
052    }
053    
054    @Override
055    public void generate() throws IOException, SAXException, ProcessingException
056    {
057        @SuppressWarnings("unchecked")
058        Map<String, Object> jsParameters = (Map<String, Object>) objectModel.get(ObjectModelHelper.PARENT_CONTEXT);
059        Boolean success = (Boolean) jsParameters.get("success");
060        @SuppressWarnings("unchecked")
061        Collection<String> contentIds = (Collection<String>) jsParameters.get("contentIds");
062        String baseUrl = (String) jsParameters.get("baseUrl");
063        String error = (String) jsParameters.get("error");
064        
065        AttributesImpl attrs = new AttributesImpl();
066        attrs.addCDATAAttribute("baseUrl", baseUrl);
067        attrs.addCDATAAttribute("success", success.toString());
068        if (StringUtils.isNotEmpty(error))
069        {
070            attrs.addCDATAAttribute("error", error);
071        }
072        
073        contentHandler.startDocument();
074        XMLUtils.startElement(contentHandler, "import", attrs);
075        
076        if (contentIds != null)
077        {
078            for (String id : contentIds)
079            {
080                Content content = _resolver.resolveById(id);
081                
082                AttributesImpl contentAttrs = new AttributesImpl();
083                
084                contentAttrs.addCDATAAttribute("id", id);
085                contentAttrs.addCDATAAttribute("title", StringUtils.defaultString(_contentHelper.getTitle(content)));
086                XMLUtils.createElement(contentHandler, "content", contentAttrs);
087            }
088        }
089        
090        XMLUtils.endElement(contentHandler, "import");
091        contentHandler.endDocument();
092    }
093    
094}