Beispiel #1
0
        private TypedRelations GetGenericRelations(RelationType relationType, Entity entity)
        {
            Expression<Func<Relation, bool>> relationSelector = r => r.SubjectiveEntityId == entity.Id;
            if (relationType.IsSymmetric)
            {
                relationSelector = r => r.SubjectiveEntityId == entity.Id || r.ObjectiveEntityId == entity.Id;
            }
            else if (relationType.IsInverse)
            {
                relationSelector = r => r.ObjectiveEntityId == entity.Id;
            }

            // The relationType.Id may be nagative in case of inverse relations, but it is positive in the database.
            IQueryable<Relation> relationsQuery = Db.Relations.Where (r => r.RelationTypeId == Math.Abs (relationType.Id));
            relationsQuery = relationsQuery.Where (relationSelector);

            // Retrieve all the relations and store them into the helper structure.
            Dictionary<long, List<Relation>> relationsByRelatedEntityIds = new Dictionary<long, List<Relation>> ();
            foreach (Relation relation in relationsQuery)
            {
                long relatedEntityId = relation.SubjectiveEntityId == entity.Id ? relation.ObjectiveEntityId : relation.SubjectiveEntityId;
                if (!relationsByRelatedEntityIds.ContainsKey (relatedEntityId))
                {
                    relationsByRelatedEntityIds[relatedEntityId] = new List<Relation> ();
                }
                relationsByRelatedEntityIds[relatedEntityId].Add (relation);
            }

            TypedRelations genericRelations = new TypedRelations (relationType);
            IEntityModel entityModel = RemoteFacade.Instance.GetEntityModel (relationType.ObjectiveEntityType, Db);
            foreach (Entity relatedEntity in entityModel.GetEntitiesByIds (relationsByRelatedEntityIds.Keys))
            {
                foreach (Relation relation in relationsByRelatedEntityIds[relatedEntity.Id])
                {
                    genericRelations.Relations[relation] = relatedEntity;
                }
            }
            return genericRelations;
        }
Beispiel #2
0
 public TypedRelations(RelationType relationType)
 {
     RelationType = relationType;
     Relations = new Dictionary<Relation, Entity> ();
 }
Beispiel #3
0
 private Relation GetGenericRelation(RelationType relationType, long relationId)
 {
     return Db.Relations.Where (r => r.Id == relationId && r.RelationTypeId == relationType.Id).FirstOrDefault ();
 }
Beispiel #4
0
 private void AddGenericRelationType(RelationType relationType)
 {
     if (!genericRelationTypes.ContainsKey (relationType.SubjectiveEntityType))
     {
         genericRelationTypes[relationType.SubjectiveEntityType] = new Dictionary<long, RelationType> ();
     }
     genericRelationTypes[relationType.SubjectiveEntityType][relationType.Id] = relationType;
 }
Beispiel #5
0
 private void AddGenericRelation(RelationType relationType, Entity entity, Entity relatedEntity)
 {
     Db.Relations.Add (new Relation ()
     {
         // The relationType may be inverse so the relation type id may be negative.
         RelationTypeId = Math.Abs (relationType.Id),
         SubjectiveEntity = relationType.IsInverse ? relatedEntity : entity,
         ObjectiveEntity = relationType.IsInverse ? entity : relatedEntity
     });
     Db.SaveChanges ();
 }
Beispiel #6
0
 public Entity GetNewObjectiveEntity(RelationType relationType, string identification)
 {
     IEntityModel entityModel = RemoteFacade.Instance.GetEntityModel (relationType.ObjectiveEntityType, Db);
     return entityModel.GetNewEntityWithIdentification (identification);
 }
Beispiel #7
0
        /// <summary>
        ///     Adds a new relation to the person.
        /// </summary>
        /// <param name="p">Person</param>
        /// <param name="division">Stb division.</param>
        /// <param name="comment">Comment</param>
        private void AddStbRelationToPerson(Person p, StbDivision division, string comment, RelationType type)
        {
            Relation r = new Relation ();
            r.SubjectiveEntity = division;
            r.RelationType = type;
            r.ObjectiveEntity = p;
            r.Note = comment;

            _ctx.Relations.Add (r);
        }