protected override void PersistUpdatedItem(IMedia entity) { //Updates Modified date ((Models.Media)entity).UpdatingEntity(); //Look up parent to get and set the correct Path and update SortOrder if ParentId has changed if (((ICanBeDirty)entity).IsPropertyDirty("ParentId")) { var parent = Database.First <NodeDto>("WHERE id = @ParentId", new { ParentId = entity.ParentId }); entity.Path = string.Concat(parent.Path, ",", entity.Id); entity.Level = parent.Level + 1; var maxSortOrder = Database.ExecuteScalar <int>( "SELECT coalesce(max(sortOrder),0) FROM umbracoNode WHERE parentid = @ParentId AND nodeObjectType = @NodeObjectType", new { ParentId = entity.ParentId, NodeObjectType = NodeObjectTypeId }); entity.SortOrder = maxSortOrder + 1; } var factory = new MediaFactory(NodeObjectTypeId, entity.Id); //Look up Content entry to get Primary for updating the DTO var contentDto = Database.SingleOrDefault <ContentDto>("WHERE nodeId = @Id", new { Id = entity.Id }); factory.SetPrimaryKey(contentDto.PrimaryKey); var dto = factory.BuildDto(entity); //Updates the (base) node data - umbracoNode var nodeDto = dto.ContentDto.NodeDto; var o = Database.Update(nodeDto); //Only update this DTO if the contentType has actually changed if (contentDto.ContentTypeId != entity.ContentTypeId) { //Create the Content specific data - cmsContent var newContentDto = dto.ContentDto; Database.Update(newContentDto); } //Updates the current version - cmsContentVersion //Assumes a Version guid exists and Version date (modified date) has been set/updated Database.Update(dto); //Create the PropertyData for this version - cmsPropertyData var propertyFactory = new PropertyFactory(entity.ContentType, entity.Version, entity.Id); var propertyDataDtos = propertyFactory.BuildDto(entity.Properties); var keyDictionary = new Dictionary <int, int>(); //Add Properties foreach (var propertyDataDto in propertyDataDtos) { if (propertyDataDto.Id > 0) { Database.Update(propertyDataDto); } else { int primaryKey = Convert.ToInt32(Database.Insert(propertyDataDto)); keyDictionary.Add(propertyDataDto.PropertyTypeId, primaryKey); } } //Update Properties with its newly set Id if (keyDictionary.Any()) { foreach (var property in entity.Properties) { property.Id = keyDictionary[property.PropertyTypeId]; } } ((ICanBeDirty)entity).ResetDirtyProperties(); }
protected override void PersistUpdatedItem(IMedia entity) { //Updates Modified date ((Models.Media)entity).UpdatingEntity(); //Ensure unique name on the same level entity.Name = EnsureUniqueNodeName(entity.ParentId, entity.Name, entity.Id); //Ensure that strings don't contain characters that are invalid in XML entity.SanitizeEntityPropertiesForXmlStorage(); //Look up parent to get and set the correct Path and update SortOrder if ParentId has changed if (entity.IsPropertyDirty("ParentId")) { var parent = Database.First <NodeDto>("WHERE id = @ParentId", new { ParentId = entity.ParentId }); entity.Path = string.Concat(parent.Path, ",", entity.Id); entity.Level = parent.Level + 1; var maxSortOrder = Database.ExecuteScalar <int>( "SELECT coalesce(max(sortOrder),0) FROM umbracoNode WHERE parentid = @ParentId AND nodeObjectType = @NodeObjectType", new { ParentId = entity.ParentId, NodeObjectType = NodeObjectTypeId }); entity.SortOrder = maxSortOrder + 1; } var factory = new MediaFactory(NodeObjectTypeId, entity.Id); //Look up Content entry to get Primary for updating the DTO var contentDto = Database.SingleOrDefault <ContentDto>("WHERE nodeId = @Id", new { Id = entity.Id }); factory.SetPrimaryKey(contentDto.PrimaryKey); var dto = factory.BuildDto(entity); //Updates the (base) node data - umbracoNode var nodeDto = dto.ContentDto.NodeDto; var o = Database.Update(nodeDto); //Only update this DTO if the contentType has actually changed if (contentDto.ContentTypeId != entity.ContentTypeId) { //Create the Content specific data - cmsContent var newContentDto = dto.ContentDto; Database.Update(newContentDto); } //In order to update the ContentVersion we need to retrieve its primary key id var contentVerDto = Database.SingleOrDefault <ContentVersionDto>("WHERE VersionId = @Version", new { Version = entity.Version }); dto.Id = contentVerDto.Id; //Updates the current version - cmsContentVersion //Assumes a Version guid exists and Version date (modified date) has been set/updated Database.Update(dto); //Create the PropertyData for this version - cmsPropertyData var propertyFactory = new PropertyFactory(entity.ContentType.CompositionPropertyTypes.ToArray(), entity.Version, entity.Id); var propertyDataDtos = propertyFactory.BuildDto(entity.Properties); var keyDictionary = new Dictionary <int, int>(); //Add Properties foreach (var propertyDataDto in propertyDataDtos) { if (propertyDataDto.Id > 0) { Database.Update(propertyDataDto); } else { int primaryKey = Convert.ToInt32(Database.Insert(propertyDataDto)); keyDictionary.Add(propertyDataDto.PropertyTypeId, primaryKey); } } //Update Properties with its newly set Id if (keyDictionary.Any()) { foreach (var property in entity.Properties) { property.Id = keyDictionary[property.PropertyTypeId]; } } UpdatePropertyTags(entity, _tagRepository); entity.ResetDirtyProperties(); }
protected override void PersistNewItem(IMedia entity) { ((Models.Media)entity).AddingEntity(); var factory = new MediaFactory(NodeObjectTypeId, entity.Id); var dto = factory.BuildDto(entity); //NOTE Should the logic below have some kind of fallback for empty parent ids ? //Logic for setting Path, Level and SortOrder var parent = Database.First <NodeDto>("WHERE id = @ParentId", new { ParentId = entity.ParentId }); int level = parent.Level + 1; int sortOrder = Database.ExecuteScalar <int>("SELECT COUNT(*) FROM umbracoNode WHERE parentID = @ParentId AND nodeObjectType = @NodeObjectType", new { ParentId = entity.ParentId, NodeObjectType = NodeObjectTypeId }); //Create the (base) node data - umbracoNode var nodeDto = dto.ContentDto.NodeDto; nodeDto.Path = parent.Path; nodeDto.Level = short.Parse(level.ToString(CultureInfo.InvariantCulture)); nodeDto.SortOrder = sortOrder; var o = Database.IsNew(nodeDto) ? Convert.ToInt32(Database.Insert(nodeDto)) : Database.Update(nodeDto); //Update with new correct path nodeDto.Path = string.Concat(parent.Path, ",", nodeDto.NodeId); Database.Update(nodeDto); //Update entity with correct values entity.Id = nodeDto.NodeId; //Set Id on entity to ensure an Id is set entity.Path = nodeDto.Path; entity.SortOrder = sortOrder; entity.Level = level; //Create the Content specific data - cmsContent var contentDto = dto.ContentDto; contentDto.NodeId = nodeDto.NodeId; Database.Insert(contentDto); //Create the first version - cmsContentVersion //Assumes a new Version guid and Version date (modified date) has been set dto.NodeId = nodeDto.NodeId; Database.Insert(dto); //Create the PropertyData for this version - cmsPropertyData var propertyFactory = new PropertyFactory(entity.ContentType, entity.Version, entity.Id); var propertyDataDtos = propertyFactory.BuildDto(entity.Properties); var keyDictionary = new Dictionary <int, int>(); //Add Properties foreach (var propertyDataDto in propertyDataDtos) { var primaryKey = Convert.ToInt32(Database.Insert(propertyDataDto)); keyDictionary.Add(propertyDataDto.PropertyTypeId, primaryKey); } //Update Properties with its newly set Id foreach (var property in entity.Properties) { property.Id = keyDictionary[property.PropertyTypeId]; } ((ICanBeDirty)entity).ResetDirtyProperties(); }
protected override void PersistNewItem(IMedia entity) { ((Models.Media)entity).AddingEntity(); //Ensure unique name on the same level entity.Name = EnsureUniqueNodeName(entity.ParentId, entity.Name); //Ensure that strings don't contain characters that are invalid in XML entity.SanitizeEntityPropertiesForXmlStorage(); var factory = new MediaFactory(NodeObjectTypeId, entity.Id); var dto = factory.BuildDto(entity); //NOTE Should the logic below have some kind of fallback for empty parent ids ? //Logic for setting Path, Level and SortOrder var parent = Database.First <NodeDto>("WHERE id = @ParentId", new { ParentId = entity.ParentId }); var level = parent.Level + 1; var maxSortOrder = Database.ExecuteScalar <int>( "SELECT coalesce(max(sortOrder),-1) FROM umbracoNode WHERE parentid = @ParentId AND nodeObjectType = @NodeObjectType", new { /*ParentId =*/ entity.ParentId, NodeObjectType = NodeObjectTypeId }); var sortOrder = maxSortOrder + 1; //Create the (base) node data - umbracoNode var nodeDto = dto.ContentDto.NodeDto; nodeDto.Path = parent.Path; nodeDto.Level = short.Parse(level.ToString(CultureInfo.InvariantCulture)); nodeDto.SortOrder = sortOrder; var o = Database.IsNew(nodeDto) ? Convert.ToInt32(Database.Insert(nodeDto)) : Database.Update(nodeDto); //Update with new correct path nodeDto.Path = string.Concat(parent.Path, ",", nodeDto.NodeId); Database.Update(nodeDto); //Update entity with correct values entity.Id = nodeDto.NodeId; //Set Id on entity to ensure an Id is set entity.Path = nodeDto.Path; entity.SortOrder = sortOrder; entity.Level = level; //Create the Content specific data - cmsContent var contentDto = dto.ContentDto; contentDto.NodeId = nodeDto.NodeId; Database.Insert(contentDto); //Create the first version - cmsContentVersion //Assumes a new Version guid and Version date (modified date) has been set dto.NodeId = nodeDto.NodeId; Database.Insert(dto); //Create the PropertyData for this version - cmsPropertyData var propertyFactory = new PropertyFactory(entity.ContentType.CompositionPropertyTypes.ToArray(), entity.Version, entity.Id); var propertyDataDtos = propertyFactory.BuildDto(entity.Properties); var keyDictionary = new Dictionary <int, int>(); //Add Properties foreach (var propertyDataDto in propertyDataDtos) { var primaryKey = Convert.ToInt32(Database.Insert(propertyDataDto)); keyDictionary.Add(propertyDataDto.PropertyTypeId, primaryKey); } //Update Properties with its newly set Id foreach (var property in entity.Properties) { property.Id = keyDictionary[property.PropertyTypeId]; } UpdatePropertyTags(entity, _tagRepository); entity.ResetDirtyProperties(); }