コード例 #1
0
    protected override void PersistNewItem(IDataType entity)
    {
        entity.AddingEntity();

        // ensure a datatype has a unique name before creating it
        entity.Name = EnsureUniqueNodeName(entity.Name) !;

        // TODO: should the below be removed?
        // Cannot add a duplicate data type
        Sql <ISqlContext> existsSql = Sql()
                                      .SelectCount()
                                      .From <DataTypeDto>()
                                      .InnerJoin <NodeDto>().On <DataTypeDto, NodeDto>((left, right) => left.NodeId == right.NodeId)
                                      .Where <NodeDto>(x => x.Text == entity.Name);
        var exists = Database.ExecuteScalar <int>(existsSql) > 0;

        if (exists)
        {
            throw new DuplicateNameException("A data type with the name " + entity.Name + " already exists");
        }

        DataTypeDto dto = DataTypeFactory.BuildDto(entity, _serializer);

        // Logic for setting Path, Level and SortOrder
        NodeDto?parent    = Database.First <NodeDto>("WHERE id = @ParentId", new { entity.ParentId });
        var     level     = parent.Level + 1;
        var     sortOrder =
            Database.ExecuteScalar <int>(
                "SELECT COUNT(*) FROM umbracoNode WHERE parentID = @ParentId AND nodeObjectType = @NodeObjectType",
                new { entity.ParentId, NodeObjectType = NodeObjectTypeId });

        // Create the (base) node data - umbracoNode
        NodeDto 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);

        // 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;

        dto.NodeId = nodeDto.NodeId;
        Database.Insert(dto);

        entity.ResetDirtyProperties();
    }
コード例 #2
0
    protected override void PersistUpdatedItem(IDataType entity)
    {
        entity.Name = EnsureUniqueNodeName(entity.Name, entity.Id) !;

        // Cannot change to a duplicate alias
        Sql <ISqlContext> existsSql = Sql()
                                      .SelectCount()
                                      .From <DataTypeDto>()
                                      .InnerJoin <NodeDto>().On <DataTypeDto, NodeDto>((left, right) => left.NodeId == right.NodeId)
                                      .Where <NodeDto>(x => x.Text == entity.Name && x.NodeId != entity.Id);
        var exists = Database.ExecuteScalar <int>(existsSql) > 0;

        if (exists)
        {
            throw new DuplicateNameException("A data type with the name " + entity.Name + " already exists");
        }

        // Updates Modified date
        entity.UpdatingEntity();

        // Look up parent to get and set the correct Path if ParentId has changed
        if (entity.IsPropertyDirty("ParentId"))
        {
            NodeDto?parent = Database.First <NodeDto>("WHERE id = @ParentId", new { 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 { entity.ParentId, NodeObjectType = NodeObjectTypeId });
            entity.SortOrder = maxSortOrder + 1;
        }

        DataTypeDto dto = DataTypeFactory.BuildDto(entity, _serializer);

        // Updates the (base) node data - umbracoNode
        NodeDto nodeDto = dto.NodeDto;

        Database.Update(nodeDto);
        Database.Update(dto);

        entity.ResetDirtyProperties();
    }