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;
017
018import java.math.BigDecimal;
019import java.rmi.RemoteException;
020import java.time.LocalDate;
021import java.util.List;
022import java.util.stream.Stream;
023
024import org.apache.avalon.framework.component.Component;
025import org.apache.avalon.framework.service.ServiceException;
026import org.apache.avalon.framework.service.ServiceManager;
027import org.apache.avalon.framework.service.Serviceable;
028import org.apache.commons.lang3.StringUtils;
029
030import org.ametys.cms.data.ContentDataHelper;
031import org.ametys.cms.data.ContentValue;
032import org.ametys.cms.repository.Content;
033import org.ametys.core.util.HttpUtils;
034import org.ametys.odf.ProgramItem;
035import org.ametys.odf.course.Course;
036import org.ametys.odf.courselist.CourseList;
037import org.ametys.odf.orgunit.OrgUnit;
038import org.ametys.odf.program.AbstractProgram;
039import org.ametys.odf.program.Container;
040import org.ametys.odf.program.SubProgram;
041import org.ametys.plugins.odfsync.apogee.ws.structure.AbstractApogeeStructure;
042import org.ametys.plugins.repository.AmetysObjectResolver;
043import org.ametys.runtime.config.Config;
044import org.ametys.runtime.plugin.component.AbstractLogEnabled;
045
046import gouv.education.apogee.commun.client.ws.creationse.CreationSEMetierServiceInterface;
047import gouv.education.apogee.commun.client.ws.creationse.CreationSEMetierServiceInterfaceServiceLocator;
048import gouv.education.apogee.commun.transverse.dto.offreformation.creerse.CentreInsPedagogiDTO;
049import gouv.education.apogee.commun.transverse.dto.offreformation.creerse.CentreInsPedagogiDTO2;
050import gouv.education.apogee.commun.transverse.dto.offreformation.creerse.CmpHabiliteeVdiDTO;
051import gouv.education.apogee.commun.transverse.dto.offreformation.creerse.ComposanteCGEDTO;
052import gouv.education.apogee.commun.transverse.dto.offreformation.creerse.DiplomeDTO;
053import gouv.education.apogee.commun.transverse.dto.offreformation.creerse.ElementPedagogiDTO4;
054import gouv.education.apogee.commun.transverse.dto.offreformation.creerse.ElementPedagogiDTO6;
055import gouv.education.apogee.commun.transverse.dto.offreformation.creerse.EtapeDTO;
056import gouv.education.apogee.commun.transverse.dto.offreformation.creerse.LienVetElpLseDTO;
057import gouv.education.apogee.commun.transverse.dto.offreformation.creerse.ListeElementPedagogiDTO3;
058import gouv.education.apogee.commun.transverse.dto.offreformation.creerse.Nullable2Int;
059import gouv.education.apogee.commun.transverse.dto.offreformation.creerse.Nullable3Int;
060import gouv.education.apogee.commun.transverse.dto.offreformation.creerse.NullableDecimal;
061import gouv.education.apogee.commun.transverse.dto.offreformation.creerse.NullableInt;
062import gouv.education.apogee.commun.transverse.dto.offreformation.creerse.RegimeDTO;
063import gouv.education.apogee.commun.transverse.dto.offreformation.creerse.VdiFractionnerVetDTO;
064import gouv.education.apogee.commun.transverse.dto.offreformation.creerse.VersionDiplomeDTO;
065import gouv.education.apogee.commun.transverse.dto.offreformation.creerse.VersionEtapeDTO;
066
067
068/**
069 * The component to create ODF element in Apogee
070 * In most cases :
071 * DIP / VDI (diplome / version de diplome) : it is a program
072 * ETP / VET (etape / version d'etape) : it is a subprogram or a container of type year
073 * LSE (liste d'elements) : it is a course list
074 * ELP (element pedagogique) : it is a course or a container of type semester
075 */
076public class ApogeeWS extends AbstractLogEnabled implements Serviceable, Component
077{
078    /** Avalon ROLE */
079    public static final String ROLE = ApogeeWS.class.getName();
080    
081    /** The ametys object resolver */
082    protected AmetysObjectResolver _resolver;
083    
084    public void service(ServiceManager manager) throws ServiceException
085    {
086        _resolver = (AmetysObjectResolver) manager.lookup(AmetysObjectResolver.ROLE);
087    }
088    
089    /**
090     * Get the service to create element in Apogee
091     * @return the service
092     * @throws javax.xml.rpc.ServiceException if an error occurred getting the service
093     */
094    public CreationSEMetierServiceInterface getCreationService() throws javax.xml.rpc.ServiceException
095    {
096        String wsURL = HttpUtils.sanitize(Config.getInstance().getValue("apogee.ws.url"));
097        CreationSEMetierServiceInterfaceServiceLocator locator = new CreationSEMetierServiceInterfaceServiceLocator();
098        locator.setCreationSEMetierEndpointAddress(wsURL); // TODO user and mdp ????
099        
100        return locator.getCreationSEMetier();
101    }
102    
103    /**
104     * Create a Apogee DIP from a content
105     * @param content the content
106     * @param title the DIP title. Can be null, in this case we take the content title
107     * @param codDip the code DIP
108     * @throws RemoteException if a web service error occurred
109     * @throws javax.xml.rpc.ServiceException if an error occurred getting the service
110     */
111    public void createDIP(Content content, String title, String codDip) throws RemoteException, javax.xml.rpc.ServiceException
112    {
113        createDIP(content, title, codDip, getCreationService());
114    }
115    
116    /**
117     * Create a Apogee DIP from a content
118     * @param content the content
119     * @param title the DIP title. Can be null, in this case we take the content title
120     * @param codDip the code DIP
121     * @param creationSEMetierServiceInterface the service to create element in Apogee
122     * @throws RemoteException if a web service error occurred
123     */
124    public void createDIP(Content content, String title, String codDip, CreationSEMetierServiceInterface creationSEMetierServiceInterface) throws RemoteException
125    {
126        // Code diplome
127        String computedTitle = StringUtils.isNotBlank(title) ? title : content.getTitle();
128        
129        // codDip='Code diplôme (obligatoire)' "
130        // libDip='Libellé long diplôme (non obligatoire)' "
131        // licDip='Libellé court diplôme (obligatoire)' "
132        // codNdi='Code nature diplôme (obligatoire)'"
133        // codCycle='Code cycle SISE (obligatoire)'"
134        // codTypDip='Code type diplôme (obligatoire)'"
135        DiplomeDTO dip = new DiplomeDTO();
136        dip.setCodDip(codDip);
137        dip.setLibDip(computedTitle);
138        dip.setLicDip(StringUtils.substring(computedTitle, 0, 24)); // need to cut to 25 max
139        
140        String educationKindId = ContentDataHelper.getContentIdFromContentData(content, AbstractProgram.EDUCATION_KIND);
141        Content educationKindContent = _resolver.resolveById(educationKindId);
142        dip.setCodNdi(educationKindContent.getValue(AbstractApogeeStructure.CODE_APOGEE_ATTRIBUTE_NAME));
143
144        dip.setCodCycle(content.getValue("cycleApogee/codeApogee"));
145
146        String degreeCodeId = ContentDataHelper.getContentIdFromContentData(content, AbstractProgram.DEGREE);
147        Content degreeContent = _resolver.resolveById(degreeCodeId);
148        dip.setCodTypDip(degreeContent.getValue(AbstractApogeeStructure.CODE_APOGEE_ATTRIBUTE_NAME));
149        
150        creationSEMetierServiceInterface.creerDIP(dip);
151    }
152    
153    /**
154     * Create a Apogee VDI from a content
155     * @param content the content
156     * @param title the VDI title. Can be null, in this case we take the content title
157     * @param codDip the code of the diplome in Apogee
158     * @param versionDIP the version of the diplome in Apogee
159     * @throws RemoteException if a web service error occurred
160     * @throws javax.xml.rpc.ServiceException if an error occurred getting the service
161     */
162    public void createVDI(Content content, String title, String codDip, Long versionDIP) throws RemoteException, javax.xml.rpc.ServiceException
163    {
164        createVDI(content, title, codDip, versionDIP, getCreationService());
165    }
166    
167    /**
168     * Create a Apogee VDI from a content
169     * @param content the content
170     * @param title the VDI title. Can be null, in this case we take the content title
171     * @param codDip the code of the diplome in Apogee
172     * @param versionDIP the version of the diplome in Apogee
173     * @param creationSEMetierServiceInterface the service to create element in Apogee
174     * @throws RemoteException if a web service error occurred
175     */
176    public void createVDI(Content content, String title, String codDip, Long versionDIP, CreationSEMetierServiceInterface creationSEMetierServiceInterface) throws RemoteException
177    {
178        String computedTitle = StringUtils.isNotBlank(title) ? title : content.getTitle();
179        
180        // codDip='Code diplôme (obligatoire)' "
181        // codVersionDip='Code version diplôme (obligatoire)' "
182        // licVersionDip='Libellé court version diplôme (non obligatoire)' "
183        // debutRecrutement='Année universitaire début recrutement (obligatoire)' "
184        // finRecrutement='Année universitaire fin recrutement (obligatoire)' "
185        // debutValidation='Année universitaire début validation (obligatoire)' "
186        // finValidation='Année universitaire fin validation (obligatoire)' "
187        // listComposantesHabilitees='Code composante 1,Code composante 2,... (obligatoire)' "
188        VersionDiplomeDTO vdi = new VersionDiplomeDTO();
189        vdi.setCodDip(codDip);
190        
191        vdi.setCodVersionDip(new NullableInt(versionDIP.intValue()));
192        vdi.setLicVersionDip(StringUtils.substring(computedTitle, 0, 24));
193        
194        LocalDate startRecruitmentDate = content.getValue("start-date-recruitment");
195        LocalDate endRecruitmentDate = content.getValue("end-date-recruitment");
196        vdi.setDebutRecrutement(String.valueOf(startRecruitmentDate.getYear()));
197        vdi.setFinRecrutement(String.valueOf(endRecruitmentDate.getYear()));
198        
199        LocalDate startValidationDate = content.getValue("start-date-validation");
200        LocalDate endValidationDate = content.getValue("end-date-validation");
201        vdi.setDebutValidation(String.valueOf(startValidationDate.getYear()));
202        vdi.setFinValidation(String.valueOf(endValidationDate.getYear()));
203        
204        // Create tab of orgUnits VDI
205        CmpHabiliteeVdiDTO[] tabCmpHabiliteeVDIs = ContentDataHelper.getContentIdsListFromMultipleContentData(content, ProgramItem.ORG_UNITS_REFERENCES)
206                    .stream()
207                    .map(id -> _getOrgUnitById(id))
208                    .filter(o -> o != null)
209                    .map(o -> _createCmpHabiliteeVdiDTO(o))
210                    .toArray(CmpHabiliteeVdiDTO[]::new);
211        vdi.setListComposantesHabilitees(tabCmpHabiliteeVDIs);
212        
213        creationSEMetierServiceInterface.creerVDI(vdi);
214    }
215    
216    /**
217     * Create a Apogee ETP from a content
218     * @param content the content
219     * @param title the ETP title. Can be null, in this case we take the content title
220     * @param codETP the code ETP
221     * @throws RemoteException if a web service error occurred
222     * @throws javax.xml.rpc.ServiceException if an error occurred getting the service
223     */
224    public void createETP(Content content, String title, String codETP) throws RemoteException, javax.xml.rpc.ServiceException
225    {
226        createETP(content, title, codETP, getCreationService());
227    }
228    
229    /**
230     * Create a Apogee ETP from a content
231     * @param content the content
232     * @param title the ETP title. Can be null, in this case we take the content title
233     * @param codETP the code ETP
234     * @param creationSEMetierServiceInterface the service to create element in Apogee
235     * @throws RemoteException if a web service error occurred
236     */
237    public void createETP(Content content, String title, String codETP, CreationSEMetierServiceInterface creationSEMetierServiceInterface) throws RemoteException
238    {
239        String computedTitle = StringUtils.isNotBlank(title) ? title : content.getTitle();
240        
241        // codEtp='Code étape (obligatoire)' "
242        // licEtp='Libellé court étape (obligatoire)' "
243        // libEtp='Libellé long étape (non obligatoire)' "
244        // codCycle='Code cycle SISE (obligatoire)' "
245        // listComposantesCGE='[Code composante 1 (obligatoire),Code CGE 1 (obligatoire),capacité accueil 1 (non obligatoire)];[Code composante 2 (obligatoire),Code CGE 2 (obligatoire),capacité accueil 2 (non obligatoire)];... (obligatoire)' "
246        EtapeDTO etp = new EtapeDTO();
247        etp.setCodEtp(codETP);
248        etp.setLicEtp(StringUtils.substring(computedTitle, 0, 24)); // need to cut to 25 max
249        etp.setLibEtp(computedTitle);
250        
251        etp.setCodCycle(content.getValue("cycleApogee/codeApogee"));
252        
253        if (content instanceof SubProgram subProgram)
254        {
255            // Create tab of orgUnits CGE
256            ComposanteCGEDTO[] tabComp = subProgram.getOrgUnits()
257                        .stream()
258                        .map(id -> _getOrgUnitById(id))
259                        .filter(o -> o != null)
260                        .map(o -> _createComposanteCGEDTO(o))
261                        .toArray(ComposanteCGEDTO[]::new);
262            etp.setListComposantesCGE(tabComp);
263        }
264        else if (content instanceof Container)
265        {
266            ContentValue value = content.getValue("orgUnit");
267            ComposanteCGEDTO[] tabComp = new ComposanteCGEDTO[1];
268            tabComp[0] = _createComposanteCGEDTO((OrgUnit) value.getContent());
269            etp.setListComposantesCGE(tabComp);
270        }
271        
272        creationSEMetierServiceInterface.creerETP(etp);
273    }
274    
275    /**
276     * Create a Apogee VET from a content
277     * @param content the content
278     * @param title the VET title. Can be null, in this case we take the content title
279     * @param codETP the ETP code in Apogee
280     * @param versionETP the version ETP in Apogee
281     * @throws RemoteException if a web service error occurred
282     * @throws javax.xml.rpc.ServiceException if an error occurred getting the service
283     */
284    public void createVET(Content content, String title, String codETP, Long versionETP) throws RemoteException, javax.xml.rpc.ServiceException
285    {
286        createVET(content, title, codETP, versionETP, getCreationService());
287    }
288    
289    /**
290     * Create a Apogee VET from a content
291     * @param content the content
292     * @param title the VET title. Can be null, in this case we take the content title
293     * @param codETP the ETP code in Apogee
294     * @param versionETP the version ETP in Apogee
295     * @param creationSEMetierServiceInterface the service to create element in Apogee
296     * @throws RemoteException if a web service error occurred
297     */
298    public void createVET(Content content, String title, String codETP, Long versionETP, CreationSEMetierServiceInterface creationSEMetierServiceInterface) throws RemoteException
299    {
300        String computedTitle = StringUtils.isNotBlank(title) ? title : content.getTitle();
301        
302        // codEtp='Code étape (obligatoire)' "
303        // codVersionEtp='Code version étape (obligatoire)' "
304        // libWeb='Libellé web version étape (non obligatoire)' "
305        // codDureeEtp='Code durée étape' (obligatoire)' "
306        // codComposante='Code composante organisatrice (obligatoire)' "
307        // listRegimes='Code régime 1,Code régime 2,... (obligatoire)' "
308        // listCIP='[Code centre d'inscription pédagogique 1 (obligatoire),Témoin IA avant IP 1 (non obligatoire),Témoin inscription automatique aux éléments obligatoires 1 (non obligatoire)];[Code centre d'inscription pédagogique 2 (obligatoire),Témoin ...];... (obligatoire)' "
309        VersionEtapeDTO vet = new VersionEtapeDTO();
310        vet.setCodEtp(codETP);
311        vet.setCodVersionEtp(new NullableInt(versionETP.intValue()));
312        vet.setLibWeb(computedTitle);
313        
314        vet.setCodDureeEtp(content.getValue("duration-apogee/codeApogee"));
315
316        if (content instanceof SubProgram subProgram) // TODO prendre orgunit principale
317        {
318            String orgUnitId = subProgram.getOrgUnits().get(0);
319            OrgUnit orgUnit = _getOrgUnitById(orgUnitId);
320            vet.setCodComposante(orgUnit.getValue(AbstractApogeeStructure.CODE_APOGEE_ATTRIBUTE_NAME));
321        }
322        else if (content instanceof Container)
323        {
324            ContentValue value = content.getValue("orgUnit");
325            vet.setCodComposante(value.getContent().getValue(AbstractApogeeStructure.CODE_APOGEE_ATTRIBUTE_NAME));
326        }
327        
328        // Create tab of regime
329        String[] inscriptionTypes = content.getValue("inscription-types/codeApogee", true);
330        RegimeDTO[] tabRegimes = Stream.of(inscriptionTypes)
331                    .map(it -> _createRegime(it))
332                    .toArray(RegimeDTO[]::new);
333        vet.setListRegimes(tabRegimes);
334
335        // Create tab of CIP
336        String[] cips = content.getValue("cips/codeApogee", true);
337        CentreInsPedagogiDTO2[] tabCIP = Stream.of(cips)
338                    .map(cip -> _createCIPVET(cip))
339                    .toArray(CentreInsPedagogiDTO2[]::new);
340        vet.setListCIP(tabCIP);
341
342        creationSEMetierServiceInterface.creerVET(vet);
343    }
344    
345    /**
346     * Create in Apogee the link between a DIP and a ETP
347     * @param codDIP the DIP code in Apogee
348     * @param versionDIP the DIP version in Apogee
349     * @param codETP the ETP code in Apogee
350     * @param versionETP the ETP version in Apogee
351     * @throws RemoteException if a web service error occurred
352     * @throws javax.xml.rpc.ServiceException if an error occurred getting the service
353     */
354    public void createLinkDIPETP(String codDIP, Long versionDIP, String codETP, Long versionETP) throws RemoteException, javax.xml.rpc.ServiceException
355    {
356        createLinkDIPETP(codDIP, versionDIP, codETP, versionETP, getCreationService());
357    }
358    
359    /**
360     * Create in Apogee the link between a DIP and a ETP
361     * @param codDIP the DIP code in Apogee
362     * @param versionDIP the DIP version in Apogee
363     * @param codETP the ETP code in Apogee
364     * @param versionETP the ETP version in Apogee
365     * @param creationSEMetierServiceInterface the service to create element in Apogee
366     * @throws RemoteException if a web service error occurred
367     */
368    public void createLinkDIPETP(String codDIP, Long versionDIP, String codETP, Long versionETP, CreationSEMetierServiceInterface creationSEMetierServiceInterface) throws RemoteException
369    {
370        // codDip='Code diplôme (obligatoire)' "
371        // codVersionDip='Code version diplôme (obligatoire)' "
372        // codEtp='Code étape (obligatoire)' "
373        // codVersionEtp='Code version étape (obligatoire)'"
374        // anMinEtpDip='Année minimale étape pour diplôme (obligatoire)' "
375        // anMaxEtpDip='Année maximale étape pour diplôme (obligatoire)' "
376        VdiFractionnerVetDTO lien = new VdiFractionnerVetDTO();
377        lien.setCodDip(codDIP);
378        lien.setCodVersionDip(new Nullable3Int(versionDIP.intValue()));
379        lien.setCodEtp(codETP);
380        lien.setCodVersionEtp(new Nullable3Int(versionETP.intValue()));
381        lien.setAnMinEtpDip(new NullableInt("1")); //TODO
382        lien.setAnMaxEtpDip(new NullableInt("2")); //TODO
383
384        creationSEMetierServiceInterface.creerLienVetVdi(lien);
385    }
386    
387    /**
388     * Create in Apogee the link between a ETP and a LSE or ELP
389     * @param codETP the ETP code in Apogee. Can be null if codELP is set.
390     * @param versionETP the ETP version in Apogee. Can be null if codELP is set.
391     * @param codLSE the LSE code in Apogee.
392     * @param codELP the ELP version in Apogee. Can be null if codETP and versionETP is set.
393     * @param nbELP the number of ELP. Can be null if the LSE is mandatory or optional.
394     * @param ectsMin the min number of ects. Can be null if the LSE is mandatory or optional.
395     * @param ectsMax the max number of ects. Can be null if the LSE is mandatory or optional.
396     * @throws RemoteException if a web service error occurred
397     * @throws javax.xml.rpc.ServiceException if an error occurred getting the service
398     */
399    public void createLinkETPELPLSE(String codETP, Long versionETP, String codLSE, String codELP, Long nbELP, Double ectsMin, Double ectsMax) throws RemoteException, javax.xml.rpc.ServiceException
400    {
401        createLinkETPELPLSE(codETP, versionETP, codLSE, codELP, nbELP, ectsMin, ectsMax, getCreationService());
402    }
403    
404    /**
405     * Create in Apogee the link between a ETP and a LSE or ELP
406     * @param codETP the ETP code in Apogee. Can be null if codELP is set.
407     * @param versionETP the ETP version in Apogee. Can be null if codELP is set.
408     * @param codLSE the LSE code in Apogee.
409     * @param codELP the ELP version in Apogee. Can be null if codETP and versionETP is set.
410     * @param nbELP the number of ELP. Can be null if the LSE is mandatory or optional.
411     * @param ectsMin the min number of ects. Can be null if the LSE is mandatory or optional.
412     * @param ectsMax the max number of ects. Can be null if the LSE is mandatory or optional.
413     * @param creationSEMetierServiceInterface the service to create element in Apogee
414     * @throws RemoteException if a web service error occurred
415     */
416    public void createLinkETPELPLSE(String codETP, Long versionETP, String codLSE, String codELP, Long nbELP, Double ectsMin, Double ectsMax, CreationSEMetierServiceInterface creationSEMetierServiceInterface) throws RemoteException
417    {
418        // codElp='Code élément pédagogique (obligatoire)' "
419        // codEtp='Code étape (obligatoire)'"
420        // codVersionEtp='Code version étape (obligatoire)'"
421        // codListeElp='Code Liste d'éléments pédagogiques (obligatoire)'"
422        // nbrMinElp='Nombre minimum d'éléments à choisir dans la liste'"
423        // nbrMaxElp='Nombre maximum d'éléments à choisir dans la liste'"
424        // nbrMinCredits='Nombre minimum de crédits ECTS à choisir dans la liste'"
425        // nbrMaxCredits='Nombre maximum de crédits ECTS à choisir dans la liste'"
426        
427        LienVetElpLseDTO lien = new LienVetElpLseDTO();
428        if (StringUtils.isNotBlank(codETP))
429        {
430            lien.setCodEtp(codETP);
431        }
432        
433        if (versionETP != null)
434        {
435            lien.setCodVersionEtp(new Nullable3Int(versionETP.intValue()));
436        }
437        else
438        {
439            lien.setCodVersionEtp(new Nullable3Int());
440        }
441
442        lien.setCodListeElp(codLSE);
443        lien.setCodElp(codELP);
444        
445        if (nbELP != null)
446        {
447            lien.setNbrMinElp(new Nullable2Int(nbELP.intValue()));
448            lien.setNbrMaxElp(new Nullable2Int(nbELP.intValue()));
449            
450            lien.setNbrMinCredits(new NullableDecimal(new BigDecimal(ectsMin)));
451            lien.setNbrMaxCredits(new NullableDecimal(new BigDecimal(ectsMax)));
452        }
453        creationSEMetierServiceInterface.creerLienVetElpLse(lien);
454    }
455    
456    /**
457     * Create a Apogee LSE from a course list
458     * @param courseList the course list
459     * @param title the LSE title. Can be null, in this case we take the content title
460     * @param codLSE the code LSE
461     * @throws RemoteException if a web service error occurred
462     * @throws javax.xml.rpc.ServiceException if an error occurred getting the service
463     */
464    public void createLSE(CourseList courseList, String title, String codLSE) throws RemoteException, javax.xml.rpc.ServiceException
465    {
466        createLSE(courseList, title, codLSE, getCreationService());
467    }
468    
469    /**
470     * Create a Apogee LSE from a course list
471     * @param courseList the course list
472     * @param title the LSE title. Can be null, in this case we take the content title
473     * @param codLSE the code LSE
474     * @param creationSEMetierServiceInterface the service to create element in Apogee
475     * @throws RemoteException if a web service error occurred
476     */
477    public void createLSE(CourseList courseList, String title, String codLSE, CreationSEMetierServiceInterface creationSEMetierServiceInterface) throws RemoteException
478    {
479        String computedTitle = StringUtils.isNotBlank(title) ? title : courseList.getTitle();
480        
481        // Création du tableau d'ELP
482        int i = 0;
483        List<Course> courses = courseList.getCourses();
484        ElementPedagogiDTO4[] tabELP = new ElementPedagogiDTO4[courses.size()];
485        for (Course course : courses)
486        {
487            tabELP[i] = new ElementPedagogiDTO4(course.getValue(AbstractApogeeStructure.CODE_APOGEE_ATTRIBUTE_NAME));
488            i++;
489        }
490
491        // codListeElp='Code Liste d'éléments pédagogiques (obligatoire)' "
492        // typListeElp='Code type de liste (obligatoire)' "
493        // libCourtListeElp='Libellé court liste d'éléments pédagogiques (obligatoire)'"
494        // libListeElp='Libelle long liste d'éléments pédagogiques (obligatoire)'"
495        // listElementPedagogi='Code élément pédagogique,Code élément pédagogique 2,Code élément pédagogique 3,... (au moins 1 codElp, obligatoire)'"
496        ListeElementPedagogiDTO3 lse = new ListeElementPedagogiDTO3();
497        lse.setCodListeElp(codLSE);
498        
499        String choiceTypeAmetys = courseList.getValue("choiceType");
500        String typList = "O";
501        if (choiceTypeAmetys.equals("MANDATORY"))
502        {
503            typList = "F";
504        }
505        else if (choiceTypeAmetys.equals("CHOICE"))
506        {
507            typList = "X";
508        }
509        lse.setTypListeElp(typList);
510        lse.setLibCourtListeElp(StringUtils.substring(computedTitle, 0, 24));
511        lse.setLibListeElp(computedTitle);
512        lse.setListElementPedagogi(tabELP);
513        
514        creationSEMetierServiceInterface.creerLSE(lse);
515    }
516    
517    /**
518     * Create a Apogee mandatory LSE with his ELP
519     * @param titleLSE the title of the LSE
520     * @param codLSE the code of the LSE
521     * @param codELP the code of the ELP
522     * @throws RemoteException if a web service error occurred
523     * @throws javax.xml.rpc.ServiceException if an error occurred getting the service
524     */
525    public void createMandatoryLSE(String titleLSE, String codLSE, String codELP) throws RemoteException, javax.xml.rpc.ServiceException
526    {
527        createMandatoryLSE(titleLSE, codLSE, codELP, getCreationService());
528    }
529    
530    /**
531     * Create a Apogee mandatory LSE with his ELP
532     * @param titleLSE the title of the LSE
533     * @param codLSE the code of the LSE
534     * @param codELP the code of the ELP
535     * @param creationSEMetierServiceInterface the service to create element in Apogee
536     * @throws RemoteException if a web service error occurred
537     */
538    public void createMandatoryLSE(String titleLSE, String codLSE, String codELP, CreationSEMetierServiceInterface creationSEMetierServiceInterface) throws RemoteException
539    {
540        // Création du tableau d'ELP
541        ElementPedagogiDTO4[] tabELP = new ElementPedagogiDTO4[1];
542        tabELP[0] = new ElementPedagogiDTO4(codELP);
543
544        // codListeElp='Code Liste d'éléments pédagogiques (obligatoire)' "
545        // typListeElp='Code type de liste (obligatoire)' "
546        // libCourtListeElp='Libellé court liste d'éléments pédagogiques (obligatoire)'"
547        // libListeElp='Libelle long liste d'éléments pédagogiques (obligatoire)'"
548        // listElementPedagogi='Code élément pédagogique,Code élément pédagogique 2,Code élément pédagogique 3,... (au moins 1 codElp, obligatoire)'"
549        ListeElementPedagogiDTO3 lse = new ListeElementPedagogiDTO3();
550        lse.setCodListeElp(codLSE);
551        
552        lse.setTypListeElp("F");
553        lse.setLibCourtListeElp(StringUtils.substring(titleLSE, 0, 24));
554        lse.setLibListeElp(titleLSE);
555        lse.setListElementPedagogi(tabELP);
556        
557        creationSEMetierServiceInterface.creerLSE(lse);
558    }
559    
560    /**
561     * Create a Apogee ELP from a content
562     * @param content the content
563     * @param title the ELP title. Can be null, in this case we take the content title
564     * @param codELP the code ELP
565     * @throws RemoteException if a web service error occurred
566     * @throws javax.xml.rpc.ServiceException if an error occurred getting the service
567     */
568    public void createELP(Content content, String title, String codELP) throws RemoteException, javax.xml.rpc.ServiceException
569    {
570        createELP(content, title, codELP, getCreationService());
571    }
572    
573    /**
574     * Create a Apogee ELP from a content
575     * @param content the content
576     * @param title the ELP title. Can be null, in this case we take the content title
577     * @param codELP the code ELP
578     * @param creationSEMetierServiceInterface the service to create element in Apogee
579     * @throws RemoteException if a web service error occurred
580     */
581    public void createELP(Content content, String title, String codELP, CreationSEMetierServiceInterface creationSEMetierServiceInterface) throws RemoteException
582    {
583        String computedTitle = StringUtils.isNotBlank(title) ? title : content.getTitle();
584        
585        // Create the ELP DTO
586        // codElp='Code élément pédagogique (obligatoire)' "
587        // libCourtElp='Libellé court élément pédagogique pédagogiques (obligatoire)'"
588        // libElp='Libelle long élément pédagogique (obligatoire)'"
589        // codNatureElp='Code de la nature ELP' (obligatoire)"
590        // codComposante='Code composante' (obligatoire)"
591        // listCentreInsPedagogi='Code CIP 1,Code CIP 2,Code CIP 3, ... (au moins 1 codCip, obligatoire)'"
592        ElementPedagogiDTO6 elp = new ElementPedagogiDTO6();
593        elp.setCodElp(codELP);
594        elp.setLibElp(computedTitle);
595        elp.setLibCourtElp(StringUtils.substring(computedTitle, 0, 24));
596        
597        if (content instanceof Container)
598        {
599            ContentValue value = content.getValue("nature");
600            elp.setCodNatureElp(value.getContent().getValue(AbstractApogeeStructure.CODE_APOGEE_ATTRIBUTE_NAME));
601        }
602        else if (content instanceof Course)
603        {
604            ContentValue value = content.getValue("courseType");
605            elp.setCodNatureElp(value.getContent().getValue(AbstractApogeeStructure.CODE_APOGEE_ATTRIBUTE_NAME));
606        }
607        
608        if (content instanceof Course) // TODO prendre orgunit principale
609        {
610            String orgUnitId = ((Course) content).getOrgUnits().get(0);
611            OrgUnit orgUnit = _getOrgUnitById(orgUnitId);
612            elp.setCodComposante(orgUnit.getValue(AbstractApogeeStructure.CODE_APOGEE_ATTRIBUTE_NAME));
613        }
614        else if (content instanceof Container)
615        {
616            ContentValue value = content.getValue("orgUnit");
617            elp.setCodComposante(value.getContent().getValue(AbstractApogeeStructure.CODE_APOGEE_ATTRIBUTE_NAME));
618        }
619        
620        // Create tab of CIP
621        String[] cips = content.getValue("cips/codeApogee", true);
622        CentreInsPedagogiDTO[] tabCIP = Stream.of(cips)
623                    .map(cip -> _createCIPELP(cip))
624                    .toArray(CentreInsPedagogiDTO[]::new);
625        elp.setListCentreInsPedagogi(tabCIP);
626        
627        creationSEMetierServiceInterface.creerELP_v2(elp);
628    }
629    
630    /**
631     * Get orgUnit content from id
632     * @param orgUnitId the orgUnit id
633     * @return the orgUnit content
634     */
635    private OrgUnit _getOrgUnitById(String orgUnitId)
636    {
637        try
638        {
639            OrgUnit orgUnit = _resolver.resolveById(orgUnitId);
640            return orgUnit;
641        }
642        catch (Exception e)
643        {
644            getLogger().warn("Can't get orgunit from id " + orgUnitId, e);
645            return null;
646        }
647    }
648    
649    /**
650     * Create CIP object for VET Apogee
651     * @param code the code of the CIP
652     * @return the CIP object
653     */
654    private CentreInsPedagogiDTO2 _createCIPVET(String code)
655    {
656        CentreInsPedagogiDTO2 cip = new CentreInsPedagogiDTO2();
657        cip.setCodCIP(code);
658        
659        return cip;
660    }
661    
662    /**
663     * Create CIP object for ELP Apogee
664     * @param code the code of the CIP
665     * @return the CIP object
666     */
667    private CentreInsPedagogiDTO _createCIPELP(String code)
668    {
669        return new CentreInsPedagogiDTO(code);
670    }
671    
672    /**
673     * Create Regime object for Apogee
674     * @param code the code of the regime
675     * @return the Regime object
676     */
677    private RegimeDTO _createRegime(String code)
678    {
679        RegimeDTO regime = new RegimeDTO();
680        regime.setCodRegime(code);
681        
682        return regime;
683    }
684    
685    /**
686     * Create the orgUnit CGE for Apogee
687     * @param orgUnit the Ametys orgUnit
688     * @return the orgUnit CGE
689     */
690    private ComposanteCGEDTO _createComposanteCGEDTO(OrgUnit orgUnit)
691    {
692        ComposanteCGEDTO comp = new ComposanteCGEDTO();
693        comp.setCodComposante(orgUnit.getValue(AbstractApogeeStructure.CODE_APOGEE_ATTRIBUTE_NAME));
694        comp.setCodCGE(orgUnit.getValue("codeCGE"));
695        
696        return comp;
697    }
698    
699    /**
700     * Create the orgUnit VDI for Apogee
701     * @param orgUnit the Ametys orgUnit
702     * @return the orgUnit VDI
703     */
704    private CmpHabiliteeVdiDTO _createCmpHabiliteeVdiDTO(OrgUnit orgUnit)
705    {
706        CmpHabiliteeVdiDTO comp = new CmpHabiliteeVdiDTO();
707        comp.setCodComposanteHabilitee(orgUnit.getValue(AbstractApogeeStructure.CODE_APOGEE_ATTRIBUTE_NAME));
708        
709        return comp;
710    }
711}