protected override void PersistNewItem(IContent entity) { ((Content)entity).AddingEntity(); //Ensure unique name on the same level entity.Name = EnsureUniqueNodeName(entity.ParentId, entity.Name); var factory = new ContentFactory(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.ContentVersionDto.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; //Assign the same permissions to it as the parent node // http://issues.umbraco.org/issue/U4-2161 var parentPermissions = GetPermissionsForEntity(entity.ParentId).ToArray(); //if there are parent permissions then assign them, otherwise leave null and permissions will become the // user's default permissions. if (parentPermissions.Any()) { var userPermissions = parentPermissions.Select( permissionDto => new KeyValuePair <object, string>( permissionDto.UserId, permissionDto.Permission)); AssignEntityPermissions(entity, userPermissions); //flag the entity's permissions changed flag so we can track those changes. //Currently only used for the cache refreshers to detect if we should refresh all user permissions cache. ((Content)entity).PermissionsChanged = true; } //Create the Content specific data - cmsContent var contentDto = dto.ContentVersionDto.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 var contentVersionDto = dto.ContentVersionDto; contentVersionDto.NodeId = nodeDto.NodeId; Database.Insert(contentVersionDto); //Create the Document specific data for this version - cmsDocument //Assumes a new Version guid has been generated 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(IContent entity) { ((Content)entity).AddingEntity(); //ensure the default template is assigned if (entity.Template == null) { entity.Template = entity.ContentType.DefaultTemplate; } //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 ContentFactory(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.ContentVersionDto.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; //Assign the same permissions to it as the parent node // http://issues.umbraco.org/issue/U4-2161 var permissionsRepo = new PermissionRepository <IContent>(UnitOfWork, _cacheHelper, SqlSyntax); var parentPermissions = permissionsRepo.GetPermissionsForEntity(entity.ParentId).ToArray(); //if there are parent permissions then assign them, otherwise leave null and permissions will become the // user's default permissions. if (parentPermissions.Any()) { var userPermissions = ( from perm in parentPermissions from p in perm.AssignedPermissions select new EntityPermissionSet.UserPermission(perm.UserId, p)).ToList(); permissionsRepo.ReplaceEntityPermissions(new EntityPermissionSet(entity.Id, userPermissions)); //flag the entity's permissions changed flag so we can track those changes. //Currently only used for the cache refreshers to detect if we should refresh all user permissions cache. ((Content)entity).PermissionsChanged = true; } //Create the Content specific data - cmsContent var contentDto = dto.ContentVersionDto.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 var contentVersionDto = dto.ContentVersionDto; contentVersionDto.NodeId = nodeDto.NodeId; Database.Insert(contentVersionDto); //Create the Document specific data for this version - cmsDocument //Assumes a new Version guid has been generated 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]; } //lastly, check if we are a creating a published version , then update the tags table if (entity.Published) { UpdatePropertyTags(entity, _tagRepository); } // published => update published version infos, else leave it blank if (entity.Published) { dto.DocumentPublishedReadOnlyDto = new DocumentPublishedReadOnlyDto { VersionId = dto.VersionId, Newest = true, NodeId = dto.NodeId, Published = true }; ((Content)entity).PublishedVersionGuid = dto.VersionId; } entity.ResetDirtyProperties(); }
protected override void PersistUpdatedItem(IContent entity) { var publishedState = ((Content)entity).PublishedState; //check if we need to create a new version bool shouldCreateNewVersion = entity.ShouldCreateNewVersion(publishedState); if (shouldCreateNewVersion) { //Updates Modified date and Version Guid ((Content)entity).UpdatingEntity(); } else { entity.UpdateDate = DateTime.Now; } //Ensure unique name on the same level entity.Name = EnsureUniqueNodeName(entity.ParentId, entity.Name, entity.Id); //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; //Question: If we move a node, should we update permissions to inherit from the new parent if the parent has permissions assigned? // if we do that, then we'd need to propogate permissions all the way downward which might not be ideal for many people. // Gonna just leave it as is for now, and not re-propogate permissions. } var factory = new ContentFactory(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.ContentVersionDto.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.ContentVersionDto.ContentDto; Database.Update(newContentDto); } //If Published state has changed then previous versions should have their publish state reset. //If state has been changed to unpublished the previous versions publish state should also be reset. //if (((ICanBeDirty)entity).IsPropertyDirty("Published") && (entity.Published || publishedState == PublishedState.Unpublished)) if (entity.ShouldClearPublishedFlagForPreviousVersions(publishedState, shouldCreateNewVersion)) { var publishedDocs = Database.Fetch <DocumentDto>("WHERE nodeId = @Id AND published = @IsPublished", new { Id = entity.Id, IsPublished = true }); foreach (var doc in publishedDocs) { var docDto = doc; docDto.Published = false; Database.Update(docDto); } } var contentVersionDto = dto.ContentVersionDto; if (shouldCreateNewVersion) { //Look up (newest) entries by id in cmsDocument table to set newest = false //NOTE: This is only relevant when a new version is created, which is why its done inside this if-statement. var documentDtos = Database.Fetch <DocumentDto>("WHERE nodeId = @Id AND newest = @IsNewest", new { Id = entity.Id, IsNewest = true }); foreach (var documentDto in documentDtos) { var docDto = documentDto; docDto.Newest = false; Database.Update(docDto); } //Create a new version - cmsContentVersion //Assumes a new Version guid and Version date (modified date) has been set Database.Insert(contentVersionDto); //Create the Document specific data for this version - cmsDocument //Assumes a new Version guid has been generated Database.Insert(dto); } else { //In order to update the ContentVersion we need to retreive its primary key id var contentVerDto = Database.SingleOrDefault <ContentVersionDto>("WHERE VersionId = @Version", new { Version = entity.Version }); contentVersionDto.Id = contentVerDto.Id; Database.Update(contentVersionDto); Database.Update(dto); } //Create the PropertyData for this version - cmsPropertyData var propertyFactory = new PropertyFactory(((Content)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 (shouldCreateNewVersion == false && 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) { if (keyDictionary.ContainsKey(property.PropertyTypeId) == false) { continue; } property.Id = keyDictionary[property.PropertyTypeId]; } } ((ICanBeDirty)entity).ResetDirtyProperties(); }
protected override void PersistNewItem(IContent entity) { ((Content)entity).AddingEntity(); var factory = new ContentFactory(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.ContentVersionDto.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.ContentVersionDto.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 var contentVersionDto = dto.ContentVersionDto; contentVersionDto.NodeId = nodeDto.NodeId; Database.Insert(contentVersionDto); //Create the Document specific data for this version - cmsDocument //Assumes a new Version guid has been generated 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(); }