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.plugins.odfsync.apogee.ws.structure;
017
018import java.rmi.RemoteException;
019import java.util.List;
020
021import org.ametys.cms.repository.Content;
022import org.ametys.odf.courselist.CourseList;
023import org.ametys.odf.program.Container;
024import org.ametys.odf.program.Program;
025import org.ametys.odf.program.ProgramPart;
026import org.ametys.odf.program.SubProgram;
027import org.ametys.plugins.odfsync.export.ExportReport;
028import org.ametys.plugins.odfsync.export.ExportReport.ExportStatus;
029
030import gouv.education.apogee.commun.client.ws.creationse.CreationSEMetierServiceInterface;
031
032/**
033 * The structure to export in Apogee the following program
034 * <br>Program / SubProgram / Container (semester) / UE / ELP / ...
035 * <br>into DIP-VDI / ETP-VET / ELP / LSE / ELP / ...
036 */
037public class ApogeeDefaultStructure extends AbstractApogeeStructure
038{
039    @Override
040    public void checkProgram(Program program, ExportReport report)
041    {
042        // Check mandatory data for program
043        checkMandatoryDataForContent(program, getDIPMandatoryData(program), report);
044        checkMandatoryDataForContent(program, getVDIMandatoryData(program), report);
045        
046        // Check mandatory data for program orgUnits
047        checkMandatoryDataForOrgunits(program, program.getOrgUnits(), getOrgUnitMandatoryDataForDIP(), report);
048        
049        // Check the program structure
050        List<ProgramPart> programPartChildren = program.getProgramPartChildren();
051        for (ProgramPart programPart : programPartChildren)
052        {
053            if (programPart instanceof SubProgram)
054            {
055                checkSubProgram((SubProgram) programPart, report);
056            }
057            else
058            {
059                // The structure is not handled by this export
060                report.setStatus(ExportStatus.CONTENT_STRUCTURE_INVALID);
061                break;
062            }
063        }
064        
065        if (programPartChildren.isEmpty())
066        {
067            // The structure is not handled by this export
068            report.setStatus(ExportStatus.CONTENT_STRUCTURE_INVALID);
069        }
070    }
071    
072    @Override
073    public void checkSubProgram(SubProgram subProgram, ExportReport report)
074    {
075        // Check mandatory data for subProgram
076        checkMandatoryDataForContent(subProgram, getETPMandatoryData(subProgram), report);
077        checkMandatoryDataForContent(subProgram, getVETMandatoryData(subProgram), report);
078        
079        // Check mandatory data for subProgram orgUnits
080        checkMandatoryDataForOrgunits(subProgram, subProgram.getOrgUnits(), getOrgUnitMandatoryDataForETP(), report);
081        
082        // Check the subProgram structure
083        List<ProgramPart> programPartChildren = subProgram.getProgramPartChildren();
084        for (ProgramPart childProgramPart : programPartChildren)
085        {
086            if (childProgramPart instanceof Container)
087            {
088                Container containerChildProgramPart = (Container) childProgramPart;
089                String childContainerNatureCode = getContainerNatureCode(containerChildProgramPart);
090                
091                checkContainerAsSemester(containerChildProgramPart, report, childContainerNatureCode);
092            }
093            else
094            {
095                // The structure is not handled by this export
096                report.setStatus(ExportStatus.CONTENT_STRUCTURE_INVALID);
097                break;
098            }
099        }
100        
101        if (programPartChildren.isEmpty())
102        {
103            // The structure is not handled by this export
104            report.setStatus(ExportStatus.CONTENT_STRUCTURE_INVALID);
105        }
106    }
107    
108    @Override
109    public void checkContainerAsYear(Container container, ExportReport report, String containerNatureCode)
110    {
111        throw new UnsupportedOperationException("No year container in this structure");
112    }
113    
114    @Override
115    public void createProgram(Program program, ExportReport report)
116    {
117        try
118        {
119            CreationSEMetierServiceInterface creationService = _apogeeWS.getCreationService();
120            String codDIP = getCodeApogee(program);
121            Long versionDIP = getVersionApogee(program);
122            _apogeeWS.createDIP(program, null, codDIP, creationService);
123            _apogeeWS.createVDI(program, null, codDIP, versionDIP, creationService);
124            
125            for (ProgramPart pp : program.getProgramPartChildren())
126            {
127                _createSubProgram((SubProgram) pp, program, creationService, report);
128            }
129        }
130        catch (Exception e)
131        {
132            report.setStatus(ExportStatus.ERROR);
133            getLogger().error("An error occurred exporting the program '{}' ({}) in Apogee", program.getTitle(), program.getId(), e);
134        }
135    }
136    
137    /**
138     * Create a subProgram in Apogee
139     * @param subProgram the subProgram to create
140     * @param programParent the program parent in Apogee
141     * @param creationService the service to create element in Apogee
142     * @param report the Apogee export report
143     * @throws RemoteException if an export error occurred
144     */
145    protected void _createSubProgram(SubProgram subProgram, Content programParent, CreationSEMetierServiceInterface creationService, ExportReport report) throws RemoteException
146    {
147        String codDIP = getCodeApogee(programParent);
148        Long versionDIP = getVersionApogee(programParent);
149        String codETP = getCodeApogee(subProgram);
150        Long versionETP = getVersionApogee(subProgram);
151        
152        _apogeeWS.createETP(subProgram, null, codETP, creationService);
153        _apogeeWS.createVET(subProgram, null, codETP, versionETP, creationService);
154        
155        _apogeeWS.createLinkDIPETP(codDIP, versionDIP, codETP, versionETP, creationService);
156        
157        for (ProgramPart pp : subProgram.getProgramPartChildren())
158        {
159            _createContainerAsELP((Container) pp, programParent, subProgram, creationService, report);
160        }
161    }
162    
163    /**
164     * Create a container as ELP in Apogee
165     * @param container the container to create
166     * @param programParent the program parent in Apogee
167     * @param parentSubProgram the parent year container
168     * @param creationService the service to create element in Apogee
169     * @param report the Apogee export report
170     * @throws RemoteException if an export error occurred
171     */
172    protected void _createContainerAsELP(Container container, Content programParent, Content parentSubProgram, CreationSEMetierServiceInterface creationService, ExportReport report) throws RemoteException
173    {
174        String codETP = getCodeApogee(parentSubProgram);
175        Long versionETP = getVersionApogee(parentSubProgram);
176        String codELP = getCodeApogee(container);
177        
178        // Create the ELP from the container
179        _apogeeWS.createELP(container, null, codELP, creationService);
180        
181        // Create a mandatory LSE with random code
182        String codLSE = org.ametys.core.util.StringUtils.generateKey();
183        _apogeeWS.createMandatoryLSE("LSE - " + parentSubProgram.getTitle(), codLSE, codELP, creationService);
184        
185        // Create the link between ETP and LSE (year container and semester container)
186        _apogeeWS.createLinkETPELPLSE(codETP, versionETP, codLSE, null, null, null, null, creationService);
187        
188        for (ProgramPart pp : container.getProgramPartChildren())
189        {
190            _createCourseList((CourseList) pp, container, creationService, report);
191        }
192    }
193}