/// <summary>
        /// Asigna y/o crea un rol
        /// </summary>
        /// <param name="pHealthInstitutionId"></param>
        /// <param name="pProfesionalId_ToAssign"></param>
        /// <param name="pUserId"></param>
        /// <param name="roleName"></param>
        public static void Profesional_UsersInRoles_Assign(Guid pHealthInstitutionId, Int32 pProfesionalId_ToAssign, Guid pUserId, string roleName)
        {
            Back.BE.HealthInstitution_Profesional wHealthInstitution_Profesional = new Back.BE.HealthInstitution_Profesional();
            Back.BE.HealtInstitute_UsersInRoles   wHealtInstitute_UsersInRoles   = new BE.HealtInstitute_UsersInRoles();
            using (Health.Back.BE.HealthEntities dc = new Health.Back.BE.HealthEntities(Common.CnnString_Entities))
            {
                //Si no existe la relacion del profesional con la inst.. se genera
                if (!dc.HealthInstitution_Profesional.Any(p =>
                                                          p.HealthInstitutionId.Equals(pHealthInstitutionId) &&
                                                          p.Profesional.IdProfesional.Equals(pProfesionalId_ToAssign)))
                {
                    wHealthInstitution_Profesional.HealthInstitutionId = pHealthInstitutionId;
                    wHealthInstitution_Profesional.ProfesionalId       = pProfesionalId_ToAssign;
                    wHealthInstitution_Profesional.UserId      = pUserId;
                    wHealthInstitution_Profesional.IsLockedOut = false;
                    wHealthInstitution_Profesional.ActiveFlag  = true;

                    if (roleName.Equals("inst_owner") || roleName.Equals("inst_admin"))
                    {
                        wHealthInstitution_Profesional.IsAdmin = roleName.Equals("inst_admin");
                        wHealthInstitution_Profesional.IsOwner = roleName.Equals("inst_owner");
                    }
                    dc.HealthInstitution_Profesional.AddObject(wHealthInstitution_Profesional);
                }


                wHealtInstitute_UsersInRoles.UserId = pUserId;
                wHealtInstitute_UsersInRoles.HealthInstitutionId = pHealthInstitutionId;
                wHealtInstitute_UsersInRoles.RoleName            = roleName;

                dc.HealtInstitute_UsersInRoles.AddObject(wHealtInstitute_UsersInRoles);

                dc.SaveChanges();
            }
        }
        /// <summary>
        /// Elimina rol a Institución
        /// Si es inst_owner o inst_admin establece .IsAdmin o.IsOwbner a falce respectivamente y posteriormente elimina el rol
        /// Si NO : l SOLO Elimina el rol
        /// </summary>
        /// <param name="pHealthInstitutionId"></param>
        /// <param name="userId"></param>
        /// <param name="rolName"></param>
        public static void Profesional_RemoveRol(Guid pHealthInstitutionId, Int32 pProfesionalId, Guid userId, string rolName)
        {
            Back.BE.HealthInstitution_Profesional wHealthInstitution_Profesional = null;
            Back.BE.HealtInstitute_UsersInRoles   wHealtInstitute_UsersInRoles   = null;
            using (Health.Back.BE.HealthEntities dc = new Health.Back.BE.HealthEntities(Common.CnnString_Entities))
            {
                wHealthInstitution_Profesional = dc.HealthInstitution_Profesional.Where(p =>
                                                                                        p.HealthInstitutionId.Equals(pHealthInstitutionId) &&
                                                                                        p.Profesional.IdProfesional.Equals(pProfesionalId)).FirstOrDefault();

                //Si solo queda un unico rol el cual sera removido el usuario queda automaticamente en estado no vigente
                if (dc.HealtInstitute_UsersInRoles.Count(p => p.UserId.Equals(userId) && p.HealthInstitutionId.Equals(pHealthInstitutionId)) == 1)
                {
                    wHealthInstitution_Profesional.ActiveFlag = false;
                    wHealthInstitution_Profesional.IsOwner    = false;
                    wHealthInstitution_Profesional.IsAdmin    = false;
                }
                else
                {
                    if (rolName.Equals("inst_owner") || rolName.Equals("inst_admin"))
                    {
                        if (rolName.Equals("inst_owner"))
                        {
                            wHealthInstitution_Profesional.IsOwner = false;
                        }

                        if (rolName.Equals("inst_admin"))
                        {
                            wHealthInstitution_Profesional.IsAdmin = false;
                        }
                    }
                }

                //busca la entidad HealtInstitute_UsersInRoles
                wHealtInstitute_UsersInRoles = dc.HealtInstitute_UsersInRoles.Where(p =>
                                                                                    p.UserId.Equals(userId) &&
                                                                                    p.HealthInstitutionId.Equals(pHealthInstitutionId) &&
                                                                                    p.RoleName.Equals(rolName)).FirstOrDefault();

                //Elimina la entidad HealtInstitute_UsersInRoles
                if (wHealtInstitute_UsersInRoles != null)
                {
                    dc.HealtInstitute_UsersInRoles.DeleteObject(wHealtInstitute_UsersInRoles);
                }

                dc.SaveChanges();
            }
        }
        /// <summary>
        /// Crea HealtInstitute_UsersInRoles = inst_admin
        /// Crea HealthInstitution_Profesional
        /// </summary>
        /// <param name="pHealthInstitutionId"></param>
        /// <param name="pProfesionalId_ToAssign"></param>
        /// <param name="pUserId"></param>
        public static void Profesional_Associate_As_AdminOwner(Guid pHealthInstitutionId, Int32 pProfesionalId_ToAssign, Guid pUserId, string rolname)
        {
            Back.BE.HealthInstitution_Profesional wHealthInstitution_Profesional = new Back.BE.HealthInstitution_Profesional();
            Back.BE.HealtInstitute_UsersInRoles   wHealtInstitute_UsersInRoles   = new BE.HealtInstitute_UsersInRoles();
            using (Health.Back.BE.HealthEntities dc = new Health.Back.BE.HealthEntities(Common.CnnString_Entities))
            {
                wHealtInstitute_UsersInRoles.UserId = pUserId;
                wHealtInstitute_UsersInRoles.HealthInstitutionId = pHealthInstitutionId;
                wHealtInstitute_UsersInRoles.RoleName            = rolname;

                wHealthInstitution_Profesional.IsAdmin             = true;
                wHealthInstitution_Profesional.HealthInstitutionId = pHealthInstitutionId;
                wHealthInstitution_Profesional.ProfesionalId       = pProfesionalId_ToAssign;
                wHealthInstitution_Profesional.UserId = pUserId;

                dc.HealthInstitution_Profesional.AddObject(wHealthInstitution_Profesional);
                dc.HealtInstitute_UsersInRoles.AddObject(wHealtInstitute_UsersInRoles);

                dc.SaveChanges();
            }
        }
        /// <summary>
        /// Crea una Institución
        /// Establece como profesional owner de  pHealthInstitution a pProfesionalId/pUserId
        /// </summary>
        /// <param name="pHealthInstitution">Institución</param>
        /// <param name="pProfesionalId">Profesional</param>
        /// <param name="pUserId"></param>
        public static void Create_From_Owner(Health.Back.BE.HealthInstitution pHealthInstitution, Int32 pProfesionalId, Guid pUserId)
        {
            Back.BE.HealthInstitution_Profesional wHealthInstitution_Profesional = new Back.BE.HealthInstitution_Profesional();
            Back.BE.HealtInstitute_UsersInRoles wHealtInstitute_UsersInRoles = new BE.HealtInstitute_UsersInRoles();

            using (Health.Back.BE.HealthEntities dc = new Health.Back.BE.HealthEntities(Common.CnnString_Entities))
            {
                //Busco la institución padre si existe
                var parentHI = dc.HealthInstitutions.Where(p => !p.HealthInstitutionIdParent.HasValue
                    && p.HealthInstitution_Profesional.Any(prof => prof.ProfesionalId.Equals(pProfesionalId))).FirstOrDefault();
                if (parentHI != null)
                    pHealthInstitution.HealthInstitutionIdParent = parentHI.HealthInstitutionId;

                pHealthInstitution.HealthInstitutionId = Guid.NewGuid();
                pHealthInstitution.CreatedDate = System.DateTime.Now;
                dc.HealthInstitutions.AddObject(pHealthInstitution);

                wHealthInstitution_Profesional.HealthInstitutionId = pHealthInstitution.HealthInstitutionId;
                wHealthInstitution_Profesional.ProfesionalId = pProfesionalId;
                wHealthInstitution_Profesional.ActiveFlag = true;
                wHealthInstitution_Profesional.UserId = pUserId;
                wHealthInstitution_Profesional.IsLockedOut = false;
                wHealthInstitution_Profesional.IsOwner = true;
                dc.HealthInstitution_Profesional.AddObject(wHealthInstitution_Profesional);


                wHealtInstitute_UsersInRoles.UserId = pUserId;
                wHealtInstitute_UsersInRoles.HealthInstitutionId = pHealthInstitution.HealthInstitutionId;
                wHealtInstitute_UsersInRoles.RoleName = "inst_owner";

                dc.HealtInstitute_UsersInRoles.AddObject(wHealtInstitute_UsersInRoles);

                dc.SaveChanges();

            }


        }
        /// <summary>
        /// Crea una Institución
        /// Establece como profesional owner de  pHealthInstitution a pProfesionalId/pUserId
        /// </summary>
        /// <param name="pHealthInstitution">Institución</param>
        /// <param name="pProfesionalId">Profecional</param>
        /// <param name="pUserId"></param>
        public static void Create_From_Owner(Health.Back.BE.HealthInstitution pHealthInstitution, Int32 pProfesionalId, Guid pUserId)
        {
            Back.BE.HealthInstitution_Profesional wHealthInstitution_Profesional = new Back.BE.HealthInstitution_Profesional();
            Back.BE.HealtInstitute_UsersInRoles   wHealtInstitute_UsersInRoles   = new BE.HealtInstitute_UsersInRoles();

            using (Health.Back.BE.HealthEntities dc = new Health.Back.BE.HealthEntities(Common.CnnString_Entities))
            {
                //Busco la institución padre si existe
                var parentHI = dc.HealthInstitutions.Where(p => !p.HealthInstitutionIdParent.HasValue &&
                                                           p.HealthInstitution_Profesional.Any(prof => prof.ProfesionalId.Equals(pProfesionalId))).FirstOrDefault();
                if (parentHI != null)
                {
                    pHealthInstitution.HealthInstitutionIdParent = parentHI.HealthInstitutionId;
                }

                pHealthInstitution.HealthInstitutionId = Guid.NewGuid();
                pHealthInstitution.CreatedDate         = System.DateTime.Now;
                dc.HealthInstitutions.AddObject(pHealthInstitution);

                wHealthInstitution_Profesional.HealthInstitutionId = pHealthInstitution.HealthInstitutionId;
                wHealthInstitution_Profesional.ProfesionalId       = pProfesionalId;
                wHealthInstitution_Profesional.ActiveFlag          = true;
                wHealthInstitution_Profesional.UserId      = pUserId;
                wHealthInstitution_Profesional.IsLockedOut = false;
                wHealthInstitution_Profesional.IsOwner     = true;
                dc.HealthInstitution_Profesional.AddObject(wHealthInstitution_Profesional);


                wHealtInstitute_UsersInRoles.UserId = pUserId;
                wHealtInstitute_UsersInRoles.HealthInstitutionId = pHealthInstitution.HealthInstitutionId;
                wHealtInstitute_UsersInRoles.RoleName            = "inst_owner";

                dc.HealtInstitute_UsersInRoles.AddObject(wHealtInstitute_UsersInRoles);

                dc.SaveChanges();
            }
        }
        /// <summary>
        /// Asigna y/o crea un rol
        /// </summary>
        /// <param name="pHealthInstitutionId"></param>
        /// <param name="pProfesionalId_ToAssign"></param>
        /// <param name="pUserId"></param>
        /// <param name="roleName"></param>
        public static void Profesional_UsersInRoles_Assign(Guid pHealthInstitutionId, Int32 pProfesionalId_ToAssign, Guid pUserId, string roleName)
        {
            Back.BE.HealthInstitution_Profesional wHealthInstitution_Profesional = new Back.BE.HealthInstitution_Profesional();
            Back.BE.HealtInstitute_UsersInRoles wHealtInstitute_UsersInRoles = new BE.HealtInstitute_UsersInRoles();
            using (Health.Back.BE.HealthEntities dc = new Health.Back.BE.HealthEntities(Common.CnnString_Entities))
            {
                //Si no existe la relacion del profesional con la inst.. se genera
                if (!dc.HealthInstitution_Profesional.Any(p =>
                 p.HealthInstitutionId.Equals(pHealthInstitutionId)
                 && p.Profesional.IdProfesional.Equals(pProfesionalId_ToAssign)))
                {
                    wHealthInstitution_Profesional.HealthInstitutionId = pHealthInstitutionId;
                    wHealthInstitution_Profesional.ProfesionalId = pProfesionalId_ToAssign;
                    wHealthInstitution_Profesional.UserId = pUserId;
                    wHealthInstitution_Profesional.IsLockedOut = false;
                    wHealthInstitution_Profesional.ActiveFlag = true;

                    if (roleName.Equals("inst_owner") || roleName.Equals("inst_admin"))
                    {
                        wHealthInstitution_Profesional.IsAdmin = roleName.Equals("inst_admin");
                        wHealthInstitution_Profesional.IsOwner = roleName.Equals("inst_owner");
                    }
                    dc.HealthInstitution_Profesional.AddObject(wHealthInstitution_Profesional);
                }


                wHealtInstitute_UsersInRoles.UserId = pUserId;
                wHealtInstitute_UsersInRoles.HealthInstitutionId = pHealthInstitutionId;
                wHealtInstitute_UsersInRoles.RoleName = roleName;

                dc.HealtInstitute_UsersInRoles.AddObject(wHealtInstitute_UsersInRoles);

                dc.SaveChanges();
            }
        }
        /// <summary>
        /// Crea HealtInstitute_UsersInRoles = inst_admin
        /// Crea HealthInstitution_Profesional
        /// </summary>
        /// <param name="pHealthInstitutionId"></param>
        /// <param name="pProfesionalId_ToAssign"></param>
        /// <param name="pUserId"></param>
        public static void Profesional_Associate_As_AdminOwner(Guid pHealthInstitutionId, Int32 pProfesionalId_ToAssign, Guid pUserId, string rolname)
        {
            Back.BE.HealthInstitution_Profesional wHealthInstitution_Profesional = new Back.BE.HealthInstitution_Profesional();
            Back.BE.HealtInstitute_UsersInRoles wHealtInstitute_UsersInRoles = new BE.HealtInstitute_UsersInRoles();
            using (Health.Back.BE.HealthEntities dc = new Health.Back.BE.HealthEntities(Common.CnnString_Entities))
            {

                wHealtInstitute_UsersInRoles.UserId = pUserId;
                wHealtInstitute_UsersInRoles.HealthInstitutionId = pHealthInstitutionId;
                wHealtInstitute_UsersInRoles.RoleName = rolname;

                wHealthInstitution_Profesional.IsAdmin = true;
                wHealthInstitution_Profesional.HealthInstitutionId = pHealthInstitutionId;
                wHealthInstitution_Profesional.ProfesionalId = pProfesionalId_ToAssign;
                wHealthInstitution_Profesional.UserId = pUserId;

                dc.HealthInstitution_Profesional.AddObject(wHealthInstitution_Profesional);
                dc.HealtInstitute_UsersInRoles.AddObject(wHealtInstitute_UsersInRoles);

                dc.SaveChanges();
            }

        }