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.cdmfr;
017
018import java.util.Map;
019
020import org.apache.avalon.framework.service.ServiceException;
021import org.apache.avalon.framework.service.ServiceManager;
022import org.apache.cocoon.environment.ObjectModelHelper;
023import org.apache.cocoon.environment.Request;
024import org.apache.cocoon.environment.Response;
025import org.xml.sax.ContentHandler;
026import org.xml.sax.SAXException;
027
028import org.ametys.cms.repository.Content;
029import org.ametys.odf.program.Program;
030import org.ametys.plugins.repository.AmetysObjectResolver;
031
032/**
033 * Basic implementation of the CDM-fr export helper.
034 */
035public class SimpleExportCDMfrHelper implements ExportCDMfrHelper
036{
037    private AmetysObjectResolver _resolver;
038    private ExportCDMfrManager _exportCMDfrManager;
039    
040    public void service(ServiceManager smanager) throws ServiceException
041    {
042        _resolver = (AmetysObjectResolver) smanager.lookup(AmetysObjectResolver.ROLE);
043        _exportCMDfrManager = (ExportCDMfrManager) smanager.lookup(ExportCDMfrManager.ROLE);
044    }
045    
046    @Override
047    public void export(Map objectModel, ContentHandler contentHandler) throws SAXException
048    {
049        Request request = ObjectModelHelper.getRequest(objectModel);
050
051        // Add the program to the request
052        String programId = request.getParameter("id");
053        Program program = _resolver.resolveById(programId);
054        request.setAttribute(Content.class.getName(), program);
055        
056        // Configure the response header
057        Response response = ObjectModelHelper.getResponse(objectModel);
058        response.setHeader("Content-Disposition", "attachment; filename=\"" + program.getCDMId() + ".xml" + "\"");
059        
060        _exportCMDfrManager.generateCDM(contentHandler, program);
061    }
062}