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 org.apache.avalon.framework.component.Component;
019import org.apache.avalon.framework.context.Context;
020import org.apache.avalon.framework.context.ContextException;
021import org.apache.avalon.framework.context.Contextualizable;
022import org.apache.avalon.framework.service.ServiceException;
023import org.apache.avalon.framework.service.ServiceManager;
024import org.apache.avalon.framework.service.Serviceable;
025import org.apache.cocoon.components.ContextHelper;
026import org.apache.cocoon.environment.Request;
027import org.apache.cocoon.environment.Response;
028import org.xml.sax.ContentHandler;
029import org.xml.sax.SAXException;
030
031import org.ametys.cms.repository.Content;
032import org.ametys.odf.ODFHelper;
033import org.ametys.odf.program.Program;
034import org.ametys.plugins.repository.AmetysObjectResolver;
035
036/**
037 * Default helper to export a {@link Program} to CDM-fr format.
038 */
039public class ExportCDMfrHelper implements Component, Serviceable, Contextualizable
040{
041    /** Avalon Role */
042    public static final String ROLE = ExportCDMfrHelper.class.getName();
043    
044    /** The name of request parameter to use to get the valid version of content */
045    public static final String REQUEST_PARAM_VALID_LABEL = "validLabel";
046    
047    /** The name of request parameter to set this export for a Ametys application */
048    public static final String REQUEST_PARAM_EXPORT_FOR_AMETYS = "forAmetys";
049    
050    /** The Ametys object resolver */
051    protected AmetysObjectResolver _resolver;
052    /** The component for CDM export */
053    protected ExportCDMfrManager _exportCMDfrManager;
054    /** The Avalon context */
055    protected Context _context;
056    
057    public void service(ServiceManager smanager) throws ServiceException
058    {
059        _resolver = (AmetysObjectResolver) smanager.lookup(AmetysObjectResolver.ROLE);
060        _exportCMDfrManager = (ExportCDMfrManager) smanager.lookup(ExportCDMfrManager.ROLE);
061    }
062    
063    public void contextualize(Context context) throws ContextException
064    {
065        _context = context;
066    }
067    
068    /**
069     * Generate the CDM-fr XML export for the current program.
070     * @param contentHandler The content handler
071     * @throws SAXException If an error occurs during sax
072     */
073    public void export(ContentHandler contentHandler) throws SAXException
074    {
075        Program program = _getProgram();
076        
077        _prepareRequest(program);
078        
079        _prepareResponse(program);
080        
081        _doExport(contentHandler, program);
082    }
083    
084    /**
085     * Do export
086     * @param contentHandler The content handler to sax into
087     * @param program the porogram to export
088     * @throws SAXException If an error occurs during sax
089     */
090    protected void _doExport(ContentHandler contentHandler, Program program) throws SAXException
091    {
092        _exportCMDfrManager.generateCDM(contentHandler, program);
093    }
094    
095    /**
096     * Get the program to export
097     * @return the program to export
098     */
099    protected Program _getProgram()
100    {
101        // Get the program from request
102        Request request = _getRequest();
103
104        String programId = request.getParameter("id");
105        Program program = _resolver.resolveById(programId);
106        
107        // Add the program to the request
108        request.setAttribute(Content.class.getName(), program);
109        
110        return program;
111    }
112    
113    /**
114     * Prepare the request before export
115     * @param program the program to export
116     */
117    protected void _prepareRequest(Program program)
118    {
119        Request request = _getRequest();
120        
121        if (request.getParameter(REQUEST_PARAM_VALID_LABEL) != null)
122        {
123            request.setAttribute(ODFHelper.REQUEST_ATTRIBUTE_VALID_LABEL, true);
124        }
125        
126        if (request.getParameter(REQUEST_PARAM_EXPORT_FOR_AMETYS) != null)
127        {
128            request.setAttribute(ExportCDMfrManager.REQUEST_ATTRIBUTE_EXPORT_FOR_AMETYS, true);
129        }
130    }
131    
132    /**
133     * Prepare the response
134     * @param program the program to export
135     */
136    protected void _prepareResponse(Program program)
137    {
138        Response response = ContextHelper.getResponse(_context);
139        response.setHeader("Content-Disposition", "attachment; filename=\"" + program.getCDMId() + ".xml" + "\"");
140    }
141    
142    /**
143     * Get the request
144     * @return the request
145     */
146    protected Request _getRequest()
147    {
148        return ContextHelper.getRequest(_context);
149    }
150}