Locks this item.
WebDAV engine passes LockInfo structure to this method by reference. All fields of the structure are provided by WebDAV client except Token field. In your Lock implementation you must create lock token and set Token member. You must also associate generated token with the hierarchy item in the repository during this call. The token is sent to the WebDAV client.
public WebDAVResponse Lock(ref LockInfo lockInfo)
{
if(ItemHasLock(lockInfo.Shared))
return new LockedResponse();
if(lockInfo.Deep)
{
MultistatusResponse mr = FindLocksDown(this, lockInfo.Shared);
if(mr.Responses.Length > 0)
return mr;
}
SqlConnection conn = new SqlConnection(connStr);
SqlCommand cmd;
conn.Open();
try
{
cmd = conn.CreateCommand();
cmd.CommandText = "INSERT INTO Lock (ItemID,Token,Shared,Deep,Expires,Owner)"
+" VALUES(@ItemID, @Token, @Shared, @Deep, @Expires, @Owner)";
lockInfo.Token = Guid.NewGuid().ToString();
cmd.Parameters.Add("@ItemID", SqlDbType.Int).Value = ID;
cmd.Parameters.Add("@Token", SqlDbType.Char).Value = lockInfo.Token;
cmd.Parameters.Add("@Shared", SqlDbType.Bit).Value = lockInfo.Shared;
cmd.Parameters.Add("@Deep", SqlDbType.Bit).Value = lockInfo.Deep;
cmd.Parameters.Add("@Expires", SqlDbType.DateTime);
if(lockInfo.Timeout >= 0)
{
cmd.Parameters["@Expires"].Value = DateTime.Now.AddSeconds(lockInfo.Timeout);
}
else
{
cmd.Parameters["@Expires"].Value = SqlDateTime.Null;
}
cmd.Parameters.Add("@Owner", SqlDbType.NVarChar).Value = lockInfo.Owner;
cmd.ExecuteNonQuery();
}
finally
{
conn.Close();
}
return new OkResponse();
}
protected bool ItemHasLock(bool skipShared)
{
LockInfo[] locks = ActiveLocks;
if(locks.Length == 0)
return false;
return !(skipShared && locks[0].Shared);
}
protected static MultistatusResponse FindLocksDown(IHierarchyItem root, bool skipShared)
{
MultistatusResponse mr = new MultistatusResponse();
IFolder folder = root as IFolder;
if(folder != null)
foreach(IHierarchyItem child in folder.Children)
{
HierarchyItem dbchild = child as HierarchyItem;
if(dbchild.ItemHasLock(skipShared))
mr.AddResponses(new ItemResponse(dbchild.Path, new LockedResponse()));
MultistatusResponse mrchild = FindLocksDown(child, skipShared);
mr.AddResponses(mrchild.Responses);
}
return mr;
}
ILock Interface | ITHit.WebDAV.Server Namespace