Exemplo n.º 1
0
        public void CopyPart_ExistingOnlyInDraft()
        {
            var version = versions.AddVersion(root, asPreviousVersion: false);

            versionRepository.GetVersions(root).Count().ShouldBe(1);
            var part = CreateOneItem <Items.DataItem>(0, "part", version);

            part.ID       = 0;
            part.ZoneName = "ZoneOne";
            part.SetVersionKey("one");
            versionRepository.Save(version);

            request["item"]         = root.Path;
            request["below"]        = root.Path;
            request["versionKey"]   = part.GetVersionKey();
            request["versionIndex"] = version.VersionIndex.ToString();
            request["zone"]         = "ZoneTwo";

            var response = copyer.HandleRequest(request);

            var draft = versionRepository.GetVersions(root).Single();

            draft.Version.Children.Single(c => c.ZoneName == "ZoneTwo").Name.ShouldBe(null);
            draft.Version.Children.Single(c => c.ZoneName == "ZoneOne").Name.ShouldBe("part");
        }
Exemplo n.º 2
0
		protected void OnPreviewCommand(object sender, CommandEventArgs e)
		{
			var ctx = ie.CreateCommandContext();

            try
            {
                if (ctx.Content.VersionOf.HasValue)
                {
                    var draftOfTopEditor = ctx.Content.FindPartVersion(CurrentItem);
                    ie.UpdateObject(new CommandContext(ie.Definition, draftOfTopEditor, Interfaces.Editing, Context.User));

                    Repository.Save(ctx.Content);
                }
                else
                {
                    Commands.Save(ctx);
                }

                var page = Find.ClosestPage(ctx.Content);
                Url previewUrl = Engine.GetContentAdapter<NodeAdapter>(page).GetPreviewUrl(page);
                if (Request["edit"] == "drag")
                    previewUrl = previewUrl.SetQueryParameter("edit", "drag");

                if (ctx.Content.VersionOf.HasValue)
                {
                    var item = ie.CurrentItem;
                    previewUrl = previewUrl.SetQueryParameter(PathData.VersionIndexQueryKey, item.VersionIndex);
                }

                //Items that have no versions (first version) and are drafts, will be updated to unpublished state
                //  in order for Save and Preview to be able to work cross sites
                if (!ctx.Content.VersionOf.HasValue && ctx.Content.VersionIndex == 0)
                {
                    var item = ie.CurrentItem;
                    item.State = ContentState.Unpublished;
                    Engine.Persister.Save(item);
                }

                previewUrl = previewUrl.SetQueryParameter("Preview", "true");

                Engine.AddActivity(new ManagementActivity { Operation = "Preview", PerformedBy = User.Identity.Name, Path = ie.CurrentItem.Path, ID = ie.CurrentItem.ID });

                HandleResult(ctx, Request["returnUrl"], previewUrl);
            }
            catch (N2Exception ex)
            {
                SetErrorMessage(cvException, ex);
            }
        }
Exemplo n.º 3
0
        private string MoveItem(NameValueCollection request)
        {
            var         path = PartsExtensions.EnsureDraft(versions, versionRepository, navigator, request);
            ContentItem item = path.CurrentItem;
            ContentItem page = path.CurrentPage;

            item.ZoneName = request["zone"];

            var         beforeItem = PartsExtensions.GetBeforeItem(navigator, request, page);
            ContentItem parent;

            if (beforeItem != null)
            {
                parent = beforeItem.Parent;
                int newIndex = parent.Children.IndexOf(beforeItem);
                ValidateLocation(item, parent);
                Utility.Insert(item, parent, newIndex);
            }
            else
            {
                parent = PartsExtensions.GetBelowItem(navigator, request, page);
                ValidateLocation(item, parent);
                Utility.Insert(item, parent, parent.Children.Count);
            }

            Utility.UpdateSortOrder(parent.Children);
            versionRepository.Save(page);

            return(page.Url.ToUrl().SetQueryParameter("edit", "drag"));
        }
Exemplo n.º 4
0
        private string CreateItem(TemplateDefinition template, NameValueCollection request)
        {
            var path = new PathData(navigator.Navigate(request["below"]));

            if (!versionRepository.TryParseVersion(request[PathData.VersionIndexQueryKey], request[PathData.VersionKeyQueryKey], path))
            {
                path.CurrentItem = versions.AddVersion(path.CurrentItem, asPreviousVersion: false);
            }
            var parent = path.CurrentItem;

            ContentItem item = activator.CreateInstance(template.Definition.ItemType, parent);

            item.ZoneName    = request["zone"];
            item.TemplateKey = template.Name;

            string beforeVersionKey = request["beforeVersionKey"];
            string beforeSortOrder  = request["beforeSortOrder"];
            string before           = request["before"];

            if (string.IsNullOrEmpty(beforeSortOrder))
            {
                item.AddTo(parent);
            }
            else
            {
                int index = int.Parse(beforeSortOrder);
                parent.InsertChildBefore(item, index);
            }

            versionRepository.Save(parent);
            return(request["returnUrl"].ToUrl().SetQueryParameter(PathData.VersionIndexQueryKey, parent.VersionIndex));
        }
