protected override void PersistUpdatedItem(IMember entity) { //Updates Modified date ((Member)entity).UpdatingEntity(); //Ensure that strings don't contain characters that are invalid in XML entity.SanitizeEntityPropertiesForXmlStorage(); var dirtyEntity = (ICanBeDirty)entity; //Look up parent to get and set the correct Path and update SortOrder if ParentId has changed if (dirtyEntity.IsPropertyDirty("ParentId")) { var parent = Database.First <NodeDto>("WHERE id = @ParentId", new { ParentId = ((IUmbracoEntity)entity).ParentId }); ((IUmbracoEntity)entity).Path = string.Concat(parent.Path, ",", entity.Id); ((IUmbracoEntity)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 = ((IUmbracoEntity)entity).ParentId, NodeObjectType = NodeObjectTypeId }); ((IUmbracoEntity)entity).SortOrder = maxSortOrder + 1; } var factory = new MemberFactory(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 != ((Member)entity).ContentTypeId) { //Create the Content specific data - cmsContent var newContentDto = dto.ContentVersionDto.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.ContentVersionDto.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.ContentVersionDto); //Updates the cmsMember entry if it has changed //NOTE: these cols are the REAL column names in the db var changedCols = new List <string>(); if (dirtyEntity.IsPropertyDirty("Email")) { changedCols.Add("Email"); } if (dirtyEntity.IsPropertyDirty("Username")) { changedCols.Add("LoginName"); } // DO NOT update the password if it has not changed or if it is null or empty if (dirtyEntity.IsPropertyDirty("RawPasswordValue") && entity.RawPasswordValue.IsNullOrWhiteSpace() == false) { changedCols.Add("Password"); } //only update the changed cols if (changedCols.Count > 0) { Database.Update(dto, changedCols); } //TODO ContentType for the Member entity //Create the PropertyData for this version - cmsPropertyData var propertyFactory = new PropertyFactory(entity.ContentType.CompositionPropertyTypes.ToArray(), entity.Version, entity.Id); var keyDictionary = new Dictionary <int, int>(); //Add Properties // - don't try to save the property if it doesn't exist (or doesn't have an ID) on the content type // - this can occur if the member type doesn't contain the built-in properties that the // - member object contains. var propsToPersist = entity.Properties.Where(x => x.PropertyType.HasIdentity).ToArray(); var propertyDataDtos = propertyFactory.BuildDto(propsToPersist); 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 ((Member)entity).Properties) { property.Id = keyDictionary[property.PropertyTypeId]; } } UpdatePropertyTags(entity, _tagRepository); dirtyEntity.ResetDirtyProperties(); }
protected override void PersistUpdatedItem(IMember entity) { //Updates Modified date ((Member)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 = ((IUmbracoEntity)entity).ParentId }); ((IUmbracoEntity)entity).Path = string.Concat(parent.Path, ",", entity.Id); ((IUmbracoEntity)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 = ((IUmbracoEntity)entity).ParentId, NodeObjectType = NodeObjectTypeId }); ((IUmbracoEntity)entity).SortOrder = maxSortOrder + 1; } var factory = new MemberFactory(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 != ((Member)entity).ContentTypeId) { //Create the Content specific data - cmsContent var newContentDto = dto.ContentVersionDto.ContentDto; Database.Update(newContentDto); } //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 }); dto.ContentVersionDto.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.ContentVersionDto); //Updates the cmsMember entry Database.Update(dto); //TODO ContentType for the Member entity //Create the PropertyData for this version - cmsPropertyData var propertyFactory = new PropertyFactory(entity.ContentType, entity.Version, entity.Id); var keyDictionary = new Dictionary <int, int>(); //Add Properties // - don't try to save the property if it doesn't exist (or doesn't have an ID) on the content type // - this can occur if the member type doesn't contain the built-in properties that the // - member object contains. var existingProperties = entity.Properties .Where(property => entity.ContentType.PropertyTypes.Any(x => x.Alias == property.Alias && x.HasIdentity)) .ToList(); var propertyDataDtos = propertyFactory.BuildDto(existingProperties); 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 ((Member)entity).Properties) { property.Id = keyDictionary[property.PropertyTypeId]; } } ((ICanBeDirty)entity).ResetDirtyProperties(); }