public static string GetIdentity(this IStorageEntity storageEntity)
        {
            switch (storageEntity)
            {
            case IStorageAccessAuthorization storageAccessAuthorization:
                return(storageAccessAuthorization.Id);

            case IStorageBag storageBag:
                return($"{storageBag.TargetId}_{storageBag.Key}");

            case IStorageConditionAuthorization storageConditionAuthorization:
                return(storageConditionAuthorization.Id);

            case IStorageSecurityIdentity storageSecurityIdentity:
                return(storageSecurityIdentity.Id);

            case IStorageSecurityIdentityRelation storageSecurityIdentityRelation:
                return
                    ($"{storageSecurityIdentityRelation.ParentId}_{storageSecurityIdentityRelation.SecurityIdentityId}");

            case IStorageSecurityItem storageSecurityItem:
                return(storageSecurityItem.Id);

            case IStorageSecurityItemRelation storageSecurityItemRelation:
                return($"{storageSecurityItemRelation.ParentId}_{storageSecurityItemRelation.SecurityItemId}");

            default:
                throw new ArgumentOutOfRangeException();
            }
        }
Пример #2
0
        /// <summary>
        /// 
        /// </summary>
        /// <param name="entity"></param>
        public bool Create(IStorageEntity entity)
        {
            if (entity == null)
                throw new ArgumentNullException("entity");

            var mongoEntity = entity as MongoEntity;

            if (mongoEntity == null)
                throw new Exception("The specified entity is not mongo storage object.");

            return MongoStaticContext.Context.Entities.Create(mongoEntity);
        }
Пример #3
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="entity"></param>
        public bool Create(IStorageEntity entity)
        {
            lock (lockObject)
            {
                if (entity == null)
                {
                    throw new ArgumentNullException("entity");
                }

                var mongoEntity = entity as MongoEntity;

                if (mongoEntity == null)
                {
                    throw new Exception("The specified entity is not mongo storage object.");
                }

                cachedEntities = null;

                return(MongoStaticContext.Context.Entities.Create(mongoEntity));
            }
        }
Пример #4
0
 public virtual StoreId GetStoreId(IStorageEntity entity)
 {
     return(this.ToStoreId(entity.Id, entity.ChangeKey));
 }
        /// <summary>
        /// Saves entity document
        /// </summary>
        /// <param name="entity"></param>
        public bool Save(IStorageEntity entity)
        {
            lock (lockObject)
            {
                if (entity == null)
                    throw new ArgumentNullException("entity");

                var mongoEntity = entity as MongoEntity;

                if (mongoEntity == null)
                    throw new Exception("The specified entity is not mongo storage object.");

                cachedEntities = null;

                return MongoStaticContext.Context.Entities.Save(mongoEntity);
            }
        }
Пример #6
0
        private void RecurseForCreateFolderPath(string path, int childId, IStorageEntity obj)
        {
            //check for root folder
            if (string.IsNullOrEmpty(path))
            {
                path = "/";
            }

            IList <StorageFolder> array = _storageFolderRepository.Find(c => c.Path == path).ToList();

            if (!array.Any())
            {
                //folder does not exist so insert and move up
                StorageFolder folder = new StorageFolder();
                folder.Path = path;
                if (obj != null)
                {
                    if (obj.GetType() == typeof(StorageFile))
                    {
                        folder.Files.Add((StorageFile)obj);
                    }

                    if (obj.GetType() == typeof(StorageFolder))
                    {
                        folder.Folders.Add((StorageFolder)obj);
                    }
                }

                folder = _storageFolderRepository.Add(folder);

                if (!string.IsNullOrEmpty(path.Trim()))
                {
                    int lastIndex = path.LastIndexOf('/');
                    if (lastIndex != -1)
                    {
                        path = path.Substring(0, lastIndex);
                        RecurseForCreateFolderPath(path, folder.Id, folder);
                    }
                }
            }
            else
            {
                StorageFolder       j_obj = array[0];
                List <IStorageFile> files = j_obj.Files;
                var id = j_obj.Id;

                if (id != childId)
                {
                    var list = new List <StorageFile>();
                    foreach (StorageFile file in files)
                    {
                        list.Add(file);
                    }

                    if (!list.Where(c => c.Id == childId).Any())
                    {
                        files.Add((StorageFile)obj);
                    }
                }

                j_obj.Files = files;

                _storageFolderRepository.Update(j_obj);
            }
        }
Пример #7
0
 public StorageEntity(IStorageEntity se) => Entity = se;
