一步一步建立我的MIS系统:业务流程处理:BusinessObject

//#define Debug

using System;
using System.Data;
using System.Collections;
using System.Collections.Generic;

using System.Text;
using EwEAI.Data;

namespace EwEAI.Business.Classes
{
    /// <summary>
    /// Summary description for BusinessObject
    /// </summary>
    public abstract class BusinessObject
    {

        #region 构造函数

        protected BusinessObject(string TableName)
        {
            _TableName = TableName;
            InializeStruct();

            _BusinessData = new DataSet("DataSet_" + TableName);
        }

    
        #endregion

        #region SQL语句

        public SQLCollections SQLS = new SQLCollections();


        public class SQLCollections
        {
            public string this[string TableName]
            {
                get
                {
                    return TableSQLS[TableName].ToString();
                }
                set
                {
                    TableSQLS[TableName] = value;
                }
            }
            private Hashtable TableSQLS = new Hashtable(5);
        }

        #endregion
 
        #region  业务数据


        #region 数据更新

        public virtual void Undo()
        {
            if (Undobuffer.Count == 0)
                return;

            DataRow UndoRow = Undobuffer.Pop();

            UndoRow.RejectChanges();

        }

        public void SaveDeleteRow(DataRow row)
        {
            Undobuffer.Push(row);
        }

        public void Update(DataSet set)
        {
            if (set == null)
                return;

            ProcessBusinessFlow(set);

            Hashtable UpdateSQLS = new Hashtable();
            foreach (DataTable table in set.Tables)
                UpdateSQLS[table.TableName] = SQLS[table.TableName];

            DBWR.Instance.Update(set, UpdateSQLS);
            Undobuffer.Clear();
            BusinessData.AcceptChanges();
        }

        //业务流程处理
        protected abstract void ProcessBusinessFlow(DataSet set);


        #endregion

        #region 数据查找

        public static bool IsForeignKey(DataColumn c, DataTable table)
        {
            foreach (Constraint constraint in table.Constraints)
            {
                if (constraint is ForeignKeyConstraint)
                {
                    foreach (DataColumn fc in ((ForeignKeyConstraint)constraint).Columns)
                    {
                        if (c == fc)
                            return true;
                    }
                }
            }
            return false;
        }

        public string TableCaption(string tableName)
        {
            return _TableCaptions[tableName].ToString();
        }
        protected Hashtable _TableCaptions = new Hashtable();

        public void FillData()
        {
            FillData("");
        }

        public void FillData(string Where)
        {
            _BusinessData.Dispose();
            _BusinessData = new DataSet("DataSet_" + TableName);

            DataObject MasterData = new DataObject(TableName, IT.UserMan.UserName);
            string ASQL = "";
            if ((_MasterTable = MasterData.GetData(Where, ref ASQL)) == null)
                return ;

            SQLS[TableName] = ASQL;

            _MasterTable.TableName = TableName;
            BusinessData.Tables.Add(_MasterTable);

            //
            //填充所有的明细表       
            //
            FillChildData(TableName, ref _BusinessData);

            GetTableAuthority();

            SetCaption();


            foreach (DataTable table in BusinessData.Tables)
            {
                foreach (Constraint cnstrnt in table.Constraints)
                {
                    if (cnstrnt is ForeignKeyConstraint)
                    {
                        (cnstrnt as ForeignKeyConstraint).UpdateRule = Rule.Cascade;
                        (cnstrnt as ForeignKeyConstraint).DeleteRule = Rule.Cascade;
                        (cnstrnt as ForeignKeyConstraint).AcceptRejectRule = AcceptRejectRule.Cascade;
                    }
                }
            }

            AfterFill();

        }

        public int GetIndexByName(object key)
        {
            return BusinessData.Tables[TableName].DefaultView.Find(key);
        }

        protected virtual void AfterFill()
        {           
        }

        private void SetCaption()
        {
            foreach (DataTable table in _BusinessData.Tables)
            {
                DataTable CaptionTable = GetCaption(table.TableName);
                if (CaptionTable == null)
                    continue;

                CaptionTable.DefaultView.Sort = "FieldName ASC";
                foreach (DataColumn c in table.Columns)
                {
                    int index = CaptionTable.DefaultView.Find(c.ColumnName);
                    c.Caption = CaptionTable.DefaultView[index]["Caption"].ToString();
                }
                CaptionTable.Dispose();
            }
        }


