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.plugins.odfpilotage.report.AbstractPilotageReport;
025
026/**
027 * The abstract class for pilotage reports.
028 */
029public abstract class AbstractReport extends AbstractPilotageReport
030{
031    @Override
032    protected boolean isSupportedTarget(PilotageReportTarget target)
033    {
034        return PilotageReportTarget.ORGUNIT.equals(target);
035    }
036    
037    @Override
038    protected String launchByProgram(Map<String, String> reportParameters) throws Exception
039    {
040        throw new UnsupportedOperationException("Impossible to launch the report '" + getType() + "' on a program.");
041    }
042    
043    @Override
044    protected String launchByOrgUnit(Map<String, String> reportParameters) throws Exception
045    {
046        String orgunitId = reportParameters.get(PARAMETER_ORGUNIT);
047        String catalog = reportParameters.get(PARAMETER_CATALOG);
048        String lang = reportParameters.get(PARAMETER_LANG);
049
050        String orgUnitRef = "all";
051        
052        // Get the uai codes corresponding to the organization units we want to be part of the reports
053        List<String> uaiCodes = _reportHelper.getUaiCodes(orgunitId);
054        
055        try
056        {
057            for (String uaiCode : uaiCodes)
058            {
059                try
060                {
061                    // FIXME BiM Contrôler le nom du fichier accronym ou UAICode
062                    _launchByOrgUnit(uaiCode, catalog, lang);
063                }
064                catch (Exception e)
065                {
066                    getLogger().error("Erreur d'écriture du rapport '{}'.", getType(), e);
067                }
068            }
069        }
070        finally
071        {
072            if (uaiCodes.size() == 1)
073            {
074                OrgUnit orgUnit = _resolver.resolveById(orgunitId);
075                orgUnitRef = orgUnit.getAcronym();
076                if (StringUtils.isEmpty(orgUnitRef))
077                {
078                    orgUnitRef = uaiCodes.get(0);
079                }
080            }
081        }
082        
083        return catalog + "-" + lang + "-" + orgUnitRef;
084    }
085
086    /**
087     * Launch a report generation on an orgunit.
088     * @param uaiCode The UAI code of the {@link OrgUnit}
089     * @param catalog The catalog
090     * @param lang The language
091     * @throws Exception if an exception occurs
092     */
093    protected abstract void _launchByOrgUnit(String uaiCode, String catalog, String lang) throws Exception;
094    
095    /**
096     * Get the report filename for a given orgunit
097     * @param catalog The catalog
098     * @param lang The lang
099     * @param accronymOrUaiCode The UAI code
100     * @return the file name
101     */
102    protected String _getReportFileName(String catalog, String lang, String accronymOrUaiCode)
103    {
104        StringBuilder sb = new StringBuilder();
105        sb.append(catalog);
106        sb.append("-");
107        sb.append(lang);
108        sb.append("-");
109        sb.append(accronymOrUaiCode);
110        sb.append("-[");
111        sb.append(getType());
112        sb.append("-");
113        sb.append(_currentFormattedDate);
114        sb.append("]");
115        return sb.toString();
116    }
117}