public void SaveRelations(ContentRelationsDto contentRelations) { if (contentRelations.Sets == null || !contentRelations.Sets.Any()) { return; } var relations = relationService.GetByParentId(contentRelations.ParentId).ToList(); var parentEntity = entityService.Get(contentRelations.ParentId, contentRelations.ParentType); foreach (var set in contentRelations.Sets) { var typeId = set.RelationTypeId; var type = relationService.GetRelationTypeById(set.RelationTypeId); var setRelations = relations.Where(r => r.RelationTypeId == typeId); foreach (var removeRelation in setRelations) { relationService.Delete(removeRelation); } foreach (var relation in set.Relations) { if (relation.State == RelationStateEnum.Deleted || relation.Readonly) { continue; } var childEntity = entityService.Get(relation.ChildId, UmbracoObjectTypesExtensions.GetUmbracoObjectType(type.ChildObjectType)); relationService.Relate(parentEntity, childEntity, type); } } }
/// <summary> /// Gets the Child objects from a list of Relations as a list of <see cref="IUmbracoEntity"/> objects. /// </summary> /// <param name="relations">List of relations to retrieve child objects from</param> /// <param name="loadBaseType">Optional bool to load the complete object graph when set to <c>False</c></param> /// <returns>An enumerable list of <see cref="IUmbracoEntity"/></returns> public IEnumerable <IUmbracoEntity> GetChildEntitiesFromRelations(IEnumerable <IRelation> relations, bool loadBaseType = false) { foreach (var relation in relations) { var objectType = UmbracoObjectTypesExtensions.GetUmbracoObjectType(relation.RelationType.ChildObjectType); yield return(_entityService.Get(relation.ChildId, objectType, loadBaseType)); } }
/// <summary> /// Gets the Parent objects from a list of Relations as a list of <see cref="IUmbracoEntity"/> objects. /// </summary> /// <param name="relations">List of relations to retrieve parent objects from</param> /// <returns>An enumerable list of <see cref="IUmbracoEntity"/></returns> public IEnumerable <IUmbracoEntity> GetParentEntitiesFromRelations(IEnumerable <IRelation> relations) { foreach (var relation in relations) { var objectType = UmbracoObjectTypesExtensions.GetUmbracoObjectType(relation.RelationType.ParentObjectType); yield return(_entityService.Get(relation.ParentId, objectType)); } }
/// <summary> /// Gets the UmbracoObjectType from the integer id of an IUmbracoEntity. /// </summary> /// <param name="id">Id of the entity</param> /// <returns><see cref="UmbracoObjectTypes"/></returns> public virtual UmbracoObjectTypes GetObjectType(int id) { using (var uow = _uowProvider.GetUnitOfWork()) { var sql = new Sql().Select("nodeObjectType").From <NodeDto>().Where <NodeDto>(x => x.NodeId == id); var nodeObjectTypeId = uow.Database.ExecuteScalar <string>(sql); var objectTypeId = new Guid(nodeObjectTypeId); return(UmbracoObjectTypesExtensions.GetUmbracoObjectType(objectTypeId)); } }
/// <summary> /// Gets the UmbracoObjectType from the integer id of an IUmbracoEntity. /// </summary> /// <param name="key">Unique Id of the entity</param> /// <returns><see cref="UmbracoObjectTypes"/></returns> public virtual UmbracoObjectTypes GetObjectType(Guid key) { using (var uow = UowProvider.GetUnitOfWork(readOnly: true)) { var sql = new Sql().Select("nodeObjectType").From <NodeDto>().Where <NodeDto>(x => x.UniqueId == key); var nodeObjectTypeId = uow.Database.ExecuteScalar <Guid>(sql); var objectTypeId = nodeObjectTypeId; return(UmbracoObjectTypesExtensions.GetUmbracoObjectType(objectTypeId)); } }
/// <summary> /// Gets the Parent and Child objects from a Relation as a <see cref="Tuple"/>"/> with <see cref="IUmbracoEntity"/>. /// </summary> /// <param name="relation">Relation to retrieve parent and child object from</param> /// <param name="loadBaseType">Optional bool to load the complete object graph when set to <c>False</c></param> /// <returns>Returns a Tuple with Parent (item1) and Child (item2)</returns> public Tuple <IUmbracoEntity, IUmbracoEntity> GetEntitiesFromRelation(IRelation relation, bool loadBaseType = false) { var childObjectType = UmbracoObjectTypesExtensions.GetUmbracoObjectType(relation.RelationType.ChildObjectType); var parentObjectType = UmbracoObjectTypesExtensions.GetUmbracoObjectType(relation.RelationType.ParentObjectType); var child = _entityService.Get(relation.ChildId, childObjectType, loadBaseType); var parent = _entityService.Get(relation.ParentId, parentObjectType, loadBaseType); return(new Tuple <IUmbracoEntity, IUmbracoEntity>(parent, child)); }
/// <summary> /// Gets the UmbracoObjectType from an IUmbracoEntity. /// </summary> /// <param name="entity"><see cref="IUmbracoEntity"/></param> /// <returns><see cref="UmbracoObjectTypes"/></returns> public virtual UmbracoObjectTypes GetObjectType(IUmbracoEntity entity) { var entityImpl = entity as UmbracoEntity; if (entityImpl == null) { return(GetObjectType(entity.Id)); } return(UmbracoObjectTypesExtensions.GetUmbracoObjectType(entityImpl.NodeObjectTypeId)); }
/// <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)) { var relationService = Services.RelationService; this._relationType = relationService.GetRelationTypeById(id); if (this._relationType != null) { this._parentObjectType = UmbracoObjectTypesExtensions.GetUmbracoObjectType(this._relationType.ParentObjectType); this._childObjectType = UmbracoObjectTypesExtensions.GetUmbracoObjectType(this._relationType.ChildObjectType); // ----------- 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.IsBidirectional) { 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); } } else { throw new Exception("Invalid RelationType ID"); } }
/// <summary> /// Gets the Parent and Child objects from a list of Relations as a list of <see cref="IUmbracoEntity"/> objects. /// </summary> /// <param name="relations">List of relations to retrieve parent and child objects from</param> /// <returns>An enumerable list of <see cref="Tuple"/> with <see cref="IUmbracoEntity"/></returns> public IEnumerable <Tuple <IUmbracoEntity, IUmbracoEntity> > GetEntitiesFromRelations( IEnumerable <IRelation> relations) { foreach (var relation in relations) { var childObjectType = UmbracoObjectTypesExtensions.GetUmbracoObjectType(relation.RelationType.ChildObjectType); var parentObjectType = UmbracoObjectTypesExtensions.GetUmbracoObjectType(relation.RelationType.ParentObjectType); var child = _entityService.Get(relation.ChildId, childObjectType); var parent = _entityService.Get(relation.ParentId, parentObjectType); yield return(new Tuple <IUmbracoEntity, IUmbracoEntity>(parent, child)); } }
/// <summary> /// Gets a collection of <see cref="IUmbracoEntity"/> /// </summary> /// <param name="objectTypeId">Guid id of the UmbracoObjectType</param> /// <returns>An enumerable list of <see cref="IUmbracoEntity"/> objects</returns> public virtual IEnumerable <IUmbracoEntity> GetAll(Guid objectTypeId) { var umbracoObjectType = UmbracoObjectTypesExtensions.GetUmbracoObjectType(objectTypeId); var entityType = GetEntityType(umbracoObjectType); var typeFullName = entityType.FullName; Mandate.That <NotSupportedException>(_supportedObjectTypes.ContainsKey(typeFullName), () => { throw new NotSupportedException ("The passed in type is not supported"); }); using (var repository = _repositoryFactory.CreateEntityRepository(_uowProvider.GetUnitOfWork())) { return(repository.GetAll(objectTypeId)); } }
private IUmbracoEntity GetEntity(Guid childObjectType, int childId) { switch (UmbracoObjectTypesExtensions.GetUmbracoObjectType(childObjectType)) { case UmbracoObjectTypes.Document: return(contentService.GetById(childId)); case UmbracoObjectTypes.Media: return(mediaService.GetById(childId)); case UmbracoObjectTypes.DocumentType: return(contentTypeService.GetContentType(childId)); case UmbracoObjectTypes.MediaType: return(contentTypeService.GetMediaType(childId)); } throw new Exception("Unknown child type"); }
private void TreeControllerBaseOnTreeNodesRendering(TreeControllerBase sender, TreeNodesRenderingEventArgs eventArgs) { if (eventArgs.QueryStrings.HasKey("relationEditor")) { var parentType = (UmbracoObjectTypes)Enum.Parse(typeof(UmbracoObjectTypes), eventArgs.QueryStrings.Get("parentType")); var parentAlias = eventArgs.QueryStrings.Get("parentTypeAlias"); var relationAlias = eventArgs.QueryStrings.Get("relationAlias"); var config = Configuration.Get(parentType, parentAlias); if (!config.Enabled) { return; } var relConfig = config.Get(relationAlias); if (!relConfig.Enabled) { return; } var childTypes = relConfig.EnabledChildTypes.Select(t => t.Alias).ToArray(); if (!childTypes.Any()) { return; } var relation = UmbracoContext.Current.Application.Services.RelationService.GetRelationTypeByAlias(relationAlias); var childObjectType = UmbracoObjectTypesExtensions.GetUmbracoObjectType(relation.ChildObjectType); foreach (var node in eventArgs.Nodes) { var id = Convert.ToInt32(node.Id); var alias = EntityHelper.FindAlias(childObjectType, id); if (!alias.IsNullOrWhiteSpace() && !childTypes.Contains(alias, IgnoreCase)) { node.SetNotPublishedStyle(); node.AdditionalData.Add("relationDisallowed", "true"); } } } }
/// <summary> /// Gets the Parent object from a Relation as an <see cref="IUmbracoEntity"/> /// </summary> /// <param name="relation">Relation to retrieve parent object from</param> /// <param name="loadBaseType">Optional bool to load the complete object graph when set to <c>False</c></param> /// <returns>An <see cref="IUmbracoEntity"/></returns> public IUmbracoEntity GetParentEntityFromRelation(IRelation relation, bool loadBaseType = false) { var objectType = UmbracoObjectTypesExtensions.GetUmbracoObjectType(relation.RelationType.ParentObjectType); return(_entityService.Get(relation.ParentId, objectType, loadBaseType)); }
/// <summary> /// Gets the Child object from a Relation as an <see cref="IUmbracoEntity"/> /// </summary> /// <param name="relation">Relation to retrieve child object from</param> /// <returns>An <see cref="IUmbracoEntity"/></returns> public IUmbracoEntity GetChildEntityFromRelation(IRelation relation) { var objectType = UmbracoObjectTypesExtensions.GetUmbracoObjectType(relation.RelationType.ChildObjectType); return(_entityService.Get(relation.ChildId, objectType)); }
/// <summary> /// /// </summary> /// <param name="contextId">the id of the content, media or member item</param> /// <param name="propertyAlias">the property alias of the picker using relation mapping</param> /// <param name="relationTypeAlias">the alias of the relation type to use</param> /// <param name="relationsOnly"></param> /// <param name="pickedIds">the ids of all picked items that are to be related to the contextId</param> internal static void UpdateRelationMapping(int contextId, string propertyAlias, string relationTypeAlias, bool relationsOnly, int[] pickedIds) { IRelationType relationType = ApplicationContext.Current.Services.RelationService.GetRelationTypeByAlias(relationTypeAlias); if (relationType != null) { // get all relations of this type List <IRelation> relations = ApplicationContext.Current.Services.RelationService.GetAllRelationsByRelationType(relationType.Id).ToList(); // construct object used to identify a relation (this is serialized into the relation comment field) RelationMappingComment relationMappingComment = new RelationMappingComment(contextId, propertyAlias); // filter down potential relations, by relation type direction if (relationType.IsBidirectional && relationsOnly) { relations = relations.Where(x => x.ChildId == contextId || x.ParentId == contextId).ToList(); relations = relations.Where(x => new RelationMappingComment(x.Comment).DataTypeDefinitionId == relationMappingComment.DataTypeDefinitionId).ToList(); } else { relations = relations.Where(x => x.ChildId == contextId).ToList(); relations = relations.Where(x => new RelationMappingComment(x.Comment).PropertyTypeId == relationMappingComment.PropertyTypeId).ToList(); if (relationMappingComment.IsInArchetype()) { relations = relations.Where(x => new RelationMappingComment(x.Comment).MatchesArchetypeProperty(relationMappingComment.PropertyAlias)).ToList(); } } // check current context is of the correct object type (as according to the relation type) if (ApplicationContext.Current.Services.EntityService.GetObjectType(contextId) == UmbracoObjectTypesExtensions.GetUmbracoObjectType(relationType.ChildObjectType)) { // for each picked item foreach (int pickedId in pickedIds) { // check picked item context if of the correct object type (as according to the relation type) if (ApplicationContext.Current.Services.EntityService.GetObjectType(pickedId) == UmbracoObjectTypesExtensions.GetUmbracoObjectType(relationType.ParentObjectType)) { // if relation doesn't already exist (new picked item) if (!relations.Exists(x => x.ParentId == pickedId)) { // create relation Relation relation = new Relation(pickedId, contextId, relationType); relation.Comment = relationMappingComment.GetComment(); ApplicationContext.Current.Services.RelationService.Save(relation); } // housekeeping - remove 'the' relation from the list being processed (there should be only one) relations.RemoveAll(x => x.ChildId == contextId && x.ParentId == pickedId && x.RelationTypeId == relationType.Id); } } } // delete relations for any items left on the list being processed if (relations.Any()) { foreach (IRelation relation in relations) { ApplicationContext.Current.Services.RelationService.Delete(relation); } } } }