jqentitymanage

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Web;
using System.Xml;
using HraWeb.Common;
using System.Web.UI.WebControls;
using Spring.Expressions.Parser.antlr.debug;
using Utility;
using Elmah;
using System.Xml.Serialization;

namespace WebApp.Common
{
public class EntityDetail<T> : BasePage where T : Framework.Domain.Entity,new()
{
protected string TableName = string.Empty;
//在edit页面上,bpEdit是一个用来展示查询数据的控件
protected WebControls.Web.UI.BindingControl bpEdit
{
get;
set;
}
/// <summary>
/// 操作日志的保存
/// </summary>
/// <param name="obj"></param>
/// <param name="optype"></param>
protected virtual void SaveLog(T obj,int optype)
{
Contract.Domain.SysOplog op = new Contract.Domain.SysOplog();
op.CreateDate = DateTime.Now;
op.CreateOid = CurrentUser.OfficeId;
op.CreatePid = CurrentUser.PositionId;
op.CreateUid = CurrentUser.UserId;
op.CreateUname = CurrentUser.UserName;
op.Moudleid = Request["_menuId"];
switch (optype)
{
case 0:
op.Opcommand ="新增";
op.OpType = "新增";
break;
case 1:
op.Opcommand = "修改";
op.OpType = "修改";
break;
case 2:
op.Opcommand = "删除";
op.OpType = "删除";
break;
}
op.Entity = typeof(T).ToString();
op.Opdata = Newtonsoft.Json.JsonConvert.SerializeObject(obj);
op.State.MarkNew();
Dao.SaveOrUpdate(op);
}
protected virtual void Page_Load(object sender, EventArgs e)
{
switch (HttpContext.Current.Request["_method"])
{
case null:
case "":
InitPage(HttpContext.Current.Request["Id"]);
//编辑操作
if (!string.IsNullOrEmpty(HttpContext.Current.Request["Id"]))
{
try
{
LoadData(HttpContext.Current.Request["Id"]);
}
catch (Exception ex)
{
JSUtil.log(ex);
ErrorSignal.FromCurrentContext().Raise(ex);
// Utility.JSUtil.ExecuteScript(string.Format(" $.messager.alert('异常提示', '改记录可能不存在请尝试刷新页面', '改记录可能不存在请尝试刷新页面');"));
}
}
else
{
//新增操作,如果页面有重用,那么就传入一个辨识页面的参数
//例如,tranBond有定息和浮息之分,那么可以用instrumentTypeId来分辨这个页面是定息的还是浮息的
//如果页面没有重用,那么这个参数为空
LoadDefaultSeetings(Request["InstrumentTypeId"]);
var t = this.FindControl("txt_Id_") as TextBox;
if (t != null)
{
t.Text = string.Empty;
}
ControlModify();
}
break;
case "Save":
T a = SaveData();
//移除实体缓存
if(a!=null)
{
RemoveCache(a.GetType().Name);
}

if (a == null)
{
return ;
}
string s = Newtonsoft.Json.JsonConvert.SerializeObject(a);
if (!string.IsNullOrEmpty(Request["txt_Id_"]))
{
SaveLog(a, 1);
}
else
{
SaveLog(a, 0);
}
HttpContext.Current.Response.Write(s);
HttpContext.Current.Response.End();
break;
case "Delete":
T aa = Activator.CreateInstance<T>();
aa.Id = HttpContext.Current.Request["Id"];
SaveLog(aa, 2);
Delete();
break;
//将页面的选项设置成默认配置
case "SaveSettings":
T seetingInfo = SetData();
setXmlInfo(seetingInfo, "rewrite", Request["InstrumentTypeId"]);
Response.Write("1");
Response.End();
break;
}
}
/// <summary>
/// 调整默认配置中的一些配置
/// </summary>
protected virtual void ControlModify()
{

}

protected Contract.IService.IEntityService<T> svc
{
get;
set;
}
/// <summary>
/// 初始化页面
/// </summary>
/// <param name="id"></param>
protected virtual void InitPage(string id)
{

}
/// <summary>
/// 在查询的时候加载数据
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
protected virtual T LoadData(string id)
{
T a;
try
{
if (svc == null)
{
a = Dao.FindById(typeof(T).Name, id) as T;
}
else
{
a = svc.FindById(id);

}
}
catch (Exception ex)
{
a=new T();
}
if (bpEdit == null)
{
bpEdit = this.FindControl("bpEdit") as WebControls.Web.UI.BindingControl;
if (bpEdit == null)
{
throw new Exception("请设置绑定bpEdit");
}
}
bpEdit.LoadData(a, 0);
return a;
}

/// <summary>
/// 在新增的时候加载默认配置
/// </summary>
/// <param name="TypeId">页面类型Id,如果页面不是共用情况,那么设置为空</param>
/// <returns></returns>
protected virtual T LoadDefaultSeetings(string TypeId)
{
T a;
a = new T();
a = getXmlInfo(a, TypeId) as T;
if (bpEdit == null)
{
bpEdit = this.FindControl("bpEdit") as WebControls.Web.UI.BindingControl;
if (bpEdit == null)
{
throw new Exception("请设置绑定bpEdit");
}
}
bpEdit.LoadData(a, 0);
return a;
}
/// <summary>
/// 删除
/// </summary>
protected virtual void Delete()
{

T a = Activator.CreateInstance<T>();
string typeName = a.GetType().Name;
a.Id = HttpContext.Current.Request["Id"];
a.State.MarkDeleted();
if (svc == null)
{
a = Dao.SaveOrUpdate(a) as T;
}
else
{
a = svc.SaveOrUpdate(a);
}
RemoveCache(typeName);
//svc.SaveOrUpdate(a);
HttpContext.Current.Response.Write("1");
HttpContext.Current.Response.End();

}
/// <summary>
/// 将form表单的数据封装成一个泛型
/// </summary>
/// <returns></returns>
protected virtual T SetData()
{
return WebHelper.Web.UI.BindingPanel.SaveData<T>(HttpContext.Current.Request.Form, 0);

}
/// <summary>
/// 新增或者保存来自前台的数据
/// </summary>
/// <returns></returns>
protected virtual T SaveData()
{
T a = SetData();
if (string.IsNullOrEmpty(a.Id))
{
a.State.MarkNew();
}
else
{
a.State.MarkDirty();
}
if (svc == null)
{
a = Dao.SaveOrUpdate(a) as T;
}
else
{
try
{
a = svc.SaveOrUpdate(a);
}
catch (Exception ex)
{

;
}
}
try
{
if (!string.IsNullOrEmpty(Request["EntityLog"]))
{
Contract.Domain.BaseEntityLog log = new Contract.Domain.BaseEntityLog();
log.Change = Request["EntityLog"];
log.FormId = a.Id;
log.CreateUid = CurrentUser.UserId;
log.CreateUname = CurrentUser.UserName;
log.TableName = TableName;
log.State.MarkNew();
if (!string.IsNullOrEmpty(log.Change))
{
var list = Newtonsoft.Json.JsonConvert.DeserializeObject<IList<jsonLog>>(log.Change);
System.Text.StringBuilder sb = new System.Text.StringBuilder();
foreach (var l in list)
{
if (sb.Length == 0)
{
sb.Append(l.title.Replace(" ", "").Replace(" ", "") + l.change.Replace(" ", "").Replace(" ", "")+" ");
}
else
{
sb.Append(l.title.Replace(" ", "").Replace(" ", "") + l.change.Replace(" ", "").Replace(" ", "")+" ");
}
}
log.ChangeContext = sb.ToString();
}
log.TableName = TableName;
Dao.SaveOrUpdate(log);
}
}
catch (Exception ex)
{
Utility.JSUtil.log(ex);
ErrorSignal.FromCurrentContext().Raise(ex);
}

return a;
}

/// <summary>
/// 在xml中获得实体
/// </summary>
/// <param name="obj">传入一个实体泛型</param>
/// <param name="uiTypeId">从用户界面传入的辨识页面的id</param>
/// <returns></returns>
public virtual object getXmlInfo(T obj, string uiTypeId)
{
object s;
using (MemoryStream ms = new MemoryStream())
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(HttpContext.Current.Server.MapPath("~/Config/DefaultFormValue.xml"));
XmlNode tempNode = xmlDoc.SelectSingleNode("/Initialization/" + typeof(T).Name);
if (!string.IsNullOrEmpty(uiTypeId))
{
tempNode = xmlDoc.SelectSingleNode("/Initialization/" + typeof(T).Name + uiTypeId + "/" + typeof(T).Name);
}
if (tempNode != null)
{
XmlSerializer serializer = new XmlSerializer(typeof(T));
XmlDocument tempDoc = new XmlDocument();
XmlDeclaration xmldecl;
xmldecl = tempDoc.CreateXmlDeclaration("1.0", "utf-8", null);
tempDoc.AppendChild(xmldecl);
XmlNode temRoot = tempDoc.CreateElement(typeof(T).Name);
tempDoc.AppendChild(temRoot);
temRoot.InnerXml = tempNode.InnerXml;
tempDoc.Save(ms);
ms.Seek(0, SeekOrigin.Begin);
s = serializer.Deserialize(ms);
}
else
{
s = null;
}
}
return s;
}

/// <summary>
/// 设置xml默认配置
/// </summary>
/// <param name="obj">传入一个实体泛型</param>
/// <param name="method">当mehod为“rewrite”的时候,重写xml配置</param>
/// <param name="uiTypeId"></param>
public virtual void setXmlInfo(T obj, string method, string uiTypeId)
{
XmlSerializer serializer = new XmlSerializer(typeof(T));
using (MemoryStream ms = new MemoryStream())
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(HttpContext.Current.Server.MapPath("~/Config/DefaultFormValue.xml"));
XmlNode tempNode = xmlDoc.SelectSingleNode("/Initialization/" + typeof(T).Name);
if (!string.IsNullOrEmpty(uiTypeId))
{
tempNode = xmlDoc.SelectSingleNode("/Initialization/" + typeof(T).Name + uiTypeId);
}
if (method == "rewrite" || tempNode == null)
{
serializer.Serialize(ms, obj);
XmlDocument tempDoc = new XmlDocument();
tempDoc.LoadXml(Encoding.UTF8.GetString(ms.ToArray()));
XmlElement instrumentTypeelElement = xmlDoc.CreateElement(typeof(T).Name);
if (!string.IsNullOrEmpty(uiTypeId))
{
instrumentTypeelElement = xmlDoc.CreateElement(typeof(T).Name + uiTypeId);
XmlElement typeElement = xmlDoc.CreateElement(typeof(T).Name);
typeElement.InnerXml = tempDoc.SelectSingleNode(typeof(T).Name).InnerXml;
instrumentTypeelElement.AppendChild(typeElement);
}
else
{
instrumentTypeelElement.InnerXml = tempDoc.SelectSingleNode(typeof(T).Name).InnerXml;
}
XmlNode root = xmlDoc.SelectSingleNode("Initialization");
if (tempNode == null)
{
root.AppendChild(instrumentTypeelElement);
}
else
{
if (method == "rewrite")
{
root.ReplaceChild(instrumentTypeelElement, tempNode);
}
}
xmlDoc.Save(HttpContext.Current.Server.MapPath("~/Config/DefaultFormValue.xml"));
}
}
}
}

}

原文地址:https://www.cnblogs.com/kexb/p/4893589.html