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.plugins.odfpilotage.report.impl;
017
018import java.util.List;
019import java.util.Map;
020
021import org.apache.commons.lang3.StringUtils;
022
023import org.ametys.odf.orgunit.OrgUnit;
024import org.ametys.odf.program.Program;
025import org.ametys.plugins.odfpilotage.report.AbstractPilotageReport;
026
027/**
028 * The abstract class for extractions
029 */
030public abstract class AbstractExtract extends AbstractPilotageReport
031{
032    @Override
033    protected String launchByProgram(Map<String, String> reportParameters) throws Exception
034    {
035        String programId = reportParameters.get(PARAMETER_PROGRAM);
036        Program program = (Program) _resolver.resolveById(programId);
037        _saxProgram(program);
038        return "formation-" + program.getCatalog() + "-" + program.getLanguage() + "-" + program.getTitle();
039    }
040    
041    @Override
042    protected String launchByOrgUnit(Map<String, String> reportParameters) throws Exception
043    {
044        String orgunitId = reportParameters.get(PARAMETER_ORGUNIT);
045        String catalog = reportParameters.get(PARAMETER_CATALOG);
046        String lang = reportParameters.get(PARAMETER_LANG);
047
048        String contextName = null;
049        OrgUnit orgUnit = null;
050        if (StringUtils.isNotBlank(orgunitId))
051        {
052            orgUnit = _resolver.resolveById(orgunitId);
053            contextName = "composante-" + catalog + "-" + lang + "-" + orgUnit.getUAICode();
054        }
055        else
056        {
057            contextName = "composantes-" + catalog + "-" + lang;
058        }
059        _saxOrgUnit(orgUnit, lang, catalog);
060
061        return contextName;
062    }
063    
064    private void _saxOrgUnit(OrgUnit orgUnit, String lang, String catalog)
065    {
066        List<Program> selectedPrograms = _reportHelper.filterProgramsFromOrgUnits(orgUnit, lang, catalog);
067        for (Program program : selectedPrograms)
068        {
069            _saxProgram(program);
070        }
071    }
072    
073    /**
074     * Sax a program for the extraction.
075     * @param program Program to sax
076     */
077    protected abstract void _saxProgram(Program program);
078}