Example #1
0
        public void AddRelationType(IRelationType relationType)
        {
            AssertNotNull(relationType, "relationType");
            ValidateRelationType(relationType);

            _relationTypes[relationType.Name] = relationType;
        }
Example #2
0
 /// <summary>
 /// Constructor for reconstructing the entity from the data source
 /// </summary>
 /// <param name="parentId"></param>
 /// <param name="childId"></param>
 /// <param name="parentObjectType"></param>
 /// <param name="childObjectType"></param>
 /// <param name="relationType"></param>
 public Relation(int parentId, int childId, Guid parentObjectType, Guid childObjectType, IRelationType relationType)
 {
     _parentId        = parentId;
     _childId         = childId;
     _relationType    = relationType;
     ParentObjectType = parentObjectType;
     ChildObjectType  = childObjectType;
 }
Example #3
0
 /// <summary>
 /// Gets the entity identifier of the entity.
 /// </summary>
 /// <param name="entity">The entity.</param>
 /// <returns>The entity identifier of the entity.</returns>
 public static GuidUdi GetUdi(this IRelationType entity)
 {
     if (entity == null)
     {
         throw new ArgumentNullException("entity");
     }
     return(new GuidUdi(Constants.UdiEntityType.RelationType, entity.Key).EnsureClosed());
 }
Example #4
0
 /// <inheritdoc />
 public bool HasRelations(IRelationType relationType)
 {
     using (var scope = ScopeProvider.CreateCoreScope(autoComplete: true))
     {
         var query = Query <IRelation>().Where(x => x.RelationTypeId == relationType.Id);
         return(_relationRepository.Get(query)?.Any() ?? false);
     }
 }
Example #5
0
        public bool ExistCompositeRoles(IRelationType relationType)
        {
            this.AssertNotDeleted();
            HashSet <Strategy> roleStrategies;

            this.compositesRoleByRoleType.TryGetValue(relationType.RoleType, out roleStrategies);
            return(roleStrategies != null);
        }
 /// <summary>
 /// Checks whether any relations exists for the passed in <see cref="RelationType"/>.
 /// </summary>
 /// <param name="relationType"><see cref="RelationType"/> to check for relations</param>
 /// <returns>Returns <c>True</c> if any relations exists for the given <see cref="RelationType"/>, otherwise <c>False</c></returns>
 public bool HasRelations(IRelationType relationType)
 {
     using (var repository = RepositoryFactory.CreateRelationRepository(UowProvider.GetUnitOfWork()))
     {
         var query = new Query <IRelation>().Where(x => x.RelationTypeId == relationType.Id);
         return(repository.GetByQuery(query).Any());
     }
 }
Example #7
0
        public bool ExistUnitRole(IRelationType relationType)
        {
            this.AssertNotDeleted();
            var roleType = relationType.RoleType;

            this.session.MemoryDatabase.UnitRoleChecks(this, roleType);
            return(this.unitRoleByRoleType.ContainsKey(roleType));
        }
Example #8
0
 /// <inheritdoc />
 public bool AreRelated(int parentId, int childId, IRelationType relationType)
 {
     using (var scope = ScopeProvider.CreateCoreScope(autoComplete: true))
     {
         var query = Query <IRelation>().Where(x => x.ParentId == parentId && x.ChildId == childId && x.RelationTypeId == relationType.Id);
         return(_relationRepository.Get(query)?.Any() ?? false);
     }
 }
