protected override void PersistUpdatedItem(ITemplate entity) { EnsureValidAlias(entity); //store the changed alias if there is one for use with updating files later var originalAlias = entity.Alias; if (entity.IsPropertyDirty("Alias")) { //we need to check what it currently is before saving and remove that file var current = Get(entity.Id); originalAlias = current.Alias; } var template = (Template)entity; if (entity.IsPropertyDirty("MasterTemplateId")) { var parent = Get(template.MasterTemplateId.Value); if (parent != null) { entity.Path = string.Concat(parent.Path, ",", entity.Id); } else { //this means that the master template has been removed, so we need to reset the template's //path to be at the root entity.Path = string.Concat("-1,", entity.Id); } } //Get TemplateDto from db to get the Primary key of the entity var templateDto = Database.SingleOrDefault <TemplateDto>("WHERE nodeId = @Id", new { Id = entity.Id }); //Save updated entity to db template.UpdateDate = DateTime.Now; ; var dto = TemplateFactory.BuildDto(template, NodeObjectTypeId, templateDto.PrimaryKey); Database.Update(dto.NodeDto); Database.Update(dto); //re-update if this is a master template, since it could have changed! var axisDefs = GetAxisDefinitions(dto); template.IsMasterTemplate = axisDefs.Any(x => x.ParentId == dto.NodeId); //now do the file work SaveFile((Template)entity, originalAlias); entity.ResetDirtyProperties(); // ensure that from now on, content is lazy-loaded if (template.GetFileContent == null) { template.GetFileContent = file => GetFileContent((Template)file, false); } }
protected override void PersistNewItem(ITemplate entity) { using (var stream = new MemoryStream(Encoding.UTF8.GetBytes(entity.Content))) { if (entity.GetTypeOfRenderingEngine() == RenderingEngine.Mvc) { string viewName = string.Concat(entity.Alias, ".cshtml"); _viewsFileSystem.AddFile(viewName, stream, true); } else { string masterpageName = string.Concat(entity.Alias, ".master"); _masterpagesFileSystem.AddFile(masterpageName, stream, true); } } //TODO Possibly ensure unique alias here (as is done in the legacy Template class)? //Save to db var template = entity as Template; template.AddingEntity(); var factory = new TemplateFactory(NodeObjectTypeId); var dto = factory.BuildDto(template); //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 = template.ParentId }); int level = parent.Level + 1; int sortOrder = Database.ExecuteScalar <int>("SELECT COUNT(*) FROM umbracoNode WHERE parentID = @ParentId AND nodeObjectType = @NodeObjectType", new { ParentId = template.ParentId, NodeObjectType = NodeObjectTypeId }); //Create the (base) node data - umbracoNode var nodeDto = dto.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); //Insert template dto dto.NodeId = nodeDto.NodeId; Database.Insert(dto); //Update entity with correct values template.Id = nodeDto.NodeId; //Set Id on entity to ensure an Id is set template.Path = nodeDto.Path; template.SortOrder = sortOrder; template.Level = level; template.ResetDirtyProperties(); }
protected override void PersistNewItem(ITemplate entity) { EnsureValidAlias(entity); //Save to db var template = (Template)entity; template.AddingEntity(); TemplateDto dto = TemplateFactory.BuildDto(template, NodeObjectTypeId, template.Id); //Create the (base) node data - umbracoNode NodeDto nodeDto = dto.NodeDto; nodeDto.Path = "-1," + dto.NodeDto.NodeId; var o = Database.IsNew(nodeDto) ? Convert.ToInt32(Database.Insert(nodeDto)) : Database.Update(nodeDto); //Update with new correct path ITemplate?parent = Get(template.MasterTemplateId !.Value); if (parent != null) { nodeDto.Path = string.Concat(parent.Path, ",", nodeDto.NodeId); } else { nodeDto.Path = "-1," + dto.NodeDto.NodeId; } Database.Update(nodeDto); //Insert template dto dto.NodeId = nodeDto.NodeId; Database.Insert(dto); //Update entity with correct values template.Id = nodeDto.NodeId; //Set Id on entity to ensure an Id is set template.Path = nodeDto.Path; // Only save file when not in production runtime mode if (_runtimeSettings.CurrentValue.Mode != RuntimeMode.Production) { //now do the file work SaveFile(template); } template.ResetDirtyProperties(); // ensure that from now on, content is lazy-loaded if (template.GetFileContent == null) { template.GetFileContent = file => GetFileContent((Template)file, false); } }
protected override void PersistUpdatedItem(ITemplate entity) { using (var stream = new MemoryStream(Encoding.UTF8.GetBytes(entity.Content))) { if (entity.GetTypeOfRenderingEngine() == RenderingEngine.Mvc) { string viewName = string.Concat(entity.Alias, ".cshtml"); _viewsFileSystem.AddFile(viewName, stream, true); } else { string masterpageName = string.Concat(entity.Alias, ".master"); _masterpagesFileSystem.AddFile(masterpageName, stream, true); } } //Look up parent to get and set the correct Path if ParentId has changed if (((ICanBeDirty)entity).IsPropertyDirty("ParentId")) { var parent = Database.First <NodeDto>("WHERE id = @ParentId", new { ParentId = ((Template)entity).ParentId }); entity.Path = string.Concat(parent.Path, ",", entity.Id); ((Template)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 = ((Template)entity).ParentId, NodeObjectType = NodeObjectTypeId }); ((Template)entity).SortOrder = maxSortOrder + 1; } //Get TemplateDto from db to get the Primary key of the entity var templateDto = Database.SingleOrDefault <TemplateDto>("WHERE nodeId = @Id", new { Id = entity.Id }); //Save updated entity to db var template = entity as Template; template.UpdateDate = DateTime.Now; var factory = new TemplateFactory(templateDto.PrimaryKey, NodeObjectTypeId); var dto = factory.BuildDto(template); Database.Update(dto.NodeDto); Database.Update(dto); ((ICanBeDirty)entity).ResetDirtyProperties(); }
protected override void PersistUpdatedItem(ITemplate entity) { EnsureValidAlias(entity); //store the changed alias if there is one for use with updating files later var originalAlias = entity.Alias; if (entity.IsPropertyDirty("Alias")) { //we need to check what it currently is before saving and remove that file var current = Get(entity.Id); originalAlias = current.Alias; } var template = (Template)entity; if (entity.IsPropertyDirty("MasterTemplateId")) { var parent = Get(template.MasterTemplateId.Value); if (parent != null) { entity.Path = string.Concat(parent.Path, ",", entity.Id); } } //Get TemplateDto from db to get the Primary key of the entity var templateDto = Database.SingleOrDefault <TemplateDto>("WHERE nodeId = @Id", new { Id = entity.Id }); //Save updated entity to db template.UpdateDate = DateTime.Now; var factory = new TemplateFactory(templateDto.PrimaryKey, NodeObjectTypeId); var dto = factory.BuildDto(template); Database.Update(dto.NodeDto); Database.Update(dto); //re-update if this is a master template, since it could have changed! var axisDefs = GetAxisDefinitions(dto); template.IsMasterTemplate = axisDefs.Any(x => x.ParentId == dto.NodeId); //now do the file work if (DetermineTemplateRenderingEngine(entity) == RenderingEngine.Mvc) { var result = _viewHelper.UpdateViewFile(entity, originalAlias); if (result != entity.Content) { entity.Content = result; //re-persist it... though we don't really care about the templates in the db do we??!! dto.Design = result; Database.Update(dto); } } else { var result = _masterPageHelper.UpdateMasterPageFile(entity, originalAlias, this); if (result != entity.Content) { entity.Content = result; //re-persist it... though we don't really care about the templates in the db do we??!! dto.Design = result; Database.Update(dto); } } entity.ResetDirtyProperties(); // ensure that from now on, content is lazy-loaded if (template.GetFileContent == null) { template.GetFileContent = file => GetFileContent((Template)file, false); } }
protected override void PersistNewItem(ITemplate entity) { EnsureValidAlias(entity); //Save to db var template = (Template)entity; template.AddingEntity(); var factory = new TemplateFactory(NodeObjectTypeId); var dto = factory.BuildDto(template); //Create the (base) node data - umbracoNode var nodeDto = dto.NodeDto; nodeDto.Path = "-1," + dto.NodeDto.NodeId; var o = Database.IsNew(nodeDto) ? Convert.ToInt32(Database.Insert(nodeDto)) : Database.Update(nodeDto); //Update with new correct path var parent = Get(template.MasterTemplateId.Value); if (parent != null) { nodeDto.Path = string.Concat(parent.Path, ",", nodeDto.NodeId); } else { nodeDto.Path = "-1," + dto.NodeDto.NodeId; } Database.Update(nodeDto); //Insert template dto dto.NodeId = nodeDto.NodeId; Database.Insert(dto); //Update entity with correct values template.Id = nodeDto.NodeId; //Set Id on entity to ensure an Id is set template.Path = nodeDto.Path; //now do the file work if (DetermineTemplateRenderingEngine(entity) == RenderingEngine.Mvc) { var result = _viewHelper.CreateView(template, true); if (result != entity.Content) { entity.Content = result; //re-persist it... though we don't really care about the templates in the db do we??!! dto.Design = result; Database.Update(dto); } } else { var result = _masterPageHelper.CreateMasterPage(template, this, true); if (result != entity.Content) { entity.Content = result; //re-persist it... though we don't really care about the templates in the db do we??!! dto.Design = result; Database.Update(dto); } } template.ResetDirtyProperties(); // ensure that from now on, content is lazy-loaded if (template.GetFileContent == null) { template.GetFileContent = file => GetFileContent((Template)file, false); } }