        public bool TableHasInsertPrivilege(string TableName)
        {
            return InsertPrivilege[TableName];
        }
        public bool TableHasUpdatePrivilege(string TableName)
        {
            return UpdatePrivilege[TableName];
        }

        public bool TableHasDeletePrivilege(string TableName)
        {
            return DeletePrivilege[TableName];
        }


        private Privileges InsertPrivilege = new Privileges();
        private Privileges UpdatePrivilege = new Privileges();
        private Privileges DeletePrivilege = new Privileges();

        private void GetTableAuthority()
        {
            foreach (DataTable table in _BusinessData.Tables)
            {
                bool[] Authority = GetTableAuthority(table.TableName);

                InsertPrivilege[table.TableName] = Authority[0];
                UpdatePrivilege[table.TableName] = Authority[1];
                DeletePrivilege[table.TableName] = Authority[2];

                table.DefaultView.AllowNew = Authority[0];
                table.DefaultView.AllowDelete = Authority[2];
                table.DefaultView.AllowEdit = Authority[1];

                table.RowChanged += new DataRowChangeEventHandler(table_RowChanged);
                table.RowDeleting += new DataRowChangeEventHandler(table_RowDeleting);
                table.RowChanging += new DataRowChangeEventHandler(table_RowChanging);
            }
        }

        private Stack<DataRow> Undobuffer = new Stack<DataRow>(300);

        void table_RowChanged(object sender, DataRowChangeEventArgs e)
        {
            if (e.Action != DataRowAction.Commit
                && e.Action != DataRowAction.Nothing
                && e.Action != DataRowAction.Rollback)
            {
                Undobuffer.Push(e.Row);
            }
        }

        void table_RowChanging(object sender, DataRowChangeEventArgs e)
        {
            if (e.Action == DataRowAction.Add)
            {
                if (!TableHasInsertPrivilege(e.Row.Table.TableName))
                    throw new Exception("没有新增权限!");
            }
        }

        void table_RowDeleting(object sender, DataRowChangeEventArgs e)
        {
            if (!TableHasDeletePrivilege(e.Row.Table.TableName))
                throw new Exception("没有删除权限!");
        }
          

        public void FillChildData(string ParentName,ref DataSet BusinessData)
        {
            DataTable MasterTable = BusinessData.Tables[ParentName];
            ApplyFieldAuthority(ref MasterTable);

            DataObject[] ChildList = GetChildren(ParentName);
            if (ChildList == null)
                return;

            foreach (DataObject AData in ChildList)
            {
                DataTable ChildTable = AData.GetStruct();
                {
                    if (ChildTable == null)
                        continue;
                }

                //
                //1、得到该表的外键
                //2、把主表当前行相应外键列的值的组合作为查询条件
                //3、填充数据
                //
                foreach (DataRow MasterRow in BusinessData.Tables[ParentName].Rows)
                    FillAChildTable(AData, ref ChildTable, MasterRow, ref BusinessData);
                ChildTable.AcceptChanges();

                ChildTable.TableName = AData.TableName;
                BusinessData.Merge(ChildTable);

                FillChildData(ChildTable.TableName, ref BusinessData);
            }           
        }

        private void FillAChildTable(DataObject AData,ref DataTable ChildTable, DataRow MasterRow,
     ref DataSet BusinessData)
        {
            //构建查询条件
            string ChildWhere = "";

            string[] ChildColumns = Children[AData.TableName].ChildColumns;
            string[] ParentColumns = Children[AData.TableName].ParentColumns;

            string CurColumn = "";    //外键
            string ParentColumn = ""; //主表相应外键的列

            for (int i = 0; i < ChildColumns.Length; i++)
            {
                CurColumn = ChildColumns[i];
                ParentColumn = GetParentColumn(CurColumn,ParentColumns);
                if (ParentColumn == null)
                    ParentColumn = ParentColumns[i];

                ChildWhere = ChildWhere + CurColumn + "=";

                if (ChildTable.Columns[CurColumn].DataType == typeof(string)
                    ||ChildTable.Columns[CurColumn].DataType==typeof(DateTime))
                    ChildWhere = ChildWhere + "'" + MasterRow[ParentColumn].ToString() + "'";
                else if (ChildTable.Columns[CurColumn].DataType == typeof(int)
                    || ChildTable.Columns[CurColumn].DataType == typeof(long)
                    || ChildTable.Columns[CurColumn].DataType == typeof(byte)
                    || ChildTable.Columns[CurColumn].DataType == typeof(float))
                    ChildWhere = ChildWhere + MasterRow[ParentColumn].ToString();

                if (i < ChildColumns.Length - 1)
                    ChildWhere = ChildWhere + " and ";
            }
            //填充数据
            string ASQL = "";
            DataTable newTable = AData.GetData(ChildWhere,ref ASQL);

            SQLS[AData.TableName] = ASQL;

   if (newTable != null)
   {
    BusinessData.Merge(newTable);
   }
               
        }

