Skip to content
Permalink
Browse files

C1 Console 'is another user editing this check' take data items langu…

…age version into account. Fix #673
  • Loading branch information...
mawtex committed Sep 26, 2019
1 parent 300c99f commit 70435b5f819308d711fe76704ad951ea19396cba
Showing with 49 additions and 31 deletions.
  1. +48 −30 Composite/C1Console/Actions/ActionLockingFacade.cs
  2. +1 −1 Composite/Data/Types/ILockingInformation.cs
@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
@@ -21,7 +21,7 @@ namespace Composite.C1Console.Actions
public static class ActionLockingFacade
{
private static readonly string LogTitle = typeof(ActionLockingFacade).Name;
private static Dictionary<EntityToken, LockingInformation> _lockingInformations = null;
private static Dictionary<string, LockingInformation> _lockingInformations = null;
private static readonly object _lock = new object();
private static readonly IFormatter _ownerIdFormatter = new BinaryFormatter();

@@ -90,7 +90,8 @@ public static void ReleaseLock(EntityToken entityToken, object ownerId)
{
EnsureInitialization();

RemoveLockingInformation(entityToken, ownerId);
string lockKey = GetLockKey(entityToken);
RemoveLockingInformation(lockKey, ownerId);
}
}

@@ -108,14 +109,14 @@ public static void ReleaseAllLocks(object ownerId)
{
EnsureInitialization();

List<EntityToken> entityTokens =
List<string> lockKeys =
(from li in _lockingInformations
where object.Equals(li.Value.OwnerId, ownerId)
select li.Key).ToList();

foreach (EntityToken entityToken in entityTokens)
foreach (string lockKey in lockKeys)
{
RemoveLockingInformation(entityToken, ownerId);
RemoveLockingInformation(lockKey, ownerId);
}
}
}
@@ -132,8 +133,8 @@ public static bool IsLocked(EntityToken entityToken)
using (GlobalInitializerFacade.CoreIsInitializedScope)
{
EnsureInitialization();

return _lockingInformations.ContainsKey(entityToken);
string lockKey = GetLockKey(entityToken);
return _lockingInformations.ContainsKey(lockKey);
}
}

