public CacheObjectReference FindByName(string systemName)
        {
            CacheObjectReference result = null;

            _all.TryGetValue(systemName, out result);
            return(result);
        }
 public void Add(CacheObjectReference reference)
 {
     lock (this)
     {
         var list = _all.Values.ToList();
         list.Add(reference);
         SetDictionary(list);
     }
 }
 public void Rename(string oldName, string newName)
 {
     if (oldName == null)
     {
         throw new ArgumentNullException("oldName");
     }
     if (newName == null)
     {
         throw new ArgumentNullException("newName");
     }
     lock (this)
     {
         var oldObj = FindByName(oldName);
         if (oldObj == null)
         {
             throw new Exception("Не удалось найти ссылку с именем " + oldName);
         }
         var newObj = new CacheObjectReference(oldObj.Id, newName);
         var list   = _all.Values.Where(x => x.Id != oldObj.Id).ToList();
         list.Add(newObj);
         SetDictionary(list);
     }
 }
Exemplo n.º 4
0
        protected override void ValidateChanges(UnitTestCacheWriteObject oldObj, UnitTestCacheWriteObject newObj)
        {
            if (newObj.AccountId == Guid.Empty)
            {
                throw new Exception("unitTest.AccountId == Guid.Empty");
            }
            if (newObj.ComponentId == Guid.Empty)
            {
                throw new Exception("unitTest.ComponentId == Guid.Empty");
            }
            if (newObj.CreateDate == DateTime.MinValue)
            {
                throw new Exception("unitTest.CreateDate == DateTime.MinValue");
            }
            if (newObj.StatusDataId == Guid.Empty)
            {
                throw new Exception("unitTest.StatusDataId == Guid.Empty");
            }

            if (oldObj.AccountId != newObj.AccountId)
            {
                throw new Exception("oldObj.AccountId != newObj.AccountId");
            }

            if (StringHelper.GetLength(newObj.DisplayName) > 255)
            {
                throw new Exception("StringHelper.GetLength(newObj.DisplayName) > 255");
            }
            if (StringHelper.GetLength(newObj.SystemName) > 255)
            {
                throw new Exception("StringHelper.GetLength(newObj.SystemName) > 255");
            }
            if (StringHelper.GetLength(newObj.DisableComment) > 1000)
            {
                throw new Exception("StringHelper.GetLength(newObj.DisableComment) > 1000");
            }

            var newComponentRequest = new AccountCacheRequest()
            {
                AccountId = newObj.AccountId,
                ObjectId  = newObj.ComponentId
            };

            // если изменился родитель
            if (oldObj.ComponentId != newObj.ComponentId)
            {
                // получим нового родителя
                using (var newComponent = AllCaches.Components.Write(newComponentRequest))
                {
                    // проверим, что у нового родителя нет ребенка с таким же именем
                    var child = newComponent.UnitTests.FindByName(newObj.SystemName);
                    if (child != null)
                    {
                        throw new Exception("У нового компонента уже есть проверка с таким именем");
                    }

                    // получим старого родителя
                    var oldComponentRequest = new AccountCacheRequest()
                    {
                        AccountId = newObj.AccountId,
                        ObjectId  = oldObj.ComponentId
                    };
                    using (var oldComponent = AllCaches.Components.Write(oldComponentRequest))
                    {
                        var reference = new CacheObjectReference(newObj.Id, newObj.SystemName);
                        newComponent.WriteUnitTests.Add(reference);
                        oldComponent.WriteUnitTests.Delete(newObj.Id);
                        oldComponent.BeginSave();
                    }
                    newComponent.BeginSave();
                }
            }
            // если изменилось только системное имя
            else if (string.Equals(oldObj.SystemName, newObj.SystemName, StringComparison.InvariantCultureIgnoreCase) == false)
            {
                // проверим что у компонента нет проверки с таким SystemName
                using (var component = AllCaches.Components.Write(newComponentRequest))
                {
                    var child = component.UnitTests.FindByName(newObj.SystemName);
                    if (child != null)
                    {
                        throw new Exception("У компонента уже есть проверка с таким SystemName");
                    }
                    component.WriteUnitTests.Rename(oldObj.SystemName, newObj.SystemName);
                    component.BeginSave();
                }
            }
        }
