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 virtual ContentItem Clone(ContentItem contentItem) { // Mostly taken from: http://orchard.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)); }
// <Data /> // Import Data public void ExecuteRecipeStep(RecipeContext recipeContext) { if (!String.Equals(recipeContext.RecipeStep.Name, "Data", StringComparison.OrdinalIgnoreCase)) { return; } // First pass to resolve content items from content identities for all content items, new and old. var importContentSession = new ImportContentSession(_orchardServices.ContentManager); foreach (var element in recipeContext.RecipeStep.Step.Elements()) { var elementId = element.Attribute("Id"); if (elementId == null) continue; var identity = elementId.Value; var status = element.Attribute("Status"); importContentSession.Set(identity, element.Name.LocalName); var item = importContentSession.Get(identity); } // Second pass to import the content items. foreach (var element in recipeContext.RecipeStep.Step.Elements()) { _orchardServices.ContentManager.Import(element, importContentSession); } recipeContext.Executed = true; }
// <Data /> // Import Data public void ExecuteRecipeStep(RecipeContext recipeContext) { if (!String.Equals(recipeContext.RecipeStep.Name, "Data", StringComparison.OrdinalIgnoreCase)) { return; } var importContentSession = new ImportContentSession(_orchardServices.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, 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 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 var nextIdentity = importContentSession.GetNextInBatch(); while (nextIdentity != null) { if (!string.IsNullOrEmpty(recipeContext.ExecutionId) && elementDictionary[nextIdentity.ToString()].HasAttributes) { var itemId = elementDictionary[nextIdentity.ToString()].FirstAttribute.Value; _recipeJournal.WriteJournalEntry(recipeContext.ExecutionId, T("Data: Importing {0}.", itemId).Text); } _orchardServices.ContentManager.Import( elementDictionary[nextIdentity.ToString()], importContentSession); nextIdentity = importContentSession.GetNextInBatch(); } startIndex += batchSize; //Create a new transaction for each batch if (startIndex < elementDictionary.Count) { _transactionManager.RequireNew(); } } } catch (Exception) { //Ensure a failed batch is rolled back _transactionManager.Cancel(); throw; } recipeContext.Executed = true; }
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 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); }
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); }
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 GetItemExistsAndLatestVersionOptionReturnsPublishedItem() { 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()); }
private void BatchedInvoke(RecipeExecutionContext context, string batchLabel, Action<string, string, XElement, ImportContentSession, IDictionary<string, XElement>> contentItemAction) { var importContentSession = new ImportContentSession(_orchardServices.ContentManager); // Populate local dictionary with elements and their ids. var elementDictionary = CreateElementDictionary(context.RecipeStep.Step); // Populate import session with all identities to be imported. foreach (var identity in elementDictionary.Keys) { importContentSession.Set(identity, elementDictionary[identity].Name.LocalName); } // Determine if the import is to be batched in multiple transactions. var batchSize = GetBatchSizeForDataStep(context.RecipeStep.Step); var startIndex = 0; var itemIndex = 0; Logger.Debug("Using batch size {0} for '{1}'.", batchSize, batchLabel); try { while (startIndex < elementDictionary.Count) { Logger.Debug("Batch '{0}' execution starting at index {1}.", batchLabel, startIndex); 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. var nextIdentity = importContentSession.GetNextInBatch(); while (nextIdentity != null) { var itemId = ""; var nextIdentityValue = nextIdentity.ToString(); if (elementDictionary[nextIdentityValue].HasAttributes) { itemId = elementDictionary[nextIdentityValue].FirstAttribute.Value; } Logger.Information("Handling content item '{0}' (item {1}/{2} of '{3}').", itemId, itemIndex + 1, elementDictionary.Count, batchLabel); try { contentItemAction(itemId, nextIdentityValue, elementDictionary[nextIdentityValue], importContentSession, elementDictionary); } catch (Exception ex) { Logger.Error(ex, "Error while handling content item '{0}' (item {1}/{2} of '{3}').", itemId, itemIndex + 1, elementDictionary.Count, batchLabel); throw; } itemIndex++; nextIdentity = importContentSession.GetNextInBatch(); } startIndex += batchSize; // Create a new transaction for each batch. if (startIndex < elementDictionary.Count) { _transactionManager.RequireNew(); } Logger.Debug("Finished batch '{0}' starting at index {1}.", batchLabel, startIndex); } } catch (Exception) { // Ensure a failed batch is rolled back. _transactionManager.Cancel(); throw; } }