Ejemplo n.º 1
0
        /// <summary>
        /// Saves the specified user entity
        /// </summary>
        public UserEntity SaveUserEntity(UserEntity userEntity)
        {
            var persistence = ApplicationContext.Current.GetService <IDataPersistenceService <UserEntity> >();

            if (persistence == null)
            {
                throw new InvalidOperationException("Persistence service not found");
            }
            var breService = ApplicationContext.Current.GetService <IBusinessRulesService <UserEntity> >();

            UserEntity retVal = userEntity.Clean() as UserEntity;

            try
            {
                if (!userEntity.Key.HasValue)
                {
                    throw new KeyNotFoundException();
                }

                var old = persistence.Get(userEntity.Key.Value)?.Clone();
                if (old == null)
                {
                    throw new KeyNotFoundException(userEntity.Key.Value.ToString());
                }

                userEntity = breService?.BeforeUpdate(userEntity) ?? userEntity;
                retVal     = persistence.Update(userEntity);
                breService?.AfterUpdate(userEntity);

                var diff = ApplicationContext.Current.GetService <IPatchService>().Diff(old, retVal);

                this.DataUpdated?.Invoke(this, new AuditDataEventArgs(retVal));
                SynchronizationQueue.Outbound.Enqueue(diff, Synchronization.Model.DataOperationType.Update);
            }
            catch (KeyNotFoundException)
            {
                userEntity = breService?.AfterInsert(userEntity) ?? userEntity;
                retVal     = persistence.Insert(userEntity);
                breService?.AfterInsert(userEntity);
                this.DataCreated?.Invoke(this, new AuditDataEventArgs(retVal));

                SynchronizationQueue.Outbound.Enqueue(retVal, Synchronization.Model.DataOperationType.Insert);
            }

            // We should update that user as well
            if (userEntity.SecurityUserKey.HasValue)
            {
                var user = this.GetUser(userEntity.SecurityUserKey.Value);
                if (userEntity.Telecoms.Count > 0)
                {
                    var cellPhone = userEntity.Telecoms.FirstOrDefault(o => o.AddressUseKey == TelecomAddressUseKeys.MobileContact);
                    var email     = userEntity.Telecoms.FirstOrDefault(o => o.Value?.Contains("@") == true);
                    if (cellPhone != null)
                    {
                        user.PhoneNumber = cellPhone.Value;
                    }
                    if (email != null)
                    {
                        user.Email = email.Value;
                    }

                    this.SecurityAttributesChanged?.Invoke(this, new SecurityAuditDataEventArgs(user, $"email={email}", $"telephone={cellPhone}"));
                }
                this.SaveUser(user);

                retVal.SecurityUser = user;
            }

            return(retVal);
        }