예제 #1
0
        /// <summary>
        /// Gets the predefined name for the newly created node. This can either be a dictionary entry for multilingual installations or a standard string
        /// </summary>
        /// <param name="node">The node under which the new node will be created</param>
        /// <param name="rule">The rule being processed</param>
        /// <param name="culture">The culture name, or empty string for non-variants</param>
        /// <returns></returns>
        private string GetAssignedNodeName(IContent node, AutoNodeRule rule, string culture)
        {
            string assignedNodeName = null;

            //Get the dictionary item if a dictionary key has been specified in config
            if (rule.DictionaryItemForName != "")
            {
                try
                {
                    var lsvc = Current.Services.LocalizationService;
                    if (!string.IsNullOrEmpty(culture))
                    {
                        assignedNodeName = lsvc.GetDictionaryItemByKey(rule.DictionaryItemForName).Translations.First(t => t.Language.CultureInfo.Name.InvariantEquals(culture)).Value;
                    }
                    else
                    {
                        assignedNodeName = lsvc.GetDictionaryItemByKey(rule.DictionaryItemForName).Translations.First().Value;
                    }
                }
                catch (Exception ex)
                {
                    _logger.Error <AutoNode>(ex, Resources.ErrorDictionaryKeyNotFound);
                }
            }

            //If no dictionary key has been found, fallback to the standard name setting
            if (string.IsNullOrEmpty(assignedNodeName))
            {
                assignedNodeName = rule.NodeName;
            }

            return(assignedNodeName);
        }
예제 #2
0
        /// <summary>
        /// Gets an existing child node
        /// </summary>
        /// <param name="node">The parent node</param>
        /// <param name="rule">The rule being processed</param>
        /// <param name="assignedNodeName">The name the rule dictates for a new node.
        /// This will be used when checking whether to create a new node or not,
        /// depending on whether the rule's setting "createIfExistsWithDifferentName" is set to true</param>
        /// <returns>Null if there is no existing node fulfilling the critera or the node if it exists.</returns>
        private IContent GetExistingChildNode(IContent node, AutoNodeRule rule, string assignedNodeName = "")
        {
            //TODO: trycatch and exit if not found
            int typeIdToCreate = _cts.Get(rule.DocTypeAliasToCreate).Id;

            long totalRecords;
            var  query = new Query <IContent>(Current.SqlContext);

            //Find if an existing node is already there.
            //If we find an existing node a new one will NOT be created.
            //An existing node can be, depending on configuration, a node of the same type OR a node of the same type with the same name.
            IContent existingNode = null;

            if (_cs.HasChildren(node.Id))
            {
                existingNode = _cs.GetPagedChildren(node.Id, 0, 1, out totalRecords
                                                    , filter: query.Where(
                                                        x => x.ContentTypeId == typeIdToCreate &&
                                                        (x.Name.Equals(assignedNodeName, StringComparison.CurrentCultureIgnoreCase) || !rule.CreateIfExistsWithDifferentName)
                                                        )
                                                    ).FirstOrDefault();
            }

            return(existingNode);
        }