Пример #8
0
        private List <ErrorModel> ValidateRelation(EntityRelation relation, ValidationType validationType)
        {
            List <ErrorModel> errors = new List <ErrorModel>();

            if (validationType == ValidationType.Update)
            {
                //we cannot update relation with missing Id (Guid.Empty means id is missing)
                //of if there is no relation with this id already
                if (relation.Id == Guid.Empty)
                {
                    errors.Add(new ErrorModel("id", null, "Id is required!"));
                }
                else if (relationRepository.Read(relation.Id) == null)
                {
                    errors.Add(new ErrorModel("id", relation.Id.ToString(), "Entity relation with such Id does not exist!"));
                }
            }
            else if (validationType == ValidationType.Create)
            {
                //if id is null, them we later will assing one before create process
                //otherwise check if relation with same id already exists
                if (relation.Id != Guid.Empty && (relationRepository.Read(relation.Id) != null))
                {
                    errors.Add(new ErrorModel("id", relation.Id.ToString(), "Entity relation with such Id already exist!"));
                }
            }
            else if (validationType == ValidationType.RelationsOnly)
            {
                //no need to check anything, we need to check only Entities and Fields relations
                //this case is here only for readability
            }

            IStorageEntityRelation existingRelation = null;

            if (validationType == ValidationType.Create || validationType == ValidationType.Update)
            {
                //validate name
                // - if name string is correct
                // - then if relation with same name already exists
                var nameValidationErrors = ValidationUtility.ValidateName(relation.Name);
                if (nameValidationErrors.Count > 0)
                {
                    errors.AddRange(nameValidationErrors);
                }
                else
                {
                    existingRelation = relationRepository.Read(relation.Name);
                    if (validationType == ValidationType.Create)
                    {
                        //if relation with same name alfready exists
                        if (existingRelation != null)
                        {
                            errors.Add(new ErrorModel("name", relation.Name, string.Format("Entity relation '{0}' exists already!", relation.Name)));
                        }
                    }
                    else if (validationType == ValidationType.Update)
                    {
                        //if relation with same name alfready and different Id already exists
                        if (existingRelation != null && existingRelation.Id != relation.Id)
                        {
                            errors.Add(new ErrorModel("name", relation.Name, string.Format("Entity relation '{0}' exists already!", relation.Name)));
                        }
                    }
                }
            }
            else if (validationType == ValidationType.RelationsOnly)
            {
                //no need to check anything, we need to check only Entities and Fields relations
                //this case is here only for readability
            }

            errors.AddRange(ValidationUtility.ValidateLabel(relation.Label));

            IStorageEntity originEntity = entityRepository.Read(relation.OriginEntityId);
            IStorageEntity targetEntity = entityRepository.Read(relation.TargetEntityId);
            IStorageField  originField  = null;
            IStorageField  targetField  = null;

            if (originEntity == null)
            {
                errors.Add(new ErrorModel("originEntity", relation.OriginEntityId.ToString(), "The origin entity do not exist."));
            }
            else
            {
                originField = originEntity.Fields.SingleOrDefault(x => x.Id == relation.OriginFieldId);
                if (originField == null)
                {
                    errors.Add(new ErrorModel("originField", relation.OriginFieldId.ToString(), "The origin field do not exist."));
                }
                if (!(originField is IStorageGuidField))
                {
                    errors.Add(new ErrorModel("originField", relation.OriginFieldId.ToString(), "The origin field should be Unique Identifier (GUID) field."));
                }
            }

            if (targetEntity == null)
            {
                errors.Add(new ErrorModel("targetEntity", relation.TargetEntityId.ToString(), "The target entity do not exist."));
            }
            else
            {
                targetField = targetEntity.Fields.SingleOrDefault(x => x.Id == relation.TargetFieldId);
                if (targetField == null)
                {
                    errors.Add(new ErrorModel("targetField", relation.TargetFieldId.ToString(), "The target field do not exist."));
                }
                if (!(targetField is IStorageGuidField))
                {
                    errors.Add(new ErrorModel("targetField", relation.TargetFieldId.ToString(), "The target field should be Unique Identifier (GUID) field."));
                }
            }


            //the second level validation requires no errors on first one
            //so if there are errors in first level we return them
            if (errors.Count > 0)
            {
                return(errors);
            }


            if (validationType == ValidationType.Update)
            {
                if (existingRelation.RelationType != relation.RelationType)
                {
                    errors.Add(new ErrorModel("relationType", relation.RelationType.ToString(),
                                              "The initialy selected relation type is readonly and cannot be changed."));
                }

                if (existingRelation.OriginEntityId != relation.OriginEntityId)
                {
                    errors.Add(new ErrorModel("originEntityId", relation.OriginEntityId.ToString(),
                                              "The origin entity differ from initial one. The initialy selected origin entity is readonly and cannot be changed."));
                }

                if (existingRelation.OriginFieldId != relation.OriginFieldId)
                {
                    errors.Add(new ErrorModel("originFieldId", relation.OriginFieldId.ToString(),
                                              "The origin field differ from initial one. The initialy selected origin field is readonly and cannot be changed."));
                }

                if (existingRelation.TargetEntityId != relation.TargetEntityId)
                {
                    errors.Add(new ErrorModel("targetEntityId", relation.TargetEntityId.ToString(),
                                              "The target entity differ from initial one. The initialy selected target entity is readonly and cannot be changed."));
                }

                if (existingRelation.TargetFieldId != relation.TargetFieldId)
                {
                    errors.Add(new ErrorModel("TargetFieldId", relation.TargetFieldId.ToString(),
                                              "The target field differ from initial one. The initialy selected target field is readonly and cannot be changed."));
                }
            }
            else if (validationType == ValidationType.Create)
            {
                if (relation.RelationType == EntityRelationType.OneToMany || relation.RelationType == EntityRelationType.OneToOne)
                {
                    //validate if target and origin field is same field for following relations
                    if (relation.OriginEntityId == relation.TargetEntityId && relation.OriginFieldId == relation.TargetFieldId)
                    {
                        errors.Add(new ErrorModel("", "", "The origin and target fields cannot be the same."));
                    }

                    //validate there is no other already existing relation with same parameters
                    foreach (var rel in relationRepository.Read())
                    {
                        if (rel.OriginEntityId == relation.OriginEntityId && rel.TargetEntityId == relation.TargetEntityId &&
                            rel.OriginFieldId == relation.OriginFieldId && rel.TargetFieldId == relation.TargetFieldId)
                        {
                            errors.Add(new ErrorModel("", "", "There is already existing relation with same parameters."));
                        }
                    }
                }



                if (relation.RelationType == EntityRelationType.OneToOne || relation.RelationType == EntityRelationType.ManyToMany)
                {
                    if (!originField.Required)
                    {
                        errors.Add(new ErrorModel("originFieldId", relation.OriginFieldId.ToString(), "The origin field must be specified as Required"));
                    }

                    if (!originField.Unique)
                    {
                        errors.Add(new ErrorModel("originFieldId", relation.OriginFieldId.ToString(), "The origin field must be specified as Unique"));
                    }

                    if (!targetField.Required)
                    {
                        errors.Add(new ErrorModel("targetFieldId", relation.TargetFieldId.ToString(), "The target field must be specified as Required"));
                    }

                    if (!targetField.Unique)
                    {
                        errors.Add(new ErrorModel("targetFieldId", relation.TargetFieldId.ToString(), "The target field must be specified as Unique"));
                    }
                }

                if (relation.RelationType == EntityRelationType.OneToMany)
                {
                    if (!originField.Required)
                    {
                        errors.Add(new ErrorModel("originFieldId", relation.OriginFieldId.ToString(), "The origin field must be specified as Required"));
                    }

                    if (!originField.Unique)
                    {
                        errors.Add(new ErrorModel("originFieldId", relation.OriginFieldId.ToString(), "The origin field must be specified as Unique"));
                    }
                }
            }
            if (validationType == ValidationType.RelationsOnly)
            {
                if (relation.RelationType == EntityRelationType.OneToOne || relation.RelationType == EntityRelationType.ManyToMany)
                {
                    if (!originField.Required)
                    {
                        errors.Add(new ErrorModel("originFieldId", relation.OriginFieldId.ToString(), "The origin field must be specified as Required"));
                    }

                    if (!originField.Unique)
                    {
                        errors.Add(new ErrorModel("originFieldId", relation.OriginFieldId.ToString(), "The origin field must be specified as Unique"));
                    }

                    if (!targetField.Required)
                    {
                        errors.Add(new ErrorModel("targetFieldId", relation.TargetFieldId.ToString(), "The target field must be specified as Required"));
                    }

                    if (!targetField.Unique)
                    {
                        errors.Add(new ErrorModel("targetFieldId", relation.TargetFieldId.ToString(), "The target field must be specified as Unique"));
                    }
                }

                if (relation.RelationType == EntityRelationType.OneToMany)
                {
                    if (!originField.Required)
                    {
                        errors.Add(new ErrorModel("originFieldId", relation.OriginFieldId.ToString(), "The origin field must be specified as Required"));
                    }

                    if (!originField.Unique)
                    {
                        errors.Add(new ErrorModel("originFieldId", relation.OriginFieldId.ToString(), "The origin field must be specified as Unique"));
                    }
                }
            }

            return(errors);
        }
