Esempio 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);
            }
        }