Example #9
0
 /// <summary>
 /// Initializes a new instance of the <see cref="RelationTypeManyXmlWriter"/> class.
 /// </summary>
 /// <param name="relationType">The relation type.</param>
 /// <param name="xmlWriter">The XML writer.</param>
 internal RelationTypeManyXmlWriter(IRelationType relationType, XmlWriter xmlWriter)
 {
     this.relationType          = relationType;
     this.xmlWriter             = xmlWriter;
     this.rolesStringBuilder    = new StringBuilder();
     this.previousAssociationId = -1;
     this.isClosed = false;
 }
 /// <summary>
 /// Checks whether two items are related with a given relation type
 /// </summary>
 /// <param name="parentId">Id of the Parent relation</param>
 /// <param name="childId">Id of the Child relation</param>
 /// <param name="relationTypeAlias">Type of relation</param>
 /// <returns>Returns <c>True</c> if any relations exists with the given Ids and relation type, otherwise <c>False</c></returns>
 public bool AreRelated(int parentId, int childId, IRelationType relationType)
 {
     using (var repository = RepositoryFactory.CreateRelationRepository(UowProvider.GetUnitOfWork()))
     {
         var query = new Query <IRelation>().Where(x => x.ParentId == parentId && x.ChildId == childId && x.RelationTypeId == relationType.Id);
         return(repository.GetByQuery(query).Any());
     }
 }
Example #11
0
        public virtual void RemoveCompositeRole(IRelationType relationType)
        {
            this.AssertExist();

            RoleAssertions.CompositeRoleChecks(this, relationType.RoleType);

            this.Roles.RemoveCompositeRole(relationType.RoleType);
        }
Example #12
0
        public virtual IObject GetCompositeAssociation(IRelationType relationType)
        {
            this.AssertExist();

            var association = this.Session.GetAssociation(this, relationType.AssociationType);

            return(association?.Strategy.GetObject());
        }
Example #13
0
        public virtual IObject GetCompositeRole(IRelationType relationType)
        {
            this.AssertExist();

            var role = this.Roles.GetCompositeRole(relationType.RoleType);

            return((role == null) ? null : this.Session.State.GetOrCreateReferenceForExistingObject(role.Value, this.Session).Strategy.GetObject());
        }
Example #14
0
        public virtual object GetUnitRole(IRelationType relationType)
        {
            this.AssertExist();

            var roleType = relationType.RoleType;

            return(this.Roles.GetUnitRole(roleType));
        }
 public RelationType(int id)
 {
     RelationTypeEntity = ApplicationContext.Current.Services.RelationService.GetRelationTypeById(id);
     if (RelationTypeEntity == null)
     {
         throw new NullReferenceException("No relation type found with id " + id);
     }
 }