Пример #9
0
 public override ICommentDomainEntity ConvertToDomain(IStorageEntity obj)
 {
     return(((ICommentEntity)obj).ConvertToDomain());
 }
Пример #10
0
 public abstract TD ConvertToDomain(IStorageEntity obj);
 private void AddNewAction(StorageActionType actionType, IStorageEntity entity)
 {
     AddNewAction(actionType, Enumerable.Repeat(entity, 1));
 }
Пример #12
0
        public virtual async Task <TD> CreateAsync(IStorageEntity obj)
        {
            var ins = await _repository.CreateAsync((TE)obj);

            return(await GetDomainMetaData(ConvertToDomain(ins)));
        }
Пример #13
0
 private StorageItem GetStorageItem(IStorageEntity storageEntity)
 {
     return(GetStorageItem(storageEntity, storageEntity.GetIdentity(),
                           GetStorageType(storageEntity.GetType())));
 }
Пример #14
0
 private StorageItem GetStorageItem(IStorageEntity storageEntity, string id, StorageType storageType)
 {
     return(new StorageItem {
         Id = id, Type = storageType, Data = JsonConvert.SerializeObject(storageEntity)
     });
 }
 public override IUserDomainEntity ConvertToDomain(IStorageEntity obj)
 {
     return(((IUserEntity)obj).ConvertToDomain());
 }
 public override IEntryDomainEntity ConvertToDomain(IStorageEntity obj)
 {
     return(((IEntryEntity)obj).ConvertToDomain());
 }