/*
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.Data.Objects;
using System.Text;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using Everest.Library;
using Everest.Library.Mvc;
using Everest.Library.Extjs;
using Everest.Library.Json;
using Everest.Library.ExtensionMethod;
using Everest.Library.Data.Rule;
using Everest.Library.Extjs.Tree;
using Everest.Library.Data;
using Everest.CmsServices.Models;
using Everest.CmsServices.Services;
namespace Everest.CmsServices.Controllers{
public class WorkflowController : CmsExtController
{
IEverestCmsDataContext dataContext = EverestCmsEntities.GetDataContext();
#region IStandardActions Members
[PermissionFilter(Permission = FolderType.Workflow)]
public ActionResult GetList()
{
int start, limit;
EnsurePaging(out start, out limit);
string application = Request.Form["application"];
string workflowName = Request.Form["WorkflowName"];
IQueryable<Cms_Workflow> query = dataContext.QueryWorkflows(application, workflowName);
query = OrderByRequest(query);
var queryResult = query.Select<Cms_Workflow, object>(s => new
{
s.WorkflowId,
s.UUID,
s.WorkflowName,
Application = s.aspnet_Applications.ApplicationName
});
return Json(new ExtJsonReaderObject(queryResult.Skip(start).Take(limit).ToArray(), queryResult.Count()));
}
[PermissionFilter(Permission = FolderType.Workflow)]
public ActionResult GetDetail()
{
int workflowId = int.Parse(Request.Form["WorkflowId"]);
object jsonData = GetWorkflowDetail(workflowId);
return Json(new JsonResultData() { success = true, data = jsonData });
}
private object GetWorkflowDetail(int workflowId)
{
var workflow = dataContext.QueryWorkflow(workflowId).Select(wf => new
{
wf.WorkflowId,
wf.UUID,
wf.WorkflowName,
FormTitle = wf.WorkflowName,
Application = wf.aspnet_Applications.ApplicationName,
WorkflowSequences = wf.Cms_WorkflowSequence.OrderBy(ws => ws.SequenceOrder).Select(ws => new WorkflowSequenceQueried
{
RoleName = ws.RoleName,
StepName = ws.StepName,
IntContentStatus = ws.ContentStatus
})
}).First();
return workflow;
}
[PermissionFilter(Permission = FolderType.Workflow)]
public ActionResult Submit(bool add, bool? closeForm)
{
JsonResultData resultData = new JsonResultData();
Cms_Workflow workflow;
try
{
if (add)
{
string application = Request.Form["application"];
workflow = new Cms_Workflow();
workflow.aspnet_Applications = dataContext.QueryApplication(application).First();
dataContext.AddToCms_Workflow(workflow);
}
else
{
int workflowId = int.Parse(Request.Form["oldData.WorkflowId"]);
workflow = dataContext.QueryWorkflow(workflowId).First();
workflow.ClearSequences(dataContext);
}
workflow.WorkflowName = Request.Form["WorkflowName"];
#region WorkflowSequences
Dictionary<string, string>[] workflowSequences = Request.Form["WorkflowSequences"].DeserializeJSON<Dictionary<string, string>[]>();
for (int i = 0; i < workflowSequences.Length; i++)
{
var sequence = Cms_WorkflowSequence.CreateCms_WorkflowSequence(dataContext, workflowSequences[i]);
sequence.SequenceOrder = i + 1;
workflow.Cms_WorkflowSequence.Add(sequence);
}
#endregion
dataContext.SaveChanges();
if (closeForm.Value == false && resultData.success)
{
resultData.closeForm = false;
resultData.data = GetWorkflowDetail(workflow.WorkflowId);
}
}
catch (RuleViolationException ruleException)
{
ruleException.Issues.UpdateResultDataWithViolations(resultData);
Everest.Library.HealthMonitor.HealthMonitoringLogging.LogError(ruleException);
}
return Json(resultData);
}
[PermissionFilter(Permission = FolderType.Workflow)]
public ActionResult Delete(Guid[] uuid, string[] workflowName)
{
var resultData = new JsonResultData();
for (int i = 0; i < uuid.Length; i++)
{
var guid = uuid[i];
if (BeReferenced(guid))
{
resultData.message = string.Format(Resources.ItemCouldNotBeDeleted, workflowName[i]);
}
else
{
var workflow = dataContext.QueryWorkflow(guid).First();
dataContext.DeleteObject(workflow);
dataContext.SaveChanges();
}
}
return Json(resultData);
}
#endregion
private bool BeReferenced(Guid uuid)
{
return dataContext.QueryFoldersByWorkflow(uuid).Exists();
}
/// <summary>
/// Useds the by.
/// </summary>
/// <param name="uuid">The UUID.</param>
/// <returns></returns>
public ActionResult UsedBy(Guid uuid)
{
IList<Dictionary<string, object>> treeNodes = new List<Dictionary<string, object>>();
#region Use in TextFolders
var foldersNode = new TreeNode();
treeNodes.Add(foldersNode.Attributes);
foldersNode.Text = Resources.Folders;
foldersNode.Expanded = true;
foldersNode.IconCls = FolderType.Content.ToString();
var folders = dataContext.QueryFoldersByWorkflow(uuid).
Select(f => new
{
f.aspnet_Applications.ApplicationName,
Folder = f
});
foldersNode.children = new List<IDictionary<string, object>>();
foreach (var folder in folders)
{
var folderNode = new TreeNode();
foldersNode.children.Add(folderNode.Attributes);
folderNode.Text = new UniqueName(CmsGlobal.GetApplicationName(folder.ApplicationName), folder.Folder.FolderName).ToString();
folderNode.IconCls = FolderType.Content.ToString();
folderNode.AddAttribute("UUID", folder.Folder.UUID.ToString());
folderNode.AddAttribute("dataUrl", "folder/usedby");
}
#endregion
return Json(treeNodes);
}
#region Combobox
public ActionResult GetWorkflows(string application)
{
var queryable = dataContext.QueryWorkflows(application);
var items = queryable.ToComboboxItems(r => r.WorkflowName, r => r.UUID.ToString(), true);
return Json(new ExtJsonReaderObject(items, items.Count));
}
#endregion
/// <summary>
///
/// </summary>
/// <returns></returns>
public ActionResult Process(Guid contentUUID, Guid workflowHistoryUUID, int processResult, string processComment)
{
JsonResultData resultData = new JsonResultData();
var content = dataContext.QueryContent(contentUUID).First();
var workflowService = UnityManager.Resolve<WorkflowService>();
workflowService.ProcessWorkflowSequence(dataContext, content, workflowHistoryUUID, (ProcessResult)processResult, User.Identity.Name,
processComment);
return Json(resultData);
}
public ActionResult GetWorkflowHistories(Guid contentUUID)
{
var workflowService = UnityManager.Resolve<WorkflowService>();
var histories = workflowService.TraceWorkflow(dataContext, contentUUID);
return Json(new ExtJsonReaderObject(histories, histories.Count()));
}
}
}
|