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. jfrog artifactory docker 安装试用

    预备环境(docker 安装模式,使用的免费版本): docker-ce (启用镜像加速) 1. 镜像拉取 docker.bintray.io/jfrog/artifactory-oss 2. 启动 ...

  2. @SessionAttributes和@ModelAttribute

    一.@ModelAttribute 在默认情况下,ModelMap 中的属性作用域是 request 级别是,也就是说,当本次请求结束后,ModelMap 中的属性将销毁.如果希望在多个请求中共享 M ...

  3. flask之redis

    redis 连接需要host port passwod Hash:key-fields-value(做缓存)相当于一个key对于一个map,map中还有key-valueList:有顺序可重复(处理不 ...

  4. Erlang基础 -- 介绍 -- Erlang特点

    前言 Erlang是具有多重范型的编程语言,具有很多特点,主要的特点有以下几个: 函数式 并发性 分布式 健壮性 软实时 热更新 递增式代码加载 动态类型 解释型 函数式 Erlang是函数式编程语言 ...

  5. CAN总线过载帧

    过载帧 过载帧与主动错误帧具有相同的格式.但是,过载帧只能在帧间间隔产生,因此可通过这种方式区分过载帧和错误帧(错误帧是在帧传输时发出的).过载帧由两个字段组成,即过载标志和随后的过载定界符.过载标志 ...

  6. js的console你知道多少

    js的console你知道多少? 列出所有的console属性 console.dir(console) 或者 console.dirxml(console) 记录代码执行时间 console.tim ...

  7. Storm集群详细部署

    1.安装zookeeper 3.1下载zookeeper安装包, 建议下载3.4.5及以上的版本 http://www.apache.org/dyn/closer.cgi/zookeeper/ 3.2 ...

  8. leetcode598

    public class Solution { public int MaxCount(int m, int n, int[,] ops) { ); ); || col == ) { return m ...

  9. apache配置多个虚拟主机 localhost访问不了解决方案

    在httpd-vhosts.conf,重定向localhost <VirtualHost *:80>    ServerAdmin webmaster@dummy-host2.exampl ...

  10. PHP的Enum(枚举)的实现

    转载请保留原文地址:http://www.cnblogs.com/zsxfbj/p/php_enum.html PHP其实有Enum类库的,需要安装perl扩展,所以不是php的标准扩展,因此代码的实 ...