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. goreplay(gor) golang 流量拷贝工具试用

    1. 项目地址 https://github.com/buger/goreplay 2. 安装 wget https://github.com/buger/goreplay/releases/down ...

  2. miniofs 配置使用

    1. rpm  // RPM 包下载 https://github.com/minio/minfs/releases/tag/RELEASE.2017-02-26T20-20-56Z // 安装 yu ...

  3. EasyWeChat微信开放平台第三方平台接入

    EasyWeChat微信开放平台第三方平台接入 https://www.cnblogs.com/bainiu/p/8022729.html

  4. 使用XV-11激光雷达做hector_slam

    大家在学习ROS中不可避免需要使用激光雷达,高精地图.实时定位以及障碍物检测等多项技术,而这些技术都离不开光学雷达的支持,但是呢雷达这真是太贵了,大部分人是负担不起(实验室.研究所土豪可以略过),但是 ...

  5. Linux添加路由

    在Linux的VM中可以添加.删除路由. 中如图的拓扑结构中需要在中间的VM上添加路由,使最左边的VM和最右边的VM实现互通. 在这台VM上需要添加IP Forwarding的功能,在操作系统中也需要 ...

  6. GOF23设计模式之建造者模式(builder)

    一.建造者模式概述 建造者模式的本质: 1.分离了对象子组件的单独构造(由Builder负责)和装配(由Director负责).从而可以构造出复杂的对象.这个模式适用于:某个对象的过程复杂的情况下使用 ...

  7. Invalid byte tag in constant pool: 19

    环境: windows 2008 server R2   ; tomcat 8.5.3 ;   jdk-1.8.0_91 故障截图: 报的就是 Invalid byte tag in constant ...

  8. Java标准I/O流介绍

    1.I/O是什么? I/O 是Input/Output(输入.输出)的简称,输入流可以理解为向内存输入,输出流是从内存输出. 2.流 流是一个连续的数据流,可以从流中读取数据,也可以往流中写数据.流与 ...

  9. 引用WebService出现错误

    在引用WebService作为服务引用的时候,由于VS生成时生成了两个入口点,导致出现 引发的异常:“System.Windows.Markup.XamlParseException”(位于 Pres ...

  10. easyUI datagrid表格添加“暂无记录”显示

    扩展grid的onAfterRender事件 var myview = $.extend({}, $.fn.datagrid.defaults.view, {     onAfterRender: f ...