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.apogee.ws.ApogeeExportReport;
028import org.ametys.plugins.odfsync.apogee.ws.ApogeeExportReport.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, ApogeeExportReport 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.setExportStatus(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.setExportStatus(ExportStatus.CONTENT_STRUCTURE_INVALID);
069        }
070    }
071    
072    @Override
073    public void checkSubProgram(SubProgram subProgram, ApogeeExportReport 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                checkContainerAsSemester((Container) childProgramPart, report);
089            }
090            else
091            {
092                // The structure is not handled by this export
093                report.setExportStatus(ExportStatus.CONTENT_STRUCTURE_INVALID);
094                break;
095            }
096        }
097        
098        if (programPartChildren.isEmpty())
099        {
100            // The structure is not handled by this export
101            report.setExportStatus(ExportStatus.CONTENT_STRUCTURE_INVALID);
102        }
103    }
104    
105    @Override
106    public void checkContainerAsYear(Container container, ApogeeExportReport report)
107    {
108        throw new UnsupportedOperationException("No year container in this structure");
109    }
110    
111    @Override
112    public void checkContainerAsSemester(Container container, ApogeeExportReport report)
113    {
114        if (isSemesterContainer(container, report))
115        {
116            // The container is a semester so check the data as a semester (ELP in Apogee)
117            checkMandatoryDataForContent(container, getELPMandatoryData(container), report);
118            
119            // Check the container structure
120            for (ProgramPart childProgramPart : container.getProgramPartChildren())
121            {
122                if (childProgramPart instanceof CourseList)
123                {
124                    checkCourseList((CourseList) childProgramPart, report);
125                }
126                else
127                {
128                    report.setExportStatus(ExportStatus.CONTENT_STRUCTURE_INVALID);
129                    break;
130                }
131            }
132        }
133        else
134        {
135            // The structure is not handled by this export
136            report.setExportStatus(ExportStatus.CONTENT_STRUCTURE_INVALID);
137        }
138    }
139    
140    @Override
141    public void createProgram(Program program, ApogeeExportReport report)
142    {
143        try
144        {
145            CreationSEMetierServiceInterface creationService = _apogeeWS.getCreationService();
146            String codDIP = getCodeApogee(program);
147            Long versionDIP = getVersionApogee(program);
148            _apogeeWS.createDIP(program, null, codDIP, creationService);
149            _apogeeWS.createVDI(program, null, codDIP, versionDIP, creationService);
150            
151            for (ProgramPart pp : program.getProgramPartChildren())
152            {
153                _createSubProgram((SubProgram) pp, program, creationService, report);
154            }
155        }
156        catch (Exception e)
157        {
158            report.setExportStatus(ExportStatus.ERROR);
159            getLogger().error("An error occurred exporting the program '{}' ({}) in Apogee", program.getTitle(), program.getId(), e);
160        }
161    }
162    
163    /**
164     * Create a subProgram in Apogee
165     * @param subProgram the subProgram to create
166     * @param programParent the program parent in Apogee
167     * @param creationService the service to create element in Apogee
168     * @param report the Apogee export report
169     * @throws RemoteException if an export error occurred
170     */
171    protected void _createSubProgram(SubProgram subProgram, Content programParent, CreationSEMetierServiceInterface creationService, ApogeeExportReport report) throws RemoteException
172    {
173        String codDIP = getCodeApogee(programParent);
174        Long versionDIP = getVersionApogee(programParent);
175        String codETP = getCodeApogee(subProgram);
176        Long versionETP = getVersionApogee(subProgram);
177        
178        _apogeeWS.createETP(subProgram, null, codETP, creationService);
179        _apogeeWS.createVET(subProgram, null, codETP, versionETP, creationService);
180        
181        _apogeeWS.createLinkDIPETP(codDIP, versionDIP, codETP, versionETP, creationService);
182        
183        for (ProgramPart pp : subProgram.getProgramPartChildren())
184        {
185            _createContainerAsELP((Container) pp, programParent, subProgram, creationService, report);
186        }
187    }
188    
189    /**
190     * Create a container as ELP in Apogee
191     * @param container the container to create
192     * @param programParent the program parent in Apogee
193     * @param parentSubProgram the parent year container
194     * @param creationService the service to create element in Apogee
195     * @param report the Apogee export report
196     * @throws RemoteException if an export error occurred
197     */
198    protected void _createContainerAsELP(Container container, Content programParent, Content parentSubProgram, CreationSEMetierServiceInterface creationService, ApogeeExportReport report) throws RemoteException
199    {
200        String codETP = getCodeApogee(parentSubProgram);
201        Long versionETP = getVersionApogee(parentSubProgram);
202        String codELP = getCodeApogee(container);
203        
204        // Create the ELP from the container
205        _apogeeWS.createELP(container, null, codELP, creationService);
206        
207        // Create a mandatory LSE with random code
208        String codLSE = org.ametys.core.util.StringUtils.generateKey();
209        _apogeeWS.createMandatoryLSE("LSE - " + parentSubProgram.getTitle(), codLSE, codELP, creationService);
210        
211        // Create the link between ETP and LSE (year container and semester container)
212        _apogeeWS.createLinkETPELPLSE(codETP, versionETP, codLSE, null, null, null, null, creationService);
213        
214        for (ProgramPart pp : container.getProgramPartChildren())
215        {
216            _createCourseList((CourseList) pp, container, creationService, report);
217        }
218    }
219}