/// <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>
        /// <returns></returns>
        internal static IEnumerable<int> GetRelatedIds(int contextId, string propertyAlias, string relationTypeAlias, bool relationsOnly)
        {
            IRelationType relationType = ApplicationContext.Current.Services.RelationService.GetRelationTypeByAlias(relationTypeAlias);

            if (relationType != null)
            {
                // get all relations of this type
                IEnumerable<IRelation> relations = ApplicationContext.Current.Services.RelationService.GetAllRelationsByRelationType(relationType.Id);

                // 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);
                    relations = relations.Where(x => new RelationMappingComment(x.Comment).DataTypeDefinitionId == relationMappingComment.DataTypeDefinitionId);
                }
                else
                {
                    relations = relations.Where(x => x.ChildId == contextId);
                    relations = relations.Where(x => new RelationMappingComment(x.Comment).PropertyTypeId == relationMappingComment.PropertyTypeId);

                    if (relationMappingComment.IsInArchetype())
                    {
                        relations = relations.Where(x => new RelationMappingComment(x.Comment).MatchesArchetypeProperty(relationMappingComment.PropertyAlias));
                    }
                }

                return relations.Select(x => (x.ParentId != contextId) ? x.ParentId : x.ChildId);
            }

            return null;
        }
        public IEnumerable <int> GetRelatedIds([FromUri] int contextId, [FromUri] string propertyAlias, [FromUri] string relationTypeAlias, [FromUri] bool relationsOnly)
        {
            IRelationType relationType = ApplicationContext.Services.RelationService.GetRelationTypeByAlias(relationTypeAlias);

            if (relationType != null)
            {
                // get all relations of this type
                IEnumerable <IRelation> relations = ApplicationContext.Services.RelationService.GetAllRelationsByRelationType(relationType.Id);

                // 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);
                    relations = relations.Where(x => new RelationMappingComment(x.Comment).DataTypeDefinitionId == relationMappingComment.DataTypeDefinitionId);
                }
                else
                {
                    relations = relations.Where(x => x.ChildId == contextId);
                    relations = relations.Where(x => new RelationMappingComment(x.Comment).PropertyTypeId == relationMappingComment.PropertyTypeId);

                    if (relationMappingComment.IsInArchetype())
                    {
                        relations = relations.Where(x => new RelationMappingComment(x.Comment).MatchesArchetypeProperty(relationMappingComment.PropertyAlias));
                    }
                }

                return(relations.Select(x => (x.ParentId != contextId) ? x.ParentId : x.ChildId));
            }

            return(null);
        }
        public void UpdateRelationMapping([FromUri] int contextId, [FromUri] int parentId, [FromUri] string propertyAlias, [FromUri] string relationTypeAlias, [FromUri] bool relationsOnly, [FromBody] dynamic data)
        {
            IRelationType relationType = ApplicationContext.Services.RelationService.GetRelationTypeByAlias(relationTypeAlias);

            if (relationType != null)
            {
                // WARNING: Duplicate code
                // get all relations of this type
                IEnumerable <IRelation> relations = ApplicationContext.Services.RelationService.GetAllRelationsByRelationType(relationType.Id);

                // construct object used to identify a relation (this is serialized into the relation comment field)
                RelationMappingComment relationMappingComment = new RelationMappingComment(contextId, propertyAlias);

                if (relationType.IsBidirectional && relationsOnly)
                {
                    relations = relations.Where(x => x.ChildId == contextId || x.ParentId == contextId);
                    relations = relations.Where(x => new RelationMappingComment(x.Comment).DataTypeDefinitionId == relationMappingComment.DataTypeDefinitionId);
                }
                else
                {
                    relations = relations.Where(x => x.ChildId == contextId);
                    relations = relations.Where(x => new RelationMappingComment(x.Comment).PropertyTypeId == relationMappingComment.PropertyTypeId);

                    if (relationMappingComment.IsInArchetype())
                    {
                        relations = relations.Where(x => new RelationMappingComment(x.Comment).MatchesArchetypeProperty(relationMappingComment.PropertyAlias));
                    }
                }

                // clear any existing relations
                foreach (IRelation relation in relations)
                {
                    ApplicationContext.Services.RelationService.Delete(relation);
                }

                // create new relations
                LegacyRelationType legacyRelationType = new LegacyRelationType(relationType.Id);

                // ensure context type is valid
                if (uQuery.GetUmbracoObjectType(contextId) == legacyRelationType.GetChildUmbracoObjectType())
                {
                    foreach (int pickedId in ((JArray)data).Select(x => x.Value <int>()))
                    {
                        // ensure picked type is valid
                        if (uQuery.GetUmbracoObjectType(pickedId) == legacyRelationType.GetParentUmbracoObjectType())
                        {
                            LegacyRelation.MakeNew(pickedId, contextId, legacyRelationType, relationMappingComment.GetComment());
                        }
                    }
                }
            }
        }
Beispiel #4
0
        public void UpdateRelationMapping([FromUri] int contextId, [FromUri] string propertyAlias, [FromUri] string relationTypeAlias, [FromUri] bool relationsOnly, [FromBody] dynamic data)
        {
            IRelationType relationType = ApplicationContext.Services.RelationService.GetRelationTypeByAlias(relationTypeAlias);

            if (relationType != null)
            {
                // WARNING: Duplicate code
                // get all relations of this type
                IEnumerable<IRelation> relations = ApplicationContext.Services.RelationService.GetAllRelationsByRelationType(relationType.Id);

                // construct object used to identify a relation (this is serialized into the relation comment field)
                RelationMappingComment relationMappingComment = new RelationMappingComment(contextId, propertyAlias);

                if (relationType.IsBidirectional && relationsOnly)
                {
                    relations = relations.Where(x => x.ChildId == contextId || x.ParentId == contextId);
                    relations = relations.Where(x => new RelationMappingComment(x.Comment).DataTypeDefinitionId == relationMappingComment.DataTypeDefinitionId);
                }
                else
                {
                    relations = relations.Where(x => x.ChildId == contextId);
                    relations = relations.Where(x => new RelationMappingComment(x.Comment).PropertyTypeId == relationMappingComment.PropertyTypeId);
                }

                // clear any existing relations
                foreach (IRelation relation in relations)
                {
                    ApplicationContext.Services.RelationService.Delete(relation);
                }

                // create new relations
                LegacyRelationType legacyRelationType = new LegacyRelationType(relationType.Id);

                // ensure context type is valid
                if (uQuery.GetUmbracoObjectType(contextId) == legacyRelationType.GetChildUmbracoObjectType())
                {
                    foreach (int pickedId in ((JArray)data).Select(x => x.Value<int>()))
                    {
                        // ensure picked type is valid
                        if (uQuery.GetUmbracoObjectType(pickedId) == legacyRelationType.GetParentUmbracoObjectType())
                        {
                            LegacyRelation.MakeNew(pickedId, contextId, legacyRelationType, relationMappingComment.GetComment());
                        }
                    }
                }
            }
        }
        /// <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);
                    }
                }
            }
        }
        /// <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);
                    }
                }
            }
        }