/*
Kooboo is a content management system based on ASP.NET MVC framework. Copyright 2009 Yardi Technology Limited.
This program is free software: you can redistribute it and/or modify it under the terms of the
GNU General Public License version 3 as published by the Free Software Foundation.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with this program.
If not, see http://www.kooboo.com/gpl3/.
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Everest.Library;
using Everest.CmsServices.Models;
using Everest.Library.Data.Entity;
using Everest.Library.Data;
using Everest.Library.ExtensionMethod;
namespace Everest.CmsServices.Services{
public class TraceWorkflowData
{
public Guid SequenceUUID { get; set; }
public string RoleName { get; set; }
public ContentStatus ContentStatus { get; set; }
public ProcessResult ProcessResult { get; set; }
}
public class WorkflowService
{
public virtual void StartWorkflow(IEverestCmsDataContext dataContext, Cms_Content content)
{
content.Cms_FolderReference.Load(content.Cms_Folder, content.EntityState);
if (content.Cms_Folder != null)
{
content.Cms_Folder.Cms_WorkflowReference.Load(content.Cms_Folder.Cms_Workflow, content.Cms_Folder.EntityState);
var workflow = content.Cms_Folder.Cms_Workflow;
if (workflow != null)
{
IEnumerable<string> roles = UnityManager.Resolve<PermissionService>().GetRoles(content.UserName);
var workflowSequence = dataContext.QueryWorkflowSequenceByRoles(workflow.UUID, roles).FirstOrDefault();
if (workflowSequence == null)
{
throw new Exception(string.Format(Resources.WorkflowHaventSequence, string.Join(",", roles.ToArray())));
}
content.ContentStatus = workflowSequence.ContentStatus;
Cms_WorkflowHistory workflowHistory = new Cms_WorkflowHistory();
workflowHistory.Cms_Workflow = workflow;
workflowHistory.Cms_Content = content;
workflowHistory.PostDate = DateTime.Now;
workflowHistory.ProcessStep = 1;
workflowHistory.ProcessResult = (int)ProcessResult.Unprocessed;
//copy sequence info
workflowHistory.RoleName = workflowSequence.RoleName;
workflowHistory.StepName = workflowSequence.StepName;
workflowHistory.ContentStatus = workflowSequence.ContentStatus;
workflowHistory.SequenceOrder = workflowSequence.SequenceOrder;
workflowHistory.StartSequenceOrder = workflowHistory.SequenceOrder;
dataContext.AddToCms_WorkflowHistory(workflowHistory);
dataContext.SaveChanges();
}
}
}
public virtual void ProcessWorkflowSequence(IEverestCmsDataContext dataContext, Cms_Content content, Guid workflowHistoryUUID, ProcessResult result, string userName, string processComment)
{
var workflowHistory = dataContext.QueryWorkflowHistory(workflowHistoryUUID).First();
workflowHistory.Cms_WorkflowReference.Load(workflowHistory.Cms_Workflow, workflowHistory.EntityState);
//workflowHistory.Cms_WorkflowSequenceReference.Load(workflowHistory.Cms_WorkflowSequence, workflowHistory.EntityState);
workflowHistory.ProcessResult = (int)result;
workflowHistory.ProcessUserName = userName;
workflowHistory.ProcessComment = processComment;
workflowHistory.ProcessDate = DateTime.Now;
if (result != ProcessResult.Stopped)
{
Cms_WorkflowSequence nextSequence = null;
if (result == ProcessResult.Processed)
{
nextSequence = dataContext.QueryNextWorkflowSequence(workflowHistory.Cms_Workflow, workflowHistory.SequenceOrder).FirstOrDefault();
//if next sequence is null,then set the workhistory to Finished.
if (nextSequence == null)
{
workflowHistory.ProcessResult = (int)ProcessResult.AllSequenceFinished;
}
content.ContentStatus = workflowHistory.ContentStatus.Value;
}
if (result == ProcessResult.Rejected)
{
nextSequence = dataContext.QueryPreviousWorkflowSequence(workflowHistory.Cms_Workflow, workflowHistory.SequenceOrder).FirstOrDefault();
}
if (nextSequence != null)
{
var nextWorkflowHistory = new Cms_WorkflowHistory();
nextWorkflowHistory.Cms_Workflow = workflowHistory.Cms_Workflow;
//nextWorkflowHistory.Cms_WorkflowSequence = nextSequence;
nextWorkflowHistory.Cms_Content = content;
nextWorkflowHistory.PostDate = DateTime.Now;
nextWorkflowHistory.ProcessStep = workflowHistory.ProcessStep + 1;
nextWorkflowHistory.PreResult = (int)result;
nextWorkflowHistory.ProcessResult = (int)ProcessResult.Unprocessed;
//copy sequence info
nextWorkflowHistory.RoleName = nextSequence.RoleName;
nextWorkflowHistory.StepName = nextSequence.StepName;
nextWorkflowHistory.ContentStatus = nextSequence.ContentStatus;
nextWorkflowHistory.SequenceOrder = nextSequence.SequenceOrder;
nextWorkflowHistory.StartSequenceOrder = workflowHistory.StartSequenceOrder;
dataContext.AddToCms_WorkflowHistory(nextWorkflowHistory);
}
}
dataContext.SaveChanges();
}
/// <summary>
/// Traces the workflow.
/// </summary>
/// <param name="dataContext">The data context.</param>
/// <param name="content">The content.</param>
/// <returns></returns>
public virtual IEnumerable<QueriedWorkflowHistory> TraceWorkflow(IEverestCmsDataContext dataContext, Guid contentUUID)
{
return dataContext.QueryWorkflowHistories(contentUUID).OrderBy(wfh => wfh.HistoryId).Select(wfh => new QueriedWorkflowHistory()
{
ContentStatus = wfh.ContentStatus.Value,
PostDate = wfh.PostDate,
PreResult = wfh.PreResult,
ProcessComment = wfh.ProcessComment,
ProcessDate = wfh.ProcessDate,
ProcessResult = wfh.ProcessResult.Value,
ProcessUserName = wfh.ProcessUserName,
RoleName = wfh.RoleName,
StepName = wfh.StepName
});
}
public virtual bool IsUserInWorkflow(Cms_Workflow workflow, string userName)
{
var dataContext = EverestCmsEntities.GetDataContext();
IEnumerable<string> roles = UnityManager.Resolve<PermissionService>().GetRoles(userName);
return dataContext.QueryWorkflowSequenceByRoles(workflow.UUID, roles).
Any(wf => 1 == 1);
}
public virtual bool CanEditContentByStatus(Guid contentUUID, int contentStatus, string userName)
{
if (StringExtensions.IsNullOrEmptyTrim(userName))
{
return true;
}
var dataContext = EverestCmsEntities.GetDataContext();
IEnumerable<string> roles = UnityManager.Resolve<PermissionService>().GetRoles(userName);
var workflow = dataContext.QueryWorkflowByByContent(contentUUID).FirstOrDefault();
if (workflow != null)
{
return dataContext.QueryWorkflowSequenceByRoles(workflow.UUID, roles)
.Any(wf => wf.ContentStatus >= contentStatus);
}
return true;
}
}
}
|