From 289b9743c5fe2a1987acd09627b115227aee2bc2 Mon Sep 17 00:00:00 2001 From: Morten Christensen Date: Wed, 27 Feb 2013 10:23:48 -0100 Subject: [PATCH] Fixes regression issue with installing document types using legacy api. --- src/Umbraco.Core/Models/ContentTypeBase.cs | 5 +- .../Factories/PropertyGroupFactory.cs | 8 ++- .../Repositories/ContentTypeBaseRepository.cs | 57 +++++++++++-------- 3 files changed, 39 insertions(+), 31 deletions(-) diff --git a/src/Umbraco.Core/Models/ContentTypeBase.cs b/src/Umbraco.Core/Models/ContentTypeBase.cs index 9390aeaf8f..fe2d1a3810 100644 --- a/src/Umbraco.Core/Models/ContentTypeBase.cs +++ b/src/Umbraco.Core/Models/ContentTypeBase.cs @@ -347,6 +347,7 @@ namespace Umbraco.Core.Models if (PropertyTypeExists(propertyType.Alias) == false) { _propertyTypes.Add(propertyType); + _propertyTypes.CollectionChanged += PropertyTypesChanged; return true; } @@ -378,9 +379,5 @@ namespace Umbraco.Core.Models { _parentId = id; } - - //TODO Implement moving PropertyType between groups. - /*public bool MovePropertyTypeToGroup(string propertyTypeAlias, string groupName) - {}*/ } } \ No newline at end of file diff --git a/src/Umbraco.Core/Persistence/Factories/PropertyGroupFactory.cs b/src/Umbraco.Core/Persistence/Factories/PropertyGroupFactory.cs index 0e35cb956d..4721090614 100644 --- a/src/Umbraco.Core/Persistence/Factories/PropertyGroupFactory.cs +++ b/src/Umbraco.Core/Persistence/Factories/PropertyGroupFactory.cs @@ -44,7 +44,7 @@ namespace Umbraco.Core.Persistence.Factories var typeDtos = groupDto.PropertyTypeDtos.Where(x => x.Id > 0); foreach (var typeDto in typeDtos) { - group.PropertyTypes.Add(new PropertyType(typeDto.DataTypeDto.ControlId, + var propertyType = new PropertyType(typeDto.DataTypeDto.ControlId, typeDto.DataTypeDto.DbType.EnumParse(true)) { Alias = typeDto.Alias, @@ -56,8 +56,10 @@ namespace Umbraco.Core.Persistence.Factories Mandatory = typeDto.Mandatory, SortOrder = typeDto.SortOrder, PropertyGroupId = groupDto.Id - }); - + }; + + propertyType.ResetDirtyProperties(); + group.PropertyTypes.Add(propertyType); } group.ResetDirtyProperties(); propertyGroups.Add(group); diff --git a/src/Umbraco.Core/Persistence/Repositories/ContentTypeBaseRepository.cs b/src/Umbraco.Core/Persistence/Repositories/ContentTypeBaseRepository.cs index f2eaa4280b..5ae358c81f 100644 --- a/src/Umbraco.Core/Persistence/Repositories/ContentTypeBaseRepository.cs +++ b/src/Umbraco.Core/Persistence/Repositories/ContentTypeBaseRepository.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.Globalization; using System.Linq; +using System.Threading.Tasks; using Umbraco.Core.Models; using Umbraco.Core.Models.EntityBase; using Umbraco.Core.Models.Rdbms; @@ -216,32 +217,37 @@ namespace Umbraco.Core.Persistence.Repositories }); } - //Delete PropertyTypes by excepting entries from db with entries from collections - var dbPropertyTypes = Database.Fetch("WHERE contentTypeId = @Id", new {Id = entity.Id}); - var dbPropertyTypeAlias = dbPropertyTypes.Select(x => x.Id); - var entityPropertyTypes = entity.PropertyTypes.Where(x => x.HasIdentity).Select(x => x.Id); - var items = dbPropertyTypeAlias.Except(entityPropertyTypes); - foreach (var item in items) + if (((ICanBeDirty) entity).IsPropertyDirty("PropertyTypes") || entity.PropertyTypes.Any(x => x.IsDirty())) { - //Before a PropertyType can be deleted, all Properties based on that PropertyType should be deleted. - Database.Delete("WHERE propertytypeid = @Id", new {Id = item}); - - Database.Delete("WHERE contentTypeId = @Id AND id = @PropertyTypeId", - new {Id = entity.Id, PropertyTypeId = item}); + //Delete PropertyTypes by excepting entries from db with entries from collections + var dbPropertyTypes = Database.Fetch("WHERE contentTypeId = @Id", new {Id = entity.Id}); + var dbPropertyTypeAlias = dbPropertyTypes.Select(x => x.Id); + var entityPropertyTypes = entity.PropertyTypes.Where(x => x.HasIdentity).Select(x => x.Id); + var items = dbPropertyTypeAlias.Except(entityPropertyTypes); + foreach (var item in items) + { + //Before a PropertyType can be deleted, all Properties based on that PropertyType should be deleted. + Database.Delete("WHERE propertytypeid = @Id", new {Id = item}); + Database.Delete("WHERE contentTypeId = @Id AND id = @PropertyTypeId", + new {Id = entity.Id, PropertyTypeId = item}); + } } - //Delete Tabs/Groups by excepting entries from db with entries from collections - var dbPropertyGroups = - Database.Fetch("WHERE contenttypeNodeId = @Id", new {Id = entity.Id}) - .Select(x => new Tuple(x.Id, x.Text)); - var entityPropertyGroups = entity.PropertyGroups.Select(x => new Tuple(x.Id, x.Name)); - var tabs = dbPropertyGroups.Except(entityPropertyGroups); - foreach (var tab in tabs) + if (((ICanBeDirty) entity).IsPropertyDirty("PropertyGroups") || entity.PropertyGroups.Any(x => x.IsDirty())) { - Database.Update("SET parentGroupId = NULL WHERE parentGroupId = @TabId", - new {TabId = tab.Item1}); - Database.Delete("WHERE contenttypeNodeId = @Id AND text = @Name", - new {Id = entity.Id, Name = tab.Item2}); + //Delete Tabs/Groups by excepting entries from db with entries from collections + var dbPropertyGroups = + Database.Fetch("WHERE contenttypeNodeId = @Id", new {Id = entity.Id}) + .Select(x => new Tuple(x.Id, x.Text)); + var entityPropertyGroups = entity.PropertyGroups.Select(x => new Tuple(x.Id, x.Name)); + var tabs = dbPropertyGroups.Except(entityPropertyGroups); + foreach (var tab in tabs) + { + Database.Update("SET parentGroupId = NULL WHERE parentGroupId = @TabId", + new {TabId = tab.Item1}); + Database.Delete("WHERE contenttypeNodeId = @Id AND text = @Name", + new {Id = entity.Id, Name = tab.Item2}); + } } //Run through all groups to insert or update entries @@ -332,8 +338,11 @@ namespace Umbraco.Core.Persistence.Repositories HelpText = dto.HelpText, Mandatory = dto.Mandatory, SortOrder = dto.SortOrder - }); - + }).ToList(); + + //Reset dirty properties + Parallel.ForEach(list, currentFile => currentFile.ResetDirtyProperties()); + return new PropertyTypeCollection(list); } }