/// <summary> /// Deserialize the relations for a relation type. /// </summary> private IEnumerable <uSyncChange> DeserializeRelations(XElement node, IRelationType relationType, SyncSerializerOptions options) { var changes = new List <uSyncChange>(); var existing = relationService .GetAllRelationsByRelationType(relationType.Id) .ToList(); var relations = node.Element("Relations"); // do we do this, or do we remove them all! if (relations == null) { return(Enumerable.Empty <uSyncChange>()); } var newRelations = new List <string>(); foreach (var relationNode in relations.Elements("Relation")) { var parentKey = relationNode.Element("Parent").ValueOrDefault(Guid.Empty); var childKey = relationNode.Element("Child").ValueOrDefault(Guid.Empty); if (parentKey == Guid.Empty || childKey == Guid.Empty) { continue; } var parentItem = entityService.Get(parentKey); var childItem = entityService.Get(childKey); if (parentItem == null || childItem == null) { continue; } if (!existing.Any(x => x.ParentId == parentItem.Id && x.ChildId == childItem.Id)) { // missing from the current list... add it. relationService.Save(new Relation(parentItem.Id, childItem.Id, relationType)); changes.Add(uSyncChange.Create(relationType.Alias, parentItem.Name, childItem.Name)); } newRelations.Add($"{parentItem.Id}_{childItem.Id}"); } if (options.DeleteItems()) { var obsolete = existing.Where(x => !newRelations.Contains($"{x.ParentId}_{x.ChildId}")); foreach (var obsoleteRelation in obsolete) { changes.Add(uSyncChange.Delete(relationType.Alias, obsoleteRelation.ParentId.ToString(), obsoleteRelation.ChildId.ToString())); relationService.Delete(obsoleteRelation); } } return(changes); }
public void Handle(ContentMovedNotification notification) { foreach (var item in notification.MoveInfoCollection.Where(x => x.OriginalPath.Contains(Constants.System.RecycleBinContentString))) { const string relationTypeAlias = Constants.Conventions.RelationTypes.RelateParentDocumentOnDeleteAlias; var relations = _relationService.GetByChildId(item.Entity.Id); foreach (var relation in relations.Where(x => x.RelationType.Alias.InvariantEquals(relationTypeAlias))) { _relationService.Delete(relation); } } }
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); } } }
public void Unlock(int pageId) { var existingLocks = _relationService.GetByParentOrChildId(pageId, ContentGuardRelationTypeAlias); foreach (var pageLock in existingLocks) { _relationService.Delete(pageLock); } }
public IActionResult DeleteById(int id) { var relationType = _relationService.GetRelationTypeById(id); if (relationType == null) { return(NotFound()); } _relationService.Delete(relationType); return(Ok()); }
public ActionResult Delete(RelationViewModel model) { try { service.Delete(model); service.Save(); return(RedirectToAction("Index")); } catch { return(View()); } }
private void ContentService_Saved(IContentService sender, SaveEventArgs<IContent> e) { var documentToMediaRelationType = relationService.GetRelationTypeByAlias(documentToMediaRelationTypeAlias); var documentToDocumentRelationType = relationService.GetRelationTypeByAlias(documentToDocumentRelationTypeAlias); var entitiesSaved = e.SavedEntities; foreach (var entity in entitiesSaved) { //Deletes all relation for parentID(entityID) before going on to recreate them later in the function foreach (var relation in relationService.GetByParentId(entity.Id)) { relationService.Delete(relation); } foreach (var prop in entity.Properties) { try { if (prop.PropertyType.Name.Equals("contentBlocks") || prop.PropertyType.Name.Equals("FeatureBlocks")) { var propValue = prop?.GetValue(); if (propValue != null) { //processArchetypeBlocks(prop, entity); } else { continue; } } if (/*getMediaContentPropertyAliases().Contains(prop.PropertyType.Name)*/true) { var propValue = prop.GetValue(); if (propValue == null) continue; processRelations(prop, entity); } } catch (Exception msg) { Current.Logger.Error(GetType(), "\n\nException while creating relation. Content id: " + entity.Id+". Entity type: "+ entity.ContentType.Alias+". Property: "+prop?.Alias+". Property type: "+prop?.PropertyType.Alias,msg); continue; } } } }
private static void ContentService_Saved(IContentService contentService, ContentSavedEventArgs e) { foreach (IContent content in e.SavedEntities) { List <string> editors = new List <string> { Umbraco.Core.Constants.PropertyEditors.Aliases.Grid, BentoItemDataEditor.EditorAlias, BentoStackDataEditor.EditorAlias }; foreach (Property contentProperty in content.Properties.Where(x => editors.Contains(x.PropertyType.PropertyEditorAlias))) { IDataType editor = DataTypeService.GetDataType(contentProperty.PropertyType.DataTypeId); IRelationType bentoBlocksRelationType = RelationService.GetRelationTypeByAlias(RelationTypes.BentoItemsAlias); if (bentoBlocksRelationType == null) { RelationType relationType = new RelationType( RelationTypes.BentoItemsAlias, RelationTypes.BentoItemsName, true, UmbracoObjectTypes.Document.GetGuid(), UmbracoObjectTypes.Document.GetGuid()); RelationService.Save(relationType); bentoBlocksRelationType = RelationService.GetRelationTypeByAlias(RelationTypes.BentoItemsAlias); } //todo: this does work but it's a bit brute force... //i guess we could store the current relationships and then store the ones we're creating and compare them and then //delete the ones from the old list that arent in the new list? but that's a lot of db hits... IEnumerable <IRelation> rels = RelationService.GetByParentId(content.Id); foreach (IRelation currentRelation in rels.Where(x => x.RelationType.Id == bentoBlocksRelationType.Id)) { RelationService.Delete(currentRelation); } if (contentProperty.PropertyType.PropertyEditorAlias == BentoItemDataEditor.EditorAlias) { foreach (Property.PropertyValue value in contentProperty.Values) { if (value.PublishedValue == null) { break; } var area = JsonConvert.DeserializeObject <Area>(value.PublishedValue.ToString()); if (area.Id <= 0) { continue; } var bentoContent = contentService.GetById(area.Id); if (bentoContent == null) { continue; } BentoItemConfiguration config = (BentoItemConfiguration)editor.Configuration; ProcessRelationship(contentService, bentoContent, content, bentoBlocksRelationType, config.ItemDoctypeCompositionAlias); } } else { foreach (Property.PropertyValue value in contentProperty.Values) { if (value.PublishedValue == null) { break; } string valueString = value.PublishedValue?.ToString(); if (string.IsNullOrWhiteSpace(valueString)) { continue; } IEnumerable <StackItem> items = JsonConvert.DeserializeObject <IEnumerable <StackItem> >(valueString, new StackItemConverter()); var itemList = items.Where(x => x.Areas != null && x.Areas.Any()) .SelectMany(stackItem => stackItem.Areas.Where(x => x.Id > 0), (stackItem, x) => contentService.GetById(x.Id)) .Where(bentoContent => bentoContent != null) .Distinct(); BentoStackConfiguration config = (BentoStackConfiguration)editor.Configuration; foreach (IContent item in itemList) { ProcessRelationship(contentService, item, content, bentoBlocksRelationType, config.ItemDoctypeCompositionAlias); } } } } } }
/// <summary> /// 删除 /// </summary> /// <param name="ids"></param> /// <returns></returns> public ActionResult Delete(string ID) { var result = IRelationService.Delete(ID); return(JResult(result)); }