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