/// <summary> /// Process the queued list of entities that are waiting to be encoded. This /// encodes all entities, generates new Guids for any entities that need them, /// and then maps all references to the new Guids. /// </summary> /// <param name="guidEvaluation">A function that is called for each entity to determine if it needs a new Guid or not.</param> /// <returns>A DataContainer that is ready for JSON export.</returns> public ExportedEntitiesContainer GetExportedEntities() { var container = new ExportedEntitiesContainer(); // // Find out if we need to give new Guid values to any entities. // foreach (var queuedEntity in Entities) { queuedEntity.EncodedEntity = Export(queuedEntity); if (queuedEntity.ReferencePaths[0].Count == 0 || queuedEntity.RequiresNewGuid) { queuedEntity.EncodedEntity.GenerateNewGuid = true; } } // // Convert to a data container. // foreach (var queuedEntity in Entities) { container.Entities.Add(queuedEntity.EncodedEntity); if (queuedEntity.ReferencePaths.Count == 1 && queuedEntity.ReferencePaths[0].Count == 0) { container.RootEntities.Add(queuedEntity.EncodedEntity.Guid); } } return(container); }
/// <summary> /// Attempt to import the container of entities into the Rock database. Creates /// a transaction inside the RockContext to perform all the entity creation so /// if an error occurs everything will be left in a clean state. /// </summary> /// <param name="container">The container of all the encoded entities.</param> /// <param name="dryRun">If true then we only attempt the import, nothing is actually saved.</param> /// <param name="messages">Any messages, errors or otherwise, that should be displayed to the user.</param> /// <returns>true if the import succeeded, false if it did not.</returns> public bool Import(ExportedEntitiesContainer container, bool dryRun, out List <string> messages) { messages = new List <string>(); // // Ensure we know about all referenced entity types. // var missingTypes = container.GetMissingEntityTypes(); if (missingTypes.Any()) { messages.Add(string.Format("The following EntityTypes are unknown and indicate you may be missing a plug-in: <ul><li>{0}</li></ul>", string.Join("</li><li>", missingTypes))); return(false); } // // Generate a new Guid if we were asked to. // foreach (var encodedEntity in container.Entities) { if (encodedEntity.GenerateNewGuid) { MapNewGuid(encodedEntity.Guid); } } using (var transaction = RockContext.Database.BeginTransaction()) { try { // // Walk each encoded entity and either verify an existing entity or // create a new entity. // foreach (var encodedEntity in container.Entities) { Type entityType = FindEntityType(encodedEntity.EntityType); Guid entityGuid = FindMappedGuid(encodedEntity.Guid); var entity = GetExistingEntity(encodedEntity.EntityType, entityGuid); if (entity == null) { try { entity = CreateNewEntity(encodedEntity); } catch (Exception e) { throw new Exception(String.Format("Error importing encoded entity: {0}", encodedEntity.ToJson()), e); } messages.Add(string.Format("Created: {0}, {1}", encodedEntity.EntityType, entityGuid)); } else { messages.Add(string.Format("Found Existing: {0}, {1}", encodedEntity.EntityType, entityGuid)); } } // // Either commit the transaction or roll it back if we are doing a dry run. // if (!dryRun) { transaction.Commit(); } else { transaction.Rollback(); } return(true); } catch (Exception e) { transaction.Rollback(); for (Exception ex = e; ex != null; ex = ex.InnerException) { messages.Add(ex.Message + "\n" + ex.StackTrace); } return(false); } } }