Example #16
0
        public IObject GetCompositeAssociation(IRelationType relationType)
        {
            this.AssertNotDeleted();
            Strategy strategy;

            this.compositeAssociationByAssociationType.TryGetValue(relationType.AssociationType, out strategy);
            return(strategy?.GetObject());
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="RelationTypeManyXmlWriter"/> class.
 /// </summary>
 /// <param name="relationType">The relation type.</param>
 /// <param name="xmlWriter">The XML writer.</param>
 public RelationTypeManyXmlWriter(IRelationType relationType, XmlWriter xmlWriter)
 {
     this.relationType = relationType;
     this.xmlWriter = xmlWriter;
     this.rolesStringBuilder = new StringBuilder();
     this.previousAssociationId = -1;
     this.isClosed = false;
 }
Example #18
0
 public static IRelation SimpleMockedRelation(int id, int child, int parent, IRelationType relType)
 {
     return(Mock.Of <IRelation>(content =>
                                content.ChildId == child &&
                                content.ParentId == parent &&
                                content.Id == id &&
                                content.RelationType == relType &&
                                content.CreateDate == DateTime.Now));
 }
Example #19
0
        public bool ExistRole(IRelationType relationType)
        {
            if (relationType.RoleType.ObjectType is IUnit)
            {
                return(this.ExistUnitRole(relationType));
            }

            return(relationType.RoleType.IsMany ? this.ExistCompositeRoles(relationType) : this.ExistCompositeRole(relationType));
        }
Example #20
0
        public IObject GetCompositeRole(IRelationType relationType)
        {
            this.AssertNotDeleted();
            var roleType = relationType.RoleType;

            this.compositeRoleByRoleType.TryGetValue(roleType, out var strategy);

            return(strategy?.GetObject());
        }
Example #21
0
        public bool ExistCompositeAssociations(IRelationType relationType)
        {
            this.AssertNotDeleted();
            HashSet <Strategy> strategies;

            this.compositesAssociationByAssociationType.TryGetValue(relationType.AssociationType, out strategies);

            return(strategies != null);
        }
        private static IRelation DtoToEntity(RelationDto dto, IRelationType relationType)
        {
            var entity = RelationFactory.BuildEntity(dto, relationType);

            // reset dirty initial properties (U4-1946)
            entity.ResetDirtyProperties(false);

            return(entity);
        }
Example #23
0
        private void LoadCompositeRelations(XmlReader reader, IRelationType relationType)
        {
            while (reader.Read())
            {
                switch (reader.NodeType)
                {
                case XmlNodeType.Element:
                    if (reader.Name.Equals(Serialization.Relation))
                    {
                        var associationString = reader.GetAttribute(Serialization.Association);
                        var association       = new ObjectIdLong(long.Parse(associationString));

                        if (reader.IsEmptyElement)
                        {
                            this.OnRelationNotLoaded(relationType.Id, association.ToString(), null);
                        }
                        else
                        {
                            var value       = reader.ReadString();
                            var roleStrings = value.Split(Serialization.ObjectsSplitterCharArray);

                            if (relationType.RoleType.IsOne && roleStrings.Length > 1)
                            {
                                foreach (var roleString in roleStrings)
                                {
                                    this.OnRelationNotLoaded(relationType.Id, association.ToString(), roleString);
                                }
                            }

                            foreach (var roleString in roleStrings)
                            {
                                var role = new ObjectIdLong(long.Parse(roleString));
                                //this.OnRelationNotLoaded(relationType.Id, association.ToString(), r);

                                var cmdText = @"
INSERT INTO " + this.Mapping.GetTableName(relationType) + " (" + Mapping.ColumnNameForAssociation + "," + Mapping.ColumnNameForRole + @")
VALUES (" + Mapping.ParameterNameForAssociation + "," + Mapping.ParameterNameForRole + ")";

                                using (var command = this.CreateCommand(cmdText))
                                {
                                    command.Parameters.Add(Mapping.ParameterNameForAssociation, Mapping.DbTypeForId).Value = association.Value;
                                    command.Parameters.Add(Mapping.ParameterNameForRole, Mapping.DbTypeForId).Value        = role.Value;

                                    command.ExecuteNonQuery();
                                }
                            }
                        }
                    }

                    break;

                case XmlNodeType.EndElement:
                    return;
                }
            }
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="TableSchemaValidationError"/> class.
 /// </summary>
 /// <param name="objectType">The invalid object type.</param>
 /// <param name="relationType">The invalid relation type.</param>
 /// <param name="role">The invalid role.</param>
 /// <param name="tableName">Name of the table.</param>
 /// <param name="columnName">Name of the column.</param>
 /// <param name="errorKind">The kind of validation error.</param>
 /// <param name="message">The validation error message.</param>
 public TableSchemaValidationError(IObjectType objectType, IRelationType relationType, IRoleType role, string tableName, string columnName, SchemaValidationErrorKind errorKind, string message)
 {
     this.objectType   = objectType;
     this.relationType = relationType;
     this.role         = role;
     this.tableName    = tableName;
     this.columnName   = columnName;
     this.kind         = errorKind;
     this.message      = message;
 }
Example #25
0
        private Dictionary <long, long> GetAssociationIdByRoleId(IRelationType relationType)
        {
            if (!this.associationIdByRoleIdByRelationTypeId.TryGetValue(relationType, out var associationIdByRoleId))
            {
                associationIdByRoleId = new Dictionary <long, long>();
                this.associationIdByRoleIdByRelationTypeId[relationType] = associationIdByRoleId;
            }

            return(associationIdByRoleId);
        }
 // Umbraco.Code.MapAll -CreateDate -UpdateDate -DeleteDate
 private static void Map(RelationTypeSave source, IRelationType target, MapperContext context)
 {
     target.Alias           = source.Alias;
     target.ChildObjectType = source.ChildObjectType;
     target.Id = source.Id.TryConvertTo <int>().Result;
     target.IsBidirectional  = source.IsBidirectional;
     target.Key              = source.Key;
     target.Name             = source.Name;
     target.ParentObjectType = source.ParentObjectType;
 }
Example #27
0
        public object GetUnitRole(IRelationType relationType)
        {
            this.AssertNotDeleted();

            var roleType = relationType.RoleType;

            this.MemorySession.MemoryDatabase.UnitRoleChecks(this, roleType);

            return(this.GetInternalizedUnitRole(roleType));
        }
Example #28
0
 /// <inheritdoc />
 public IRelation GetByParentAndChildId(int parentId, int childId, IRelationType relationType)
 {
     using (var scope = ScopeProvider.CreateScope(autoComplete: true))
     {
         var query = Query <IRelation>().Where(x => x.ParentId == parentId &&
                                               x.ChildId == childId &&
                                               x.RelationTypeId == relationType.Id);
         return(_relationRepository.Get(query).FirstOrDefault());
     }
 }
Example #29
0
        private Dictionary <long, object> GetRoleByAssociationId(IRelationType relationType)
        {
            if (!this.roleByAssociationIdByRelationTypeId.TryGetValue(relationType, out var roleByAssociationId))
            {
                roleByAssociationId = new Dictionary <long, object>();
                this.roleByAssociationIdByRelationTypeId[relationType] = roleByAssociationId;
            }

            return(roleByAssociationId);
        }
Example #30
0
        public virtual bool ExistAssociation(IRelationType relationType)
        {
            var associationType = relationType.AssociationType;

            if (associationType.IsMany)
            {
                return(this.ExistCompositeAssociations(relationType));
            }

            return(this.ExistCompositeAssociation(relationType));
        }
Example #31
0
        public virtual object GetRole(IRelationType relationType)
        {
            if (relationType.RoleType.ObjectType.IsUnit)
            {
                return(this.GetUnitRole(relationType));
            }

            return(relationType.RoleType.IsMany
                       ? (object)this.GetCompositeRoles(relationType)
                       : this.GetCompositeRole(relationType));
        }
Example #32
0
        public virtual object GetAssociation(IRelationType relationType)
        {
            var associationType = relationType.AssociationType;

            if (associationType.IsMany)
            {
                return(this.GetCompositeAssociations(relationType));
            }

            return(this.GetCompositeAssociation(relationType));
        }
Example #33
0
 public Relation(int parentId, int childId, IRelationType relationType)
 {
     _parentId = parentId;
     _childId = childId;
     _relationType = relationType;
 }
Example #34
0
 public string GetTableName(IRelationType relationType)
 {
     string tableName;
     this.tableNameByRelationType.TryGetValue(relationType, out tableName);
     return tableName;
 }
Example #35
0
        protected void LoadCompositeRelations(IRelationType relationType, List<CompositeRelation> relations)
        {
            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");
                            }

                            if (this.reader.IsEmptyElement)
                            {
                                throw new Exception("Role is missing");
                            }

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

                            var value = this.reader.ReadString();
                            var rs = value.Split(Serialization.ObjectsSplitterCharArray);

                            if (associationConcreteClass == null ||
                                !this.database.ContainsConcreteClass(relationType.AssociationType.ObjectType, associationConcreteClass) ||
                                (relationType.RoleType.IsOne && rs.Length > 1))
                            {
                                foreach (var r in rs)
                                {
                                    this.OnRelationNotLoaded(relationType.Id, associationIdString, r);
                                }
                            }
                            else
                            {
                                foreach (var r in rs)
                                {
                                    var role = this.database.ObjectIds.Parse(r);
                                    IObjectType roleConcreteClass;
                                    this.objectTypeByObjectId.TryGetValue(role, out roleConcreteClass);

                                    if (roleConcreteClass == null ||
                                        !this.database.ContainsConcreteClass(relationType.RoleType.ObjectType, roleConcreteClass))
                                    {
                                        this.OnRelationNotLoaded(relationType.Id, associationIdString, r);
                                    }
                                    else
                                    {
                                        relations.Add(new CompositeRelation(association, role));
                                    }
                                }
                            }
                        }
                        else
                        {
                            throw new Exception("Unknown child element <" + this.reader.Name + "> in parent element <" + Serialization.RelationTypeComposite + ">");
                        }

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

                        return;
                }
            }
        }
