コード例 #1
0
        public virtual ContentItem Clone(ContentItem contentItem)
        {
            var element = Export(contentItem);

            // If a handler prevents this element from being exported, it can't be cloned.
            if (element == null)
            {
                throw new InvalidOperationException("The content item couldn't be cloned because a handler prevented it from being exported.");
            }

            var elementId = element.Attribute("Id");
            var copyId    = elementId.Value + "-copy";

            elementId.SetValue(copyId);
            var status = element.Attribute("Status");

            if (status != null)
            {
                status.SetValue("Draft");                 // So the copy is always a draft.
            }
            var importContentSession = new ImportContentSession(this);

            importContentSession.Set(copyId, element.Name.LocalName);
            Import(element, importContentSession);
            CompleteImport(element, importContentSession);

            return(importContentSession.Get(copyId, element.Name.LocalName));
        }
コード例 #2
0
        public void CompleteImport(XElement element, ImportContentSession importContentSession)
        {
            var elementId = element.Attribute("Id");

            if (elementId == null)
            {
                return;
            }

            var identity = elementId.Value;

            if (String.IsNullOrWhiteSpace(identity))
            {
                return;
            }

            var item    = importContentSession.Get(identity, VersionOptions.Latest, XmlConvert.DecodeName(element.Name.LocalName));
            var context = new ImportContentContext(item, element, importContentSession);

            Handlers.Invoke(contentHandler => contentHandler.ImportCompleted(context), Logger);
        }
コード例 #3
0
        // Insert or Update imported data into the content manager.
        // Call content item handlers.
        public void Import(XElement element, ImportContentSession importContentSession)
        {
            var elementId = element.Attribute("Id");

            if (elementId == null)
            {
                return;
            }

            var identity = elementId.Value;

            if (String.IsNullOrWhiteSpace(identity))
            {
                return;
            }

            var status = element.Attribute("Status");

            var item = importContentSession.Get(identity, VersionOptions.Latest, XmlConvert.DecodeName(element.Name.LocalName));

            if (item == null)
            {
                item = New(XmlConvert.DecodeName(element.Name.LocalName));
                if (status != null && status.Value == "Draft")
                {
                    Create(item, VersionOptions.Draft);
                }
                else
                {
                    Create(item);
                }
            }
            else
            {
                // If the existing item is published, create a new draft for that item.
                if (item.IsPublished())
                {
                    item = Get(item.Id, VersionOptions.DraftRequired);
                }
            }

            // Create a version record if import handlers need it.
            if (item.VersionRecord == null)
            {
                item.VersionRecord = new ContentItemVersionRecord {
                    ContentItemRecord = new ContentItemRecord {
                        ContentType = AcquireContentTypeRecord(item.ContentType)
                    },
                    Number    = 1,
                    Latest    = true,
                    Published = true
                };
            }

            var context = new ImportContentContext(item, element, importContentSession);

            Handlers.Invoke(contentHandler => contentHandler.Importing(context), Logger);
            Handlers.Invoke(contentHandler => contentHandler.Imported(context), Logger);

            var savedItem = Get(item.Id, VersionOptions.Latest);

            // The item has been pre-created in the first pass of the import, create it in db.
            if (savedItem == null)
            {
                if (status != null && status.Value == "Draft")
                {
                    Create(item, VersionOptions.Draft);
                }
                else
                {
                    Create(item);
                }
            }

            if (status == null || status.Value == Published)
            {
                Publish(item);
            }
        }