public virtual ContentItem Clone(ContentItem contentItem) { // Mostly taken from: http://Coevery.codeplex.com/discussions/396664 var importContentSession = new ImportContentSession(this); var element = Export(contentItem); // If a handler prevents this element from being exported, it can't be cloned if (element == null) { throw new InvalidOperationException("The content item couldn't be cloned because a handler prevented it from being exported."); } var elementId = element.Attribute("Id"); var copyId = elementId.Value + "-copy"; elementId.SetValue(copyId); var status = element.Attribute("Status"); if (status != null) { status.SetValue("Draft"); // So the copy is always a draft. } importContentSession.Set(copyId, element.Name.LocalName); Import(element, importContentSession); return(importContentSession.Get(copyId, element.Name.LocalName)); }
public void GetItemExistsAndDraftRequiredVersionOptionReturnsDraft() { var session = new ImportContentSession(_contentManager.Object); session.Set(_testItemIdentity1.ToString(), "TestContentType"); var sessionItem = session.Get(_testItemIdentity1.ToString(), VersionOptions.DraftRequired); Assert.IsNotNull(sessionItem); Assert.That(1, Is.EqualTo(sessionItem.Id)); Assert.IsFalse(sessionItem.IsPublished()); }
public void GetItemExistsAndNoVersionOptionsReturnsPublishedItem() { var session = new ImportContentSession(_contentManager.Object); session.Set(_testItemIdentity1.ToString(), "TestContentType"); var sessionItem = session.Get(_testItemIdentity1.ToString()); Assert.IsNotNull(sessionItem); Assert.AreEqual(1, sessionItem.Id); Assert.IsTrue(sessionItem.IsPublished()); }
// <Data /> // Import Data public void ExecuteRecipeStep(RecipeContext recipeContext) { if (!String.Equals(recipeContext.RecipeStep.Name, "Data", StringComparison.OrdinalIgnoreCase)) { return; } var importContentSession = new ImportContentSession(_coeveryServices.ContentManager); // Populate local dictionary with elements and their ids var elementDictionary = CreateElementDictionary(recipeContext.RecipeStep.Step); //Populate import session with all identities to be imported foreach (var identity in elementDictionary.Keys) { importContentSession.Set(identity.ToString(), elementDictionary[identity].Name.LocalName); } //Determine if the import is to be batched in multiple transactions var startIndex = 0; int batchSize = GetBatchSizeForDataStep(recipeContext.RecipeStep.Step); //Run the import ContentIdentity nextIdentity = null; try { while (startIndex < elementDictionary.Count) { importContentSession.InitializeBatch(startIndex, batchSize); //the session determines which items are included in the current batch //so that dependencies can be managed within the same transaction nextIdentity = importContentSession.GetNextInBatch(); while (nextIdentity != null) { _coeveryServices.ContentManager.Import(elementDictionary[nextIdentity], importContentSession); nextIdentity = importContentSession.GetNextInBatch(); } startIndex += batchSize; //Create a new transaction for each batch if (startIndex < elementDictionary.Count) { _coeveryServices.ContentManager.Clear(); _transactionManager.RequireNew(); } } } catch (Exception) { //Ensure a failed batch is rolled back _transactionManager.Cancel(); throw; } recipeContext.Executed = true; }
public void GetNextInBatchInitialisedTwoBatchesReturnsItemsOnceEach() { var session = new ImportContentSession(_contentManager.Object); session.Set(_testItemIdentity1.ToString(), "TestContentType"); session.Set(_testItemIdentity2.ToString(), "TestContentType"); session.Set(_testItemIdentity3.ToString(), "TestContentType"); session.Set(_testItemIdentity4.ToString(), "TestContentType"); session.Set(_testItemIdentity5.ToString(), "TestContentType"); session.InitializeBatch(0, 2); var firstIdentity = session.GetNextInBatch(); //get later item as dependency var dependencyItem = session.Get(_testItemIdentity5.ToString(), VersionOptions.Latest); var dependencyIdentity = session.GetNextInBatch(); var secondIdentity = session.GetNextInBatch(); var afterBatch1 = session.GetNextInBatch(); session.InitializeBatch(2, 2); var thirdIdentity = session.GetNextInBatch(); var fourthdentity = session.GetNextInBatch(); var afterBatch2 = session.GetNextInBatch(); session.InitializeBatch(4, 2); var fifthIdentity = session.GetNextInBatch(); var afterBatch3 = session.GetNextInBatch(); var comparer = new ContentIdentity.ContentIdentityEqualityComparer(); Assert.That(comparer.Equals(_testItemIdentity1, firstIdentity)); Assert.That(comparer.Equals(_testItemIdentity5, dependencyIdentity)); Assert.That(comparer.Equals(_testItemIdentity2, secondIdentity)); Assert.That(afterBatch1, Is.Null); Assert.That(comparer.Equals(_testItemIdentity3, thirdIdentity)); Assert.That(comparer.Equals(_testItemIdentity4, fourthdentity)); Assert.That(afterBatch2, Is.Null); Assert.That(fifthIdentity, Is.Null); //already processed as dependency Assert.That(afterBatch3, Is.Null); }
// Insert or Update imported data into the content manager. // Call content item handlers. public void Import(XElement element, ImportContentSession importContentSession) { var elementId = element.Attribute("Id"); if (elementId == null) { return; } var identity = elementId.Value; if (String.IsNullOrWhiteSpace(identity)) { return; } var status = element.Attribute("Status"); var item = importContentSession.Get(identity, VersionOptions.Latest, XmlConvert.DecodeName(element.Name.LocalName)); if (item == null) { item = New(XmlConvert.DecodeName(element.Name.LocalName)); if (status != null && status.Value == "Draft") { Create(item, VersionOptions.Draft); } else { Create(item); } } // create a version record if import handlers need it if (item.VersionRecord == null) { item.VersionRecord = new ContentItemVersionRecord { ContentItemRecord = new ContentItemRecord { ContentType = AcquireContentTypeRecord(item.ContentType) }, Number = 1, Latest = true, Published = true }; } var context = new ImportContentContext(item, element, importContentSession); foreach (var contentHandler in Handlers) { contentHandler.Importing(context); } foreach (var contentHandler in Handlers) { contentHandler.Imported(context); } var savedItem = Get(item.Id, VersionOptions.Latest); // the item has been pre-created in the first pass of the import, create it in db if (savedItem == null) { if (status != null && status.Value == "Draft") { Create(item, VersionOptions.Draft); } else { Create(item); } } if (status == null || status.Value == Published) { Publish(item); } }
public void ItemsSetAndUninitializedReturnsAllItems() { var importContentSession = new ImportContentSession(_contentManager.Object); importContentSession.Set("/Id=One", "TestType"); importContentSession.Set("/Id=Two", "TestType"); importContentSession.Set("/Id=Three", "TestType"); var comparer = new ContentIdentity.ContentIdentityEqualityComparer(); Assert.That(comparer.Equals(importContentSession.GetNextInBatch(), new ContentIdentity("/Id=One"))); Assert.That(comparer.Equals(importContentSession.GetNextInBatch(), new ContentIdentity("/Id=Two"))); Assert.That(comparer.Equals(importContentSession.GetNextInBatch(), new ContentIdentity("/Id=Three"))); Assert.That(importContentSession.GetNextInBatch(), Is.Null); }
public void ItemsSetAndBatchInitialisedReturnsBatchedItems() { var importContentSession = new ImportContentSession(_contentManager.Object); importContentSession.Set("/Id=One", "TestType"); importContentSession.Set("/Id=Two", "TestType"); importContentSession.Set("/Id=Three", "TestType"); importContentSession.Set("/Id=Four", "TestType"); importContentSession.Set("/Id=Five", "TestType"); importContentSession.InitializeBatch(1, 2); var comparer = new ContentIdentity.ContentIdentityEqualityComparer(); Assert.That(comparer.Equals(importContentSession.GetNextInBatch(), new ContentIdentity("/Id=Two"))); Assert.That(comparer.Equals(importContentSession.GetNextInBatch(), new ContentIdentity("/Id=Three"))); Assert.That(importContentSession.GetNextInBatch(), Is.Null); importContentSession.InitializeBatch(2, 5); //item with "/Id=Three" should not be returned twice in the same session Assert.That(comparer.Equals(importContentSession.GetNextInBatch(), new ContentIdentity("/Id=Four"))); Assert.That(comparer.Equals(importContentSession.GetNextInBatch(), new ContentIdentity("/Id=Five"))); Assert.That(importContentSession.GetNextInBatch(), Is.Null); }
public void GetNextInBatchReturnsNullWhenNoItemsSet() { var importContentSession = new ImportContentSession(_contentManager.Object); Assert.That(importContentSession.GetNextInBatch(), Is.Null); }
public void GetNextInBatchInitialisedWithOneItemReturnsOneItemThenNull() { var session = new ImportContentSession(_contentManager.Object); session.Set(_testItemIdentity1.ToString(), "TestContentType"); session.InitializeBatch(0, 1); var firstIdentity = session.GetNextInBatch(); var secondIdentity = session.GetNextInBatch(); var comparer = new ContentIdentity.ContentIdentityEqualityComparer(); Assert.That(comparer.Equals(_testItemIdentity1, firstIdentity)); Assert.That(secondIdentity, Is.Null); }