Exemplo n.º 5
0
        public void MovingPart_InDraft()
        {
            var version = versions.AddVersion(root, asPreviousVersion: false);

            versionRepository.GetVersions(root).Count().ShouldBe(1);
            var part = CreateOneItem <Items.DataItem>(0, "part", version);

            part.ID       = 0;
            part.ZoneName = "ZoneOne";
            part.SetVersionKey("one");
            versionRepository.Save(version);

            request[PathData.ItemQueryKey] = root.Path;
            request["below"] = root.Path;
            request[PathData.VersionKeyQueryKey]   = part.GetVersionKey();
            request[PathData.VersionIndexQueryKey] = version.VersionIndex.ToString();
            request["zone"] = "ZoneTwo";

            var response = mover.HandleRequest(request);

            var draft = versionRepository.GetVersions(root).Single();

            versionRepository.DeserializeVersion(draft).Children.Single().ZoneName.ShouldBe("ZoneTwo");
        }
Exemplo n.º 6
0
        public ContentVersion UpgradeVersion(ContentItem version)
        {
            using (var tx = itemRepository.BeginTransaction())
            {
                var clone = version.CloneForVersioningRecursive();
                clone.VersionOf = version.VersionOf.Value;

                foreach (var child in version.VersionOf.Children.FindParts())
                {
                    child.CloneForVersioningRecursive().AddTo(clone);
                }

                var newVersion = versionRepository.Save(clone);
                itemRepository.Delete(version);

                tx.Commit();

                return(newVersion);
            }
        }
Exemplo n.º 7
0
        private string CopyItem(NameValueCollection request)
        {
            var         path = PartsExtensions.EnsureDraft(versions, versionRepository, navigator, request);
            ContentItem item = path.CurrentItem;
            ContentItem page = path.CurrentPage;

            item          = item.Clone(true);
            item.Name     = null;
            item.ZoneName = request["zone"];
            foreach (var child in Find.EnumerateChildren(item, true, false))
            {
                child.SetVersionKey(Guid.NewGuid().ToString());
            }

            var         beforeItem = PartsExtensions.GetBeforeItem(navigator, request, page);
            ContentItem parent;

            if (beforeItem != null)
            {
                parent = beforeItem.Parent;
                int newIndex = parent.Children.IndexOf(beforeItem);
                ValidateLocation(item, parent);
                Utility.Insert(item, parent, newIndex);
            }
            else
            {
                parent = PartsExtensions.GetBelowItem(navigator, request, page);
                ValidateLocation(item, parent);
                Utility.Insert(item, parent, parent.Children.Count);
            }

            Utility.UpdateSortOrder(parent.Children);
            versionRepository.Save(page);

            return(page.Url.ToUrl().SetQueryParameter("edit", "drag"));
        }
        public override NameValueCollection HandleRequest(NameValueCollection request)
        {
            bool isNew    = false;
            var  response = new NameValueCollection();

            try
            {
                // locate the item to be modified
                var original = FindItem(versions, versionRepository, navigator, request);

                var disc = request["discriminator"];
                if (definitionManager != null && !string.IsNullOrEmpty(disc) && !definitionManager.GetDefinition(original).Discriminator.Equals(disc))
                {
                    throw new Exception("unexpected part type for " + original);
                }
#if DEBUG2
                origPage = Find.ClosestPage(original);
                Dump(origPage);
                Logger.DebugFormat("AJAX found item {0} on page {1}", original, origPage);
#endif
                // get or create a draft to work with
                var path = PartsExtensions.EnsureDraft(original, versions, versionRepository, out isNew);
                var item = path.CurrentItem;
                if (definitionManager != null && !string.IsNullOrEmpty(disc) && !definitionManager.GetDefinition(item).Discriminator.Equals(disc))
                {
                    throw new Exception("unexpected part type for " + item);
                }

                Page = path.CurrentPage;
#if DEBUG2
                if (isNew || page != origPage)
                {
                    Logger.DebugFormat("AJAX changing item {0} on page {1}", item, page);
                }
#endif
                // perform action on item
                if (UpdateItem(request, item))
                {
                    // ensure proper user name
                    if (Principal != null)
                    {
                        Page.SavedBy = item.SavedBy = Principal.Identity.Name;
                    }
                    versionRepository.Save(Page);
                    Logger.DebugFormat("AJAX saved page {0} by {1}", Page, Page.SavedBy);
#if DEBUG
                    Dump(Page);
#endif
                }
                //response["redirect"] = isNew ? (string) Page.Url.ToUrl().SetQueryParameter("edit", "drag") : "#";
                response["redirect"] = (string)Page.Url.ToUrl().SetQueryParameter("edit", "drag").SetQueryParameter("random", Guid.NewGuid().ToString());
            }
            catch (Exception ex)
            {
                if (isNew)
                {
                    Logger.ErrorFormat("TODO Rollback Draft {0} after {1}", Page, ex.Message);
                    // TODO
                }

                throw;
            }
            return(response);
        }
        public void MasterVersion_CanBeSavedAsVersion_AndRetrieved()
        {
            var master = CreateOneItem <NormalPage>(0, "pageX", null);

            persister.Save(master);

            var draft = repository.Save(master);

            repository.Repository.Dispose();

            var savedDraft = repository.GetVersion(master);

            savedDraft.Published.ShouldBe(master.Published, TimeSpan.FromSeconds(10));
            //savedDraft.PublishedBy.ShouldBe(master.SavedBy);
            savedDraft.Saved.ShouldBe(N2.Utility.CurrentTime(), TimeSpan.FromSeconds(10));
            savedDraft.SavedBy.ShouldBe(draft.SavedBy);
            savedDraft.State.ShouldBe(master.State);
            savedDraft.VersionIndex.ShouldBe(master.VersionIndex);
            savedDraft.VersionDataXml.ShouldContain("pageX");
        }