예제 #3
0
        private List <AutoNodeRule> GetRules()
        {
            List <AutoNodeRule> retVal = new List <AutoNodeRule>();

            foreach (XmlNode xmlConfigEntry in XmlConfig.SelectNodes("/autoNode/rule"))
            {
                if (xmlConfigEntry.NodeType == XmlNodeType.Element)
                {
                    string createdDocTypeAlias  = xmlConfigEntry.Attributes["createdDocTypeAlias"].Value;
                    string docTypeAliasToCreate = xmlConfigEntry.Attributes["docTypeAliasToCreate"].Value;
                    string nodeName             = xmlConfigEntry.Attributes["nodeName"].Value;

                    bool bringNewNodeFirst = (xmlConfigEntry.Attributes["bringNewNodeFirst"] != null)
                        ? bool.Parse(xmlConfigEntry.Attributes["bringNewNodeFirst"].Value)
                        : false;

                    bool onlyCreateIfNoChildren = (xmlConfigEntry.Attributes["onlyCreateIfNoChildren"] != null)
                        ? bool.Parse(xmlConfigEntry.Attributes["onlyCreateIfNoChildren"].Value)
                        : false;

                    bool createIfExistsWithDifferentName = (xmlConfigEntry.Attributes["createIfExistsWithDifferentName"] != null)
                        ? bool.Parse(xmlConfigEntry.Attributes["createIfExistsWithDifferentName"].Value)
                        : true;

                    string dictionaryItemForName = (xmlConfigEntry.Attributes["dictionaryItemForName"] != null)
                      ? xmlConfigEntry.Attributes["dictionaryItemForName"].Value
                      : "";

                    bool keepNewNodeUnpublished = (xmlConfigEntry.Attributes["keepNewNodeUnpublished"] != null)
                      ? bool.Parse(xmlConfigEntry.Attributes["keepNewNodeUnpublished"].Value)
                      : false;

                    string blueprint = (xmlConfigEntry.Attributes["blueprint"] != null)
                      ? xmlConfigEntry.Attributes["blueprint"].Value
                      : "";

                    var rule = new AutoNodeRule(
                        createdDocTypeAlias
                        , docTypeAliasToCreate
                        , nodeName
                        , bringNewNodeFirst
                        , onlyCreateIfNoChildren
                        , createIfExistsWithDifferentName
                        , dictionaryItemForName
                        , keepNewNodeUnpublished
                        , blueprint);

                    retVal.Add(rule);
                }
            }
            _logger.Info <AutoNode>(Resources.InfoLoadConfigComplete);

            _rules = retVal;

            return(retVal);
        }
예제 #4
0
        /// <summary>
        /// Creates a new node under a given node, according to settings of the rule in effect
        /// </summary>
        /// <param name="node">The node to create a new node under</param>
        /// <param name="rule">The rule that will apply settings for the new node's creation</param>
        /// <param name="hasChildren">Indicates if the node has children</param>
        /// <param name="culture">The culture name, or empty string for non-variants</param>
        private void CreateOrPublishNode(IContent node, AutoNodeRule rule, bool hasChildren, string culture = "")
        {
            if (_logVerbose)
            {
                _logger.Info <AutoNode>(Resources.InfoTryCreateNode, rule.DocTypeAliasToCreate, node.Id.ToString(), node.ContentType.Alias.ToString());
            }

            //If rule says only if no children and there are children, abort process
            if (rule.OnlyCreateIfNoChildren && hasChildren)
            {
                if (_logVerbose)
                {
                    _logger.Info <AutoNode>(Resources.InfoAbortCreateNodeRuleRestrictions);
                }
                return;
            }

            CreateNewNodeCultureAware(node, rule, culture);
        }
예제 #5
0
        /// <summary>
        /// Gets a blueprint specified on a rule.
        /// </summary>
        /// <param name="rule">The rule in which the blueprint is specified</param>
        /// <returns>Null if the blueprint is not found</returns>
        private IContent GetBlueprint(AutoNodeRule rule)
        {
            if (string.IsNullOrEmpty(rule.Blueprint))
            {
                return(null);
            }
            var contentTypeId = _cts.GetAllContentTypeIds(new string[] { rule.DocTypeAliasToCreate }).FirstOrDefault();

            if (contentTypeId <= 0)
            {
                return(null);
            }
            var bps = _cs.GetBlueprintsForContentTypes(contentTypeId);

            if (bps == null || bps.Count() == 0)
            {
                return(null);
            }
            var bp = bps.Where(x => x.Name == rule.Blueprint).FirstOrDefault();

            return(bp);
        }
예제 #6
0
 /// <summary>
 /// Registers a new rule object
 /// </summary>
 /// <param name="rule">The rule object</param>
 public void RegisterRule(AutoNodeRule rule)
 {
     _rules.Add(rule);
 }
예제 #7
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;
            }
        }