Example #1
0
        protected void LoadUnitRelations(IRelationType relationType, Dictionary<IObjectType, List<UnitRelation>> relationsByExclusiveRootClass)
        {
            while (this.reader.Read())
            {
                switch (this.reader.NodeType)
                {
                    case XmlNodeType.Element:
                        if (this.reader.Name.Equals(Serialization.Relation))
                        {
                            var associationIdString = this.reader.GetAttribute(Serialization.Association);
                            if (string.IsNullOrEmpty(associationIdString))
                            {
                                throw new Exception("Association id is missing");
                            }

                            var associationId = this.database.ObjectIds.Parse(associationIdString);
                            IObjectType associationConcreteClass;
                            this.objectTypeByObjectId.TryGetValue(associationId, out associationConcreteClass);

                            if (this.reader.IsEmptyElement)
                            {
                                if (associationConcreteClass == null ||
                                    !this.database.ContainsConcreteClass(relationType.AssociationType.ObjectType, associationConcreteClass))
                                {
                                    this.OnRelationNotLoaded(relationType.Id, associationIdString, string.Empty);
                                }
                                else
                                {
                                    var exclusiveRootClass = associationConcreteClass;
                                    switch (((IUnit)relationType.RoleType.ObjectType).UnitTag)
                                    {
                                        case UnitTags.AllorsString:
                                            {
                                                List<UnitRelation> relations;
                                                if (
                                                    !relationsByExclusiveRootClass.TryGetValue(associationConcreteClass, out relations))
                                                {
                                                    relations = new List<UnitRelation>();
                                                    relationsByExclusiveRootClass[exclusiveRootClass] = relations;
                                                }

                                                var unitRelation = new UnitRelation(associationId, string.Empty);
                                                relations.Add(unitRelation);
                                            }

                                            break;

                                        case UnitTags.AllorsBinary:
                                            {
                                                List<UnitRelation> relations;
                                                if (!relationsByExclusiveRootClass.TryGetValue(associationConcreteClass, out relations))
                                                {
                                                    relations = new List<UnitRelation>();
                                                    relationsByExclusiveRootClass[exclusiveRootClass] = relations;
                                                }

                                                var unitRelation = new UnitRelation(associationId, EmptyByteArray);
                                                relations.Add(unitRelation);
                                            }

                                            break;

                                        default:
                                            this.OnRelationNotLoaded(relationType.Id, associationIdString, string.Empty);
                                            break;
                                    }
                                }
                            }
                            else
                            {
                                var value = this.reader.ReadString();
                                if (associationConcreteClass == null ||
                                   !this.database.ContainsConcreteClass(relationType.AssociationType.ObjectType, associationConcreteClass))
                                {
                                    this.OnRelationNotLoaded(relationType.Id, associationIdString, value);
                                }
                                else
                                {
                                    try
                                    {
                                        var exclusiveRootClass = (IComposite)associationConcreteClass;
                                        var unitTypeTag = ((IUnit)relationType.RoleType.ObjectType).UnitTag;
                                        var unit = Serialization.ReadString(value, unitTypeTag);

                                        List<UnitRelation> relations;
                                        if (!relationsByExclusiveRootClass.TryGetValue(associationConcreteClass, out relations))
                                        {
                                            relations = new List<UnitRelation>();
                                            relationsByExclusiveRootClass[exclusiveRootClass] = relations;
                                        }

                                        var unitRelation = new UnitRelation(associationId, unit);
                                        relations.Add(unitRelation);
                                    }
                                    catch
                                    {
                                        this.OnRelationNotLoaded(relationType.Id, associationIdString, value);
                                    }
                                }
                            }
                        }
                        else
                        {
                            throw new Exception("Unknown child element <" + this.reader.Name + "> in parent element <" + Serialization.RelationTypeUnit + ">");
                        }

                        break;
                    case XmlNodeType.EndElement:
                        if (!this.reader.Name.Equals(Serialization.RelationTypeUnit))
                        {
                            throw new Exception("Expected closing element </" + Serialization.RelationTypeUnit + ">");
                        }

                        return;
                }
            }
        }
Example #2
0
        internal void SetUnitRole(Reference association, IRoleType roleType, object role)
        {
            if (this.setUnitRoleRelationsByRoleTypeByExclusiveClass == null)
            {
                this.setUnitRoleRelationsByRoleTypeByExclusiveClass = new Dictionary<IClass, Dictionary<IRoleType, List<UnitRelation>>>();
            }

            var exclusiveClass = association.Class.ExclusiveClass;

            Dictionary<IRoleType, List<UnitRelation>> setUnitRoleRelationsByRoleType;
            if (!this.setUnitRoleRelationsByRoleTypeByExclusiveClass.TryGetValue(exclusiveClass, out setUnitRoleRelationsByRoleType))
            {
                setUnitRoleRelationsByRoleType = new Dictionary<IRoleType, List<UnitRelation>>();
                this.setUnitRoleRelationsByRoleTypeByExclusiveClass[exclusiveClass] = setUnitRoleRelationsByRoleType;
            }

            List<UnitRelation> relations;
            if (!setUnitRoleRelationsByRoleType.TryGetValue(roleType, out relations))
            {
                relations = new List<UnitRelation>();
                setUnitRoleRelationsByRoleType[roleType] = relations;
            }

            var unitRelation = new UnitRelation(association.ObjectId, role);
            relations.Add(unitRelation);

            if (relations.Count > BatchSize)
            {
                this.session.SetUnitRole(relations, exclusiveClass, roleType);
                relations.Clear();
            }
        }