示例#1
0
        /// <summary>
        /// Saves a single <see cref="IContent"/> object
        /// </summary>
        /// <param name="content">The <see cref="IContent"/> to save</param>
        /// <param name="changeState">Boolean indicating whether or not to change the Published state upon saving</param>
        /// <param name="userId">Optional Id of the User saving the Content</param>
        /// <param name="raiseEvents">Optional boolean indicating whether or not to raise events.</param>
        private void Save(IContent content, bool changeState, int userId = 0, bool raiseEvents = true)
        {
            if (raiseEvents)
            {
                if (Saving.IsRaisedEventCancelled(new SaveEventArgs<IContent>(content), this))
                    return;
            }

            using (new WriteLock(Locker))
            {
                var uow = _uowProvider.GetUnitOfWork();
                using (var repository = _repositoryFactory.CreateContentRepository(uow))
                {
                    content.WriterId = userId;

                    //Only change the publish state if the "previous" version was actually published or marked as unpublished
                    if (changeState && (content.Published || ((Content)content).PublishedState == PublishedState.Unpublished))
                        content.ChangePublishedState(PublishedState.Saved);

                    repository.AddOrUpdate(content);
                    uow.Commit();

                    //Preview Xml
                    var xml = content.ToXml();
                    var previewPoco = new PreviewXmlDto
                    {
                        NodeId = content.Id,
                        Timestamp = DateTime.Now,
                        VersionId = content.Version,
                        Xml = xml.ToString(SaveOptions.None)
                    };
                    var previewExists =
                        uow.Database.ExecuteScalar<int>("SELECT COUNT(nodeId) FROM cmsPreviewXml WHERE nodeId = @Id AND versionId = @Version",
                                                                   new { Id = content.Id, Version = content.Version }) != 0;
                    int previewResult = previewExists
                                            ? uow.Database.Update<PreviewXmlDto>(
                                                "SET xml = @Xml, timestamp = @Timestamp WHERE nodeId = @Id AND versionId = @Version",
                                                new
                                                {
                                                    Xml = previewPoco.Xml,
                                                    Timestamp = previewPoco.Timestamp,
                                                    Id = previewPoco.NodeId,
                                                    Version = previewPoco.VersionId
                                                })
                                            : Convert.ToInt32(uow.Database.Insert(previewPoco));
                }

                if (raiseEvents)
                    Saved.RaiseEvent(new SaveEventArgs<IContent>(content, false), this);

                Audit.Add(AuditTypes.Save, "Save Content performed by user", userId, content.Id);
            }
        }
 private void CreateAndSavePreviewXml(XElement xml, int id, Guid version, UmbracoDatabase db)
 {
     var previewPoco = new PreviewXmlDto
     {
         NodeId = id,
         Timestamp = DateTime.Now,
         VersionId = version,
         Xml = xml.ToString(SaveOptions.None)
     };
     var previewExists =
         db.ExecuteScalar<int>("SELECT COUNT(nodeId) FROM cmsPreviewXml WHERE nodeId = @Id AND versionId = @Version",
                                         new { Id = id, Version = version }) != 0;
     int previewResult = previewExists
                             ? db.Update<PreviewXmlDto>(
                                 "SET xml = @Xml, timestamp = @Timestamp WHERE nodeId = @Id AND versionId = @Version",
                                 new
                                     {
                                         Xml = previewPoco.Xml,
                                         Timestamp = previewPoco.Timestamp,
                                         Id = previewPoco.NodeId,
                                         Version = previewPoco.VersionId
                                     })
                             : Convert.ToInt32(db.Insert(previewPoco));
 }
