001/*
002 *  Copyright 2019 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.lheo;
017
018import java.io.IOException;
019import java.time.LocalDateTime;
020import java.time.format.DateTimeFormatter;
021import java.util.List;
022import java.util.Map;
023
024import org.apache.avalon.framework.service.ServiceException;
025import org.apache.avalon.framework.service.ServiceManager;
026import org.apache.cocoon.ProcessingException;
027import org.apache.cocoon.environment.ObjectModelHelper;
028import org.apache.cocoon.environment.Request;
029import org.apache.cocoon.environment.Response;
030import org.apache.cocoon.generation.ServiceableGenerator;
031import org.xml.sax.SAXException;
032
033import org.ametys.odf.program.AbstractProgram;
034
035/**
036 * Generates catalog to LHEO
037 */
038public class GlobalExportToLHEOGenerator extends ServiceableGenerator
039{
040    /** The export LHEO manager */
041    protected ExportToLHEOManager _exportLHEOManager;
042    
043    /** The LHEO query manager */
044    protected LHEOQueryManager _lheoQueryManager;
045    
046    @Override
047    public void service(ServiceManager smanager) throws ServiceException
048    {
049        super.service(smanager);
050        _exportLHEOManager = (ExportToLHEOManager) smanager.lookup(ExportToLHEOManager.ROLE);
051        _lheoQueryManager = (LHEOQueryManager) smanager.lookup(LHEOQueryManager.ROLE);
052    }
053    
054    @Override
055    public void generate() throws IOException, SAXException, ProcessingException
056    {
057        Request request = ObjectModelHelper.getRequest(objectModel);
058
059        String catalog = request.getParameter("catalog");
060        String lang = request.getParameter("lang");
061        
062        List<AbstractProgram> programList = _lheoQueryManager.getAbstractProgramsToExport(catalog, lang, Map.of());
063        
064        // Configure the response header
065        Response response = ObjectModelHelper.getResponse(objectModel);
066        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMddhhmmss");
067        response.setHeader("Content-Disposition", "attachment; filename=\"LHEO_" + catalog + "_" + LocalDateTime.now().format(formatter) + ".xml" + "\"");
068        
069        _exportLHEOManager.saxLHEO(contentHandler, programList);
070    }
071}