segunda-feira, 20 de julho de 2015

MVC EAA

using System;
using System.Collections.Generic;
using System.Web;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using CN = Lapuinka.EAA.Model;
using CAS.Web.Framewok.SecurityModule;
using Lapuinka.EAA.Model;
using Lapuinka.EAA.Controller.DataBase;

namespace Lapuinka.EAA.Controller.Web
{
    ///
    /// Fire when View needs to Reset.
    ///
    public interface IReset
    {
        void Reset();
    }
    ///
    /// Represents Page Mode
    ///
    public enum PageMode
    {
          ListViewMode = 1
        , EditMode = 2
        , InsertMode = 3
    }

    #region BasePage
    ///
    ///Creation Date:      17-Nov-2009
    ///Name: BasePage
    ///Version:     1.0.0
    ///Purpose:     Base class use for all views
    ///Author:    TAX Fabio Leandro Lapuinka
    ///
    ///Revision History
    ///Version             Date           By Whom      Change Description
    ///----------    ------         ------------ ---------------------------------------------
    ///1.0.0         10-May-10    FLL          Documentation and Code Standards
    ///
    public class BasePage: System.Web.UI.Page
    {
        #region Properties

        #region PageMode
        ///
        /// Represents Edit Mode
        ///
        public PageMode Mode
        {
            get
            {
                if (ViewState["Mode"] == null)
                   ViewState["Mode"] = PageMode.ListViewMode;
                return (PageMode)ViewState["Mode"];
            }
            set
            {
                ViewState["Mode"] = value;
            }
        }
               
        #endregion

        #region FormName
        ///
        /// Represents the Form Name
        ///
        private string FormName
        {
            get
            {
                return this.Request.FilePath.Substring((this.Request.FilePath.LastIndexOf("/") + 1));
            }
        }
        #endregion

        #region ObjectType
        ///
        /// Represents Type of Object uses in this Base
        ///
        public virtual Type ObjectType(){return null;}
        #endregion

        #region CacheSecurity
        ///
        /// Represent a security acess
        ///
        protected List ColObjectPermission
        {
            get
            {
                if (Session["ColObjectPermission"] == null)
                    Session["ColObjectPermission"] = new List();

                return (List) Session["ColObjectPermission"];

            }
            set
            {
                Session["ColObjectPermission"] = value;
            }

        }
        #endregion

        #region SecurityRule
        ///
        /// Represent a security acess
        ///
        public Security SecurityRule
        {
            get
            {
                return (Security)Session["ssnSecurity"];

            }
            set
            {
                Security obj = (Security)Session["ssnSecurity"];
                obj = value;
                Session["ssnSecurity"] = obj;
            }

        }
        #endregion

        #region MemoryDataSet
        ///
        /// Get Lookup Table
        ///
        ///
        ///
        protected DataTable getLookup(ILookup obj)
        {
            return obj.LookupUp();
       
        }
      
        #endregion
    

        #endregion

        #region Events
        ///
        /// OnInit
        ///
        ///
        ///
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
        }
        protected override void OnLoadComplete(EventArgs e)
        {
            base.OnLoadComplete(e);
            if (!this.IsPostBack)
            {
                if (!this.IsLogin() || !this.IsObjectAcess(this.ObjectType(), this.SecurityRule.GroupID))
                    Response.Redirect(System.Configuration.ConfigurationSettings.AppSettings["Page.AcessDenied"], true);
            }
        }


        #endregion

        #region Methods

        #region makeLink
        ///
        /// Make Link
        public string makeLink(Type objectType, string Link, string Description)
        {
            return makeLink(objectType, Link, Description, false);
        }
        ///
        ///
        ///
        ///
        ///
        public string makeLink(Type objectType, string Link, string Description, bool newWindow)
        {
            if (this.IsObjectAcess(objectType, this.SecurityRule.GroupID))
            {
                 return @"
  • + Link + "',"+Convert.ToInt32(newWindow)+")\">" + Description + @"
  • "
    ;
                }
                else
                    return string.Empty;
            }
            ///
            ///
            ///
            ///
            ///
            public string makeHelp(Type objectType, string Link, string Description)
            {
                if (this.IsObjectAcess(objectType, this.SecurityRule.GroupID))
                {
                    return @"
  • + Link + "')\">" + Description + @"
  • "
    ;
                }
                else
                    return string.Empty;
            }