示例#3
0
        /// <summary>
        /// Saves and Publishes a single <see cref="IContent"/> object
        /// </summary>
        /// <param name="content">The <see cref="IContent"/> to save and publish</param>
        /// <param name="omitCacheRefresh">Optional boolean to avoid having the cache refreshed when calling this Publish method. By default this method will update the cache.</param>
        /// <param name="userId">Optional Id of the User issueing the publishing</param>
        /// <param name="raiseEvents">Optional boolean indicating whether or not to raise save events.</param>
        /// <returns>True if publishing succeeded, otherwise False</returns>
        private bool SaveAndPublishDo(IContent content, bool omitCacheRefresh = false, int userId = 0, bool raiseEvents = true)
        {
            if (raiseEvents)
            {
                if (Saving.IsRaisedEventCancelled(new SaveEventArgs<IContent>(content), this))
                    return false;
            }

            using (new WriteLock(Locker))
            {
                //Has this content item previously been published? If so, we don't need to refresh the children
                var previouslyPublished = HasPublishedVersion(content.Id);
                var validForPublishing = true;

                //Check if parent is published (although not if its a root node) - if parent isn't published this Content cannot be published
                if (content.ParentId != -1 && content.ParentId != -20 && IsPublishable(content) == false)
                {
                    LogHelper.Info<ContentService>(
                        string.Format(
                            "Content '{0}' with Id '{1}' could not be published because its parent is not published.",
                            content.Name, content.Id));
                    validForPublishing = false;
                }

                //Content contains invalid property values and can therefore not be published - fire event?
                if (!content.IsValid())
                {
                    LogHelper.Info<ContentService>(
                        string.Format(
                            "Content '{0}' with Id '{1}' could not be published because of invalid properties.",
                            content.Name, content.Id));
                    validForPublishing = false;
                }

                //Publish and then update the database with new status
                bool published = validForPublishing && _publishingStrategy.Publish(content, userId);

                var uow = _uowProvider.GetUnitOfWork();
                using (var repository = _repositoryFactory.CreateContentRepository(uow))
                {
                    //Since this is the Save and Publish method, the content should be saved even though the publish fails or isn't allowed
                    content.WriterId = userId;

                    repository.AddOrUpdate(content);

                    uow.Commit();

                    var xml = content.ToXml();
                    //Preview Xml
                    var previewPoco = new PreviewXmlDto
                        {
                            NodeId = content.Id,
                            Timestamp = DateTime.Now,
                            VersionId = content.Version,
                            Xml = xml.ToString(SaveOptions.None)
                        };
                    var previewExists =
                        uow.Database.ExecuteScalar<int>("SELECT COUNT(nodeId) FROM cmsPreviewXml WHERE nodeId = @Id AND versionId = @Version",
                                                        new { Id = content.Id, Version = content.Version }) != 0;
                    int previewResult = previewExists
                                            ? uow.Database.Update<PreviewXmlDto>(
                                                "SET xml = @Xml, timestamp = @Timestamp WHERE nodeId = @Id AND versionId = @Version",
                                                new
                                                    {
                                                        Xml = previewPoco.Xml,
                                                        Timestamp = previewPoco.Timestamp,
                                                        Id = previewPoco.NodeId,
                                                        Version = previewPoco.VersionId
                                                    })
                                            : Convert.ToInt32(uow.Database.Insert(previewPoco));

                    if (published)
                    {
                        //Content Xml
                        var contentPoco = new ContentXmlDto { NodeId = content.Id, Xml = xml.ToString(SaveOptions.None) };
                        var contentExists = uow.Database.ExecuteScalar<int>("SELECT COUNT(nodeId) FROM cmsContentXml WHERE nodeId = @Id", new { Id = content.Id }) != 0;
                        int contentResult = contentExists
                                                ? uow.Database.Update(contentPoco)
                                                : Convert.ToInt32(uow.Database.Insert(contentPoco));

                    }
                }

                if (raiseEvents)
                    Saved.RaiseEvent(new SaveEventArgs<IContent>(content, false), this);

                //Save xml to db and call following method to fire event through PublishingStrategy to update cache
                if (published && omitCacheRefresh == false)
                    _publishingStrategy.PublishingFinalized(content);

                //We need to check if children and their publish state to ensure that we 'republish' content that was previously published
                if (published && omitCacheRefresh == false && previouslyPublished == false && HasChildren(content.Id))
                {
                    var descendants = GetPublishedDescendants(content);

                    _publishingStrategy.PublishingFinalized(descendants, false);
                }

                Audit.Add(AuditTypes.Publish, "Save and Publish performed by user", userId, content.Id);

                return published;
            }
        }