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("\n", "").Replace(" ", "") + l.change.Replace("\n", "").Replace(" ", "")+"\r\n");
}
else
{
sb.Append(l.title.Replace("\n", "").Replace(" ", "") + l.change.Replace("\n", "").Replace(" ", "")+"\r\n");
}
}
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"));
}
}
}
}

}

jqentitymanage的更多相关文章

  1. jqgrid子表格

    .前台 <%-- builed by manage.aspx.cmt [ver:] at // :: --%> <%@ Page Language="C#" Au ...

  2. hra 直线

    using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI ...

  3. .net正则查询

    using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI ...

  4. Siverlight MarkerSize 控制数据点半径大小 LineThickness 控制点与点之间直线的厚度

    using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI ...

  5. easyui-tabs 页签绑定click事件,动态加载jqgrid

    .前台代码 <%-- builed by manage.aspx.cmt [ver:] at // :: --%> <%@ Page Language="C#" ...

  6. jqgrid控件列分组

    <%-- builed by manage.aspx.cmt [ver:2014.48.11] at 2014/10/11 16:48:33 --%> <%@ Page Langua ...

  7. jqentitydetail

    using System;using System.Collections;using System.Collections.Generic;using System.Linq;using Syste ...

  8. 动态tab页

    1.前台代码 <%-- builed by manage.aspx.cmt  [ver:2015.25.26] at 2015-06-26 15:25:42 --%> <%@ Pag ...

随机推荐

  1. 在DIV里面让图片居中

    .content.itemDiv{ display: table-cell;width: 171px;height: 133px;text-align: center; vertical-align: ...

  2. 无法建立目录wp-content/uploads/xxxx/xx。有没有上级目录的写权限?解决办法

    首先小七已经搭建了n个wordpress网站之前没遇到过这坑爹的问题,有一天很奇怪无论是本地搭建的wp还是线上搭建的wp网站都出现了同样的问题 本地: 报错原因就是文件权限问题,所以首先就是更改wp- ...

  3. 利用ffmpeg一步一步编程实现摄像头采集编码推流直播系统

    了解过ffmpeg的人都知道,利用ffmpeg命令即可实现将电脑中摄像头的画面发布出去,例如发布为UDP,RTP,RTMP等,甚至可以发布为HLS,将m3u8文件和视频ts片段保存至Web服务器,普通 ...

  4. maven学习4 使用Maven构建Spring项目

    1. 新建一个Web项目 参考之前的博客 2.修改 pom.xml,添加Spring依赖 <project xmlns="http://maven.apache.org/POM/4.0 ...

  5. 2014.8.8 CAD系统连接

    CDA数据库连接生产库.研发库.临时库对应3个连接名 cad = (DESCRIPTION = (ADDRESS = (PROTOCOL = TCP)(HOST = backupserver)(POR ...

  6. C#通用模块专题

    开源 程序设计 常用组件 加载图片,播放音乐.视频,摄像头拍照 文件读写(txt.xml.自定义文件格式(后缀名)) 串口通信 稳定的串口读写:http://blog.csdn.net/kolvin2 ...

  7. AJAX如何获取从前台传递过来的数据然后在通过servle传递给后台

    1 用 request.getParameter接收值 <% String id1=request.getParameter("id"); out.print(id1); % ...

  8. C# RSA的加解密与签名验证

    最近做了一个CS架构的序列号生成器,用到 RSA加解密技术,以下是RSA的使用方法 RSA加密算法是一种非对称加密算法.在公钥加密标准和电子商业中RSA被广泛使用.RSA是1977年由罗纳德•李维斯特 ...

  9. leetcode690

    class Solution { public: int getImportance(vector<Employee*> employees, int id) { ; map<int ...

  10. 预编译头文件来自编译器的早期版本,或者预编译头为 C++ 而在 C 中使用它(或相反)转

    vs2010的mfc项目中编译c语言出现错误: "...预编译头文件来自编译器的早期版本,或者预编译头为 C++ 而在 C 中使用它(或相反)" 解决方法: 建工程时 建立空项目  ...