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