/*========================================================================================================================== | METHOD: SET TOPIC \-------------------------------------------------------------------------------------------------------------------------*/ /// <summary> /// Looks up a topic; if it doesn't exist, creates it. /// </summary> /// <param name="parentTopic">The topic's <see cref="Topic.Parent"/>.</param> /// <param name="key">The topic's <see cref="Topic.Key"/>.</param> /// <param name="contentType">The topic's <see cref="Topic.ContentType"/> key.</param> /// <returns>The configured topic object.</returns> Topic SetTopic(Topic parentTopic, string key, string contentType) { /*------------------------------------------------------------------------------------------------------------------------ | Validate input \-----------------------------------------------------------------------------------------------------------------------*/ Contract.Requires<ArgumentNullException>(parentTopic != null, "The parent topic must be specified."); Topic.ValidateKey(key); Topic topic = null; if (!parentTopic.Contains(key)) { /*---------------------------------------------------------------------------------------------------------------------- | Create a strongly-typed ContentType object if the contentType key is set to "ContentType" \---------------------------------------------------------------------------------------------------------------------*/ if (contentType.Equals("ContentType")) { topic = new ContentType(); } /*---------------------------------------------------------------------------------------------------------------------- | Create a strongly-typed Attribute object if the contentType key is set to "Attribute" \---------------------------------------------------------------------------------------------------------------------*/ else if (contentType.Equals("Attribute")) { topic = new Attribute(); } /*---------------------------------------------------------------------------------------------------------------------- | Create a strongly-typed ContentType object if the contentType key is set to "ContentType" \---------------------------------------------------------------------------------------------------------------------*/ else { topic = Topic.Create(key, contentType); } /*---------------------------------------------------------------------------------------------------------------------- | Set the primary topic properties (Key, ContentType, and Parent) \---------------------------------------------------------------------------------------------------------------------*/ topic.Key = key; topic.Attributes.Set("Key", key); topic.ContentType = null; topic.Attributes.Set("ContentType", contentType); topic.Parent = parentTopic; topic.Attributes.Set("ParentID", parentTopic.Id.ToString()); } else { /*---------------------------------------------------------------------------------------------------------------------- | Update the primary topic properties (Key, ContentType, and Parent) \---------------------------------------------------------------------------------------------------------------------*/ topic = parentTopic[key]; topic.Key = key; topic.Attributes.Set("Key", key); topic.ContentType = null; topic.Attributes.Set("ContentType", contentType); topic.Attributes.Set("ParentID", parentTopic.Id.ToString()); } return parentTopic[key]; }
/*========================================================================================================================== | METHOD: SET ATTRIBUTE REFERENCE \-------------------------------------------------------------------------------------------------------------------------*/ /// <summary> /// Looks up an Topic (assumed to be an attribute) from the provided Topic collection, finds the attribute with the /// associated name, and creates a Topic Pointer that points to that attribute. /// </summary> /// <param name="contentType">The <see cref="ContentType"/>for the attribute topic.</param> /// <param name="attributes">The collection of attributes for the attribute topic.</param> /// <param name="key">The <see cref="Topic.Key"/> for the attribute to be referenced.</param> /// <returns>The topic object for the referencing topic.</returns> Topic SetAttributeReference(Topic contentType, Topic attributes, string key) { /*------------------------------------------------------------------------------------------------------------------------ | Validate input \-----------------------------------------------------------------------------------------------------------------------*/ Contract.Requires<ArgumentNullException>(attributes != null, "The attributes topic must be specified."); Contract.Requires<ArgumentNullException>(contentType != null, "The contentTYpe topic must be specified."); Contract.Requires<ArgumentNullException>(String.IsNullOrWhiteSpace(key), "The key must be specified."); Topic.ValidateKey(key); if (!attributes.Contains(key)) { throw new Exception("The attribute with the key '" + key + "' does not exist in the '" + attributes.Key + "' Topic."); } /*------------------------------------------------------------------------------------------------------------------------ | Establish the derived and target attributes \-----------------------------------------------------------------------------------------------------------------------*/ Topic attribute = attributes[key]; Topic attributeReference = SetTopic(contentType, attribute.Key, attribute.Attributes.Get("ContentType")); /*------------------------------------------------------------------------------------------------------------------------ | Set the attribute reference/derivation \-----------------------------------------------------------------------------------------------------------------------*/ attributeReference.DerivedTopic = attribute; /*------------------------------------------------------------------------------------------------------------------------ | Return referencing attribute \-----------------------------------------------------------------------------------------------------------------------*/ return attributeReference; }