Exemplo n.º 1
0
        /// <summary>
        /// Gets the current context object for a given entity type.
        /// </summary>
        /// <param name="entity">The <see cref="Rock.Web.Cache.EntityTypeCache"/> containing a reference to the entity.</param>
        /// <returns>An object that implements the <see cref="Rock.Data.IEntity"/> interface referencing the context object. </returns>
        public Rock.Data.IEntity GetCurrentContext( EntityTypeCache entity )
        {
            if ( this.ModelContext.ContainsKey( entity.Name ) )
            {
                var keyModel = this.ModelContext[entity.Name];

                if ( keyModel.Entity == null )
                {
                    if ( entity.Name.Equals( "Rock.Model.Person", StringComparison.OrdinalIgnoreCase ) )
                    {
                        if ( string.IsNullOrWhiteSpace( keyModel.Key ) )
                        {
                            keyModel.Entity = new PersonService( new RockContext() )
                                .Queryable( "MaritalStatusValue,ConnectionStatusValue,RecordStatusValue,RecordStatusReasonValue,RecordTypevalue,SuffixValue,TitleValue,GivingGroup,Photo,Aliases", true, true )
                                .Where( p => p.Id == keyModel.Id ).FirstOrDefault();
                        }
                        else
                        {
                            keyModel.Entity = new PersonService( new RockContext() ).GetByPublicKey( keyModel.Key );
                        }
                    }
                    else
                    {

                        Type modelType = entity.GetEntityType();

                        if ( modelType == null )
                        {
                            // if the Type isn't found in the Rock.dll (it might be from a Plugin), lookup which assessmbly it is in and look in there
                            string[] assemblyNameParts = entity.AssemblyName.Split( new char[] { ',' } );
                            if ( assemblyNameParts.Length > 1 )
                            {
                                modelType = Type.GetType( string.Format( "{0}, {1}", entity.Name, assemblyNameParts[1] ) );
                            }
                        }

                        if ( modelType != null )
                        {
                            // In the case of core Rock.dll Types, we'll just use Rock.Data.Service<> and Rock.Data.RockContext<>
                            // otherwise find the first (and hopefully only) Service<> and dbContext we can find in the Assembly.
                            Type serviceType = typeof( Rock.Data.Service<> );
                            Type contextType = typeof( Rock.Data.RockContext );
                            if ( modelType.Assembly != serviceType.Assembly )
                            {
                                var serviceTypeLookup = Reflection.SearchAssembly( modelType.Assembly, serviceType );
                                if ( serviceTypeLookup.Any() )
                                {
                                    serviceType = serviceTypeLookup.First().Value;
                                }

                                var contextTypeLookup = Reflection.SearchAssembly( modelType.Assembly, typeof( System.Data.Entity.DbContext ) );

                                if ( contextTypeLookup.Any() )
                                {
                                    contextType = contextTypeLookup.First().Value;
                                }
                            }

                            System.Data.Entity.DbContext dbContext = Activator.CreateInstance( contextType ) as System.Data.Entity.DbContext;

                            Type service = serviceType.MakeGenericType( new Type[] { modelType } );
                            var serviceInstance = Activator.CreateInstance( service, dbContext );

                            if ( string.IsNullOrWhiteSpace( keyModel.Key ) )
                            {
                                MethodInfo getMethod = service.GetMethod( "Get", new Type[] { typeof( int ) } );
                                keyModel.Entity = getMethod.Invoke( serviceInstance, new object[] { keyModel.Id } ) as Rock.Data.IEntity;
                            }
                            else
                            {
                                MethodInfo getMethod = service.GetMethod( "GetByPublicKey" );
                                keyModel.Entity = getMethod.Invoke( serviceInstance, new object[] { keyModel.Key } ) as Rock.Data.IEntity;
                            }
                        }

                    }

                    if ( keyModel.Entity != null && keyModel.Entity is Rock.Attribute.IHasAttributes )
                    {
                        Rock.Attribute.Helper.LoadAttributes( keyModel.Entity as Rock.Attribute.IHasAttributes );
                    }

                }

                return keyModel.Entity;
            }

            return null;
        }
Exemplo n.º 2
0
        /// <summary>
        /// Raises the <see cref="E:System.Web.UI.Control.Init" /> event.
        /// </summary>
        /// <param name="e">An <see cref="T:System.EventArgs" /> object that contains the event data.</param>
        protected override void OnInit( EventArgs e )
        {
            base.OnInit( e );

            gReport.DataKeyNames = new string[] { "Guid" };
            gReport.GridRebind += gReport_GridRebind;

            int tagId = int.MinValue;
            if ( int.TryParse( PageParameter( "tagId" ), out tagId ) && tagId > 0 )
            {
                Tag _tag = new TagService().Get( tagId );

                if ( _tag != null )
                {
                    TagId = tagId;
                    TagEntityType = EntityTypeCache.Read( _tag.EntityTypeId );

                    if ( TagEntityType != null )
                    {
                        Type modelType = TagEntityType.GetEntityType();

                        lTaggedTitle.Text = "Tagged " + modelType.Name.Pluralize();

                        if ( modelType != null )
                        {
                            Dictionary<string, BoundField> boundFields = gReport.Columns.OfType<BoundField>().ToDictionary( a => a.DataField );
                            foreach ( var column in gReport.GetPreviewColumns( modelType ) )
                            {
                                int insertPos = gReport.Columns.IndexOf( gReport.Columns.OfType<DeleteField>().First() );
                                gReport.Columns.Insert( insertPos, column );
                            }

                            if ( TagEntityType.Name == "Rock.Model.Person" )
                            {
                                gReport.PersonIdField = "Id";
                            }

                            BindGrid();
                        }
                    }
                }
            }
        }