/// <summary> /// /// </summary> /// <param name="relationType"></param> /// <param name="contentNodeId">id sourced from the Content / Media / Member</param> /// <param name="pickerNodeId">id sourced from the MultiPicker</param> /// <param name="reverseIndexing">if true, reverses the parentId and child Id</param> /// <param name="instanceIdentifier">JSON string with id of MultiPicker Relations property instance</param> private static void CreateRelation(RelationType relationType, int contentNodeId, int pickerNodeId, bool reverseIndexing, string instanceIdentifier) { if (reverseIndexing) { Relation.MakeNew(pickerNodeId, contentNodeId, relationType, instanceIdentifier); } else { Relation.MakeNew(contentNodeId, pickerNodeId, relationType, instanceIdentifier); } }
/// <summary> /// Check to see if the content id side of the relation type is valid (doesn't check the node picker id side) /// </summary> /// <param name="contextUmbracoObjectType">Type of the content object (content/ media or member)</param> /// <param name="relationType">Type of the relation.</param> /// <returns> /// <c>true</c> if [is content object type valid] [the specified content object type]; otherwise, <c>false</c>. /// </returns> public bool IsContextUmbracoObjectTypeValid(uQuery.UmbracoObjectType contextUmbracoObjectType, RelationType relationType) { bool isContextObjectTypeValid = false; if (!relationType.Dual && this.options.ReverseIndexing) { // expects the current context to be the child in the relation if (contextUmbracoObjectType == relationType.GetChildUmbracoObjectType()) { isContextObjectTypeValid = true; } } else { // expects the current context to be the parent in the relation if (contextUmbracoObjectType == relationType.GetParentUmbracoObjectType()) { isContextObjectTypeValid = true; } } return isContextObjectTypeValid; }
/// <summary> /// returns the UmbracoObjectType associated as defined by the supplied relation type, and if reverse indexing has been enabled /// </summary> /// <param name="relationType">associated RealationType</param> /// <returns></returns> public uQuery.UmbracoObjectType GetPickerUmbracoObjectType(RelationType relationType) { uQuery.UmbracoObjectType pickerUmbracoObjectType = uQuery.UmbracoObjectType.Unknown; if (!relationType.Dual && this.options.ReverseIndexing) { pickerUmbracoObjectType = relationType.GetParentUmbracoObjectType(); } else { pickerUmbracoObjectType = relationType.GetChildUmbracoObjectType(); } return pickerUmbracoObjectType; }
/// <summary> /// returns a string "Property '[propertyAlias]' with RelationType '[relationTypeName]" /// </summary> /// <returns></returns> private string GetMappingDetails() { string mappingDetails = string.Empty; UmbracoContent currentContentNode = new UmbracoContent(this.CurrentContentId); Property pickerProperty = currentContentNode.getProperty(this.options.PropertyAlias); if (pickerProperty != null) { RelationType relationType = new RelationType(this.options.RelationTypeId); // Does it still exist ? TODO: check if (this.IsContextUmbracoObjectTypeValid(this.CurrentContextObjectType, relationType)) { mappingDetails = "Property '<strong>" + pickerProperty.PropertyType.Name + "</strong>' with " + "Relation Type '<strong>" + relationType.Name + "</strong>'"; if(this.options.ReverseIndexing) { mappingDetails += " <i>(Reverse Index)</i>"; } } else { throw new Exception("Conflict with this Content Object Type and that expected by the Relation Type '" + relationType.Name + "'"); } } else { throw new Exception("Can't find a Property with the Alias '" + this.options.PropertyAlias + "'"); } return mappingDetails; }
/// <summary> /// Delete all relations using the content node for a given RelationType /// </summary> /// <param name="relationType"></param> /// <param name="contentNodeId"></param> /// <param name="reverseIndexing"></param> /// <param name="instanceIdentifier">NOT USED ATM</param> private static void DeleteRelations(RelationType relationType, int contentNodeId, bool reverseIndexing, string instanceIdentifier) { //if relationType is bi-directional or a reverse index then we can't get at the relations via the API, so using SQL string getRelationsSql = "SELECT id FROM umbracoRelation WHERE relType = " + relationType.Id.ToString() + " AND "; if (reverseIndexing || relationType.Dual) { getRelationsSql += "childId = " + contentNodeId.ToString(); } if (relationType.Dual) // need to return relations where content node id is used on both sides { getRelationsSql += " OR "; } if (!reverseIndexing || relationType.Dual) { getRelationsSql += "parentId = " + contentNodeId.ToString(); } getRelationsSql += " AND comment = '" + instanceIdentifier + "'"; using (IRecordsReader relations = uQuery.SqlHelper.ExecuteReader(getRelationsSql)) { //clear data Relation relation; while (relations.Read()) { relation = new Relation(relations.GetInt("id")); // TODO: [HR] check to see if an instance identifier is used relation.Delete(); } } }
/// <summary> /// Return all relation types /// </summary> /// <returns></returns> public static IEnumerable<RelationType> GetAll() { var relationTypes = new List<RelationType>(); using (IRecordsReader dr = SqlHelper.ExecuteReader("SELECT id, [dual], name, alias FROM umbracoRelationType")) { while (dr.Read()) { var rt = new RelationType(); rt.PopulateFromReader(dr); relationTypes.Add(rt); } } return relationTypes; }
public Relation(int Id) { using (IRecordsReader dr = SqlHelper.ExecuteReader("select * from umbracoRelation where id = @id", SqlHelper.CreateParameter("@id", Id))) { if (dr.Read()) { this._id = dr.GetInt("id"); this._parentNode = new CMSNode(dr.GetInt("parentId")); this._childNode = new CMSNode(dr.GetInt("childId")); this._relType = RelationType.GetById(dr.GetInt("relType")); this._comment = dr.GetString("comment"); this._datetime = dr.GetDateTime("datetime"); } else { throw new ArgumentException("No relation found for id " + Id.ToString()); } } }
public static Relation[] GetRelations(int NodeId, RelationType Filter) { System.Collections.ArrayList tmp = new System.Collections.ArrayList(); using (IRecordsReader dr = SqlHelper.ExecuteReader("select umbracoRelation.id from umbracoRelation inner join umbracoRelationType on umbracoRelationType.id = umbracoRelation.relType and umbracoRelationType.id = @relTypeId where umbracoRelation.parentId = @id or (umbracoRelation.childId = @id and umbracoRelationType.[dual] = 1)", SqlHelper.CreateParameter("@id", NodeId), SqlHelper.CreateParameter("@relTypeId", Filter.Id))) { while(dr.Read()) { tmp.Add(dr.GetInt("id")); } } Relation[] retval = new Relation[tmp.Count]; for (int i = 0; i < tmp.Count; i++) retval[i] = new Relation((int)tmp[i]); return retval; }
public static bool IsRelated(int ParentID, int ChildId, RelationType Filter) { int count = SqlHelper.ExecuteScalar<int>("SELECT count(*) FROM umbracoRelation WHERE childId = @childId AND parentId = @parentId AND relType = @relType", SqlHelper.CreateParameter("@childId", ChildId), SqlHelper.CreateParameter("@parentId", ParentID), SqlHelper.CreateParameter("@relType", Filter.Id) ); return (count > 0); }
public static Relation MakeNew(int ParentId, int ChildId, RelationType RelType, string Comment) { // The method is synchronized SqlHelper.ExecuteNonQuery("INSERT INTO umbracoRelation (childId, parentId, relType, comment) VALUES (@childId, @parentId, @relType, @comment)", SqlHelper.CreateParameter("@childId", ChildId), SqlHelper.CreateParameter("@parentId", ParentId), SqlHelper.CreateParameter("@relType", RelType.Id), SqlHelper.CreateParameter("@comment", Comment)); return new Relation(SqlHelper.ExecuteScalar<int>("SELECT MAX(id) FROM umbracoRelation")); }
/// <summary> /// On Load event /// </summary> /// <param name="sender">this aspx page</param> /// <param name="e">EventArgs (expect empty)</param> protected void Page_Load(object sender, EventArgs e) { int id; if (int.TryParse(Request.QueryString["id"], out id)) { this.relationType = new RelationType(id); if (this.relationType != null) { // API doens't allow us to pull the object Type, so sql needed // this.parentObjectType = UmbracoHelper.GetUmbracoObjectType(uQuery.SqlHelper.ExecuteScalar<Guid>("SELECT parentObjectType FROM umbracoRelationType WHERE id = " + this.relationType.Id.ToString())); // this.childObjectType = UmbracoHelper.GetUmbracoObjectType(uQuery.SqlHelper.ExecuteScalar<Guid>("SELECT childObjectType FROM umbracoRelationType WHERE id = " + this.relationType.Id.ToString())); // uQuery has the above in a helper method, so no sql needed now this.parentObjectType = this.relationType.GetParentUmbracoObjectType(); this.childObjectType = this.relationType.GetChildUmbracoObjectType(); // ----------- if (!this.IsPostBack) { this.EnsureChildControls(); this.idLiteral.Text = this.relationType.Id.ToString(); this.nameTextBox.Text = this.relationType.Name; this.aliasTextBox.Text = this.relationType.Alias; if (this.relationType.Dual) { this.dualRadioButtonList.Items.FindByValue("1").Selected = true; } else { this.dualRadioButtonList.Items.FindByValue("0").Selected = true; } this.parentLiteral.Text = this.parentObjectType.GetFriendlyName(); // UmbracoHelper.GetFriendlyName(this.parentObjectType); this.childLiteral.Text = this.childObjectType.GetFriendlyName(); // UmbracoHelper.GetFriendlyName(this.childObjectType); this.relationsCountLiteral.Text = this.Relations.Count.ToString(); this.relationsRepeater.DataSource = this.Relations; this.relationsRepeater.DataBind(); } } else { throw new Exception("Unable to get RelationType where ID = " + id.ToString()); } } else { throw new Exception("Invalid RelationType ID"); } }
/// <summary> /// Called by the ASP.NET page framework to notify server controls that use composition-based implementation to create any child controls they contain in preparation for posting back or rendering. /// </summary> protected override void CreateChildControls() { HtmlGenericControl ul = new HtmlGenericControl("ul"); ul.Attributes.Add("style", "list-style-type:none"); RelationType relationType = new RelationType(this.options.RelationTypeId); if (relationType != null) { foreach (Relation relation in relationType.GetRelations(this.CurrentContentId)) { // this id could be a parent or child (or only the parent if is a one way relation) if (relation.Parent.Id == this.CurrentContentId) { ul.Controls.Add(BuildLinkToRelated(relation.Child)); } else { ul.Controls.Add(BuildLinkToRelated(relation.Parent)); } } } this.Controls.Add(ul); }
public static Relation[] GetRelations(int NodeId, RelationType Filter) { return(ApplicationContext.Current.Services.RelationService.GetByParentOrChildId(NodeId, Filter.RelationTypeEntity.Alias) .Select(x => new Relation(x)) .ToArray()); }
public static bool IsRelated(int ParentID, int ChildId, RelationType Filter) { return(ApplicationContext.Current.Services.RelationService.AreRelated(ParentID, ChildId, Filter.Alias)); }