// <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; }
/* <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); } }
private void Replace(ImportSyncAction action, ImportContentSession session) { // update the identifier on the item in the local instance // then let the import continue so the existing item gets paved over if (action.Action == "Replace" && !string.IsNullOrWhiteSpace(action.TargetId)) { var item = session.Get(action.TargetId); if (item == null) { return; } var newIdentifier = action.Step.Step.Attribute("Id"); if (newIdentifier == null) return; var newIdentity = new ContentIdentity(newIdentifier.Value); var existingIdentity = new ContentIdentity(action.TargetId); if (!newIdentity.Equals(existingIdentity)) { Logger.Debug("import - items {0} and {1} have different identifiers", existingIdentity.Get("Identifier"), newIdentity.Get("Identifier")); item.As<IdentityPart>().Identifier = newIdentity.Get("Identifier"); session.Store(newIdentity.Get("Identifier"), item); } } }
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)); }
/* <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; }
// <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; }
private bool LocalIdentifierExists(ImportSyncAction identifier, ImportContentSession importContentSession) { var newIdentifier = identifier.Step.Step.Attribute("Id"); if (newIdentifier == null) return false; var contentItem = importContentSession.Get(newIdentifier.Value); if (contentItem == null) return false; return true; }
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 CompleteImport(XElement element, ImportContentSession importContentSession) { var elementId = element.Attribute("Id"); if (elementId == null) { return; } var identity = elementId.Value; if (String.IsNullOrWhiteSpace(identity)) { return; } var item = importContentSession.Get(identity, VersionOptions.Latest, XmlConvert.DecodeName(element.Name.LocalName)); var context = new ImportContentContext(item, element, importContentSession); Handlers.Invoke(contentHandler => contentHandler.ImportCompleted(context), Logger); }
public void Import(IEnumerable<ImportSyncAction> actions, bool rollback) { _transactionManager.Demand(); try { // process replacements var importContentSession = new ImportContentSession(_contentManager); foreach (var sync in actions.Where(a => a.Action == "Replace")) { Logger.Debug("{0}, {1}", sync.Action, sync.TargetId); if (!LocalIdentifierExists(sync, importContentSession)) { Replace(sync, importContentSession); } else { throw new Exception(string.Format("{0} already exists, will not perform a replace import", sync.TargetId)); } } // import importContentSession = new ImportContentSession(_contentManager); foreach (var action in actions) { ImportItem(action, importContentSession); Logger.Debug("Imported a " + action.Step.Name); } if (rollback) { _transactionManager.Cancel(); Logger.Debug("Rollback import transaction"); } } catch (Exception ex) { Logger.Error("There was an error importing", ex); _transactionManager.Cancel(); throw; } finally { } }
public IEnumerable<RemoteContentItem> Fetch(Uri remoteInstanceRoot) { List<RemoteContentItem> remoteContentItems = new List<RemoteContentItem>(); var recipe = FetchRecipe(remoteInstanceRoot); var importContentSession = new ImportContentSession(_orchardServices.ContentManager); foreach (var step in recipe.RecipeSteps) { foreach (var element in step.Step.Elements()) { if (!ContentSync.SyncableContentTypes.Contains(element.Name.LocalName)) continue; var elementId = element.Attribute("Id"); if (elementId == null) continue; var identity = elementId.Value; var status = element.Attribute("Status"); var item = _orchardServices.ContentManager.New(element.Name.LocalName); var context = new ImportContentContext(item, element, importContentSession); foreach (var contentHandler in _handlers.Value) { contentHandler.Importing(context); } foreach (var contentHandler in _handlers.Value) { contentHandler.Imported(context); } remoteContentItems.Add(new RemoteContentItem() { ContentItem = item, Xml = element }); } } _orchardServices.ContentManager.Clear(); return remoteContentItems; }
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 GetNextInBatchReturnsNullWhenInitializedButNoItemsSet() { var importContentSession = new ImportContentSession(_contentManager.Object); importContentSession.InitializeBatch(0, 20); Assert.That(importContentSession.GetNextInBatch(), Is.Null); }
private void Import(XElement element) { var importContentSession = new ImportContentSession(_contentManager); _contentManager.Import(element, importContentSession); _contentManager.CompleteImport(element, importContentSession); }
// 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); } }
private void ImportItem(ImportSyncAction action, ImportContentSession session) { _contentManager.Import(action.Step.Step, session); }
public ActionResult Export(string[] sync, string direction) { if (sync == null) { return RedirectToAction("Different"); } var settings = _contentSyncSettingsRepository.Table.SingleOrDefault(); if (settings == null || string.IsNullOrWhiteSpace(settings.RemoteUrl)) { _services.Notifier.Add(NotifyType.Warning, new LocalizedString("You need to set a remote Orchard instance url")); return RedirectToAction("Settings"); } string remote = settings.RemoteUrl; StringBuilder result = new StringBuilder(); ImportContentSession importContentSession = new ImportContentSession(_contentManager); XDocument synchronisation = new XDocument(); synchronisation.Add(new XElement("ContentSync")); foreach (var action in sync) { XElement syncXml = new XElement("Sync"); var contentItem = importContentSession.Get(action); if (contentItem == null) { // todo: log continue; } syncXml.Add(new XAttribute("Action", "Import")); syncXml.Add(_contentManager.Export(contentItem)); synchronisation.Element("ContentSync").Add(syncXml); } Logger.Debug(synchronisation.ToString()); // send to other server string remoteImportEndpoint = remote + "/Admin/ContentImportExport/Import"; HttpWebRequest post = HttpWebRequest.Create(remoteImportEndpoint) as HttpWebRequest; post.Method = "POST"; using (var requestStream = post.GetRequestStream()) using (var requestWriter = new StreamWriter(requestStream, Encoding.UTF8)) { requestWriter.Write(synchronisation.ToString()); requestWriter.Flush(); } try { using (var response = post.GetResponse() as HttpWebResponse) { if (response.StatusCode == HttpStatusCode.Accepted) { _services.Notifier.Add(NotifyType.Information, new LocalizedString("The content was published successfully")); _signals.Trigger(SynchronisationMapFactory.MapInvalidationTrigger); } else { _services.Notifier.Add(NotifyType.Error, new LocalizedString("A problem occurred: " + response.StatusDescription)); } } } catch (Exception ex) { _services.Notifier.Add(NotifyType.Error, new LocalizedString("A problem occurred: " + ex.Message)); } return RedirectToAction("Different"); }
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; } }
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 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); }