protected void Check() { string entity; string relatedEntity; if (this is ManyToManyRelationship) { ManyToManyRelationship manyToManyRelationship = this as ManyToManyRelationship; entity = manyToManyRelationship.OneToManyRelationship.Entity; relatedEntity = manyToManyRelationship.ManyToOneRelationship.RelatedEntity; if (manyToManyRelationship.OneToManyRelationship.RelatedEntity != manyToManyRelationship.ManyToOneRelationship.Entity) { throw new SchemaException(SchemaMessages.RelationshipsNonConnected); } } else if (this is PlainRelationship) { PlainRelationship plainRelationship = this as PlainRelationship; entity = plainRelationship.DirectRelationships[0].Entity; relatedEntity = plainRelationship.DirectRelationships[plainRelationship.DirectRelationships.Length - 1].RelatedEntity; } else //if (this is Relationship) { entity = this.DirectRelationships[0].Entity; relatedEntity = this.DirectRelationships[this.DirectRelationships.Length - 1].RelatedEntity; } if (Entity != entity) { throw new SchemaException(string.Format(SchemaMessages.ConflictingEntities, Entity, entity)); } if (RelatedEntity != relatedEntity) { throw new SchemaException(string.Format(SchemaMessages.ConflictingRelatedEntities, RelatedEntity, relatedEntity)); } }
private static PlainRelationship Reverse(PlainRelationship relationship) { string entity = relationship.RelatedEntity; string relatedEntity = relationship.Entity; List <DirectRelationship> relationships = new List <DirectRelationship>(); for (int i = relationship.DirectRelationships.Length - 1; i >= 0; i--) { relationships.Add(relationship.DirectRelationships[i].Reverse()); } if (relationship is ManyToOneRelationship) { return(new OneToManyRelationship(entity, relatedEntity, relationships)); } else if (relationship is OneToManyRelationship) { return(new ManyToOneRelationship(entity, relatedEntity, relationships)); } else if (relationship is OneToOneRelationship) { return(new OneToOneRelationship(entity, relatedEntity, relationships)); } else { throw new NotSupportedException(relationship.GetType().ToString()); // never } }
private static void Connect(PlainRelationship relationship, List <PlainRelationship> decreasing, List <PlainRelationship> list) { PlainRelationship prev = decreasing.FirstOrDefault(r => r.RelatedEntity == relationship.Entity); if (prev == null) { PlainRelationship rel = decreasing.FirstOrDefault(r => r.Entity == relationship.Entity); if (rel is OneToOneRelationship) { prev = (PlainRelationship)rel.Reverse(); decreasing.Remove(rel); } } else { decreasing.Remove(prev); } if (prev != null) { int index = list.IndexOf(relationship); list.Insert(index, prev); Connect(prev, decreasing, list); } PlainRelationship next = decreasing.FirstOrDefault(r => r.Entity == relationship.RelatedEntity); if (next == null) { PlainRelationship rel = decreasing.FirstOrDefault(r => r.RelatedEntity == relationship.RelatedEntity); if (rel is OneToOneRelationship) { next = (PlainRelationship)rel.Reverse(); decreasing.Remove(rel); } } else { decreasing.Remove(next); } if (next != null) { list.Add(next); Connect(next, decreasing, list); } }
protected static IEnumerable <PlainRelationship> Connect(IEnumerable <PlainRelationship> relationships, string relationship) { List <PlainRelationship> decreasing = new List <PlainRelationship>(relationships); List <PlainRelationship> list = new List <PlainRelationship>(); PlainRelationship first = decreasing.First(); decreasing.Remove(first); list.Add(first); Connect(first, decreasing, list); if (list.Count < relationships.Count()) { throw new SchemaException(string.Format(SchemaMessages.RelationshipsNonConnected, relationship)); } return(list); }
private static XElement PlainRelationshipToXml(PlainRelationship relationship) { XElement relationshipSchema = new XElement(SchemaVocab.Relationship); relationshipSchema.SetAttributeValue(SchemaVocab.Name, relationship.Name); string type; if (relationship is ManyToOneRelationship) { type = SchemaVocab.ManyToOne; } else if (relationship is OneToManyRelationship) { type = SchemaVocab.OneToMany; } else if (relationship is OneToOneRelationship) { type = SchemaVocab.OneToOne; } else { throw new NotSupportedException(relationship.GetType().ToString()); // never } relationshipSchema.SetAttributeValue(SchemaVocab.Type, type); relationshipSchema.SetAttributeValue(SchemaVocab.Entity, relationship.Entity); relationshipSchema.SetAttributeValue(SchemaVocab.RelatedEntity, relationship.RelatedEntity); if (relationship.DirectRelationships.Length == 1) { DirectRelationship rel = relationship.DirectRelationships[0]; for (int i = 0; i < rel.Properties.Length; i++) { XElement xProperty = new XElement(SchemaVocab.Property); xProperty.SetAttributeValue(SchemaVocab.Name, rel.Properties[i]); xProperty.SetAttributeValue(SchemaVocab.RelatedProperty, rel.RelatedProperties[i]); relationshipSchema.Add(xProperty); } } else { foreach (DirectRelationship rel in relationship.DirectRelationships) { XElement relSchema = new XElement(SchemaVocab.Relationship); string relType; if (rel is ManyToOneDirectRelationship) { relType = SchemaVocab.ManyToOne; } else if (rel is OneToManyDirectRelationship) { relType = SchemaVocab.OneToMany; } else if (rel is OneToOneDirectRelationship) { relType = SchemaVocab.OneToOne; } else { throw new NotSupportedException(rel.GetType().ToString()); // never } relSchema.SetAttributeValue(SchemaVocab.Type, relType); relSchema.SetAttributeValue(SchemaVocab.Entity, rel.Entity); relSchema.SetAttributeValue(SchemaVocab.RelatedEntity, rel.RelatedEntity); for (int i = 0; i < rel.Properties.Length; i++) { XElement xProperty = new XElement(SchemaVocab.Property); xProperty.SetAttributeValue(SchemaVocab.Name, rel.Properties[i]); xProperty.SetAttributeValue(SchemaVocab.RelatedProperty, rel.RelatedProperties[i]); relSchema.Add(xProperty); } relationshipSchema.Add(relSchema); } } return(relationshipSchema); }