Example #36
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;
                }
            }
        }
 public RelationType()
 {
     _type = new EmptyType();
 }
Example #38
0
 public string GetProcedureNameForGetAssociation(IRelationType relationType)
 {
     string procedureName;
     this.procedureNameForGetAssociationByRelationType.TryGetValue(relationType, out procedureName);
     return procedureName;
 }
 public RelationType(IRelationType type)
 {
     _type = type;
 }
        /// <summary>
        /// Deletes all <see cref="Relation"/> objects based on the passed in <see cref="RelationType"/>
        /// </summary>
        /// <param name="relationType"><see cref="RelationType"/> to Delete Relations for</param>
        public void DeleteRelationsOfType(IRelationType relationType)
        {
            var uow = _uowProvider.GetUnitOfWork();
            using (var repository = _repositoryFactory.CreateRelationRepository(uow))
            {
                var query = new Query<IRelation>().Where(x => x.RelationTypeId == relationType.Id);
                var list = repository.GetByQuery(query).ToList();

                foreach (var relation in list)
                {
                    repository.Delete(relation);
                }
                uow.Commit();
            }
        }
        /// <summary>
        /// Relates two objects that are based on the <see cref="IUmbracoEntity"/> interface.
        /// </summary>
        /// <param name="parent">Parent entity</param>
        /// <param name="child">Child entity</param>
        /// <param name="relationType">The type of relation to create</param>
        /// <returns>The created <see cref="Relation"/></returns>
        public IRelation Relate(IUmbracoEntity parent, IUmbracoEntity child, IRelationType relationType)
        {
            //Ensure that the RelationType has an indentity before using it to relate two entities
            if(relationType.HasIdentity == false)
                Save(relationType);

            var relation = new Relation(parent.Id, child.Id, relationType);
            var uow = _uowProvider.GetUnitOfWork();
            using (var repository = _repositoryFactory.CreateRelationRepository(uow))
            {
                repository.AddOrUpdate(relation);
                uow.Commit();

                return relation;
            }
        }
 /// <summary>
 /// Checks whether any relations exists for the passed in <see cref="RelationType"/>.
 /// </summary>
 /// <param name="relationType"><see cref="RelationType"/> to check for relations</param>
 /// <returns>Returns <c>True</c> if any relations exists for the given <see cref="RelationType"/>, otherwise <c>False</c></returns>
 public bool HasRelations(IRelationType relationType)
 {
     using (var repository = _repositoryFactory.CreateRelationRepository(_uowProvider.GetUnitOfWork()))
     {
         var query = new Query<IRelation>().Where(x => x.RelationTypeId == relationType.Id);
         return repository.GetByQuery(query).Any();
     }
 }
 /// <summary>
 /// Deletes a <see cref="RelationType"/>
 /// </summary>
 /// <param name="relationType">RelationType to Delete</param>
 public void Delete(IRelationType relationType)
 {
     var uow = _uowProvider.GetUnitOfWork();
     using (var repository = _repositoryFactory.CreateRelationTypeRepository(uow))
     {
         repository.Delete(relationType);
         uow.Commit();
     }
 }
