IVersion Interface

IT Hit WebDAV for .NET

Represents single item version.

For a list of all members of this type, see IVersion Members.

[Visual Basic]
Public Interface IVersion
    Implements IDeltaVItem, IHierarchyItem
[C#]
public interface IVersion : IDeltaVItem, IHierarchyItem

Remarks

Defines the properties and methods that item version must implement. In addition to methods and properties provided by IDeltaVItem this interface also provides methods for getting version name, next version and previous version.

Usually you will implement IVersion interface for your resource version objects together with IResource interface. While IResource interface is optional for resource versions it may be useful if your DeltaV client application will request content of the resource version. In this case WriteToStream, ContentLength and ContentType members of the IResource interface will be requested by the engine. Copying, movig, updateng properties and content are not allowed for a version, your CopyTo, MoveTo, UpdateProperties and SaveFromStream implementations must return NotAllowedResponse.

Generally from your VersionName implementation you can return any string suitable for displaying to user as a version or the hierarchy item. This string must be unique among versions for this hierarchy item. Usually you will return “1”, “2”, etc or “3.1”, “3.4”, etc.

Successor and Predecessor properties of this interface returns next and previous version for the item. The VersionableItem property returns the hierarchy item (usually resource) to which this version belongs.

Example

public class Version : IVersion, IResource
{
    private Guid fVersionId;
    private Guid fItemId;
    private int fVersionNumber;

    private Engine fEngine;
    private string fNameField;
    private string fPath;
    private DateTime fCreatedField;
    private string fConnStr = ConfigurationManager.AppSettings["SqlConnectionString"];
    private static string fRepositoryPath = ConfigurationManager.AppSettings["RepositoryPath"].TrimEnd('\\') + "\\";
    private string fFilePath;
    
    public Version(Engine engine, Guid versionId, Guid itemId,
        int verNumber, string name, string path, DateTime created)
    {
        this.fVersionId = versionId;
        this.fVersionNumber = verNumber;
        this.fEngine = engine;
        this.fItemId = itemId;
        this.fNameField = name;
        this.fPath = path;
        this.fCreatedField = created;
        this.fFilePath = fRepositoryPath + this.fItemId + '\\' + verNumber;
    }
    
    #region IVersion Members

    public IVersion Successor
    {
        get
        {
            using (SqlConnection conn = new SqlConnection(fConnStr))
            {
                conn.Open();
                SqlCommand cmd = conn.CreateCommand();

                cmd.CommandText = "SELECT VersionId, VersionNumber, Name, Created"
                                  + " FROM Version"
                                  + " WHERE (ItemId = @ItemId) AND (VersionNumber ="
                                  + " (SELECT MIN(VersionNumber)"
                                  + "   FROM Version"
                                  + "   WHERE (ItemId = @ItemId)"
                                  + "     AND (VersionNumber > @VersionNumber)))";
                cmd.Parameters.Add("@ItemId", SqlDbType.UniqueIdentifier).Value = this.fItemId;
                cmd.Parameters.Add("@VersionNumber", SqlDbType.Int).Value = this.fVersionNumber;

                using (SqlDataReader reader = cmd.ExecuteReader())
                {
                    if (reader.Read())
                    {
                        Guid newVersionId = reader.GetGuid(reader.GetOrdinal("VersionId"));
                        int newVersionNumber = reader.GetInt32(reader.GetOrdinal("VersionNumber"));
                        string newName = reader.GetString(reader.GetOrdinal("Name"));
                        DateTime newItemCreated = reader.GetDateTime(reader.GetOrdinal("Created"));

                        string versionPath = CreateVersionPath(fPath.Remove(fPath.IndexOf('?')),
                                                               newVersionNumber);

                        return new Version(this.fEngine, newVersionId, this.fItemId,
                                           newVersionNumber, newName, versionPath,
                                           newItemCreated);
                    }
                }
            }
            return null;
        }
    }

    public IVersion Predecessor
    {
        get
        {
            using (SqlConnection conn = new SqlConnection(fConnStr))
            {
                conn.Open();
                SqlCommand cmd = conn.CreateCommand();

                cmd.CommandText = "SELECT VersionId, VersionNumber, Name, Created"
                                  + " FROM Version"
                                  + " WHERE (ItemId = @ItemId) AND (VersionNumber ="
                                  + " (SELECT MAX(VersionNumber)"
                                  + "   FROM Version"
                                  + "   WHERE (ItemId = @ItemId)"
                                  + "     AND (VersionNumber < @VersionNumber)))";
                cmd.Parameters.Add("@ItemId", SqlDbType.UniqueIdentifier).Value = this.fItemId;
                cmd.Parameters.Add("@VersionNumber", SqlDbType.Int).Value = this.fVersionNumber;

                using (SqlDataReader reader = cmd.ExecuteReader())
                {
                    if (reader.Read())
                    {
                        Guid newVersionId = reader.GetGuid(reader.GetOrdinal("VersionId"));
                        int newVersionNumber = reader.GetInt32(reader.GetOrdinal("VersionNumber"));
                        string newName = reader.GetString(reader.GetOrdinal("Name"));
                        DateTime newItemCreated = reader.GetDateTime(reader.GetOrdinal("Created"));

                        string versionPath = CreateVersionPath(fPath.Remove(fPath.IndexOf('?')),
                                                               newVersionNumber);

                        return new Version(this.fEngine, newVersionId, this.fItemId,
                                           newVersionNumber, newName, versionPath,
                                           newItemCreated);
                    }
                }
            }
            return null;
        }
    }

    public IVersionableItem VersionableItem
    {
        get
        {
            string itemPath = fPath.Remove(fPath.IndexOf('?'));
            return (IVersionableItem)fEngine.GetHierarchyItem(itemPath);
        }
    }

    public string VersionName
    {
        get { return this.Name + "(V" + this.fVersionNumber + ")"; }
    }

    #endregion  // IVersion Members

    #region IDeltaVItem Members

    public string Comment
    {
        get
        {
            string comment;

            using (SqlConnection conn = new SqlConnection(fConnStr))
            {
                conn.Open();
                SqlCommand cmd = conn.CreateCommand();
                cmd.CommandText = "SELECT Comment FROM Version WHERE VersionId = @VersionId";
                cmd.Parameters.Add("@VersionId", SqlDbType.UniqueIdentifier).Value = VersionId;
                comment = (string)cmd.ExecuteScalar();
            }

            return comment;
        }
        set
        {
            using (SqlConnection conn = new SqlConnection(fConnStr))
            {
                conn.Open();
                SqlCommand cmd = conn.CreateCommand();
                cmd.CommandText = "UPDATE Version SET Comment = @Comment WHERE VersionId = @VersionId";

                cmd.Parameters.Add("@VersionId", SqlDbType.UniqueIdentifier).Value = this.fVersionId;
                cmd.Parameters.Add("@Comment", SqlDbType.NVarChar).Value = value;
                cmd.ExecuteNonQuery();
            }
        }
    }

    public string CreatorDisplayName
    {
        get
        {
            string creatorDisplayName;

            using (SqlConnection conn = new SqlConnection(fConnStr))
            {
                conn.Open();
                SqlCommand cmd = conn.CreateCommand();
                cmd.CommandText = "SELECT CreatorDisplayName FROM Version WHERE VersionId = @VersionId";
                cmd.Parameters.Add("@VersionId", SqlDbType.UniqueIdentifier).Value = this.VersionId;
                creatorDisplayName = (string)cmd.ExecuteScalar();
            }

            return creatorDisplayName;
        }
        set
        {
            using (SqlConnection conn = new SqlConnection(fConnStr))
            {
                conn.Open();
                SqlCommand cmd = conn.CreateCommand();
                cmd.CommandText = "UPDATE Version SET CreatorDisplayName = @CreatorDisplayName"
                                  + " WHERE VersionId = @VersionId";

                cmd.Parameters.Add("@VersionId", SqlDbType.UniqueIdentifier).Value = this.VersionId;
                cmd.Parameters.Add("@CreatorDisplayName", SqlDbType.NVarChar).Value = value;
                cmd.ExecuteNonQuery();
            }
        }
    }

    #endregion

    #region IResource Members

    public string ContentType
    {
        get { return (this.VersionableItem as IResource).ContentType; }
    }

    public long ContentLength
    {
        get
        {
            FileInfo file = new FileInfo(this.FilePath);
            return file.Length;
        }
    }

    public WebDAVResponse WriteToStream(Stream output, long startIndex, long count)
    {
        long bufSize = 1048576; // 1Mb

        FileInfo file = new FileInfo(fFilePath);
        byte[] buffer = new byte[bufSize];

        using (FileStream fileStream
            = file.Open(FileMode.Open, FileAccess.Read, FileShare.Read))
        {
            fileStream.Seek(startIndex, SeekOrigin.Begin);
            int bytesRead;
            while ((bytesRead = fileStream.Read(buffer, 0, (int)(count > bufSize ? bufSize : count))) > 0)
            {
                try
                {
                    output.Write(buffer, 0, bytesRead);
                }
                catch (HttpListenerException ex)
                {
                    if ((ex.ErrorCode == 1229) || (ex.ErrorCode == 64))
                    {
                        // client closed connection
                        // 1. ErrorCode=1229. An operation was attempted on a nonexistent network connection.
                        // 2. ErrorCode=64. The specified network name is no longer available.
                        return new OkResponse();
                    }
                    throw;
                }
                count -= bytesRead;
            }
        }

        return new OkResponse();
    }

    public WebDAVResponse SaveFromStream(Stream content, string contentType)
    {
        return new NotAllowedResponse();
    }

    #endregion

    #region IHierarchyItem Members

    public string Name
    {
        get { return fNameField; }
    }

    public DateTime Created
    {
        get { return fCreatedField; }
    }

    public DateTime Modified
    {
        get { return DateTime.MinValue; }
    }

    public IFolder Parent
    {
        get { throw new Exception("The method or operation is not implemented."); }
    }

    public WebDAVResponse GetProperties(ref Property[] props)
    {
        using (SqlConnection conn = new SqlConnection(fConnStr))
        {
            conn.Open();

            SqlCommand cmd = conn.CreateCommand();
            cmd.CommandText = "SELECT Name, Namespace, PropVal"
                              + " FROM VersionProperty"
                              + " WHERE VersionId = @VersionId";

            cmd.Parameters.Add("@VersionId", SqlDbType.UniqueIdentifier).Value = fVersionId;

            using (SqlDataReader reader = cmd.ExecuteReader())
            {
                if (props == null) // get all properties
                {
                    List<Property> l = new List<Property>();
                    while (reader.Read())
                    {
                        Property p = new Property();
                        p.Name = reader.GetString(reader.GetOrdinal("Name"));
                        p.Namespace = reader.GetString(reader.GetOrdinal("Namespace"));
                        p.Value = reader.GetString(reader.GetOrdinal("PropVal"));
                        l.Add(p);
                    }
                    props = l.ToArray();
                }
                else // get selected properties
                {
                    Property p = new Property();
                    while (reader.Read())
                    {
                        p.Name = reader.GetString(reader.GetOrdinal("Name"));
                        p.Namespace = reader.GetString(reader.GetOrdinal("Namespace"));
                        for (int i = 0; i < props.Length; i++)
                            if (p.Name == props[i].Name && p.Namespace == props[i].Namespace)
                            {
                                p.Value = reader.GetString(reader.GetOrdinal("PropVal"));
                                props[i] = p;
                                break;
                            }
                    }
                }
            }
        }

        return new OkResponse();
    }

    public WebDAVResponse GetPropertyNames(ref Property[] props)
    {
        return new NotAllowedResponse();
    }

    public WebDAVResponse CopyTo(IFolder folder, string destName, bool deep)
    {
        return new NotAllowedResponse();
    }

    public WebDAVResponse MoveTo(IFolder folder, string destName)
    {
        return new NotAllowedResponse();
    }

    public WebDAVResponse Delete()
    {
        using (SqlConnection conn = new SqlConnection(fConnStr))
        {
            conn.Open();
            SqlCommand cmd = conn.CreateCommand();

            cmd.CommandText = "DELETE FROM Version WHERE VersionId = @VersionId";
            cmd.Parameters.Add("@VersionId", SqlDbType.UniqueIdentifier).Value = fVersionId;
            cmd.ExecuteNonQuery();
        }
        File.Delete(this.fFilePath);

        return new NoContentResponse();
    }

    public WebDAVResponse UpdateProperties(Property[] setProps, Property[] delProps)
    {
        return new NotAllowedResponse();
    }

    public string Path
    {
        get { return this.fPath; }
    }

    #endregion  // IHierarchyItem Members

    public static string CreateVersionPath(string itemPath, int versionNumber)
    {
        return itemPath + "?version=" + versionNumber;
    }

    internal int VersionNumber
    {
        get { return this.fVersionNumber; }
    }

    internal Guid ItemId
    {
        get { return this.fItemId; }
    }

    internal string FilePath
    {
        get { return this.fFilePath; }
    }

    internal Guid VersionId
    {
        get { return fVersionId; }
    }
}
 

Requirements

Namespace: ITHit.WebDAV.Server.DeltaV

Assembly: ITHit.WebDAV.Server (in ITHit.WebDAV.Server.dll)

See Also

IVersion Members | ITHit.WebDAV.Server.DeltaV Namespace