            public void ExportToExcel(BaseObject rpt)
            {
                OpenReport(rpt, string.Empty, string.Empty, "ViewExcel.aspx");
            }
            public void ExportToExcel(BaseObject rpt,string CustomView)
            {
                OpenReport(rpt, string.Empty, string.Empty, CustomView);
            }
            ///
            /// OpenReport with Default params
            ///
            ///
            public void OpenReport(BaseObject rpt)
            {
                OpenReport(rpt, string.Empty, string.Empty, string.Empty);
            }
            ///
            /// OpenReport
            ///
            ///
            public void OpenReport(BaseObject rpt, string width, string height)
            {
                OpenReport(rpt, width, width, string.Empty);
            }
            ///
            /// OpenReport
            /// Try to use this form directly
            /// http://localhost/EMCORP/EAA/Reserved.ReportViewerWebControl.axd?Mode=true&ReportID=2d15a465d7744a77bca10e5b254aedab&ControlID=f0b77dfcec184e6397c5cb3defa70ff4&Culture=1033&UICulture=1033&ReportStack=1&OpType=Export&FileName=Lapuinka.EAA.Model.ReportAuditActiveStatuteWaivers&ContentDisposition=OnlyHtmlInline&Format=Excel
            ///
            ///
            public void OpenReport(BaseObject rpt, string width, string height, string DestinationView)
            {
                string automaticwindowsize = string.Empty;

                if (width != string.Empty || height != string.Empty)
                    automaticwindowsize = "&height=" + height + "&width=" + width;
                else
                    automaticwindowsize = "automaticwindowsize";


                if (DestinationView == string.Empty)
                    DestinationView = "ViewReport.aspx";

                OpenWindow(rpt.GetType(), DestinationView + "?mode=1&remote=0&"+automaticwindowsize+"&" + rpt.QueryParams());
           
            }
            ///
            ///
            ///
            ///
            ///
            public void OpenWindow(Type objectType, string Link)
            {
                if (this.IsObjectAcess(objectType, this.SecurityRule.GroupID))
                {
                    this.ClientScript.RegisterClientScriptBlock(this.GetType(), Link, @"
                ");
                }

            }
            #endregion
       
            #region FindNodeRecursive
            ///
            /// Find Node
            ///
            ///
            ///
            ///
            private TreeNode FindNode(TreeNode node, string NodeValue)
            {
                string[] strValues = node.Value.Split(',');
                foreach (string strvalue in strValues)
                {
                    if (strvalue == NodeValue)
                        return node;
                }

                if (node.ChildNodes.Count > 0)
                {
                    foreach (TreeNode nodeChildren in node.ChildNodes)
                    {
                        TreeNode found = FindNode(nodeChildren, NodeValue);
                        if (found != null)
                            return found;
                    }
                }
                return null;
            }
         
            ///
            /// Find Node Recursive
            ///
            ///
            ///
            ///
            public TreeNode FindNodeRecursive(TreeNodeCollection nodeCollection, string NodeValue)
            {
                foreach (TreeNode nodeChildren in nodeCollection)
                {
                    TreeNode found = FindNode(nodeChildren, NodeValue);
                    if (found != null)
                        return found;
                }
                return null;
            }

            #region CheckAll

      
            ///
            /// Find Node
            ///
            ///
            ///
            ///
            private void CheckNode(TreeNode node, bool Flag, bool IgnoreEmptyValue)
            {
                if (node.Value != string.Empty || !IgnoreEmptyValue)
                    node.Checked = Flag;

                if (node.ChildNodes.Count > 0)
                {
                    foreach (TreeNode nodeChildren in node.ChildNodes)
                    {

                        if (nodeChildren.Value != string.Empty || !IgnoreEmptyValue)
                            nodeChildren.Checked = Flag;

                        CheckNode(nodeChildren, Flag, IgnoreEmptyValue);
                    }
                }
            }
            ///
            /// Find Node Recursive
            ///
            ///
            ///
            ///
            protected void CheckAllNodes(TreeNodeCollection nodeCollection, bool Flag, bool IgnoreEmptyValue)
            {
                if (nodeCollection.Count > 0)
                {
                    for (int i = 0; i <= nodeCollection.Count - 1; i++)
                        CheckNode(nodeCollection[i], Flag, IgnoreEmptyValue);
                }
            }
            #endregion

            #endregion

            #region GetDescription
            ///
            /// Get Description
            ///
            ///
            ///
            public string GetDescription(object value)
            {
                    return Convert.ToInt32(DBNullConvert.ToInt(value))==1 ? "Enable" : "Disable";
            }
            #endregion

            #region GetWaiver
            ///
            /// Get Description
            ///
            ///
            ///
            public string GetWaiver(object value)
            {
                return Convert.ToInt32(value) == 1 ? "Yes" : "No";
            }
            #endregion

            #region BindLookup
                    ///
            /// Bind Lookup
            ///
            ///
            ///
            public void BindLookup(ListControl control, ILookup obj)
            {
                BindLookup(control, obj, true, null);
            }
            ///
            /// Bind Lookup
            ///
            ///
            ///
            public void BindLookup(ListControl control, ILookup obj, bool UsingCache, ListItem SelectValue)
            {
                control.Items.Clear();
                control.DataSource = UsingCache ? getLookup(obj) : obj.LookupUp();
                control.DataTextField = "DataTextField";
                control.DataValueField = "DataValueField";
                control.DataBind();
                if (SelectValue != null)
                    control.Items.FindByValue(SelectValue.Value).Selected = true;
                else
                    control.SelectedIndex = -1;
            }
            #endregion

            #region IsLogin
            ///
            /// Check Login Security
            ///
            ///
            public bool IsLogin()
            {
                if (SecurityRule != null)
                    return true;

                CN.UserAccountLogin login = new Lapuinka.EAA.Model.UserAccountLogin();
                login.USU_LanID = User.Identity.Name;
                login.Pick();
                if (Convert.ToBoolean(login.UCL_flag))
                {
                    Security sec = new Security();
                    sec.UserID = login.USU_Id;
                    sec.UserName = User.Identity.Name;
                    sec.GroupID = login.GRP_ID;
                    sec.GroupDescription = login.GRP_description;
                    this.SecurityRule = sec;
                    return true;
                }
                else
                    return false;
            }
            #endregion

            #region IsObjectAcess
                  ///
            /// Check Current Object
            ///
            ///
            public bool IsObjectAcess(Type type, int GroupID)
            {
                if ((type != typeof(PublicObject))) //set or null ObjectType
                    return IsObjectAcess(type.Name, GroupID);
                else
                    return true;
            }
            ///
            /// Check Current Object
            ///
            ///
            public bool IsObjectAcess(string TypeName, int GroupID)
            {
                bool bFound = false;
                CN.ObjectPermission op = new Lapuinka.EAA.Model.ObjectPermission();
                op.OBJ_Name = TypeName;
                op.GRP_id = GroupID;

                foreach (CN.ObjectPermission op2 in ColObjectPermission)
                    if (op2.OBJ_Name == op.OBJ_Name)
                    {
                        op = op2;
                        bFound = true;
                    }

                if (!bFound)
                {
                    op.Pick();
                    ColObjectPermission.Add(op);

                }

                if (op.OPE_Search != 1) //Only Search is Required for minimal purposes.
                {
                    Session["objectDenied"] = op.OBJ_Name;
                    return false;
                }

                return true;

            }
            #endregion

            #region GetResumeText
            ///
            /// Resume Text
            ///
            ///
            ///
            ///
            public string GetResumeText(object Text, int MaxSize)
            {
                if (Text != DBNull.Value && Text != null)
                {
                    string str = Text.ToString();
                    if (MaxSize > str.Length)
                        MaxSize = str.Length;

                    if (str.Length <= MaxSize)
                        return str.Substring(0, MaxSize);
                    else
                        return Text.ToString().Substring(0, MaxSize) + "...";
                }
                else
                    return string.Empty;

            }
            #endregion

            #region setFocus
            ///
            /// Set Focus in Control
            ///
            ///
            protected void setFocus(string clientId)
            {
                this.ClientScript.RegisterClientScriptBlock(this.GetType(),"Focus", @"
                ");
            }
            #endregion

            #region Redirect
            ///
            /// Redirect
            ///
            ///
            protected void Redirect(string url)
            {
                this.ClientScript.RegisterClientScriptBlock(this.GetType(), url, @"
                ");
            }
            #endregion

            #region ShowMessageAlert
            ///
            /// Show Message Alert
            ///
            ///
            protected void ShowMessageAlert(string msg)
            {
                //TODO replace \n to string.empty
                this.ClientScript.RegisterClientScriptBlock(this.GetType(),msg, @"
                ");
            }
            #endregion

            #region ShowMessageAlertAndCloseWindow
            ///
            /// Show Message Alert
            ///
            ///
            protected void ShowMessageAlertAndCloseWindow(string msg)
            {
                //TODO replace \n to string.empty
                this.ClientScript.RegisterClientScriptBlock(this.GetType(), msg, @"
                ");
            }
            #endregion

            #endregion
        }
        #endregion

        #region BaseView
        ///
        ///Creation Date:      17-Nov-2009
        ///Name: BaseView
        ///Version:     1.0.0
        ///Purpose:     View to Use in ASP.NET Project  - MVC Partner
        ///Author:     Fabio Leandro Lapuinka
        ///
        ///Revision History
        ///Version             Date           By Whom      Change Description
        ///----------    ------         ------------ ---------------------------------------------
        ///1.0.0         10-May-10    FLL          Documentation and Code Standards
        ///
        public class BaseView : BasePage
        {
            #region Properties
           
            #region ObjectEdit
            private System.Object objectEdit;
            ///
            /// Edit Object
            ///
            protected System.Object ObjectEdit
            {
                get
                {
                    if (objectEdit == null)
                        objectEdit = Activator.CreateInstance(this.ObjectType());
                    return objectEdit;
                }
                set
                {
                    objectEdit = value;
                }

            }
            #endregion


            #region ObjectEdit
            private System.Object objectDelete;
            ///
            /// Edit Object
            ///
            protected System.Object ObjectDelete
            {
                get
                {
                    if (objectDelete == null)
                        objectDelete = Activator.CreateInstance(this.ObjectType());
                    return objectDelete;
                }
                set
                {
                    objectDelete = value;
                }

            }
            #endregion
            #endregion

            #region Methods
            ///
            /// Safety Get Value Int
            ///
            ///
            ///
            protected int getValueInt(ListItem item)
            {
                if (item != null)
                    return DBNullConvert.ToInt(item.Value);
                else
                    return 0;
            }

            ///
            /// Reset a View
            ///
            public virtual void ResetView(ControlCollection controls)
            {
                if (controls.Count > 0)
                {
                    foreach (Control ctrl in controls)
                    {
                        if (ctrl is IReset)
                        {
                            ((IReset)ctrl).Reset();
                        }

                        if (ctrl is TextBox)
                        {
                            ((TextBox)ctrl).Text = string.Empty;
                        }
                        else if (ctrl is DropDownList)
                        {
                            ((DropDownList)ctrl).SelectedIndex = -1;
                        }
                        else if (ctrl is CheckBox)
                        {
                            ((CheckBox)ctrl).Checked = false;
                        }
                        if (ctrl.Controls.Count > 0)
                        {
                            ResetView(ctrl.Controls);
                        }
                    }
                }
            }
            ///
            /// Objects to View
            ///
            public virtual void ObjectsToView()
            {
            }
            ///
            /// View to Objects
            ///
            public virtual void ViewToObjects()
            {

            }
            public virtual void AfterSave(int ID)
            {
            }
            public virtual void BeforeSave(int ID)
            {
            }
            public virtual void BeforeDelete(int ID)
            {
           
            }
            ///
            /// Fire before View Reset
            ///
            public virtual void BeforeResetView()
            {
            }
            ///
            /// Bind Data
            ///
            protected virtual void BindData()
            {
            }
            #endregion
        }

        #endregion


    }


    Postar um comentário