Exemplo n.º 5
0
        protected override void ValidateChanges(ComponentCacheWriteObject oldComponent, ComponentCacheWriteObject newComponent)
        {
            if (oldComponent == null)
            {
                throw new ArgumentNullException("oldComponent");
            }
            if (newComponent == null)
            {
                throw new ArgumentNullException("newComponent");
            }
            if (oldComponent.AccountId != newComponent.AccountId)
            {
                throw new ArgumentNullException("Нельзя изменять AccountId");
            }
            if (newComponent.AccountId == Guid.Empty)
            {
                throw new ArgumentNullException("AccountId is Empty");
            }
            if (newComponent.ParentId == null)
            {
                if (newComponent.IsRoot == false)
                {
                    throw new ArgumentException("Не указан ParentId");
                }
            }
            if (newComponent.SystemName == null)
            {
                throw new ArgumentException("Не указан SystemName");
            }
            if (StringHelper.GetLength(newComponent.DisplayName) > 255)
            {
                throw new Exception("StringHelper.GetLength(newComponent.DisplayName) > 255");
            }
            if (StringHelper.GetLength(newComponent.SystemName) > 255)
            {
                throw new Exception("StringHelper.GetLength(newComponent.SystemName) > 255");
            }
            if (StringHelper.GetLength(newComponent.DisableComment) > 1000)
            {
                throw new Exception("StringHelper.GetLength(newComponent.DisableComment) > 1000");
            }

            var accountId = newComponent.AccountId;

            // если объект удалили
            if (newComponent.IsDeleted)
            {
                using (var parent = AllCaches.Components.Write(new AccountCacheRequest()
                {
                    AccountId = newComponent.AccountId,
                    ObjectId = newComponent.ParentId.Value
                }))
                {
                    parent.WriteChilds.Delete(newComponent.Id);
                    parent.BeginSave();
                };
            }

            // если изменился родитель
            if (newComponent.ParentId != oldComponent.ParentId)
            {
                // проверим наличие нового родителя
                var parentRequest = new AccountCacheRequest()
                {
                    AccountId = accountId,
                    ObjectId  = newComponent.ParentId.Value
                };
                using (var parent = AllCaches.Components.Write(parentRequest))
                {
                    // проверим, что у нового родителя нет детей с таким же именем
                    var child = parent.Childs.FindByName(newComponent.SystemName);
                    if (child != null)
                    {
                        throw new ParameterErrorException("Системное имя должно быть уникальным");
                    }

                    // родителу можно добавить нового ребенка
                    var newReference = new CacheObjectReference(newComponent.Id, newComponent.SystemName);
                    parent.WriteChilds.Add(newReference);

                    // удлалим ссылку в старом родителе
                    if (oldComponent.ParentId.HasValue)
                    {
                        var oldParentRequest = new AccountCacheRequest()
                        {
                            AccountId = accountId,
                            ObjectId  = oldComponent.ParentId.Value
                        };
                        using (var oldParent = AllCaches.Components.Write(oldParentRequest))
                        {
                            oldParent.WriteChilds.Delete(newComponent.Id);
                            oldParent.BeginSave();
                        }
                    }
                    parent.BeginSave();
                }
            }
            // если родитель НЕ изменился, но изменилось системное имя
            else if (string.Equals(oldComponent.SystemName, newComponent.SystemName, StringComparison.InvariantCultureIgnoreCase) == false)
            {
                // проверим, что у родителя нет ребенка с таким же именем
                var parentRequest = new AccountCacheRequest()
                {
                    AccountId = accountId,
                    ObjectId  = newComponent.ParentId.Value
                };
                using (var parent = AllCaches.Components.Write(parentRequest))
                {
                    // проверим, что у нового родителя нет детей с таким же именем
                    var child = parent.Childs.FindByName(newComponent.SystemName);
                    if (child != null)
                    {
                        throw new ParameterErrorException("SystemName должен быть уникальным");
                    }

                    // родителю можно добавить нового ребенка
                    parent.WriteChilds.Rename(oldComponent.SystemName, newComponent.SystemName);
                    parent.BeginSave();
                }
            }
        }
Exemplo n.º 6
0
        protected override void ValidateChanges(MetricCacheWriteObject oldObj, MetricCacheWriteObject newObj)
        {
            if (newObj.AccountId == Guid.Empty)
            {
                throw new Exception("newObj.AccountId == Guid.Empty");
            }
            if (newObj.ComponentId == Guid.Empty)
            {
                throw new Exception("newObj.ComponentId == Guid.Empty");
            }
            if (newObj.CreateDate == DateTime.MinValue)
            {
                throw new Exception("newObj.CreateDate == DateTime.MinValue");
            }
            if (newObj.StatusDataId == Guid.Empty)
            {
                throw new Exception("newObj.StatusDataId == Guid.Empty");
            }

            if (oldObj.AccountId != newObj.AccountId)
            {
                throw new Exception("oldObj.AccountId != newObj.AccountId");
            }
            if (StringHelper.GetLength(newObj.DisableComment) > 1000)
            {
                throw new UserFriendlyException("Длина DisableComment макимум 1000 символов");
            }

            var newComponentRequest = new AccountCacheRequest()
            {
                AccountId = newObj.AccountId,
                ObjectId  = newObj.ComponentId
            };

            // если удалена
            if (newObj.IsDeleted)
            {
                // получим нового родителя
                using (var newComponent = AllCaches.Components.Write(newComponentRequest))
                {
                    newComponent.WriteMetrics.Delete(newObj.Id);
                    newComponent.BeginSave();
                }
            }
            // если изменился родитель
            else if (oldObj.ComponentId != newObj.ComponentId)
            {
                // получим нового родителя
                using (var newComponent = AllCaches.Components.Write(newComponentRequest))
                {
                    // проверим, что у нового родителя нет ребенка с таким же именем
                    var child = newComponent.Metrics.FindByMetricTypeId(newObj.MetricTypeId);
                    if (child != null)
                    {
                        throw new Exception("У нового компонента уже есть метрика с таким именем");
                    }

                    // получим старого родителя
                    var oldComponentRequest = new AccountCacheRequest()
                    {
                        AccountId = newObj.AccountId,
                        ObjectId  = oldObj.ComponentId
                    };
                    using (var oldComponent = AllCaches.Components.Write(oldComponentRequest))
                    {
                        var reference = new CacheObjectReference(newObj.Id, newObj.MetricTypeId.ToString());
                        newComponent.WriteMetrics.Add(reference);
                        oldComponent.WriteMetrics.Delete(newObj.Id);
                        oldComponent.BeginSave();
                    }
                    newComponent.BeginSave();
                }
            }
            // если изменилось только системное имя
            else if (oldObj.MetricTypeId != newObj.MetricTypeId)
            {
                // проверим что у компонента нет проверки с таким MetricTypeId
                using (var component = AllCaches.Components.Write(newComponentRequest))
                {
                    var child = component.Metrics.FindByMetricTypeId(newObj.MetricTypeId);
                    if (child != null)
                    {
                        throw new Exception("У компонента уже есть метрика с таким именем");
                    }
                    component.WriteMetrics.Rename(oldObj.MetricTypeId.ToString(), newObj.MetricTypeId.ToString());
                    component.BeginSave();
                }
            }
        }