Get() public method

Gets the specified Rock.Model.EntityType by the object type. If a match is not found, it can optionally create a new Rock.Model.EntityType for the object.
public Get ( Type type, bool createIfNotFound, PersonAlias personAlias ) : EntityType
type System.Type The to search for.
createIfNotFound bool A value that indicates if a new should be created if a match is not found. This value /// will be true if a new should be created if there is not a match; otherwise false/
personAlias PersonAlias A representing the alias of the who is searching for and possibly creating a new EntityType. This value can be /// null if the logged in person is not known (i.e. an anonymous user).
return EntityType
Ejemplo n.º 1
0
        /// <summary>
        /// Handles the SaveClick event of the mdEdit control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
        protected void mdEdit_SaveClick( object sender, EventArgs e )
        {
            var rockContext = new RockContext();
            EntityTypeService entityTypeService = new EntityTypeService( rockContext );
            EntityType entityType = entityTypeService.Get( int.Parse( hfEntityTypeId.Value ) );

            if ( entityType == null )
            {
                entityType = new EntityType();
                entityType.IsEntity = true;
                entityType.IsSecured = true;
                entityTypeService.Add( entityType );
            }

            entityType.Name = tbName.Text;
            entityType.FriendlyName = tbFriendlyName.Text;
            entityType.IsCommon = cbCommon.Checked;

            rockContext.SaveChanges();

            EntityTypeCache.Flush( entityType.Id );

            hfEntityTypeId.Value = string.Empty;

            HideDialog();

            BindGrid();
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Shows the edit.
        /// </summary>
        /// <param name="entityTypeId">The entity type id.</param>
        protected void ShowEdit( int entityTypeId )
        {
            EntityTypeService entityTypeService = new EntityTypeService( new RockContext() );
            EntityType entityType = entityTypeService.Get( entityTypeId );

            if ( entityType != null )
            {
                mdEdit.Title = ActionTitle.Edit( EntityType.FriendlyTypeName );
                hfEntityTypeId.Value = entityType.Id.ToString();
                tbName.Text = entityType.Name;
                tbName.Enabled = false; // !entityType.IsEntity;
                tbFriendlyName.Text = entityType.FriendlyName;
                cbCommon.Checked = entityType.IsCommon;
            }
            else
            {
                mdEdit.Title = ActionTitle.Add( EntityType.FriendlyTypeName );
                hfEntityTypeId.Value = 0.ToString();
                tbName.Text = string.Empty;
                tbName.Enabled = true;
                tbFriendlyName.Text = string.Empty;
                cbCommon.Checked = false;
            }

            ShowDialog( "Edit" );
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Shows the edit.
        /// </summary>
        /// <param name="entityTypeId">The entity type id.</param>
        protected void ShowEdit( int entityTypeId )
        {
            pnlList.Visible = false;
            pnlDetails.Visible = true;

            EntityTypeService entityTypeService = new EntityTypeService();
            EntityType entityType = entityTypeService.Get( entityTypeId );

            // set edit history marker
            this.AddHistory( "edit", "", "Edit Entity Type" );

            if ( entityType != null )
            {
                lActionTitle.Text = ActionTitle.Edit( EntityType.FriendlyTypeName ).FormatAsHtmlTitle();
                hfEntityTypeId.Value = entityType.Id.ToString();
                tbName.Text = entityType.Name;
                tbFriendlyName.Text = entityType.FriendlyName;
                cbCommon.Checked = entityType.IsCommon;
            }
            else
            {
                lActionTitle.Text = ActionTitle.Add( EntityType.FriendlyTypeName ).FormatAsHtmlTitle();
                hfEntityTypeId.Value = 0.ToString();
                tbName.Text = string.Empty;
                tbFriendlyName.Text = string.Empty;
                cbCommon.Checked = false;
            }

            tbName.Enabled = !entityType.IsEntity;

        }
Ejemplo n.º 4
0
        /// <summary>
        /// Handles the Click event of the btnSave control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
        protected void btnSave_Click( object sender, EventArgs e )
        {
            using ( new UnitOfWorkScope() )
            {
                BinaryFileType binaryFileType;

                BinaryFileTypeService binaryFileTypeService = new BinaryFileTypeService();
                AttributeService attributeService = new AttributeService();
                AttributeQualifierService attributeQualifierService = new AttributeQualifierService();
                CategoryService categoryService = new CategoryService();

                int binaryFileTypeId = int.Parse( hfBinaryFileTypeId.Value );

                if ( binaryFileTypeId == 0 )
                {
                    binaryFileType = new BinaryFileType();
                    binaryFileTypeService.Add( binaryFileType, CurrentPersonId );
                }
                else
                {
                    binaryFileType = binaryFileTypeService.Get( binaryFileTypeId );
                }

                binaryFileType.Name = tbName.Text;
                binaryFileType.Description = tbDescription.Text;
                binaryFileType.IconCssClass = tbIconCssClass.Text;
                binaryFileType.AllowCaching = cbAllowCaching.Checked;

                if ( !string.IsNullOrWhiteSpace( cpStorageType.SelectedValue ) )
                {
                    var entityTypeService = new EntityTypeService();
                    var storageEntityType = entityTypeService.Get( new Guid( cpStorageType.SelectedValue ) );

                    if ( storageEntityType != null )
                    {
                        binaryFileType.StorageEntityTypeId = storageEntityType.Id;
                    }
                }

                binaryFileType.LoadAttributes();
                Rock.Attribute.Helper.GetEditValues( phAttributes, binaryFileType );

                if ( !binaryFileType.IsValid )
                {
                    // Controls will render the error messages                    
                    return;
                }

                RockTransactionScope.WrapTransaction( () =>
                    {
                        binaryFileTypeService.Save( binaryFileType, CurrentPersonId );

                        // get it back to make sure we have a good Id for it for the Attributes
                        binaryFileType = binaryFileTypeService.Get( binaryFileType.Guid );

                        /* Take care of Binary File Attributes */
                        var entityTypeId = Rock.Web.Cache.EntityTypeCache.Read( typeof( BinaryFile ) ).Id;

                        // delete BinaryFileAttributes that are no longer configured in the UI
                        var attributes = attributeService.Get( entityTypeId, "BinaryFileTypeId", binaryFileType.Id.ToString() );
                        var selectedAttributeGuids = BinaryFileAttributesState.Select( a => a.Guid );
                        foreach ( var attr in attributes.Where( a => !selectedAttributeGuids.Contains( a.Guid ) ) )
                        {
                            Rock.Web.Cache.AttributeCache.Flush( attr.Id );
                            attributeService.Delete( attr, CurrentPersonId );
                            attributeService.Save( attr, CurrentPersonId );
                        }

                        // add/update the BinaryFileAttributes that are assigned in the UI
                        foreach ( var attributeState in BinaryFileAttributesState )
                        {
                            Rock.Attribute.Helper.SaveAttributeEdits( attributeState, attributeService, attributeQualifierService, categoryService,
                                entityTypeId, "BinaryFileTypeId", binaryFileType.Id.ToString(), CurrentPersonId );
                        }

                        // SaveAttributeValues for the BinaryFileType
                        Rock.Attribute.Helper.SaveAttributeValues( binaryFileType, CurrentPersonId );

                    } );
            }

            NavigateToParentPage();
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Reads the specified GUID.
        /// </summary>
        /// <param name="guid">The GUID.</param>
        /// <param name="rockContext">The rock context.</param>
        /// <returns></returns>
        public static EntityTypeCache Read( Guid guid, RockContext rockContext = null )
        {
            ObjectCache cache = MemoryCache.Default;
            object cacheObj = cache[guid.ToString()];

            EntityTypeCache entityType = null;
            if ( cacheObj != null )
            {
                entityType = Read( (int)cacheObj );
            }

            if ( entityType == null )
            {
                rockContext = rockContext ?? new RockContext();
                var entityTypeService = new EntityTypeService( rockContext );
                var entityTypeModel = entityTypeService.Get( guid );
                if ( entityTypeModel != null )
                {
                    entityType = new EntityTypeCache( entityTypeModel );

                    var cachePolicy = new CacheItemPolicy();
                    cache.Set( EntityTypeCache.CacheKey( entityType.Id ), entityType, cachePolicy );
                    cache.Set( entityType.Guid.ToString(), entityType.Id, cachePolicy );
                }
            }

            return entityType;
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Handles the Click event of the btnSave control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
        protected void btnSave_Click( object sender, EventArgs e )
        {
            EntityTypeService entityTypeService = new EntityTypeService();
            EntityType entityType = entityTypeService.Get( int.Parse( hfEntityTypeId.Value ) );

            entityType.FriendlyName = tbFriendlyName.Text;
            entityType.IsCommon = cbCommon.Checked;

            entityTypeService.Save( entityType, CurrentPersonId );

            BindGrid();

            pnlDetails.Visible = false;
            pnlList.Visible = true;

            hfEntityTypeId.Value = string.Empty;
        }
Ejemplo n.º 7
0
        /// <summary>
        /// Reads the specified name.
        /// </summary>
        /// <param name="name">The name.</param>
        /// <param name="createNew">if set to <c>true</c> [create new].</param>
        /// <param name="rockContext">The rock context.</param>
        /// <returns></returns>
        public static EntityTypeCache Read( string name, bool createNew, RockContext rockContext = null )
        {
            int? entityTypeId = null;

            lock ( obj )
            {
                if ( entityTypes.ContainsKey( name ) )
                {
                    entityTypeId = entityTypes[name];
                }
            }

            if ( entityTypeId.HasValue )
            {
                return Read( entityTypeId.Value );
            }

            var entityTypeService = new EntityTypeService( rockContext ?? new RockContext() );
            var entityTypeModel = entityTypeService.Get( name, createNew );
            if ( entityTypeModel != null )
            {
                return Read( entityTypeModel );
            }
            else
            {
                return null;
            }
        }
Ejemplo n.º 8
0
        /// <summary>
        /// Reads the specified type.
        /// </summary>
        /// <param name="type">The type.</param>
        /// <returns></returns>
        public static EntityTypeCache Read( Type type )
        {
            int? entityTypeId = null;

            lock ( obj )
            {
                if ( entityTypes.ContainsKey( type.FullName ) )
                {
                    entityTypeId = entityTypes[type.FullName];
                }
            }

            if ( entityTypeId.HasValue )
            {
                return Read( entityTypeId.Value );
            }

            var entityTypeService = new EntityTypeService();
            var entityTypeModel = entityTypeService.Get( type, true, null );
            return Read( entityTypeModel );
        }
        /// <summary>
        /// Handles the SaveClick event of the MdEditEntityType control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
        /// <exception cref="System.NotImplementedException"></exception>
        private void MdEditEntityType_SaveClick( object sender, EventArgs e )
        {
            using(RockContext rockContext = new RockContext() )
            {
                EntityTypeService entityTypeService = new EntityTypeService( rockContext );
                var entityType = entityTypeService.Get( hfIdValue.ValueAsInt() );

                entityType.IsIndexingEnabled = cbEnabledIndexing.Checked;

                rockContext.SaveChanges();

                if ( cbEnabledIndexing.Checked )
                {
                    IndexContainer.CreateIndex( entityType.IndexModelType );
                }
                else
                {
                    IndexContainer.DeleteIndex( entityType.IndexModelType );
                }

                // flush item from cache
                EntityTypeCache.Flush( entityType.Id );
            }

            mdEditEntityType.Hide();
            LoadEntities();
        }
Ejemplo n.º 10
0
        /// <summary>
        /// Reads the specified type.
        /// </summary>
        /// <param name="type">The type.</param>
        /// <param name="createIfNotFound">if set to <c>true</c> [create if not found].</param>
        /// <param name="rockContext">The rock context.</param>
        /// <returns></returns>
        public static EntityTypeCache Read( Type type, bool createIfNotFound = true, RockContext rockContext = null )
        {
            int? entityTypeId = null;

            if ( type.Namespace == "System.Data.Entity.DynamicProxies" )
            {
                type = type.BaseType;
            }

            lock ( obj )
            {
                if ( entityTypes.ContainsKey( type.FullName ) )
                {
                    entityTypeId = entityTypes[type.FullName];
                }
            }

            if ( entityTypeId.HasValue )
            {
                return Read( entityTypeId.Value );
            }

            var entityTypeService = new EntityTypeService( rockContext ?? new RockContext() );
            var entityTypeModel = entityTypeService.Get( type, createIfNotFound, null );
            return Read( entityTypeModel );
        }
        /// <summary>
        /// Handles the Click event of the gRefresh control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="RowEventArgs"/> instance containing the event data.</param>
        protected void gRefresh_Click( object sender, RowEventArgs e )
        {
            RockContext rockContext = new RockContext();
            EntityTypeService entityTypeService = new EntityTypeService( rockContext );
            var entityType = entityTypeService.Get( e.RowKeyId );

            if ( entityType != null )
            {
                IndexContainer.DeleteIndex( entityType.IndexModelType );
                IndexContainer.CreateIndex( entityType.IndexModelType );

                maMessages.Show( string.Format( "The index for {0} has been re-created.", entityType.FriendlyName ), ModalAlertType.Information );
            }
        }
Ejemplo n.º 12
0
        /// <summary>
        /// Reads the specified GUID.
        /// </summary>
        /// <param name="guid">The GUID.</param>
        /// <returns></returns>
        public static EntityTypeCache Read( Guid guid )
        {
            ObjectCache cache = MemoryCache.Default;
            object cacheObj = cache[guid.ToString()];

            if ( cacheObj != null )
            {
                return Read( (int)cacheObj );
            }
            else
            {
                var entityTypeService = new EntityTypeService();
                var entityTypeModel = entityTypeService.Get( guid );
                if ( entityTypeModel != null )
                {
                    var entityType = new EntityTypeCache( entityTypeModel );

                    var cachePolicy = new CacheItemPolicy();
                    cache.Set( EntityTypeCache.CacheKey( entityType.Id ), entityType, cachePolicy );
                    cache.Set( entityType.Guid.ToString(), entityType.Id, cachePolicy );

                    return entityType;
                }
                else
                {
                    return null;
                }
            }
        }
Ejemplo n.º 13
0
        /// <summary>
        /// Returns EntityType object from cache.  If entityBlockType does not already exist in cache, it
        /// will be read and added to cache
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static EntityTypeCache Read( int id )
        {
            string cacheKey = EntityTypeCache.CacheKey( id );

            ObjectCache cache = MemoryCache.Default;
            EntityTypeCache entityType = cache[cacheKey] as EntityTypeCache;

            if ( entityType != null )
            {
                return entityType;
            }
            else
            {
                var entityTypeService = new EntityTypeService();
                var entityTypeModel = entityTypeService.Get( id );
                if ( entityTypeModel != null )
                {
                    entityType = new EntityTypeCache( entityTypeModel );

                    var cachePolicy = new CacheItemPolicy();
                    cache.Set( cacheKey, entityType, cachePolicy );
                    cache.Set( entityType.Guid.ToString(), entityType.Id, cachePolicy );

                    return entityType;
                }
                else
                {
                    return null;
                }
            }
        }
Ejemplo n.º 14
0
        /// <summary>
        /// Handles the Click event of the btnSave control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
        protected void btnSave_Click( object sender, EventArgs e )
        {
            BinaryFileType binaryFileType;

            var rockContext = new RockContext();
            BinaryFileTypeService binaryFileTypeService = new BinaryFileTypeService( rockContext );
            AttributeService attributeService = new AttributeService( rockContext );
            AttributeQualifierService attributeQualifierService = new AttributeQualifierService( rockContext );
            CategoryService categoryService = new CategoryService( rockContext );

            int binaryFileTypeId = int.Parse( hfBinaryFileTypeId.Value );

            if ( binaryFileTypeId == 0 )
            {
                binaryFileType = new BinaryFileType();
                binaryFileTypeService.Add( binaryFileType );
            }
            else
            {
                binaryFileType = binaryFileTypeService.Get( binaryFileTypeId );
            }

            binaryFileType.Name = tbName.Text;
            binaryFileType.Description = tbDescription.Text;
            binaryFileType.IconCssClass = tbIconCssClass.Text;
            binaryFileType.AllowCaching = cbAllowCaching.Checked;
            binaryFileType.RequiresViewSecurity = cbRequiresViewSecurity.Checked;
            binaryFileType.MaxWidth = nbMaxWidth.Text.AsInteger();
            binaryFileType.MaxHeight = nbMaxHeight.Text.AsInteger();
            binaryFileType.PreferredFormat = ddlPreferredFormat.SelectedValueAsEnum<Format>();
            binaryFileType.PreferredResolution = ddlPreferredResolution.SelectedValueAsEnum<Resolution>();
            binaryFileType.PreferredColorDepth = ddlPreferredColorDepth.SelectedValueAsEnum<ColorDepth>();
            binaryFileType.PreferredRequired = cbPreferredRequired.Checked;

            if ( !string.IsNullOrWhiteSpace( cpStorageType.SelectedValue ) )
            {
                var entityTypeService = new EntityTypeService( rockContext );
                var storageEntityType = entityTypeService.Get( new Guid( cpStorageType.SelectedValue ) );

                if ( storageEntityType != null )
                {
                    binaryFileType.StorageEntityTypeId = storageEntityType.Id;
                }
            }

            binaryFileType.LoadAttributes( rockContext );
            Rock.Attribute.Helper.GetEditValues( phAttributes, binaryFileType );

            if ( !binaryFileType.IsValid )
            {
                // Controls will render the error messages
                return;
            }

            rockContext.WrapTransaction( () =>
            {
                rockContext.SaveChanges();

                // get it back to make sure we have a good Id for it for the Attributes
                binaryFileType = binaryFileTypeService.Get( binaryFileType.Guid );

                /* Take care of Binary File Attributes */
                var entityTypeId = Rock.Web.Cache.EntityTypeCache.Read( typeof( BinaryFile ) ).Id;

                // delete BinaryFileAttributes that are no longer configured in the UI
                var attributes = attributeService.Get( entityTypeId, "BinaryFileTypeId", binaryFileType.Id.ToString() );
                var selectedAttributeGuids = BinaryFileAttributesState.Select( a => a.Guid );
                foreach ( var attr in attributes.Where( a => !selectedAttributeGuids.Contains( a.Guid ) ) )
                {
                    Rock.Web.Cache.AttributeCache.Flush( attr.Id );
                    attributeService.Delete( attr );
                }
                rockContext.SaveChanges();

                // add/update the BinaryFileAttributes that are assigned in the UI
                foreach ( var attributeState in BinaryFileAttributesState )
                {
                    Rock.Attribute.Helper.SaveAttributeEdits( attributeState, entityTypeId, "BinaryFileTypeId", binaryFileType.Id.ToString(), rockContext );
                }

                // SaveAttributeValues for the BinaryFileType
                binaryFileType.SaveAttributeValues( rockContext );

            } );

            AttributeCache.FlushEntityAttributes();

            NavigateToParentPage();
        }
Ejemplo n.º 15
0
        /// <summary>
        /// Returns EntityType object from cache.  If entityBlockType does not already exist in cache, it
        /// will be read and added to cache
        /// </summary>
        /// <param name="id">The identifier.</param>
        /// <param name="rockContext">The rock context.</param>
        /// <returns></returns>
        public static EntityTypeCache Read( int id, RockContext rockContext = null )
        {
            string cacheKey = EntityTypeCache.CacheKey( id );
            ObjectCache cache = MemoryCache.Default;
            EntityTypeCache entityType = cache[cacheKey] as EntityTypeCache;

            if ( entityType == null )
            {
                rockContext = rockContext ?? new RockContext();
                var entityTypeService = new EntityTypeService( rockContext );
                var entityTypeModel = entityTypeService.Get( id );
                if ( entityTypeModel != null )
                {
                    entityType = new EntityTypeCache( entityTypeModel );

                    var cachePolicy = new CacheItemPolicy();
                    cache.Set( cacheKey, entityType, cachePolicy );
                    cache.Set( entityType.Guid.ToString(), entityType.Id, cachePolicy );
                }
            }

            return entityType;
        }
Ejemplo n.º 16
0
        /// <summary>
        /// Job that will run quick SQL queries on a schedule.
        /// 
        /// Called by the <see cref="IScheduler" /> when a
        /// <see cref="ITrigger" /> fires that is associated with
        /// the <see cref="IJob" />.
        /// </summary>
        public virtual void Execute( IJobExecutionContext context )
        {
            JobDataMap dataMap = context.JobDetail.JobDataMap;
            string selectedEntitiesSetting = dataMap.GetString( "Entities" );

            RockContext rockContext = new RockContext();

            var selectedEntityTypes = EntityTypeCache.All().Where(e => e.IsIndexingSupported && e.IsIndexingEnabled);

            string results = string.Empty;

            if ( selectedEntitiesSetting.IsNotNullOrWhitespace() )
            {
                var selectedEntityIds = selectedEntitiesSetting.Split( ',' ).Select( int.Parse ).ToList();
                selectedEntityTypes = selectedEntityTypes.Where( e => selectedEntityIds.Contains( e.Id ) );
            }

            foreach(var entityTypeCache in selectedEntityTypes )
            {
                EntityTypeService entityTypeService = new EntityTypeService( rockContext );
                var entityType = entityTypeService.Get( entityTypeCache.Id );

                IndexContainer.DeleteIndex( entityType.IndexModelType );
                IndexContainer.CreateIndex( entityType.IndexModelType );

                Type type = entityTypeCache.GetEntityType();

                if ( type != null )
                {
                    object classInstance = Activator.CreateInstance( type, null );
                    MethodInfo bulkItemsMethod = type.GetMethod( "BulkIndexDocuments" );

                    if ( classInstance != null && bulkItemsMethod != null )
                    {
                        var timer = System.Diagnostics.Stopwatch.StartNew();
                        bulkItemsMethod.Invoke( classInstance, null );
                        timer.Stop();
                        results += $"{entityType.FriendlyName}: {timer.ElapsedMilliseconds/1000}s,";
                    }
                }
            }

            context.Result = "Indexing results: " + results.Trim( ',' );
        }
Ejemplo n.º 17
0
        /// <summary>
        /// Handles the Click event of the lbSave control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
        protected void lbSave_Click( object sender, EventArgs e )
        {
            var rockContext = new RockContext();
            var userLoginService = new UserLoginService( rockContext );

            var userLogin = userLoginService.Queryable().Where( a => a.ApiKey == tbKey.Text ).FirstOrDefault();
            if ( userLogin != null && userLogin.PersonId != int.Parse( hfRestUserId.Value ) )
            {
                // this key already exists in the database. Show the error and get out of here.
                nbWarningMessage.Text = "This API Key already exists. Please enter a different one, or generate one by clicking the 'Generate Key' button below. ";
                nbWarningMessage.Visible = true;
                return;
            }

            rockContext.WrapTransaction( () =>
            {
                var personService = new PersonService( rockContext );
                var changes = new List<string>();
                var restUser = new Person();
                if ( int.Parse( hfRestUserId.Value ) != 0 )
                {
                    restUser = personService.Get( int.Parse( hfRestUserId.Value ) );
                }
                else
                {
                    personService.Add( restUser );
                }

                // the rest user name gets saved as the last name on a person
                History.EvaluateChange( changes, "Last Name", restUser.LastName, tbName.Text );
                restUser.LastName = tbName.Text;
                restUser.RecordTypeValueId = DefinedValueCache.Read( Rock.SystemGuid.DefinedValue.PERSON_RECORD_TYPE_RESTUSER.AsGuid() ).Id;
                if ( cbActive.Checked )
                {
                    restUser.RecordStatusValueId = DefinedValueCache.Read( Rock.SystemGuid.DefinedValue.PERSON_RECORD_STATUS_ACTIVE.AsGuid() ).Id;
                }
                else
                {
                    restUser.RecordStatusValueId = DefinedValueCache.Read( Rock.SystemGuid.DefinedValue.PERSON_RECORD_STATUS_INACTIVE.AsGuid() ).Id;
                }

                if ( restUser.IsValid )
                {
                    if ( rockContext.SaveChanges() > 0 )
                    {
                        if ( changes.Any() )
                        {
                            HistoryService.SaveChanges(
                                rockContext,
                                typeof( Person ),
                                Rock.SystemGuid.Category.HISTORY_PERSON_DEMOGRAPHIC_CHANGES.AsGuid(),
                                restUser.Id,
                                changes );
                        }
                    }
                }

                // the description gets saved as a system note for the person
                var entityTypeService = new EntityTypeService( rockContext );
                var entityType = entityTypeService.Get( "Rock.Model.Person" );
                var noteTypeService = new NoteTypeService( rockContext );
                var noteType = noteTypeService.Get( entityType.Id, "Timeline" );
                var noteService = new NoteService( rockContext );
                var note = noteService.Get( noteType.Id, restUser.Id ).FirstOrDefault();
                if ( note == null )
                {
                    note = new Note();
                    noteService.Add( note );
                }

                note.NoteTypeId = noteType.Id;
                note.EntityId = restUser.Id;
                note.Text = tbDescription.Text;
                rockContext.SaveChanges();

                // the key gets saved in the api key field of a user login (which you have to create if needed)
                entityType = entityTypeService.Get( "Rock.Security.Authentication.Database" );
                userLogin = userLoginService.GetByPersonId( restUser.Id ).FirstOrDefault();
                if ( userLogin == null )
                {
                    userLogin = new UserLogin();
                    userLoginService.Add( userLogin );
                }

                if ( string.IsNullOrWhiteSpace( userLogin.UserName ) )
                {
                    userLogin.UserName = Guid.NewGuid().ToString();
                }

                userLogin.ApiKey = tbKey.Text;
                userLogin.PersonId = restUser.Id;
                userLogin.EntityTypeId = entityType.Id;
                rockContext.SaveChanges();
            } );
            NavigateToParentPage();
        }