Ejemplo n.º 1
0
        public Competition Persist(ClimbingContext2 context, LogicTransaction ltr)
        {
            if (context == null)
            {
                throw new ArgumentNullException("context");
            }

            context.BeginPhysicalTransaction();
            try
            {
                var _ltr = ltr ?? context.BeginLtr();

                Competition entity = null;
                if (!string.IsNullOrEmpty(this.Iid))
                {
                    entity = context.Competitions.FirstOrDefault(c => c.Iid == this.Iid);
                    if (entity == null)
                    {
                        throw new InvalidOperationException("Invalid competition");
                    }
                    if (entity.GetNotNullableRigths(context.CurrentUserID, DbAccessCore.RightsActionEnum.Edit, context) < DbAccessCore.RightsEnum.Allow)
                    {
                        throw new InvalidOperationException("Not allowed");
                    }

                    _ltr.AddUpdatedObjectBefore(entity, context);
                }
                else
                {
                    var region = context.Teams.FirstOrDefault(t => t.Iid == this.OrganizerId);
                    if (region == null)
                    {
                        throw new InvalidOperationException("Invalid region");
                    }
                    if (region.ParentTeam == null)
                    {
                        if (!context.CurrentUserIsAdmin)
                        {
                            throw new InvalidOperationException("Not allowed");
                        }
                    }
                    else if (region.ParentTeam.GetNotNullableRigths(context.CurrentUserID, DbAccessCore.RightsActionEnum.Edit, context) < DbAccessCore.RightsEnum.Allow)
                    {
                        throw new InvalidOperationException("Not allowed");
                    }

                    entity = context.Competitions.Add(new Competition(context));
                }

                entity.Name        = this.FullName;
                entity.ShortName   = this.ShortName;
                entity.StartDate   = this.StartDate;
                entity.OrganizerId = this.OrganizerId;
                entity.Organizer   = context.Teams.FirstOrDefault(t => t.Iid == this.OrganizerId);

                if (string.IsNullOrEmpty(this.Iid))
                {
                    _ltr.AddCreatedObject(entity, context);
                }
                else
                {
                    _ltr.AddUpatedObjectAfter(entity, context);
                }

                context.SaveChanges();

                entity.SetDateTimeValue(CompetitionParamId.ApplicationsEndDate, this.ApplicationEndDate, context, _ltr);
                entity.SetDateTimeValue(CompetitionParamId.CorrectionsEndDate, this.AppCorrEndDate, context, _ltr);
                entity.SetDateTimeValue(CompetitionParamId.EndDate, this.EndDate, context, _ltr);
                entity.SetStringParameterValue(CompetitionParamId.UpdatePassword, this.Password, context, _ltr);
                entity.SetStyles(this.Styles, context, _ltr);

                if (ltr == null)
                {
                    _ltr.Commit(context);
                }

                context.SaveChanges();
                context.CommitPhysicalTransaction();
                return(entity);
            }
            catch
            {
                context.RollbackPhysicalTransaction();
                throw;
            }
        }