Example #1
0
        public static string IncrementNameSuffix(string name)
        {
            name = RepositoryPath.GetFileName(name);
            var ext      = Path.GetExtension(name);
            var fileName = Path.GetFileNameWithoutExtension(name);

            string nameBase;
            var    index = ContentNamingHelper.ParseSuffix(fileName, out nameBase);

            if (index < 0)
            {
                index = 0;
            }
            return(String.Format("{0}({1}){2}", nameBase, ++index, ext));
        }
Example #2
0
        public static string IncrementNameSuffixToLastName(string currentName, int parentNodeId)
        {
            currentName = RepositoryPath.GetFileName(currentName);
            var    ext = Path.GetExtension(currentName);
            string nameBase;
            var    fileName = Path.GetFileNameWithoutExtension(currentName);
            var    count    = ContentNamingHelper.ParseSuffix(fileName, out nameBase);

            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)
            return(ContentNamingHelper.IncrementNameSuffix(lastName));
        }
Example #3
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);
                }
            }
        }
Example #4
0
        public static void Restore(TrashBag trashBag, string targetPath, bool addNewName)
        {
            if (trashBag == null || string.IsNullOrEmpty(targetPath))
            {
                throw new RestoreException(RestoreResultType.Nonedefined);
            }

            targetPath = targetPath.TrimEnd(new [] { '/' });

            var node = trashBag.DeletedContent;

            if (node == null)
            {
                throw new InvalidOperationException("TrashBag is empty");
            }

            var targetContentPath = RepositoryPath.Combine(targetPath, node.Name);
            var targetParent      = Node.Load <GenericContent>(targetPath);

            if (targetParent == null)
            {
                throw new RestoreException(RestoreResultType.NoParent,
                                           RepositoryPath.GetParentPath(targetPath));
            }

            //assert permissions
            if (!targetParent.Security.HasPermission(PermissionType.Open))
            {
                throw new RestoreException(RestoreResultType.PermissionError, targetContentPath);
            }

            //target type check: ContentTypes field
            AssertRestoreContentType(targetParent, node);

            if (Node.Exists(targetContentPath))
            {
                var newName = ContentNamingHelper.IncrementNameSuffixToLastName(node.Name, targetParent.Id);
                targetContentPath = RepositoryPath.Combine(targetPath, newName);

                if (addNewName)
                {
                    try
                    {
                        //there is no other way right now (rename and move cannot be done at the same time)
                        node.Name = newName;
                        node.Save();
                    }
                    catch (SecurityException ex)
                    {
                        Logger.WriteException(ex);
                        throw new RestoreException(RestoreResultType.PermissionError,
                                                   targetContentPath, ex);
                    }
                    catch (Exception ex)
                    {
                        Logger.WriteException(ex);
                        throw new RestoreException(RestoreResultType.UnknownError,
                                                   targetContentPath, ex);
                    }
                }
                else
                {
                    throw new RestoreException(RestoreResultType.ExistingName,
                                               targetContentPath);
                }
            }

            var originalUser = User.Current;

            try
            {
                node.MoveTo(targetParent);

                AccessProvider.Current.SetCurrentUser(User.Administrator);

                trashBag.KeepUntil = DateTime.Today.AddDays(-1);
                trashBag.ForceDelete();
            }
            catch (SecurityException ex)
            {
                Logger.WriteException(ex);
                throw new RestoreException(RestoreResultType.PermissionError,
                                           targetContentPath, ex);
            }
            catch (Exception ex)
            {
                Logger.WriteException(ex);
                throw new RestoreException(RestoreResultType.UnknownError,
                                           targetContentPath, ex);
            }
            finally
            {
                AccessProvider.Current.SetCurrentUser(originalUser);
            }
        }