@@ -150,8 +151,10 @@ public static string LockedBy(EntityToken entityToken)
{
EnsureInitialization();

string lockKey = GetLockKey(entityToken);

LockingInformation lockingInformation;
if (!_lockingInformations.TryGetValue(entityToken, out lockingInformation))
if (!_lockingInformations.TryGetValue(lockKey, out lockingInformation))
{
return null;
}
@@ -181,10 +184,10 @@ internal static void ReleaseAll(string username)
{
EnsureInitialization();

List<Tuple<EntityToken, object>> itemsToRemove =
List<Tuple<string, object>> itemsToRemove =
(from info in _lockingInformations
where info.Value.Username == username
select new Tuple<EntityToken, object>(info.Key, info.Value.OwnerId)).ToList();
select new Tuple<string, object>(info.Key, info.Value.OwnerId)).ToList();

foreach (var item in itemsToRemove)
{
@@ -205,9 +208,11 @@ public static void RemoveLock(EntityToken entityToken)
{
EnsureInitialization();

if (_lockingInformations.ContainsKey(entityToken))
string lockKey = GetLockKey(entityToken);

if (_lockingInformations.ContainsKey(lockKey))
{
RemoveLockingInformation(entityToken, _lockingInformations[entityToken].OwnerId);
RemoveLockingInformation(lockKey, _lockingInformations[lockKey].OwnerId);
}
}
}
@@ -223,7 +228,7 @@ private static void DoInitialize()

if (_lockingInformations == null)
{
_lockingInformations = new Dictionary<EntityToken, LockingInformation>();
_lockingInformations = new Dictionary<string, LockingInformation>();

LoadLockingInformation();
}
@@ -251,9 +256,7 @@ private static void LoadLockingInformation()

try
{
EntityToken entityToken = EntityTokenSerializer.Deserialize(lockingInformation.SerializedEntityToken);

_lockingInformations.Add(entityToken, li);
_lockingInformations.Add(lockingInformation.LockKey, li);
}
catch (Exception)
{
@@ -272,8 +275,10 @@ private static void LoadLockingInformation()

private static void AddLockingInformation(EntityToken entityToken, object ownerId)
{
string lockKey = GetLockKey(entityToken);

LockingInformation lockingInformation;
if (_lockingInformations.TryGetValue(entityToken, out lockingInformation))
if (_lockingInformations.TryGetValue(lockKey, out lockingInformation))
{
if (object.Equals(lockingInformation.OwnerId, ownerId))
{
@@ -295,26 +300,26 @@ private static void AddLockingInformation(EntityToken entityToken, object ownerI

ILockingInformation li = DataFacade.BuildNew<ILockingInformation>();
li.Id = Guid.NewGuid();
li.SerializedEntityToken = EntityTokenSerializer.Serialize(entityToken);
li.LockKey = lockKey;
li.SerializedOwnerId = serializedOwnerId;
li.Username = lockingInformation.Username;

DataFacade.AddNew<ILockingInformation>(li);
_lockingInformations.Add(entityToken, lockingInformation);
_lockingInformations.Add(lockKey, lockingInformation);
}



private static void UpdateLockingInformation(EntityToken entityToken, object newOwnerId)
{
LockingInformation lockingInformation;
if (!_lockingInformations.TryGetValue(entityToken, out lockingInformation)) throw new InvalidOperationException("LockingInformation record missing");
string lockKey = GetLockKey(entityToken);

string serializedEntityToken = EntityTokenSerializer.Serialize(entityToken);
LockingInformation lockingInformation;
if (!_lockingInformations.TryGetValue(lockKey, out lockingInformation)) throw new InvalidOperationException("LockingInformation record missing");

ILockingInformation lockingInformationDataItem =
DataFacade.GetData<ILockingInformation>().
Single(f => f.SerializedEntityToken == serializedEntityToken);
Single(f => f.LockKey == lockKey);

lockingInformationDataItem.SerializedOwnerId = SerializeOwnerId(newOwnerId);
DataFacade.Update(lockingInformationDataItem);
@@ -324,27 +329,26 @@ private static void UpdateLockingInformation(EntityToken entityToken, object new



private static void RemoveLockingInformation(EntityToken entityToken, object ownerId)
private static void RemoveLockingInformation(string lockKey, object ownerId)
{
LockingInformation lockingInformation;
if (!_lockingInformations.TryGetValue(entityToken, out lockingInformation)) return;
if (!_lockingInformations.TryGetValue(lockKey, out lockingInformation)) return;

if (Equals(lockingInformation.OwnerId, ownerId))
{
_lockingInformations.Remove(entityToken);
_lockingInformations.Remove(lockKey);
}

string serializedOwnerId = SerializeOwnerId(ownerId);
string serializedEntityToken = EntityTokenSerializer.Serialize(entityToken);

ILockingInformation lockingInformationDataItem =
DataFacade.GetData<ILockingInformation>()
.SingleOrDefault(f => f.SerializedEntityToken == serializedEntityToken
.SingleOrDefault(f => f.LockKey == lockKey
&& f.SerializedOwnerId == serializedOwnerId);

if (lockingInformationDataItem == null)
{
Log.LogWarning(LogTitle, "Failed to find entity token lock. EntityToken: " + serializedEntityToken);
Log.LogWarning(LogTitle, "Failed to find entity token lock. EntityToken: " + lockKey);
return;
}

@@ -405,6 +409,20 @@ private static void Exit()
}


private static string GetLockKey(EntityToken entityToken)
{
string lockKey = entityToken.Serialize();

if (entityToken is DataEntityToken)
{
var dataEntityToken = entityToken as DataEntityToken;
lockKey = lockKey + dataEntityToken.DataSourceId.LocaleScope.ToString();
}

return lockKey;
}




private sealed class LockerToken : IDisposable
@@ -28,7 +28,7 @@ public interface ILockingInformation : IData
[StoreFieldType(PhysicalStoreFieldType.LargeString)]
[ImmutableFieldId("{34BD5C80-C5FD-4932-A1E6-3459E2D7802D}")]
[NotNullValidator()]
string SerializedEntityToken { get; set; }
string LockKey { get; set; }


/// <exclude />

0 comments on commit 70435b5

Please sign in to comment.
You can’t perform that action at this time.