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