        private string GetParentColumn(string childColumn, string[] ParentColumns)
        {
            foreach (string Parent in ParentColumns)
            {
                if (childColumn == Parent)
                    return Parent;
            }
            return null;
        }


        public DataObject[] GetChildren(string ParentName)
        {
            ArrayList temp=new ArrayList();
            for (int i = 0; i < Children.Count; i++)
            {
                if (Children[i].ParentName == ParentName)
                    temp.Add(Children[i].KernalData);
            }

            DataObject[] Result = new DataObject[temp.Count];
            for (int i = 0; i < temp.Count; i++)
                Result[i] = (DataObject)temp[i];
            return Result;
        }

        public DataTable MasterTable
        {
            get
            {
                if (BusinessData == null)
                    return null;

                return BusinessData.Tables[TableName];
            }
        }
        private DataTable _MasterTable = null;

        public DataSet BusinessData
        {
            get
            {
                return _BusinessData;
            }         
        }
        private DataSet _BusinessData = null;

       
        public void ApplyFieldAuthority(ref DataTable table)
        {
            DataObject AuthorityField = new DataObject(table.TableName, IT.UserMan.UserName);
            DataTable AuthorityTable = AuthorityField.GetFieldAuthority();

            AuthorityTable.DefaultView.Sort = "FieldName ASC";

            int index=-1;
            foreach (DataColumn c in table.Columns)
            {               
                index=AuthorityTable.DefaultView.Find(c.ColumnName);

                c.ReadOnly =index == -1 ? true : false;                  
            }
        }

        /*0 Insert 1 Update 2 Delete*/
        public bool[] GetTableAuthority(string tableName)
        {
            bool[] Result = new bool[3];

            DataObject AuthorityTable = new DataObject(tableName, IT.UserMan.UserName);
            DataTable Table = AuthorityTable.GetTableAuthority();

            foreach (DataRow dr in Table.Rows)
            {
                if (dr["InsertPrivilege"] == DBNull.Value)
                {
                    Result[0] = false;
                    break;
                }
                 EwEAI.Data.IT.PrivilegeType Privilege = ( EwEAI.Data.IT.PrivilegeType)Int32.Parse(dr["InsertPrivilege"].ToString());

                if (Privilege ==  EwEAI.Data.IT.PrivilegeType.DenyInsert)
                {
                    Result[0] = false;
                    break;
                }
                else
                {
                    Result[0] = true;
                }
            }

            foreach (DataRow dr in Table.Rows)
            {
                if (dr["UpdatePrivilege"] == DBNull.Value)
                {
                    Result[1] = false;
                    break;
                }

                EwEAI.Data.IT.PrivilegeType Privilege = (EwEAI.Data.IT.PrivilegeType)Int32.Parse(dr["UpdatePrivilege"].ToString());

                if (Privilege == EwEAI.Data.IT.PrivilegeType.DenyUpdate)
                {
                    Result[1] = false;
                    break;
                }
                else
                {
                    Result[1] = true;
                }
            }

            foreach (DataRow dr in Table.Rows)
            {
                if (dr["DeletePrivilege"] == DBNull.Value)
                {
                    Result[2] = false;
                    break;
                }

                EwEAI.Data.IT.PrivilegeType Privilege = (EwEAI.Data.IT.PrivilegeType)Int32.Parse(dr["DeletePrivilege"].ToString());

                if (Privilege == EwEAI.Data.IT.PrivilegeType.DenyDelete)
                {
                    Result[2] = false;
                    break;
                }
                else
                {
                    Result[2] = true;
                }
            }

            return Result;

        }


