SenseNet.ContentRepository.ContentNamingHelper.IncrementNameSuffixToLastName C# (CSharp) Метод

IncrementNameSuffixToLastName() публичный статический Метод

public static IncrementNameSuffixToLastName ( string currentName, int parentNodeId ) : string
currentName string
parentNodeId int
Результат string
        public static string IncrementNameSuffixToLastName(string currentName, int parentNodeId)
        {
            currentName = RepositoryPath.GetFileName(currentName);
            var ext = Path.GetExtension(currentName);
            string nameBase;
            bool inValidNumber;
            var fileName = Path.GetFileNameWithoutExtension(currentName);
            ContentNamingHelper.ParseSuffix(fileName, out nameBase, out inValidNumber);
            var lastName = DataProvider.Current.GetNameOfLastNodeWithNameBase(parentNodeId, nameBase, ext);
            
            // if there is no suffixed name in db, return with first variant
            if (lastName == null)
                return String.Format("{0}(1){1}", nameBase, ext);

            // there was a suffixed name in db in the form namebase(x), increment it
            // car(5)-> car(6), car(test)(5) -> car(test)(6), car(test) -> car(guid)
            string newNameBase;
            var newName = ContentNamingHelper.IncrementNameSuffix(lastName, out newNameBase);

            // incremented name base differs from original name base (for example 'car(hello)(2)' and 'car(5)')
            // edge case, use guid with original namebase
            if (newNameBase != nameBase)
                return String.Format("{0}({1}){2}", nameBase, Guid.NewGuid().ToString(), ext);

            // incremented name base and original name base are equal, so incremented name is correct (eg 'car(2)')
            return newName;
        }
        public static string IncrementNameSuffix(string name, out string nameBase)

Usage Example

Пример #1
0
        public void Execute()
        {
            if (this.Node.IsNew)
            {
                var gc = Node.Parent as GenericContent;
                if (gc != null)
                {
                    gc.AssertAllowedChildType(Node);
                }
            }

            var autoNamingAllowed = false;

            if (this.Node.AllowIncrementalNaming.HasValue)
            {
                autoNamingAllowed = this.Node.AllowIncrementalNaming.Value;
            }
            else
            {
                autoNamingAllowed = this.Node.Id == 0 && ContentType.GetByName(this.Node.NodeType.Name).AllowIncrementalNaming;
            }

            while (true)
            {
                try
                {
                    Node.Save(this);
                    break;
                }
                catch (Storage.Data.NodeAlreadyExistsException)
                {
                    if (!autoNamingAllowed)
                    {
                        throw;
                    }

                    this.Node.Name = ContentNamingHelper.IncrementNameSuffixToLastName(Node.Name, Node.ParentId);
                }
            }
        }
All Usage Examples Of SenseNet.ContentRepository.ContentNamingHelper::IncrementNameSuffixToLastName