private void Import(XElement element) { var importContentSession = new ImportContentSession(_contentManager); _contentManager.Import(element, importContentSession); _contentManager.CompleteImport(element, importContentSession); }
/* * <Settings> * <SiteSettingsPart PageSize="30" /> * <CommentSettingsPart ModerateComments="true" /> * </Settings> */ // Set site and part settings. public override void Execute(RecipeExecutionContext context) { var siteContentItem = _siteService.GetSiteSettings().ContentItem; var importContentSession = new ImportContentSession(_contentManager); var importContentContext = new ImportContentContext(siteContentItem, context.RecipeStep.Step, importContentSession); foreach (var contentHandler in Handlers) { contentHandler.Importing(importContentContext); } foreach (var contentPart in siteContentItem.Parts) { var partElement = importContentContext.Data.Element(contentPart.PartDefinition.Name); if (partElement == null) { continue; } Logger.Information("Importing settings part '{0}'.", contentPart.PartDefinition.Name); try { ImportSettingPart(contentPart, partElement); } catch (Exception ex) { Logger.Error(ex, "Error while importing settings part '{0}'.", contentPart.PartDefinition.Name); throw; } } foreach (var contentHandler in Handlers) { contentHandler.Imported(importContentContext); } }
// <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; }
public void GetNextInBatchReturnsNullWhenInitializedButNoItemsSet() { var importContentSession = new ImportContentSession(_contentManager.Object); importContentSession.InitializeBatch(0, 20); 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); }
// <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.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) { _orchardServices.ContentManager.Import(elementDictionary[nextIdentity], importContentSession); nextIdentity = importContentSession.GetNextInBatch(); } startIndex += batchSize; //Create a new transaction for each batch if (startIndex < elementDictionary.Count) { _orchardServices.ContentManager.Clear(); _transactionManager.RequireNew(); } } } catch (Exception) { //Ensure a failed batch is rolled back _transactionManager.Cancel(); throw; } recipeContext.Executed = true; }
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()); }
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 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 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 override void Execute(RecipeExecutionContext context) { var blueprintEntries = context.RecipeStep.Step.Elements().Select(xmlBlueprint => { var typeName = xmlBlueprint.Attribute("ElementTypeName").Value; Logger.Information("Importing custom element '{0}'.", typeName); try { var blueprint = GetOrCreateElement(typeName); blueprint.BaseElementTypeName = xmlBlueprint.Attribute("BaseElementTypeName").Value; blueprint.ElementDisplayName = xmlBlueprint.Attribute("ElementDisplayName").Value; blueprint.ElementDescription = xmlBlueprint.Attribute("ElementDescription").Value; blueprint.ElementCategory = xmlBlueprint.Attribute("ElementCategory").Value; blueprint.BaseElementState = xmlBlueprint.Element("BaseElementState").Value; var describeContext = DescribeElementsContext.Empty; var descriptor = _elementManager.GetElementDescriptorByTypeName(describeContext, blueprint.BaseElementTypeName); var baseElement = _elementManager.ActivateElement(descriptor); baseElement.Data = ElementDataHelper.Deserialize(blueprint.BaseElementState); baseElement.ExportableData = ElementDataHelper.Deserialize(xmlBlueprint.Attribute("BaseExportableData").Value); return(new { Blueprint = blueprint, BaseElement = baseElement }); } catch (Exception ex) { Logger.Error(ex, "Error while importing custom element '{0}'.", typeName); throw; } }).ToList(); var baseElements = blueprintEntries.Select(e => e.BaseElement).ToList(); var importContentSession = new ImportContentSession(_orchardServices.ContentManager); var importLayoutContext = new ImportLayoutContext { Session = new ImportContentSessionWrapper(importContentSession) }; _elementManager.Importing(baseElements, importLayoutContext); _elementManager.Imported(baseElements, importLayoutContext); _elementManager.ImportCompleted(baseElements, importLayoutContext); foreach (var blueprintEntry in blueprintEntries) { blueprintEntry.Blueprint.BaseElementState = blueprintEntry.BaseElement.Data.Serialize(); } }
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); }
/* * <Settings> * <SiteSettingsPart PageSize="30" /> * <CommentSettingsPart ModerateComments="true" /> * </Settings> */ // Set site and part settings. public void ExecuteRecipeStep(RecipeContext recipeContext) { if (!String.Equals(recipeContext.RecipeStep.Name, "Settings", StringComparison.OrdinalIgnoreCase)) { return; } var siteContentItem = _siteService.GetSiteSettings().ContentItem; var importContentSession = new ImportContentSession(_contentManager); var context = new ImportContentContext(siteContentItem, recipeContext.RecipeStep.Step, importContentSession); foreach (var contentHandler in Handlers) { contentHandler.Importing(context); } foreach (var contentPart in siteContentItem.Parts) { var partElement = context.Data.Element(contentPart.PartDefinition.Name); if (partElement == null) { continue; } if (!string.IsNullOrEmpty(recipeContext.ExecutionId)) { _recipeJournal.WriteJournalEntry(recipeContext.ExecutionId, T("Setting: {0}.", contentPart.PartDefinition.Name).Text); } ImportSettingPart(contentPart, partElement); } foreach (var contentHandler in Handlers) { contentHandler.Imported(context); } recipeContext.Executed = true; }
public void CompleteImport(XElement element, ImportContentSession importContentSession) { throw new NotImplementedException(); }
private void BatchedInvoke(RecipeExecutionContext context, string batchLabel, Action <string, string, XElement, ImportContentSession, IDictionary <string, XElement> > contentItemAction) { var importContentSession = new ImportContentSession(_tomeltServices.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; } }
public ImportContentContext(ContentItem contentItem, XElement data, ImportContentSession importContentSession) : base(contentItem) { Data = data; Session = importContentSession; }
public IdentifyDependenciesContext(XElement contentElement, ImportContentSession importContentSession) { ContentElement = contentElement; ImportContentSession = importContentSession; }
public ImportContentSessionWrapper(ImportContentSession session) { _session = session; }
// <Data /> // Import Data. public override void Execute(RecipeExecutionContext context) { 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 startIndex = 0; var itemIndex = 0; var batchSize = GetBatchSizeForDataStep(context.RecipeStep.Step); Logger.Debug("Using batch size {0}.", batchSize); // Run the import. try { while (startIndex < elementDictionary.Count) { Logger.Debug("Importing batch starting at index {0}.", 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 = ""; if (elementDictionary[nextIdentity.ToString()].HasAttributes) { itemId = elementDictionary[nextIdentity.ToString()].FirstAttribute.Value; } Logger.Information("Importing data item '{0}' (item {1}/{2}).", itemId, itemIndex + 1, elementDictionary.Count); try { _orchardServices.ContentManager.Import( elementDictionary[nextIdentity.ToString()], importContentSession); } catch (Exception ex) { Logger.Error(ex, "Error while importing data item '{0}'.", itemId); throw; } itemIndex++; nextIdentity = importContentSession.GetNextInBatch(); } startIndex += batchSize; // Create a new transaction for each batch. if (startIndex < elementDictionary.Count) { _transactionManager.RequireNew(); } Logger.Debug("Finished importing batch starting at index {0}.", startIndex); } } catch (Exception) { // Ensure a failed batch is rolled back. _transactionManager.Cancel(); throw; } }
public void Import(XElement element, ImportContentSession importContentSession) { throw new System.NotImplementedException(); }
public void CompleteImport(XElement element, ImportContentSession importContentSession) { _decoratedService.CompleteImport(element, importContentSession); }