/// <summary> /// Creates a branch new revision based on the revision passed in with the specified status if one is supplied. /// </summary> /// <typeparam name="T"></typeparam> /// <param name="readWriter"></param> /// <param name="revision">The Revision in which to create a new one from</param> /// <param name="status">Default is Draft</param> /// <returns> /// Returns the newly created revision object /// </returns> public static Revision <T> AddNew <T>(this ICoreRevisionRepository <TypedEntity> readWriter, Revision <T> revision, RevisionStatusType status = null) where T : TypedEntity { if (status == null) { status = FixedStatusTypes.Draft; } var newRev = revision.CopyToNewRevision(status); readWriter.AddOrUpdate(newRev); return(newRev); }
/// <summary> /// When editing or creating content, this will bind the model, check the model state errors, add appropriate notifications /// return the error view or redirect to the correct place and also persist the data to the repository. /// </summary> /// <param name="model"></param> /// <param name="entity"></param> /// <returns></returns> protected virtual ActionResult ProcessSubmit(TEditorModel model, Revision <TypedEntity> entity, bool isRevisional) { Mandate.ParameterNotNull(model, "model"); //bind it's data model.BindModel(this); //if there's model errors, return the view if (!ModelState.IsValid) { AddValidationErrorsNotification(); return(View("Edit", model)); } //persist the data var success = false; using (var uow = Hive.Create <IContentStore>()) { //EnsureUniqueName(model); if (entity == null) { //map to new entity entity = BackOfficeRequestContext.Application.FrameworkContext.TypeMappers.Map <TEditorModel, Revision <TypedEntity> >(model); } else { //map to existing entity BackOfficeRequestContext.Application.FrameworkContext.TypeMappers.Map(model, entity.Item); //then create a new revision entity = entity.CopyToNewRevision(); } // Try publish if (ValueProvider.GetValue("submit.Publish") != null) { success = this.TryExecuteSecuredMethod(x => x.ProcessPublish(model, entity), model.Id).Success; if (!success) { // Report unathorized NotifyForProcess(NotificationState.PublishUnathorized, model); } } // Try unpublish else if (ValueProvider.GetValue("submit.Unpublish") != null) { success = this.TryExecuteSecuredMethod(x => x.ProcessUnpublish(model, entity), model.Id).Success; if (!success) { // Report unathorized NotifyForProcess(NotificationState.UnpublishedUnauthorized, model); } } // Try save else { success = this.TryExecuteSecuredMethod(x => x.ProcessSave(model, entity), model.Id).Success; if (!success) { // Report unathorized NotifyForProcess(NotificationState.SaveUnauthorized, model); } } if (success) { if (isRevisional) { uow.Repositories.Revisions.AddOrUpdate(entity); } else { uow.Repositories.AddOrUpdate(entity.Item); } uow.Complete(); } else { uow.Abandon(); } } if (success) { // Perf: use a readonly unit here rather than delaying the writer using (var uow = ReadonlyHive.CreateReadonly <IContentStore>()) { //need to clear the URL cache for this entry BackOfficeRequestContext.RoutingEngine.ClearCache(clearGeneratedUrls: true, clearMappedUrls: true); //add path for entity for SupportsPathGeneration (tree syncing) to work GeneratePathsForCurrentEntity(uow.Repositories.GetEntityPaths <TypedEntity>(entity.Item.Id, FixedRelationTypes.DefaultRelationType)); return(RedirectToAction("Edit", new { id = entity.Item.Id })); } } return(View("Edit", model)); }
public void CompositeEntity_ReSaves() { // Arrange var childSchema = CreateAndSaveCompositeSchema(AttributeTypeRegistry, ProviderSetup); CompositeEntitySchema merged = null; var groupUnitFactory = new GroupUnitFactory(ProviderSetup, childSchema.Id.ToUri(), FakeHiveCmsManager.CreateFakeRepositoryContext(ProviderSetup.FrameworkContext)); using (var uow = groupUnitFactory.Create()) { merged = uow.Repositories.Schemas.GetComposite<EntitySchema>(childSchema.Id); } List<TypedAttribute> attribs; var entity = MockCompositeEntity(merged, out attribs); AssignFakeIdsIfPassthrough(ProviderSetup.ProviderMetadata, entity); Assert.That(entity.Attributes.Count, Is.EqualTo(attribs.Count)); var firstRevision = new Revision<TypedEntity>(entity); using (var uow = groupUnitFactory.Create()) { uow.Repositories.Revisions.AddOrUpdate(firstRevision); uow.Complete(); } PostWriteCallback.Invoke(); // Edit the data and resave var secondRevision = firstRevision.CopyToNewRevision(FixedStatusTypes.Published); secondRevision.Item.Attributes.Last().DynamicValue = "changed"; secondRevision.Item.Attributes.ForEach(x => x.Id = HiveId.Empty); using (var uow = groupUnitFactory.Create()) { uow.Repositories.Revisions.AddOrUpdate(secondRevision); uow.Complete(); } PostWriteCallback.Invoke(); // Load the data again to ensure these attributes are still "inherited" using (var uow = groupUnitFactory.Create()) { var reloaded = uow.Repositories.Get<TypedEntity>(entity.Id); Assert.That(reloaded.EntitySchema.Id.Value, Is.EqualTo(childSchema.Id.Value)); Assert.That(reloaded.Attributes.Count, Is.EqualTo(attribs.Count)); Assert.That(reloaded.Attributes.Select(x => x.AttributeDefinition), Has.Some.TypeOf<InheritedAttributeDefinition>()); } }