From 3b87a7f46a1b2bc01fd63e110dbc77ca97a77d6e Mon Sep 17 00:00:00 2001 From: Daniel Hazelbaker Date: Mon, 25 Sep 2017 16:07:18 -0700 Subject: [PATCH] Added support for detecting EntityTypes that do not exist. Fixes #6. --- Controls/ShareWorkflow.ascx.cs | 56 ++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) diff --git a/Controls/ShareWorkflow.ascx.cs b/Controls/ShareWorkflow.ascx.cs index 180f58f..e54584c 100644 --- a/Controls/ShareWorkflow.ascx.cs +++ b/Controls/ShareWorkflow.ascx.cs @@ -443,6 +443,16 @@ static public bool Import( DataContainer container, bool newGuids, RockContext r messages = new List(); var helper = new Helper( rockContext ); + // + // Ensure we know about all referenced entity types. + // + var missingTypes = container.MissingEntityTypes(); + if ( missingTypes.Any() ) + { + messages.Add( string.Format( "The following EntityTypes are unknown and indicate you may be missing a plug-in: ", string.Join( "
  • ", missingTypes ) ) ); + return false; + } + using ( var transaction = rockContext.Database.BeginTransaction() ) { try @@ -1773,6 +1783,52 @@ public DataContainer() RootEntities = new List(); } + /// + /// Check for any missing entity types that would be encountered during an import + /// operation. + /// + /// A list of strings that identify the missing entity type class names. + public List MissingEntityTypes() + { + // + // Ensure we know about all referenced entity types. + // + var missingTypes = new List(); + + // + // Get all the explicit EntityTypes for entities that are to be imported. + // + var entityTypeStrings = Entities.Select( e => e.EntityType ).ToList(); + + // + // Check for GUID and EntityType references. + // + var references = Entities.SelectMany( e => e.References ); + + entityTypeStrings.AddRange( references + .Where( r => r.Type == ReferenceType.Guid ) + .Select( r => r.EntityType ) ); + + entityTypeStrings.AddRange( references + .Where( r => r.Type == ReferenceType.EntityType ) + .Select( r => ( string ) r.Data ) ); + + // + // Just check the unique ones. + // + entityTypeStrings = entityTypeStrings.Distinct().ToList(); + + foreach ( var entityType in entityTypeStrings ) + { + if ( EntityTypeCache.Read( entityType, false, null ) == null ) + { + missingTypes.Add( entityType ); + } + } + + return missingTypes; + } + #endregion }