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 / Container (year) / Container (semester) / UE / ELP / ...
035 * <br>into DIP-VDI / ETP-VET / ELP / LSE / ELP / ...
036 */
037public class ApogeeContainerStructure 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 Container)
054            {
055                Container containerProgramPart = (Container) programPart;
056                String containerNatureCode = getContainerNatureCode(containerProgramPart);
057                
058                checkContainerAsYear(containerProgramPart, report, containerNatureCode);
059            }
060            else
061            {
062                // The structure is not handled by this export
063                report.setStatus(ExportStatus.CONTENT_STRUCTURE_INVALID);
064                break;
065            }
066        }
067        
068        if (programPartChildren.isEmpty())
069        {
070            // The structure is not handled by this export
071            report.setStatus(ExportStatus.CONTENT_STRUCTURE_INVALID);
072        }
073    }
074    
075    @Override
076    public void checkSubProgram(SubProgram subProgram, ExportReport report)
077    {
078        throw new UnsupportedOperationException("No subprogram in this structure");
079    }
080    
081    @Override
082    public void createProgram(Program program, ExportReport report)
083    {
084        try
085        {
086            CreationSEMetierServiceInterface creationService = _apogeeWS.getCreationService();
087            String codDIP = getCodeApogee(program);
088            Long versionDIP = getVersionApogee(program);
089            _apogeeWS.createDIP(program, null, codDIP, creationService);
090            _apogeeWS.createVDI(program, null, codDIP, versionDIP, creationService);
091
092            for (ProgramPart pp : program.getProgramPartChildren())
093            {
094                _createContainerAsETPVET((Container) pp, program, creationService, report);
095            }
096        }
097        catch (Exception e)
098        {
099            report.setStatus(ExportStatus.ERROR);
100            getLogger().error("An error occurred exporting the program '{}' ({}) in Apogee", program.getTitle(), program.getId(), e);
101        }
102    }
103    
104    /**
105     * Create a container as ETP/VET in Apogee
106     * @param container the container to create
107     * @param programParent the program parent in Apogee
108     * @param creationService the service to create element in Apogee
109     * @param report the Apogee export report
110     * @throws RemoteException if an export error occurred
111     */
112    protected void _createContainerAsETPVET(Container container, Content programParent, CreationSEMetierServiceInterface creationService, ExportReport report) throws RemoteException
113    {
114        String codDIP = getCodeApogee(programParent);
115        Long versionDIP = getVersionApogee(programParent);
116        String codETP = getCodeApogee(container);
117        Long versionETP = getVersionApogee(container);
118        
119        // Create the ETP / VET from the container
120        _apogeeWS.createETP(container, null, codETP, creationService);
121        _apogeeWS.createVET(container, null, codETP, versionETP, creationService);
122        
123        // Link to the DIP / VDI (Program / SubProgram)
124        _apogeeWS.createLinkDIPETP(codDIP, versionDIP, codETP, versionETP, creationService);
125        
126        for (ProgramPart pp : container.getProgramPartChildren())
127        {
128            _createContainerAsELP((Container) pp, container, creationService, report);
129        }
130    }
131    
132    /**
133     * Create a container in Apogee
134     * @param container the container to create
135     * @param parentYearContainer the parent year container
136     * @param creationService the service to create element in Apogee
137     * @param report the Apogee export report
138     * @throws RemoteException if an export error occurred
139     */
140    protected void _createContainerAsELP(Container container, Content parentYearContainer, CreationSEMetierServiceInterface creationService, ExportReport report) throws RemoteException
141    {
142        String codETP = getCodeApogee(parentYearContainer);
143        Long versionETP = getVersionApogee(parentYearContainer);
144        String codELP = getCodeApogee(container);
145        
146        // Create the ELP from the container
147        _apogeeWS.createELP(container, null, codELP, creationService);
148        
149        // Create a mandatory LSE with random code
150        String codLSE = org.ametys.core.util.StringUtils.generateKey();
151        _apogeeWS.createMandatoryLSE("LSE - " + parentYearContainer.getTitle(), codLSE, codELP, creationService);
152        
153        // Create the link between ETP and LSE (year container and semester container)
154        _apogeeWS.createLinkETPELPLSE(codETP, versionETP, codLSE, null, null, null, null, creationService);
155        
156        for (ProgramPart pp : container.getProgramPartChildren())
157        {
158            _createCourseList((CourseList) pp, container, creationService, report);
159        }
160    }
161}