diff --git a/src/Umbraco.Core/Services/IRelationService.cs b/src/Umbraco.Core/Services/IRelationService.cs index 32e7d88f2a..a219cfc43b 100644 --- a/src/Umbraco.Core/Services/IRelationService.cs +++ b/src/Umbraco.Core/Services/IRelationService.cs @@ -182,6 +182,14 @@ namespace Umbraco.Core.Services /// Returns True if any relations exists with the given Id, otherwise False bool IsRelated(int id); + /// + /// Checks whether two items are related + /// + /// Id of the Parent relation + /// Id of the Child relation + /// Returns True if any relations exists with the given Ids, otherwise False + bool AreRelated(int parentId, int childId); + /// /// Saves a /// diff --git a/src/Umbraco.Core/Services/RelationService.cs b/src/Umbraco.Core/Services/RelationService.cs index 5da9fddef0..5102731902 100644 --- a/src/Umbraco.Core/Services/RelationService.cs +++ b/src/Umbraco.Core/Services/RelationService.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Linq; +using Umbraco.Core.Events; using Umbraco.Core.Models; using Umbraco.Core.Models.EntityBase; using Umbraco.Core.Persistence; @@ -326,14 +327,18 @@ namespace Umbraco.Core.Services Save(relationType); var relation = new Relation(parent.Id, child.Id, relationType); + if (SavingRelation.IsRaisedEventCancelled(new SaveEventArgs(relation), this)) + return relation; + var uow = _uowProvider.GetUnitOfWork(); using (var repository = _repositoryFactory.CreateRelationRepository(uow)) { repository.AddOrUpdate(relation); uow.Commit(); - - return relation; } + + SavedRelation.RaiseEvent(new SaveEventArgs(relation, false), this); + return relation; } /// @@ -350,14 +355,18 @@ namespace Umbraco.Core.Services throw new ArgumentNullException(string.Format("No RelationType with Alias '{0}' exists.", relationTypeAlias)); var relation = new Relation(parent.Id, child.Id, relationType); + if (SavingRelation.IsRaisedEventCancelled(new SaveEventArgs(relation), this)) + return relation; + var uow = _uowProvider.GetUnitOfWork(); using (var repository = _repositoryFactory.CreateRelationRepository(uow)) { repository.AddOrUpdate(relation); uow.Commit(); - - return relation; } + + SavedRelation.RaiseEvent(new SaveEventArgs(relation, false), this); + return relation; } /// @@ -388,18 +397,72 @@ namespace Umbraco.Core.Services } } + /// + /// Checks whether two items are related + /// + /// Id of the Parent relation + /// Id of the Child relation + /// Returns True if any relations exists with the given Ids, otherwise False + public bool AreRelated(int parentId, int childId) + { + using (var repository = _repositoryFactory.CreateRelationRepository(_uowProvider.GetUnitOfWork())) + { + var query = new Query().Where(x => x.ParentId == parentId && x.ChildId == childId); + return repository.GetByQuery(query).Any(); + } + } + + /// + /// Checks whether two items are related with a given relation type alias + /// + /// Id of the Parent relation + /// Id of the Child relation + /// Alias of the relation type + /// Returns True if any relations exists with the given Ids and relation type, otherwise False + public bool AreRelated(int parentId, int childId, string relationTypeAlias) + { + var relType = GetRelationTypeByAlias(relationTypeAlias); + if(relType == null) + return false; + + return AreRelated(parentId, childId, relType); + } + + + /// + /// Checks whether two items are related with a given relation type + /// + /// Id of the Parent relation + /// Id of the Child relation + /// Type of relation + /// Returns True if any relations exists with the given Ids and relation type, otherwise False + public bool AreRelated(int parentId, int childId, IRelationType relationType) + { + using (var repository = _repositoryFactory.CreateRelationRepository(_uowProvider.GetUnitOfWork())) + { + var query = new Query().Where(x => x.ParentId == parentId && x.ChildId == childId && x.RelationTypeId == relationType.Id); + return repository.GetByQuery(query).Any(); + } + } + + /// /// Saves a /// /// Relation to save public void Save(IRelation relation) { + if (SavingRelation.IsRaisedEventCancelled(new SaveEventArgs(relation), this)) + return; + var uow = _uowProvider.GetUnitOfWork(); using (var repository = _repositoryFactory.CreateRelationRepository(uow)) { repository.AddOrUpdate(relation); uow.Commit(); } + + SavedRelation.RaiseEvent(new SaveEventArgs(relation, false), this); } /// @@ -408,12 +471,17 @@ namespace Umbraco.Core.Services /// RelationType to Save public void Save(IRelationType relationType) { + if (SavingRelationType.IsRaisedEventCancelled(new SaveEventArgs(relationType), this)) + return; + var uow = _uowProvider.GetUnitOfWork(); using (var repository = _repositoryFactory.CreateRelationTypeRepository(uow)) { repository.AddOrUpdate(relationType); uow.Commit(); } + + SavedRelationType.RaiseEvent(new SaveEventArgs(relationType, false), this); } /// @@ -422,12 +490,17 @@ namespace Umbraco.Core.Services /// Relation to Delete public void Delete(IRelation relation) { + if (DeletingRelation.IsRaisedEventCancelled(new DeleteEventArgs(relation), this)) + return; + var uow = _uowProvider.GetUnitOfWork(); using (var repository = _repositoryFactory.CreateRelationRepository(uow)) { repository.Delete(relation); uow.Commit(); } + + DeletedRelation.RaiseEvent(new DeleteEventArgs(relation, false), this); } /// @@ -436,12 +509,17 @@ namespace Umbraco.Core.Services /// RelationType to Delete public void Delete(IRelationType relationType) { + if (DeletingRelationType.IsRaisedEventCancelled(new DeleteEventArgs(relationType), this)) + return; + var uow = _uowProvider.GetUnitOfWork(); using (var repository = _repositoryFactory.CreateRelationTypeRepository(uow)) { repository.Delete(relationType); uow.Commit(); } + + DeletedRelationType.RaiseEvent(new DeleteEventArgs(relationType, false), this); } /// @@ -450,18 +528,21 @@ namespace Umbraco.Core.Services /// to Delete Relations for public void DeleteRelationsOfType(IRelationType relationType) { + var relations = new List(); var uow = _uowProvider.GetUnitOfWork(); using (var repository = _repositoryFactory.CreateRelationRepository(uow)) { var query = new Query().Where(x => x.RelationTypeId == relationType.Id); - var list = repository.GetByQuery(query).ToList(); + relations.AddRange(repository.GetByQuery(query).ToList()); - foreach (var relation in list) + foreach (var relation in relations) { repository.Delete(relation); } uow.Commit(); } + + DeletedRelation.RaiseEvent(new DeleteEventArgs(relations, false), this); } #region Private Methods @@ -480,5 +561,47 @@ namespace Umbraco.Core.Services return relations; } #endregion + + #region Events Handlers + /// + /// Occurs before Deleting a Relation + /// + public static event TypedEventHandler> DeletingRelation; + + /// + /// Occurs after a Relation is Deleted + /// + public static event TypedEventHandler> DeletedRelation; + + /// + /// Occurs before Saving a Relation + /// + public static event TypedEventHandler> SavingRelation; + + /// + /// Occurs after a Relation is Saved + /// + public static event TypedEventHandler> SavedRelation; + + /// + /// Occurs before Deleting a RelationType + /// + public static event TypedEventHandler> DeletingRelationType; + + /// + /// Occurs after a RelationType is Deleted + /// + public static event TypedEventHandler> DeletedRelationType; + + /// + /// Occurs before Saving a RelationType + /// + public static event TypedEventHandler> SavingRelationType; + + /// + /// Occurs after a RelationType is Saved + /// + public static event TypedEventHandler> SavedRelationType; + #endregion } } \ No newline at end of file