Example #44
0
        private void LoadCompositeRelations(IRelationType relationType)
        {
            while (this.reader.Read())
            {
                switch (this.reader.NodeType)
                {
                    // eat everything but elements
                    case XmlNodeType.Element:
                        if (this.reader.Name.Equals(Serialization.Relation))
                        {
                            var associationId = this.session.ObjectIds.Parse(this.reader.GetAttribute(Serialization.Association));
                            var association = this.LoadInstantiateStrategy(associationId);

                            var value = string.Empty;
                            if (!this.reader.IsEmptyElement)
                            {
                                value = this.reader.ReadString();
                            }

                            if (!this.reader.IsEmptyElement)
                            {
                                var roleIdsString = value;
                                var roleIdStringArray = roleIdsString.Split(Serialization.ObjectsSplitterCharArray);

                                if (association == null ||
                                    !this.session.MemoryDatabase.ContainsConcreteClass(relationType.AssociationType.ObjectType, association.UncheckedObjectType) ||
                                    (relationType.RoleType.IsOne && roleIdStringArray.Length != 1))
                                {
                                    foreach (var roleId in roleIdStringArray)
                                    {
                                        this.session.MemoryDatabase.OnRelationNotLoaded(relationType.Id, associationId.ToString(), roleId);
                                    }
                                }
                                else
                                {
                                    if (relationType.RoleType.IsOne)
                                    {
                                        var roleIdString = this.session.ObjectIds.Parse(roleIdStringArray[0]);
                                        var role = this.LoadInstantiateStrategy(roleIdString);
                                        if (role == null || !this.session.MemoryDatabase.ContainsConcreteClass((IComposite)relationType.RoleType.ObjectType, role.UncheckedObjectType))
                                        {
                                            this.session.MemoryDatabase.OnRelationNotLoaded(relationType.Id, associationId.ToString(), roleIdStringArray[0]);
                                        }
                                        else
                                        {
                                            if (relationType.RoleType.AssociationType.IsMany)
                                            {
                                                association.SetCompositeRoleMany2One(relationType.RoleType, role.GetObject());
                                            }
                                            else
                                            {
                                                association.SetCompositeRoleOne2One(relationType.RoleType, role.GetObject());
                                            }
                                        }
                                    }
                                    else
                                    {
                                        var roleStrategies = new HashSet<Strategy>();
                                        foreach (var roleIdString in roleIdStringArray)
                                        {
                                            var roleId = this.session.ObjectIds.Parse(roleIdString);
                                            var role = this.LoadInstantiateStrategy(roleId);
                                            if (role == null || !this.session.MemoryDatabase.ContainsConcreteClass((IComposite)relationType.RoleType.ObjectType, role.UncheckedObjectType))
                                            {
                                                this.session.MemoryDatabase.OnRelationNotLoaded(relationType.Id, associationId.ToString(), roleId.ToString());
                                            }
                                            else
                                            {
                                                roleStrategies.Add(role);
                                            }
                                        }

                                        if (relationType.RoleType.AssociationType.IsMany)
                                        {
                                            association.SetCompositeRolesMany2Many(relationType.RoleType, roleStrategies);
                                        }
                                        else
                                        {
                                            association.SetCompositesRoleOne2Many(relationType.RoleType, roleStrategies);
                                        }
                                    }
                                }
                            }
                        }
                        else
                        {
                            throw new Exception("Unknown child element <" + this.reader.Name + "> in parent element <" + Serialization.RelationTypeComposite + ">");
                        }

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

                        return;
                }
            }
        }
