Exemplo n.º 1
0
        public EntityRelationListResponse Read(List <DbEntity> storageEntityList = null)
        {
            EntityRelationListResponse response = new EntityRelationListResponse();

            response.Timestamp = DateTime.UtcNow;
            response.Object    = null;

            try
            {
                var relations = Cache.GetRelations();
                if (relations != null)
                {
                    response.Object  = relations;
                    response.Hash    = Cache.GetRelationsHash();
                    response.Success = true;
                    response.Message = null;
                    return(response);
                }

                relations = CurrentContext.RelationRepository.Read().Select(x => x.MapTo <EntityRelation>()).ToList();

                List <DbEntity> dbEntities = storageEntityList;
                if (dbEntities == null)
                {
                    dbEntities = new DbEntityRepository(CurrentContext).Read();
                }
                foreach (EntityRelation relation in relations)
                {
                    var originEntity = dbEntities.Single(x => x.Id == relation.OriginEntityId);
                    var targetEntity = dbEntities.Single(x => x.Id == relation.TargetEntityId);
                    relation.OriginEntityName = originEntity.Name;
                    relation.TargetEntityName = targetEntity.Name;
                    relation.OriginFieldName  = originEntity.Fields.Single(x => x.Id == relation.OriginFieldId).Name;
                    relation.TargetFieldName  = targetEntity.Fields.Single(x => x.Id == relation.TargetFieldId).Name;
                }

                if (relations != null)
                {
                    Cache.AddRelations(relations);
                }

                //we use instance from cache as return value, because in cache we deepcopy collection
                response.Object  = Cache.GetRelations();
                response.Hash    = Cache.GetRelationsHash();
                response.Success = true;
                response.Message = null;
                return(response);
            }
            catch (Exception e)
            {
                response.Success   = false;
                response.Object    = null;
                response.Hash      = null;
                response.Timestamp = DateTime.UtcNow;
                if (ErpSettings.DevelopmentMode)
                {
                    response.Message = e.Message + e.StackTrace;
                }
                return(response);
            }
        }
Exemplo n.º 2
0
        public EntityRelationResponse Update(EntityRelation relation)
        {
            EntityRelationResponse response = new EntityRelationResponse();

            response.Timestamp = DateTime.UtcNow;
            response.Object    = relation;

            bool hasPermisstion = SecurityContext.HasMetaPermission();

            if (!hasPermisstion)
            {
                response.StatusCode = HttpStatusCode.Forbidden;
                response.Success    = false;
                response.Message    = "User have no permissions to manipulate erp meta.";
                response.Errors.Add(new ErrorModel {
                    Message = "Access denied."
                });
                return(response);
            }

            response.Errors = ValidateRelation(relation, ValidationType.Update);
            if (response.Errors.Count > 0)
            {
                response.Success = false;
                response.Message = "The entity relation was not updated. Validation error occurred!";
                return(response);
            }

            try
            {
                var storageRelation = relation.MapTo <DbEntityRelation>();
                storageRelation.Name = storageRelation.Name.Trim();
                var success = CurrentContext.RelationRepository.Update(storageRelation);
                Cache.Clear();
                if (success)
                {
                    response.Success = true;
                    response.Message = "The entity relation was successfully updated!";
                    return(response);
                }
                else
                {
                    response.Success = false;
                    response.Message = "The entity relation was not updated! An internal error occurred!";
                    return(response);
                }
            }
            catch (Exception e)
            {
                Cache.Clear();
                response.Success   = false;
                response.Object    = relation;
                response.Timestamp = DateTime.UtcNow;

                if (ErpSettings.DevelopmentMode)
                {
                    response.Message = e.Message + e.StackTrace;
                }
                else
                {
                    response.Message = "The entity relation was not updated. An internal error occurred!";
                }

                return(response);
            }
        }
Exemplo n.º 3
0
        public EntityRelationResponse Read(string name)
        {
            EntityRelationResponse response = new EntityRelationResponse();

            response.Timestamp = DateTime.UtcNow;
            response.Object    = null;

            if (string.IsNullOrWhiteSpace(name))
            {
                response.Success = false;
                response.Errors.Add(new ErrorModel("name", null, "The name argument is NULL."));
                return(response);
            }

            var relations = Cache.GetRelations();

            if (relations != null)
            {
                response.Object  = relations.SingleOrDefault(x => x.Name == name);
                response.Success = true;
                if (response.Object != null)
                {
                    response.Message = "The entity relation was successfully returned!";
                }
                else
                {
                    response.Message = string.Format("The entity relation '{0}' does not exist!", name);
                }
                return(response);
            }

            try
            {
                relations        = Read().Object;
                response.Object  = relations.SingleOrDefault(x => x.Name == name);
                response.Success = false;

                if (response.Object != null)
                {
                    response.Success = true;
                    response.Message = "The entity relation was successfully returned!";
                }
                else
                {
                    response.Success = true;
                    response.Message = string.Format("The entity relation '{0}' does not exist!", name);
                }
                return(response);
            }
            catch (Exception e)
            {
                response.Success = false;

                if (ErpSettings.DevelopmentMode)
                {
                    response.Message = e.Message + e.StackTrace;
                }

                return(response);
            }
        }