예제 #1
0
        /// <summary>
        /// Saves all the data about a changed article
        /// </summary>
        /// <param name="article">The existing article to update</param>
        /// <param name="articleStruct">The new data to be saved to the article</param>
        /// <param name="saveDocumentSpecificData"></param>
        /// <param name="addVersion">Should a new version be added</param>
        /// <param name="shouldNotify">Should notifications be sent for this update</param>
        /// <returns>The updated Sitecore item representing the article</returns>
        /// <remarks>This method could stand to be refactored into smaller chunks.</remarks>
        private ArticleItem SaveArticleDetails(ArticleItem article, ArticleStruct articleStruct, bool saveDocumentSpecificData, bool addVersion, bool shouldNotify = true)
        {
            var articleItem = _sitecoreMasterService.GetItem <Item>(article._Id);
            var publication = ArticleExtension.GetAncestorItemBasedOnTemplateID(articleItem);

            articleStruct.Publication = publication.ID.Guid;
            //IIPP-243 - Moving the location of the article if needed
            if (!article.IsPublished && article.Planned_Publish_Date != articleStruct.WebPublicationDate)
            {
                MoveArticleIfNecessary(article, articleStruct);
            }
            string userID   = articleItem.Locking.GetOwner();
            bool   loggedIn = false;

            if (!IsNullOrEmpty(userID))
            {
                loggedIn = Sitecore.Context.User.IsAuthenticated;
            }

            var newVersion = article;
            var info       = new WorkflowInfo(Guid.Empty.ToString(), Guid.Empty.ToString());

            try
            {
                Item updatedVersion;

                if (addVersion)
                {
                    using (new EditContext(articleItem))
                    {
                        ItemState itemState = articleItem.State;
                        if (itemState != null)
                        {
                            WorkflowState workflowState = itemState.GetWorkflowState();
                            if (workflowState != null)
                            {
                                IWorkflow workflow = itemState.GetWorkflow();

                                string state = workflowState.StateID;
                                if (workflow != null && state != null)
                                {
                                    info = new WorkflowInfo(workflow.WorkflowID, state);
                                    //					// remove the old version from workflow and prevent from being published
                                    //					// Note: to remove an item from workflow requires using the fields, rather than the SetWorkflowInfo
                                    //					//  method, because the SetWorkflowInfo method does not allow empty strings
                                    articleItem.Fields[Sitecore.FieldIDs.WorkflowState].Value = null;
                                    articleItem.Fields[Sitecore.FieldIDs.HideVersion].Value   = "1";
                                }
                            }
                        }
                        //newVersion = article.InnerItem.Versions.AddVersion();
                        updatedVersion = articleItem.Versions.AddVersion();
                        newVersion     = _sitecoreMasterService.GetItem <ArticleItem>(updatedVersion.ID.ToString());
                    }
                }
                else
                {
                    newVersion = article;
                }
            }
            catch (Exception ex)
            {
                var ax = new ApplicationException("Workflow: Error with versioning/workflow while saving article [" + article.Article_Number + "]!", ex);
                throw ax;
            }

            try
            {
                var newVersionItem = _sitecoreMasterService.GetItem <Item>(newVersion._Id);
                using (new EditContext(newVersionItem))
                {
                    SaveArticleFields(newVersion, article, articleStruct, saveDocumentSpecificData);
                    if (saveDocumentSpecificData)
                    {
                        RenameArticleItem(newVersion, articleStruct);
                    }


                    if (info.StateID != Guid.Empty.ToString() && info.WorkflowID != Guid.Empty.ToString())
                    {
                        // Doing this twice is intentional: when we do it once, the workflow field gets set to the empty string.
                        //  I don't know why, but it does. Doing this twice sets it properly. Doing it not at all causes the
                        //  workflow field to be set to the empty string when leaving the edit context.

                        newVersionItem.Database.DataManager.SetWorkflowInfo(newVersionItem, info);
                        newVersionItem.Database.DataManager.SetWorkflowInfo(newVersionItem, info);

                        if (articleStruct.CommandID != Guid.Empty)
                        {
                            //newVersion.NotificationTransientField.ShouldSend.Checked = true;
                            _articleUtil.ExecuteCommandAndGetWorkflowState(newVersionItem, articleStruct.CommandID.ToString());

                            if (shouldNotify)
                            {
                                _emailUtil.SendNotification(articleStruct, info);
                            }
                        }
                    }
                }

                if (loggedIn)
                {
                    _sitecoreMasterService.GetItem <Item>(newVersion._Id).Locking.Lock();
                }
            }

            catch (Exception ex)
            {
                var ax = new ApplicationException("Workflow: Error with saving details while saving article [" + article.Article_Number + "]!", ex);
                throw ax;
            }

            //  Notifying the Editors when stories are edited after pushlished
            if (articleStruct.IsPublished)
            {
                // Setting the workflow to "Edit After Publish".
                try
                {
                    var newVersionItem = _sitecoreMasterService.GetItem <Item>(newVersion._Id);
                    newVersionItem.Locking.Unlock();

                    using (new EditContext(newVersionItem))
                    {
                        newVersionItem[FieldIDs.WorkflowState] = _siteWorkflow.GetEditAfterPublishState(newVersionItem)._Id.ToString();// Constants.EditAfterPublishWorkflowCommand;
                    }

                    if (loggedIn)
                    {
                        _sitecoreMasterService.GetItem <Item>(newVersion._Id).Locking.Lock();
                    }
                }
                catch (Exception ex)
                {
                    var ax =
                        new ApplicationException(
                            "Workflow: Error with changing the workflow to Edit After Publish [" + article.Article_Number + "]!", ex);
                    throw ax;
                }

                try
                {
                    _emailUtil.EditAfterPublishSendNotification(articleStruct);
                }
                catch (Exception ex)
                {
                    Sitecore.Diagnostics.Log.Error("SitecoreSaverUtil.SaveArticleDetails EditAfterPublishSendNotification(): " + ex.ToString(), this);
                }
            }
            return(newVersion);
        }