        private void BuildConstraints(ref DataSet BusinessData)
        {
            if (BusinessData == null)
                return;

            for (int i = 0; i < Children.Count; i++)
            {
                ChildData theChild = Children[i];
                DataColumn[] ParentColumns = new DataColumn[theChild.ParentColumns.Length];
                int j=0;
                foreach (string colParent in theChild.ParentColumns)
                {
                    ParentColumns[j] = BusinessData.Tables[theChild.ParentName].Columns[colParent];
                    j++;
                }

                DataColumn[] ChildColumns = new DataColumn[theChild.ChildColumns.Length];
                j = 0;
                foreach (string colChild in theChild.ChildColumns)
                {
                    ChildColumns[j] = BusinessData.Tables[theChild.TableName].Columns[colChild];
                    j++;
                }               

                BusinessData.Relations.Add(Children[i].RelationName,
                   ParentColumns,
                   ChildColumns);
            }
        }

        protected abstract void InializeStruct();

        public DataTable GetCaption(string TableName)
        {
            return DataObject.GetCaption(TableName);
        }

        #endregion


        public string TableName
        {
            get
            {
                return _TableName;
            }
        }
        private string _TableName = null;

   

        #region 明细表

        protected ChildrenCollection Children = new ChildrenCollection();


        #region 明细表集合类

        public class ChildrenCollection
        {
            public ChildrenCollection()
            {
            }

            private Hashtable _KernalData=new Hashtable(5);

            public ChildData this[string TableName]
            {
                get
                {
                    return (ChildData)_KernalData[TableName];
                }
                set
                {
                    _KernalData[TableName]=value;
                }
            }

            public ChildData this[int Index]
            {
                get
                {
                    int i=0;
                    foreach (ChildData AData in _KernalData.Values)
                    {
                        if (i == Index)
                            return AData;
                        i++;
                    }
                    return null;                       
                }
                set
                {
                    string TableName = null;
                    int i = 0;
                    foreach (ChildData AData in _KernalData.Values)
                    {
                        if (i == Index)
                        {
                            TableName = AData.TableName;
                            break;
                        }
                    }
                    if (TableName == null)
                        return;

                    this[TableName] = value;
                }
            }

            public int Count
            {
                get
                {
                    return _KernalData.Count;
                }
            }
        }

        #endregion

        #region 明细表类

        public  class ChildData
        {
             public ChildData(DataObject KernalData, string RelationName,
                string[] ParentColumns,
                string[] ChildColumns,
                string ParentName)
            {
                _TableName = KernalData.TableName;
                _RelationName = RelationName;

                _ParentColumns = ParentColumns;
                _ChildColumns = ChildColumns;

                _ParentName = ParentName;

                _KernalData = KernalData;

            }

            public DataObject KernalData
            {
                get
                {
                    return _KernalData;
                }
                set
                {
                    _KernalData = value;
                }
            }
            private DataObject _KernalData = null;

            public string TableName
            {
                get
                {
                    return _TableName;
                }
                set
                {
                    _TableName = value;
                }
            }
            private string _TableName = null;

            public string RelationName
            {
                get
                {
                    return _RelationName;
                }
                set
                {
                    _RelationName = value;
                }
            }
            private string _RelationName = null;

            public string[] ParentColumns
            {
                get
                {
                    return _ParentColumns;
                }
                set
                {
                    _ParentColumns = value;
                }
            }
            private string[] _ParentColumns = null;

            public string[] ChildColumns
            {
                get
                {
                    return _ChildColumns;
                }
                set
                {
                    _ChildColumns = value;
                }
            }
            private string[] _ChildColumns = null;

            public string ParentName
            {
                get
                {
                    return _ParentName;
                }
                set
                {
                    _ParentName = value;
                }
            }
            private string _ParentName = null;
        }

        #endregion

        #endregion

        #endregion

    }

    internal class Privileges
    {
        protected Hashtable _Privileges = new Hashtable(5);

        internal bool this[string TableName]
        {
            get
            {
                return (bool)_Privileges[TableName];
            }
            set
            {
                _Privileges[TableName] = value;
            }
        }
    }
}

阅读更多
个人分类: .Net分析与设计
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