Пример #1
0
        /// <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;
            }
        }
 public IContent CreateContentFromBlueprint(IContent blueprint, string name, int userId = -1)
 {
     return(inner.CreateContentFromBlueprint(blueprint, name, userId));
 }