using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;
using System.Web.SessionState;
namespace WebForm.Platform.WorkFlowFormDesigner
{
///
/// Publish 的摘要说明
///
public class Publish : IHttpHandler,IRequiresSessionState
{
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
string html = context.Request["html"];
string name = context.Request["name"];
string att = context.Request["att"];
string id = context.Request["id"];
Guid gid;
if (!id.IsGuid(out gid) || name.IsNullOrEmpty() || att.IsNullOrEmpty() || html.IsNullOrEmpty())
{
context.Response.Write("参数错误!");
return;
}
RoadFlow.Platform.WorkFlowForm WFF = new RoadFlow.Platform.WorkFlowForm();
RoadFlow.Data.Model.WorkFlowForm wff = WFF.Get(gid);
if (wff == null)
{
context.Response.Write("未找到表单!");
return;
}
string fileName = id + ".aspx";
System.Text.StringBuilder serverScript = new System.Text.StringBuilder("<%@ Page Language=\"C#\"%>\r\n<%\r\n");
var attrJSON = LitJson.JsonMapper.ToObject(att);
serverScript.Append("\tstring FlowID = Request.QueryString[\"flowid\"];\r\n");
serverScript.Append("\tstring StepID = Request.QueryString[\"stepid\"];\r\n");
serverScript.Append("\tstring GroupID = Request.QueryString[\"groupid\"];\r\n");
serverScript.Append("\tstring TaskID = Request.QueryString[\"taskid\"];\r\n");
serverScript.Append("\tstring InstanceID = Request.QueryString[\"instanceid\"];\r\n");
serverScript.Append("\tstring DisplayModel = Request.QueryString[\"display\"] ?? \"0\";\r\n");
serverScript.AppendFormat("\tstring DBConnID = \"{0}\";\r\n", attrJSON["dbconn"].ToString());
serverScript.AppendFormat("\tstring DBTable = \"{0}\";\r\n", attrJSON["dbtable"].ToString());
serverScript.AppendFormat("\tstring DBTablePK = \"{0}\";\r\n", attrJSON["dbtablepk"].ToString());
serverScript.AppendFormat("\tstring DBTableTitle = \"{0}\";\r\n", attrJSON["dbtabletitle"].ToString());
serverScript.Append("if(InstanceID.IsNullOrEmpty()){InstanceID = Request.QueryString[\"instanceid1\"];}");
serverScript.Append("\tRoadFlow.Platform.Dictionary BDictionary = new RoadFlow.Platform.Dictionary();\r\n");
serverScript.Append("\tRoadFlow.Platform.WorkFlow BWorkFlow = new RoadFlow.Platform.WorkFlow();\r\n");
serverScript.Append("\tRoadFlow.Platform.WorkFlowTask BWorkFlowTask = new RoadFlow.Platform.WorkFlowTask();\r\n");
serverScript.Append("\tstring fieldStatus = BWorkFlow.GetFieldStatus(FlowID, StepID);\r\n");
serverScript.Append("\tLitJson.JsonData initData = BWorkFlow.GetFormData(DBConnID, DBTable, DBTablePK, InstanceID, fieldStatus);\r\n");
serverScript.Append("\tstring TaskTitle = BWorkFlow.GetFromFieldData(initData, DBTable, DBTableTitle);\r\n");
serverScript.Append("%>\r\n");
serverScript.Append("\r\n");
serverScript.Append("\r\n");
if (attrJSON.ContainsKey("hasEditor") && "1" == attrJSON["hasEditor"].ToString())
{
serverScript.Append("\r\n");
serverScript.Append("\r\n");
serverScript.Append("\r\n");
serverScript.Append("\r\n");
}
string validatePropType = attrJSON.ContainsKey("validatealerttype") ? attrJSON["validatealerttype"].ToString() : "2";
serverScript.Append("\r\n");
if (attrJSON.ContainsKey("autotitle") && attrJSON["autotitle"].ToString().ToLower() == "true")
{
serverScript.AppendFormat("\r\n",
string.Concat(attrJSON["dbtable"].ToString(), ".", attrJSON["dbtabletitle"].ToString()),
"<%=TaskTitle.IsNullOrEmpty() ? BWorkFlow.GetAutoTaskTitle(FlowID, StepID, Request.QueryString[\"groupid\"]) : TaskTitle %>"
);
}
serverScript.AppendFormat("\r\n", string.Concat(attrJSON["dbtable"].ToString(), ".", attrJSON["dbtabletitle"].ToString()));
//serverScript.AppendFormat("\r\n", attrJSON["name"].ToString());
serverScript.AppendFormat("\r\n", attrJSON["dbconn"].ToString());
serverScript.AppendFormat("\r\n", attrJSON["dbtable"].ToString());
serverScript.AppendFormat("\r\n", attrJSON["dbtablepk"].ToString());
serverScript.AppendFormat("\r\n", attrJSON["dbtabletitle"].ToString());
serverScript.AppendFormat("\r\n", "1");
serverScript.Append("\r\n");
string file = context.Server.MapPath("Forms/" + fileName);
System.IO.Stream stream = System.IO.File.Open(file, FileMode.OpenOrCreate, FileAccess.Write, FileShare.None);
stream.SetLength(0);
StreamWriter sw = new StreamWriter(stream, System.Text.Encoding.UTF8);
sw.Write(serverScript.ToString());
sw.Write(context.Server.HtmlDecode(html));
sw.Close();
stream.Close();
string attr = wff.Attribute;
string appType = LitJson.JsonMapper.ToObject(attr)["apptype"].ToString();
RoadFlow.Platform.AppLibrary App = new RoadFlow.Platform.AppLibrary();
var app = App.GetByCode(id);
bool isAdd = false;
if (app == null)
{
app = new RoadFlow.Data.Model.AppLibrary();
app.ID = Guid.NewGuid();
app.Code = id;
isAdd = true;
}
app.Address = "/Platform/WorkFlowFormDesigner/Forms/" + fileName;
app.Note = "流程表单";
app.OpenMode = 0;
app.Params = "";
app.Title = name.Trim();
app.Type = appType.IsGuid() ? appType.ToGuid() : new RoadFlow.Platform.Dictionary().GetIDByCode("FormTypes");
if (isAdd)
{
App.Add(app);
}
else
{
App.Update(app);
}
RoadFlow.Platform.Log.Add("发布了流程表单", app.Serialize() + "内容:" + html, RoadFlow.Platform.Log.Types.流程相关);
wff.Status = 1;
WFF.Update(wff);
context.Response.Write("发布成功!");
}
public bool IsReusable
{
get
{
return false;
}
}
}
}