Example #45
0
 private void ValidateRelationType(IRelationType type)
 {
     if (_relationTypes.ContainsKey(type.Name))
     {
         throw new InvalidRelationTypeException(type.Name, "Relation type with that name already registered.");
     }
     if (type.RoleInfos.Count == 0)
     {
         throw new InvalidRelationTypeException(type.Name, "Relation type contains no roles.");
     }
     foreach (RoleInfo info in type.RoleInfos)
     {
         if (info == null)
         {
             throw new InvalidRelationTypeException(type.Name, "Relation type contains null RoleInfo.");
         }
         foreach (RoleInfo i in type.RoleInfos)
         {
             if (!object.ReferenceEquals(i, info) && i.Name == info.Name)
             {
                 throw new InvalidRelationTypeException(type.Name, "Relation type contains two roles with same name.");
             }
         }
     }
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="RelationTypeOneXmlWriter"/> class.
 /// </summary>
 /// <param name="relationType">Type of the relation.</param>
 /// <param name="xmlWriter">The XML writer.</param>
 internal RelationTypeOneXmlWriter(IRelationType relationType, XmlWriter xmlWriter)
 {
     this.relationType = relationType;
     this.xmlWriter = xmlWriter;
     this.isClosed = false;
 }
Example #47
0
 public string GetProcedureNameForSetRole(IRelationType relationType)
 {
     string procedureName;
     this.procedureNameForSetRoleByRelationType.TryGetValue(relationType, out procedureName);
     return procedureName;
 }
Example #48
0
        private void LoadUnitRelations(IRelationType relationType)
        {
            while (this.reader.Read())
            {
                switch (this.reader.NodeType)
                {
                    // eat everything but elements
                    case XmlNodeType.Element:
                        if (this.reader.Name.Equals(Serialization.Relation))
                        {
                            var associationIdString = this.reader.GetAttribute(Serialization.Association);
                            var associationId = this.session.ObjectIds.Parse(associationIdString);
                            var strategy = this.LoadInstantiateStrategy(associationId);

                            var value = string.Empty;
                            if (!this.reader.IsEmptyElement)
                            {
                                value = this.reader.ReadString();
                            }

                            if (strategy == null)
                            {
                                this.session.MemoryDatabase.OnRelationNotLoaded(relationType.Id, associationIdString, value);
                            }
                            else
                            {
                                try
                                {
                                    this.session.MemoryDatabase.UnitRoleChecks(strategy, relationType.RoleType);
                                    if (this.reader.IsEmptyElement)
                                    {
                                        var unitType = (IUnit)relationType.RoleType.ObjectType;
                                        switch (unitType.UnitTag)
                                        {
                                            case UnitTags.AllorsString:
                                                strategy.SetUnitRole(relationType.RoleType, string.Empty);
                                                break;
                                            case UnitTags.AllorsBinary:
                                                strategy.SetUnitRole(relationType.RoleType, emptyByteArray);
                                                break;
                                        }
                                    }
                                    else
                                    {
                                        var unitType = (IUnit)relationType.RoleType.ObjectType;
                                        var unitTypeTag = unitType.UnitTag;

                                        var unit = Serialization.ReadString(value, unitTypeTag);
                                        strategy.SetUnitRole(relationType.RoleType, unit);
                                    }
                                }
                                catch
                                {
                                    this.session.MemoryDatabase.OnRelationNotLoaded(relationType.Id, associationId.ToString(), 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 #49
0
        private void LoadUnitRelations(XmlReader reader, IRelationType relationType)
        {
            while (reader.Read())
            {
                switch (reader.NodeType)
                {
                    case XmlNodeType.Element:
                        if (reader.Name.Equals(Serialization.Relation))
                        {
                            var associationString = reader.GetAttribute(Serialization.Association);
                            var association = ObjectIds.Parse(associationString);
                            if (reader.IsEmptyElement)
                            {
                                object role;

                                // OnRelationNotLoaded(relationType.Id, association.ToString(), String.Empty);
                                if (relationType.RoleType.ObjectType.Id == UnitIds.StringId)
                                {
                                    role = string.Empty;
                                }
                                else if (relationType.RoleType.ObjectType.Id == UnitIds.BinaryId)
                                {
                                    role = EmptyByteArray;
                                }
                                else
                                {
                                    this.OnRelationNotLoaded(relationType.Id, association.ToString(), string.Empty);
                                    continue;
                                }

                                var cmdText = @"
            INSERT INTO " + this.SchemaName + "." + this.Mapping.GetTableName(relationType) + " (" + Mapping.ColumnNameForAssociation + "," + Mapping.ColumnNameForRole + @")
            VALUES (" + Mapping.ParameterNameForAssociation + "," + Mapping.ParameterNameForRole + ")";

                                using (var command = this.CreateCommand(cmdText))
                                {
                                    command.Parameters.Add(Mapping.ParameterNameForAssociation, this.mapping.SqlDbTypeForObject).Value = association.Value;
                                    command.Parameters.Add(Mapping.ParameterNameForRole, this.mapping.GetSqlDbType(relationType.RoleType)).Value = role;

                                    command.ExecuteNonQuery();
                                }
                            }
                            else
                            {
                                var value = reader.ReadString();
                                try
                                {
                                    // TODO:
                                    // OnRelationNotLoaded(relationType.Id, association.ToString(), value);
                                    var unitTypeTag = ((IUnit)relationType.RoleType.ObjectType).UnitTag;
                                    var role = Serialization.ReadString(value, unitTypeTag);

                                    var cmdText = @"
            INSERT INTO " + this.SchemaName + "." + this.Mapping.GetTableName(relationType) + " (" + Mapping.ColumnNameForAssociation + "," + Mapping.ColumnNameForRole + @")
            VALUES (" + Mapping.ParameterNameForAssociation + "," + Mapping.ParameterNameForRole + ")";

                                    using (var command = this.CreateCommand(cmdText))
                                    {
                                        command.Parameters.Add(Mapping.ParameterNameForAssociation, this.mapping.SqlDbTypeForObject).Value = association.Value;
                                        command.Parameters.Add(Mapping.ParameterNameForRole, this.Mapping.GetSqlDbType(relationType.RoleType)).Value = role;

                                        command.ExecuteNonQuery();
                                    }
                                }
                                catch
                                {
                                    this.OnRelationNotLoaded(relationType.Id, association.ToString(), value);
                                }
                            }
                        }

                        break;
                    case XmlNodeType.EndElement:
                        return;
                }
            }
        }
Example #50
0
        private void LoadCompositeRelations(XmlReader reader, IRelationType relationType)
        {
            while (reader.Read())
            {
                switch (reader.NodeType)
                {
                    case XmlNodeType.Element:
                        if (reader.Name.Equals(Serialization.Relation))
                        {
                            var associationString = reader.GetAttribute(Serialization.Association);
                            var association = ObjectIds.Parse(associationString);

                            if (reader.IsEmptyElement)
                            {
                                this.OnRelationNotLoaded(relationType.Id, association.ToString(), null);
                            }
                            else
                            {
                                var value = reader.ReadString();
                                var roleStrings = value.Split(Serialization.ObjectsSplitterCharArray);

                                if (relationType.RoleType.IsOne && roleStrings.Length > 1)
                                {
                                    foreach (var roleString in roleStrings)
                                    {
                                        this.OnRelationNotLoaded(relationType.Id, association.ToString(), roleString);
                                    }
                                }

                                foreach (var roleString in roleStrings)
                                {
                                    var role = ObjectIds.Parse(roleString);
                                    //this.OnRelationNotLoaded(relationType.Id, association.ToString(), r);

                                    var cmdText = @"
            INSERT INTO " + this.SchemaName + "." + this.Mapping.GetTableName(relationType) + " (" + Mapping.ColumnNameForAssociation + "," + Mapping.ColumnNameForRole + @")
            VALUES (" + Mapping.ParameterNameForAssociation + "," + Mapping.ParameterNameForRole + ")";

                                    using (var command = this.CreateCommand(cmdText))
                                    {
                                        command.Parameters.Add(Mapping.ParameterNameForAssociation, this.mapping.SqlDbTypeForObject).Value = association.Value;
                                        command.Parameters.Add(Mapping.ParameterNameForRole, this.mapping.SqlDbTypeForObject).Value = role.Value;

                                        command.ExecuteNonQuery();
                                    }
                                }
                            }
                        }

                        break;
                    case XmlNodeType.EndElement:
                        return;
                }
            }
        }
Example #51
0
 private void CreateIndex(SqlConnection connection, string indexName, IRelationType relationType, string tableName)
 {
     var sql = new StringBuilder();
     sql.Append("CREATE INDEX " + indexName + "\n");
     sql.Append("ON " + tableName + " (" + this.mapping.ColumnNameByRelationType[relationType] + ")");
     using (var command = new SqlCommand(sql.ToString(), connection))
     {
         command.ExecuteNonQuery();
     }
 }