/// <summary> /// Publishes an existing child node /// </summary> /// <param name="node">The parent node</param> /// <param name="existingNode">The node to be published</param> /// <param name="culture">The culture name, or empty string for non-variants</param> /// <param name="assignedNodeName">The name to be given to the new node according to rule settings</param> private void PublishExistingChildNode(IContent node, IContent existingNode, string culture = "", string assignedNodeName = "") { if (existingNode == null) { return; } //If the parent is NOT published, abort process. if (!node.Published) { if (_logVerbose) { _logger.Info <AutoNode>(Resources.InfoAbortCreateNodeNodeExists); } return; } if (_logVerbose) { _logger.Info <AutoNode>(Resources.InfoRepublishingExistingNode); } if (!string.IsNullOrEmpty(culture) && !existingNode.AvailableCultures.Any(x => x.InvariantEquals(culture))) { ContentCultureInfos cinfo = new ContentCultureInfos(culture); cinfo.Name = string.IsNullOrEmpty(assignedNodeName) ? node.CultureInfos.Values.Where(x => !string.IsNullOrEmpty(x.Name)).FirstOrDefault().Name : assignedNodeName; existingNode.CultureInfos.Add(cinfo); } //Republish the node if there are no pending changes if (!existingNode.Edited) { var result = _cs.SaveAndPublish(existingNode, (string.IsNullOrEmpty(culture) ? "*": culture), raiseEvents: true); if (!result.Success) { _logger.Error <AutoNode>(String.Format(Resources.ErrorRepublishNoSuccess, existingNode.Name, node.Name)); } return; } }
public void Variant_Published_Culture_Names_Track_Dirty_Changes() { IContentType contentType = new ContentTypeBuilder() .WithAlias("contentType") .WithContentVariation(ContentVariation.Culture) .Build(); Content content = new ContentBuilder() .WithId(1) .WithVersionId(1) .WithName("content") .WithContentType(contentType) .Build(); const string langFr = "fr-FR"; content.ChangeContentType(contentType); Assert.IsFalse(content.IsPropertyDirty("PublishCultureInfos")); // hasn't been changed Thread.Sleep(500); // The "Date" wont be dirty if the test runs too fast since it will be the same date content.SetCultureName("name-fr", langFr); content.PublishCulture(CultureImpact.Explicit(langFr, false)); // we've set the name, now we're publishing it Assert.IsTrue(content.IsPropertyDirty("PublishCultureInfos")); // now it will be changed since the collection has changed ContentCultureInfos frCultureName = content.PublishCultureInfos[langFr]; Assert.IsTrue(frCultureName.IsPropertyDirty("Date")); content.ResetDirtyProperties(); Assert.IsFalse(content.IsPropertyDirty("PublishCultureInfos")); // it's been reset Assert.IsTrue(content.WasPropertyDirty("PublishCultureInfos")); Thread.Sleep(500); // The "Date" wont be dirty if the test runs too fast since it will be the same date content.SetCultureName("name-fr", langFr); content.PublishCulture(CultureImpact.Explicit(langFr, false)); // we've set the name, now we're publishing it Assert.IsTrue(frCultureName.IsPropertyDirty("Date")); Assert.IsTrue(content.IsPropertyDirty("PublishCultureInfos")); // it's true now since we've updated a name }
public void Variant_Culture_Names_Track_Dirty_Changes() { IContentType contentType = new ContentTypeBuilder() .WithAlias("contentType") .WithContentVariation(ContentVariation.Culture) .Build(); Mock.Get(_contentTypeService).As <IContentTypeBaseService>().Setup(x => x.Get(It.IsAny <int>())).Returns(contentType); Content content = new ContentBuilder() .WithId(1) .WithVersionId(1) .WithName("content") .WithContentType(contentType) .Build(); const string langFr = "fr-FR"; Assert.IsFalse(content.IsPropertyDirty("CultureInfos")); // hasn't been changed Thread.Sleep(500); // The "Date" wont be dirty if the test runs too fast since it will be the same date content.SetCultureName("name-fr", langFr); Assert.IsTrue(content.IsPropertyDirty("CultureInfos")); // now it will be changed since the collection has changed ContentCultureInfos frCultureName = content.CultureInfos[langFr]; Assert.IsTrue(frCultureName.IsPropertyDirty("Date")); content.ResetDirtyProperties(); Assert.IsFalse(content.IsPropertyDirty("CultureInfos")); // it's been reset Assert.IsTrue(content.WasPropertyDirty("CultureInfos")); Thread.Sleep(500); // The "Date" wont be dirty if the test runs too fast since it will be the same date content.SetCultureName("name-fr", langFr); Assert.IsTrue(frCultureName.IsPropertyDirty("Date")); Assert.IsTrue(content.IsPropertyDirty("CultureInfos")); // it's true now since we've updated a name }
=> (content.CultureInfos?.TryGetValue(culture, out ContentCultureInfos cultureInfo) ?? false) &&
/// <summary> /// Creates a new node. /// </summary> /// <param name="node">The parent node.</param> /// <param name="rule">The rule being processed</param> /// <param name="culture">The culture name, or empty string for non-variants</param> private void CreateNewNodeCultureAware(IContent node, AutoNodeRule rule, string culture) { if (_cts.Get(rule.DocTypeAliasToCreate) == null) { _logger.Error <AutoNode>(string.Format(Resources.ErrorNodeAliasNotFound, rule.DocTypeAliasToCreate)); return; } //Get the node name that is supposed to be given to the new node. string assignedNodeName = GetAssignedNodeName(node, rule, culture); //Get the first existing node of the type and name defined by the rule IContent existingNode = GetExistingChildNode(node, rule, assignedNodeName); IContent content = null; try { //If it exists already if (existingNode != null) { content = node; PublishExistingChildNode(content, existingNode, culture, assignedNodeName); } else { //If it doesn't exist, then create it and publish it. IContent bp = GetBlueprint(rule); if (bp != null) { content = _cs.CreateContentFromBlueprint(bp, assignedNodeName); content.SetParent(node); } else { content = _cs.Create(assignedNodeName, node.Key, rule.DocTypeAliasToCreate); if (!string.IsNullOrEmpty(culture)) { ContentCultureInfos cinfo = new ContentCultureInfos(culture); cinfo.Name = assignedNodeName; content.CultureInfos.Add(cinfo); } } bool success = false; //Keep new node unpublished only for non-variants. Variants come up with strange errors here! if (rule.KeepNewNodeUnpublished && string.IsNullOrEmpty(culture)) { var result = _cs.Save(content); success = result.Success; } else { //Publish the new node var result = (string.IsNullOrEmpty(culture)) ? _cs.SaveAndPublish(content, raiseEvents: true, culture: null) : _cs.SaveAndPublish(content, raiseEvents: true, culture: culture); success = result.Success; } if (!success) { _logger.Error <AutoNode>(String.Format(Resources.ErrorCreateNode, assignedNodeName, node.Name)); return; } } // Bring the new node first if rule dictates so if (rule.BringNewNodeFirst) { if (_logVerbose) { _logger.Info <AutoNode>(Resources.InfoSortingNodes); } IEnumerable <IContent> sortedNodes = Enumerable.Empty <IContent>(); if (existingNode == null) { sortedNodes = BringLastNodeFirst(node); } else { sortedNodes = BringExistingNodeFirst(node, existingNode); } //Only sort when more than 1 if (sortedNodes != Enumerable.Empty <IContent>()) { var result = _cs.Sort(sortedNodes.Select(x => x.Id), raiseEvents: false); if (!result.Success) { _logger.Error <AutoNode>(Resources.ErrorSortFailed); } } } if (_logVerbose) { _logger.Info <AutoNode>(String.Format(Resources.InfoCreateNodeSuccess, assignedNodeName, node.Name)); } } catch (Exception ex) { _logger.Error <AutoNode>(ex, Resources.ErrorGeneric); return; } }