using EPAServeur.Context; using EPAServeur.Exceptions; using EPAServeur.IServices; using EPAServeur.Models.EP; using EPAServeur.Models.Formation; using EPAServeur.Models.Notes; using EPAServeur.Models.SaisieChamp; using IO.Swagger.ApiCollaborateur; using IO.Swagger.DTO; using IO.Swagger.Enum; using IO.Swagger.ModelCollaborateur; using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; namespace EPAServeur.Services { public class TransformDTO : ITransformDTO { /// /// Transformer un objet DetailsNoteDTO en objet Note. /// /// En général, de base, cette méthode est prévue pour être utilisée qu'à la création d'une nouvelle note, dateCreation et dateUpdate sont donc initialisée à ce moment là /// Objet DTO à transformer en note /// L'objet DTO transformé en note public Note DetailsNoteDTOToNouvelleNote(DetailsNoteDTO detailsNoteDTO) { Note note = new Note() { IdAuteur = detailsNoteDTO.IdAuteur.Value, IdCollaborateur = detailsNoteDTO.Collaborateur.Id.Value, Texte = detailsNoteDTO.Texte, Titre = detailsNoteDTO.Titre, DateCreation = detailsNoteDTO.DateCreation.Value, DateMiseAJour = detailsNoteDTO.DateMiseAjour.Value }; return note; } /// /// Transformer un objet Ep en objet EpInformationDTO. /// La liste des collaborateurs est utilisé pour alimenter le collaborateur et le référent de l'objet EpInformationDTO. /// /// /// /// public EpInformationDTO EpToEpDTO(Ep ep, IEnumerable collaborateurDTOs) { return new EpInformationDTO() { Id = ep.IdEP, Collaborateur = collaborateurDTOs.Where(c => c.Id.Equals(ep.IdCollaborateur)).FirstOrDefault(), DateDisponibilite = ep.DateDisponibilite, DatePrevisionnelle = ep.DatePrevisionnelle, Obligatoire = ep.Obligatoire, Referent = collaborateurDTOs.FirstOrDefault(c => c.Id.Equals(ep.IdReferent)), Statut = ep.Statut, Type = ep.TypeEP }; } /// /// Transformer un objet Agence en objet AgenceDTO. /// /// /// public AgenceDTO GetAgenceDTO(Agence agence) { if (agence == null) return null; AgenceDTO agenceDTO = new AgenceDTO() { Id = agence.Id, Nom = agence.Nom, Bu = new List() }; agenceDTO.Bu = agence.Bus.Select(bu => new BusinessUnitDTO() { Id = bu.Id, Nom = bu.Nom }).ToList(); return agenceDTO; } /// /// Transformer une objet BU en objet BusinessUnitDTO. /// /// /// public BusinessUnitDTO GetBusinessUnitDTO(BU businessUnit) { if (businessUnit == null) return null; BusinessUnitDTO businessUnitDTO = new BusinessUnitDTO() { Id = businessUnit.Id, Nom = businessUnit.Nom, Agence = GetAgenceDTO(businessUnit.Agence) }; return businessUnitDTO; } /// /// Récupérer un objet Champ en objet ChampDTO. /// /// /// public Champ GetChamp(ChampDTO champDTO) { if (champDTO == null) return null; Champ champ = new Champ() { IdChamp = champDTO.Id.Value, Texte = champDTO.Texte, Section = champDTO.Section, SousSection = champDTO.Soussection, Ordre = champDTO.Ordre.Value, TypeChamp = champDTO.TypeChamp, TypeSaisie = champDTO.TypeSaisie }; return champ; } /// /// Transformer un objet Champ en objet ChampDTO. /// /// /// public ChampDTO GetChampDTO(Champ champ) { if (champ == null) return null; ChampDTO champDTO = new ChampDTO() { Id = champ.IdChamp, Texte = champ.Texte, Section = champ.Section, Soussection = champ.SousSection, Ordre = champ.Ordre, TypeChamp = champ.TypeChamp, TypeSaisie = champ.TypeSaisie }; return champDTO; } /// /// Récupérer le collaborateur qui participe à la formation. /// /// /// /// public CollaborateurDTO GetCollaborateurDTO(ParticipationFormation participationFormation, IEnumerable collaborateurDTOs) { if (participationFormation == null) return null; if (collaborateurDTOs == null || !collaborateurDTOs.Any()) return null; return collaborateurDTOs.FirstOrDefault(collaborateurDTO => collaborateurDTO.Id == participationFormation.DemandeFormation.Ep.IdCollaborateur); } /// /// Récupérer le collaborateur qui a fait une demande de formation. /// /// /// /// public CollaborateurDTO GetCollaborateurDTO(DemandeFormation demandeFormation, IEnumerable collaborateurDTOs) { if (demandeFormation == null) return null; if (collaborateurDTOs == null || !collaborateurDTOs.Any()) return null; return collaborateurDTOs.FirstOrDefault(collaborateurDTO => collaborateurDTO.Id == demandeFormation.Ep.IdCollaborateur); } /// /// Transformer un objet DemandeDelegation en objet DemandeDelegationDTO. /// L'objet CollaborateurDTO est utilisé pour alimenter la propriété Referent de l'objet DemandeDelegationDTO. /// La liste de collaborateurs est utilisé pour alimenter la propriété Ep de l'objet DemandeDelegationDTO. /// /// /// /// /// public DemandeDelegationDTO GetDemandeDelegationDTO(DemandeDelegation demande, CollaborateurDTO referent, IEnumerable collaborateurs) { return new DemandeDelegationDTO() { Id = demande.IdDemandeDelegation, DateDemande = demande.DateDemande, EtatDemande = demande.EtatDemande, Referent = referent, RaisonDemande = demande.RaisonDemande, Ep = GetEpInformationDTO(demande.Ep, collaborateurs) }; } /// /// Transformer un objet Engagement en objet EngagementDTO. /// La liste de collaborateurs est utilisé pour alimenter la propriété Ep de l'objet EngagementDTO. /// /// /// /// public EngagementDTO GetEngagementDTO(Engagement engagement, IEnumerable collaborateurDTOs) { if (engagement == null || collaborateurDTOs == null || !collaborateurDTOs.Any()) return null; EngagementDTO engagementDTO = new EngagementDTO() { Id = engagement.IdEngagement, Action = engagement.Action, DateLimite = engagement.DateLimite, Dispositif = engagement.Dispositif, Modalite = engagement.Modalite, RaisonNonRealisable = engagement.RaisonNonRealisable, EtatEngagement = engagement.EtatEngagement, Ep = GetEpInformationDTO(engagement.Ep, collaborateurDTOs) }; return engagementDTO; } /// /// Transformer un objet Ep en objet EpInformationDTO. /// La liste de collaborateurs est utilisé pour alimenter la propriété Collaborateur et la propriété Referent de l'objet EpInformationDTO. /// /// /// /// public EpInformationDTO GetEpInformationDTO(Ep ep, IEnumerable collaborateurs) { CollaborateurDTO collaborateur; CollaborateurDTO referent; if (ep == null || collaborateurs == null || !collaborateurs.Any()) return null; collaborateur = collaborateurs.FirstOrDefault(collaborateurDTO => collaborateurDTO.Id == ep.IdCollaborateur); referent = collaborateurs.FirstOrDefault(collaborateurDTO => collaborateurDTO.Id == ep.IdReferent); EpInformationDTO epInformationDTO = new EpInformationDTO() { Id = ep.IdEP, Type = ep.TypeEP, Statut = ep.Statut, DateDisponibilite = ep.DateDisponibilite, DatePrevisionnelle = ep.DatePrevisionnelle, Obligatoire = ep.Obligatoire, Collaborateur = collaborateur, Referent = referent, }; return epInformationDTO; } /// /// Transformer un objet ParticipationFormation en objet EvaluationDTO. /// /// /// public EvaluationDTO GetEvaluationDTO(ParticipationFormation participationFormation) { if (participationFormation == null) return null; EvaluationDTO evaluationDTO = new EvaluationDTO() { Id = participationFormation.IdParticipationFormation, Intitule = participationFormation.Formation.Intitule, DateDebut = participationFormation.Formation.DateDebut, EstCertifiee = participationFormation.Formation.EstCertifiee, Saisies = participationFormation.Evaluation.Select(s => GetSaisieDTO(s)).ToList() }; return evaluationDTO; } /// /// Transformer un objet Formation en objet FormationDetailsDTO. /// /// /// public FormationDetailsDTO GetFormationDetailsDTO(Formation formation) { if (formation == null) return null; FormationDetailsDTO formationDTO = new FormationDetailsDTO() { Id = formation.IdFormation, Intitule = formation.Intitule, DateDebut = formation.DateDebut, DateFin = formation.DateFin, Organisme = formation.Organisme, EstCertifiee = formation.EstCertifiee, NbParticipations = formation.ParticipationsFormation.Count, Origine = GetOrigineFormationDTO(formation.Origine), Statut = GetStatutFormationDTO(formation.Statut), }; return formationDTO; } /// /// Transformer un objet Formation en objet FormationDTO. /// La liste de collaborateurs est utilisé pour alimenter la propriété Participations de l'objet FormationDTO. /// /// /// /// public FormationDTO GetFormationDTO(Formation formation, IEnumerable collaborateurDTOs) { if (formation == null || collaborateurDTOs == null || !collaborateurDTOs.Any()) return null; FormationDTO formationDTO = new FormationDTO() { Id = formation.IdFormation, Intitule = formation.Intitule, IdAgence = formation.IdAgence, DateDebut = formation.DateDebut, DateFin = formation.DateFin, Heure = formation.Heure, Jour = formation.Jour, Organisme = formation.Organisme, EstCertifiee = formation.EstCertifiee, EstRealisee = formation.EstRealisee, Origine = GetOrigineFormationDTO(formation.Origine), Statut = GetStatutFormationDTO(formation.Statut), Mode = GetModeFormationDTO(formation.ModeFormation), Type = GetTypeFormationDTO(formation.TypeFormation), Participations = GetParticipationsFormationDTO(formation.ParticipationsFormation, collaborateurDTOs) }; return formationDTO; } /// /// Transformer un objet Formation en objet FormationDTO et laisser la propriété Participations de l'objet FormationDTO null. /// /// /// public FormationDTO GetFormationDTOWhitoutParticipationFormation(Formation formation) { if (formation == null) return null; FormationDTO formationDTO = new FormationDTO() { Id = formation.IdFormation, Intitule = formation.Intitule, IdAgence = formation.IdAgence, DateDebut = formation.DateDebut, DateFin = formation.DateFin, Heure = formation.Heure, Jour = formation.Jour, Organisme = formation.Organisme, EstCertifiee = formation.EstCertifiee, EstRealisee = formation.EstRealisee, Origine = GetOrigineFormationDTO(formation.Origine), Statut = GetStatutFormationDTO(formation.Statut), Mode = GetModeFormationDTO(formation.ModeFormation), Type = GetTypeFormationDTO(formation.TypeFormation), }; return formationDTO; } /// /// Transformer un objet ModeFormationDTO en objet ModeFormation. /// /// /// public ModeFormation GetModeFormation(ModeFormationDTO modeFormationDTO) { if (modeFormationDTO == null) return null; ModeFormation modeFormation = new ModeFormation() { IdModeFormation = modeFormationDTO.Id.Value, Libelle = modeFormationDTO.Libelle }; return modeFormation; } /// /// Transformer un objet ModeFormation en objet ModeFormationDTO. /// /// /// public ModeFormationDTO GetModeFormationDTO(ModeFormation modeFormation) { if (modeFormation == null) return null; ModeFormationDTO modeFormationDTO = new ModeFormationDTO() { Id = modeFormation.IdModeFormation, Libelle = modeFormation.Libelle }; return modeFormationDTO; } /// /// Transformer un objet OrigineFormationDTO en objet OrigineFormation. /// /// /// public OrigineFormation GetOrigineFormation(OrigineFormationDTO origineFormationDTO) { if (origineFormationDTO == null) return null; OrigineFormation origineFormation = new OrigineFormation() { IdOrigineFormation = origineFormationDTO.Id.Value, Libelle = origineFormationDTO.Libelle }; return origineFormation; } /// /// Transformer un objet OrigineFormation en objet OrigineFormationDTO. /// /// /// public OrigineFormationDTO GetOrigineFormationDTO(OrigineFormation origineFormation) { if (origineFormation == null) return null; OrigineFormationDTO origineFormationDTO = new OrigineFormationDTO() { Id = origineFormation.IdOrigineFormation, Libelle = origineFormation.Libelle }; return origineFormationDTO; } /// /// Transformer un objet ParticipationFormation en objet ParticipationFormationDTO. /// La liste de collaborateurs est utilisé pour alimenter la propriété Collaborateur et la propriété Ep de l'objet FormationDTO. /// /// /// /// public ParticipationFormationDTO GetParticipationFormationDTO(ParticipationFormation participationFormation, IEnumerable collaborateurDTOs) { if (participationFormation == null || collaborateurDTOs == null || !collaborateurDTOs.Any()) return null; ParticipationFormationDTO participationFormationDTO = new ParticipationFormationDTO() { Id = participationFormation.IdParticipationFormation, DateCreation = participationFormation.DateCreation, Intitule = participationFormation.Formation.Intitule, DateDebut = participationFormation.Formation.DateDebut, Statut = GetStatutFormationDTO(participationFormation.Formation.Statut), Collaborateur = GetCollaborateurDTO(participationFormation, collaborateurDTOs), Ep = GetEpInformationDTO(participationFormation.DemandeFormation.Ep, collaborateurDTOs) }; return participationFormationDTO; } /// /// Transformer une liste d'objets ParticipationFormation en liste d'objets ParticipationFormationDTO. /// La liste de collaborateurs est utilisé pour alimenter la propriété Collaborateur et la propriété Ep de l'objet FormationDTO. /// /// /// /// public List GetParticipationsFormationDTO(List participationsFormation, IEnumerable collaborateurDTOs) { List participationFormationDTOs; if (participationsFormation == null || participationsFormation.Count == 0 || collaborateurDTOs == null || !collaborateurDTOs.Any()) return null; participationFormationDTOs = participationsFormation.Select(participationFormation => GetParticipationFormationDTO(participationFormation, collaborateurDTOs)) .OrderBy(participationFormation => participationFormation.Collaborateur.Nom) .ThenBy(participationFormation => participationFormation.Collaborateur.Prenom).ToList(); return participationFormationDTOs; } // /// Récuperer un objet OrigineDemandeFormationDTO en fonction d'un objet OrigineDemande /// /// /// public OrigineDemandeFormationDTO GetOrigineDemandeFormationDTO(OrigineDemande origineDemande) { if (origineDemande == null) return null; OrigineDemandeFormationDTO origineDemandeFormationDTO = new OrigineDemandeFormationDTO() { Id = origineDemande.IdOrigineDemande, Libelle = origineDemande.Libelle }; return origineDemandeFormationDTO; } /// /// Récuperer un objet DemandeFormationDTO en fonction d'un objet DemandeFormation et d'une liste de CollaborateurDTO /// /// /// public DemandeFormationDTO GetDemandeFormationDTO(DemandeFormation demandeFormation, IEnumerable collaborateurDTOs) { if (demandeFormation == null) return null; if (collaborateurDTOs == null || !collaborateurDTOs.Any()) return null; Formation formation; if (demandeFormation.ParticipationFormation != null) formation = demandeFormation.ParticipationFormation.Formation; else formation = null; DemandeFormationDTO demandeFormationDTO = new DemandeFormationDTO() { Id = demandeFormation.IdDemandeFormation, Libelle = demandeFormation.Libelle, Description = demandeFormation.Description, DemandeRH = demandeFormation.DemandeRH, DateDemande = demandeFormation.DateDemande, EtatDemande = demandeFormation.Etat, CommentaireRefus = demandeFormation.CommentaireRefus, DateDerniereReponse = demandeFormation.DateDerniereReponse, Origine = GetOrigineDemandeFormationDTO(demandeFormation.OrigineDemande), Collaborateur = GetCollaborateurDTO(demandeFormation, collaborateurDTOs), Ep = GetEpInformationDTO(demandeFormation.Ep, collaborateurDTOs), Formation = GetFormationDetailsDTO(formation) }; return demandeFormationDTO; } /// /// Transformer un objet SaisieDTO en objet Saisie. /// /// /// public Saisie GetSaisie(SaisieDTO saisieDTO) { if (saisieDTO == null) return null; Saisie saisie = new Saisie() { //IdSaisie = saisieDTO.Id.Value, Note = saisieDTO.Note, Texte = saisieDTO.Texte, Champ = GetChamp(saisieDTO.Champ), TypeSaisie = saisieDTO.TypeSaisie }; return saisie; } /// /// Transformer un objet Saisie en objet SaisieDTO. /// /// /// public SaisieDTO GetSaisieDTO(Saisie saisie) { if (saisie == null) return null; SaisieDTO saisieDTO = new SaisieDTO() { Id = saisie.IdSaisie, Note = saisie.Note, Texte = saisie.Texte, Champ = GetChampDTO(saisie.Champ), TypeSaisie = saisie.TypeSaisie }; return saisieDTO; } /// /// Transformer un objet StatutFormationDTO en objet StatutFormation. /// /// /// public StatutFormation GetStatutFormation(StatutFormationDTO statutFormationDTO) { if (statutFormationDTO == null) return null; StatutFormation statutFormation = new StatutFormation() { IdStatutFormation = statutFormationDTO.Id.Value, Libelle = statutFormationDTO.Libelle }; return statutFormation; } /// /// Transformer un objet StatutFormation en objet StatutFormationDTO. /// /// /// public StatutFormationDTO GetStatutFormationDTO(StatutFormation statutFormation) { if (statutFormation == null) return null; StatutFormationDTO statutFormationDTO = new StatutFormationDTO() { Id = statutFormation.IdStatutFormation, Libelle = statutFormation.Libelle }; return statutFormationDTO; } /// /// Transformer un objet TypeFormationDTO en objet TypeFormation. /// /// /// public TypeFormation GetTypeFormation(TypeFormationDTO typeFormationDTO) { if (typeFormationDTO == null) return null; TypeFormation typeFormation = new TypeFormation() { IdTypeFormation = typeFormationDTO.Id.Value, Libelle = typeFormationDTO.Libelle }; return typeFormation; } /// /// Transformer un objet TypeFormation en objet TypeFormationDTO. /// /// /// public TypeFormationDTO GetTypeFormationDTO(TypeFormation typeFormation) { if (typeFormation == null) return null; TypeFormationDTO typeFormationDTO = new TypeFormationDTO() { Id = typeFormation.IdTypeFormation, Libelle = typeFormation.Libelle }; return typeFormationDTO; } /// /// Transformer un objet Note en objet AffichageNoteDTO. /// La liste de collaborateurs est utilisé pour alimenter la propriété Collaborateur (nom et prénom) de l'objet AffichageNoteDTO. /// /// /// /// public AffichageNoteDTO NoteToAffichageDTO(Note note, IEnumerable collaborateurs) { Collaborateur collaborateur = collaborateurs.FirstOrDefault(c => c.Id.Equals(note.IdCollaborateur)); string infoCollab = "Aucun collaborateur lié"; if (collaborateur != null) infoCollab = collaborateur.Nom + " " + collaborateur.Prenom; AffichageNoteDTO affichage = new AffichageNoteDTO() { Id = note.IdNote, IdCollaborateur = note.IdCollaborateur, Collaborateur = infoCollab, Titre = note.Titre, DateMiseAJour = note.DateMiseAJour }; return affichage; } /// /// Transformer un objet Note en objet DetailsNoteDTO. /// L'objet CollaborateurDTO est utilisé pour alimenter la propriété Collaborateur de l'objet DetailsNoteDTO. /// /// /// /// public DetailsNoteDTO NoteToDetailSDTO(Note note, CollaborateurDTO collaborateur) { if (collaborateur == null) throw new CollaborateurNotFoundException("Il est impossible de récupérer une note donc le collaborateur n'existe plus"); DetailsNoteDTO details = new DetailsNoteDTO() { Id = note.IdNote, DateCreation = note.DateCreation, DateMiseAjour = note.DateMiseAJour, Titre = note.Titre, Texte = note.Texte, IdAuteur = note.IdAuteur, Collaborateur = collaborateur, }; return details; } // /// Récupérer un objet OrigineDemande en fonction d'un objet OrigineDemandeFormationDTO /// /// /// public OrigineDemande GetOrigineDemandeFormation(OrigineDemandeFormationDTO origineDemandeDTO) { if (origineDemandeDTO == null) return null; OrigineDemande origineDemandeFormation = new OrigineDemande() { IdOrigineDemande = origineDemandeDTO.Id.Value, Libelle = origineDemandeDTO.Libelle }; return origineDemandeFormation; } /// /// Mettre à jour un objet DemandeFormation en fonction d'un objet DemandeFormationDTO. /// /// Les propriétés ParticipationFormation et Ep ne sont pas mise à jour. /// /// /// public DemandeFormation SetDemandeFormationWithoutParticipationFormationAndEp(DemandeFormation demandeFormation, DemandeFormationDTO demandeFormationDTO) { if (demandeFormation == null || demandeFormationDTO == null) return null; demandeFormation.Libelle = demandeFormationDTO.Libelle; demandeFormation.Description = demandeFormationDTO.Description; demandeFormation.DemandeRH = demandeFormationDTO.DemandeRH.Value; demandeFormation.DateDemande = demandeFormationDTO.DateDemande.Value; demandeFormation.Etat = demandeFormationDTO.EtatDemande; demandeFormation.CommentaireRefus = demandeFormationDTO.CommentaireRefus; demandeFormation.DateDerniereReponse = demandeFormationDTO.DateDerniereReponse; demandeFormation.OrigineDemande = GetOrigineDemandeFormation(demandeFormationDTO.Origine); return demandeFormation; } /// /// Mettre à jour un objet Formation en fonction d'un objet FormationDTO. /// /// /// /// public Formation SetFormation(Formation formation, FormationDTO formationDTO) { if (formation == null || formationDTO == null) return null; if (formationDTO.Id.HasValue) formation.IdFormation = formationDTO.Id.Value; formation.Intitule = formationDTO.Intitule; formation.IdAgence = formationDTO.IdAgence.Value; formation.DateDebut = formationDTO.DateDebut.Value; formation.DateFin = formationDTO.DateFin.Value; formation.Heure = Convert.ToInt32(formationDTO.Heure.Value); formation.Jour = Convert.ToInt32(formationDTO.Jour.Value); formation.Organisme = formationDTO.Organisme; formation.EstCertifiee = formationDTO.EstCertifiee.Value; //formation.EstRealisee = formationDTO.EstRealisee.Value; formation.Origine = GetOrigineFormation(formationDTO.Origine); formation.Statut = GetStatutFormation(formationDTO.Statut); formation.ModeFormation = GetModeFormation(formationDTO.Mode); formation.TypeFormation = GetTypeFormation(formationDTO.Type); return formation; } /// /// Récupérer un objet Formation en fonction d'un objet FormationDetailsDTO. /// /// /// public Formation GetFormation(FormationDetailsDTO formationDetailsDTO) { if (formationDetailsDTO == null) return null; Formation formation = new Formation { Intitule = formationDetailsDTO.Intitule, DateDebut = formationDetailsDTO.DateDebut.Value, DateFin = formationDetailsDTO.DateFin.Value, Organisme = formationDetailsDTO.Organisme, EstCertifiee = formationDetailsDTO.EstCertifiee.Value, //EstRealisee = formationDTO.EstRealisee.Value, Origine = GetOrigineFormation(formationDetailsDTO.Origine), Statut = GetStatutFormation(formationDetailsDTO.Statut), }; if (formationDetailsDTO.Id.HasValue) formation.IdFormation = formationDetailsDTO.Id.Value; return formation; } /// /// Mettre à jour la réponse d'un objet Engagement en fonction d'un objet EngagementDTO. /// /// /// /// public Engagement SetReponseEngagement(Engagement engagement, EngagementDTO engagementDTO) { if (engagement == null || engagementDTO == null) return null; engagement.EtatEngagement = engagementDTO.EtatEngagement; switch (engagement.EtatEngagement) { case EtatEngagement.NonRealisable: engagement.RaisonNonRealisable = engagementDTO.RaisonNonRealisable; break; case EtatEngagement.DateLimitePassee: engagement.RaisonNonRealisable = "La date limite pour respecter l'engagement est passée."; break; default: engagement.RaisonNonRealisable = null; break; } return engagement; } } }