Beispiel #1
0
        public override int SaveChanges()
        {
            Dictionary <Guid, System.Data.Entity.Core.Objects.ObjectStateEntry> added = new Dictionary <Guid, System.Data.Entity.Core.Objects.ObjectStateEntry>();

            string userid   = null;
            string systemid = null;
            string clientIP = null;

            if (GetCurrentUserId != null)
            {
                userid = GetCurrentUserId();
            }
            if (GetCurrentSystemId != null)
            {
                systemid = GetCurrentSystemId();
            }
            if (GetCurrentClientIP != null)
            {
                clientIP = GetCurrentClientIP();
            }


            ChangeTracker.DetectChanges();

            System.Data.Entity.Core.Objects.ObjectContext ctx = ((System.Data.Entity.Infrastructure.IObjectContextAdapter) this).ObjectContext;

            List <System.Data.Entity.Core.Objects.ObjectStateEntry> objectStateEntryList =
                ctx.ObjectStateManager.GetObjectStateEntries(System.Data.Entity.EntityState.Added
                                                             | System.Data.Entity.EntityState.Modified
                                                             | System.Data.Entity.EntityState.Deleted)
                .ToList();

            this.logDebug(string.Format("Founded {0} Entity to Save.", objectStateEntryList.Count));

            var doHistorical = true;

            if (master.framework.Configuration.MasterFramework.Database.Historical.isOFF)
            {
                doHistorical = false;
            }

            if (doHistorical)
            {
                foreach (var item in objectStateEntryList.Select((value, index) => new dto.ForEach <System.Data.Entity.Core.Objects.ObjectStateEntry>()
                {
                    Index = index, Value = value
                }).ToList())
                {
                    var tableName = item.Value.EntitySet.Name;
                    this.logDebug(string.Format("Checking {0} - {1}", item.Index, tableName));

                    if (!item.Value.IsRelationship)
                    {
                        var withoutHistorical = item.Value.Entity.GetType().GetCustomAttributes(typeof(WithoutHistorical), true).Length > 0;
                        var withHistorical    = item.Value.Entity.GetType().GetCustomAttributes(typeof(WithHistorical), true).Length > 0;

                        if (withoutHistorical)
                        {
                            break;
                        }

                        WithHistorical historicalAtt = null;

                        if (withHistorical)
                        {
                            historicalAtt = item.Value.Entity.GetType().GetCustomAttributes(typeof(WithHistorical), true).FirstOrDefault() as WithHistorical;
                        }

                        var properties = item.Value.Entity.GetType().GetProperties();

                        switch (item.Value.State)
                        {
                        case System.Data.Entity.EntityState.Added:
                            #region Added
                        {
                            #region Key Attribute
                            {
                                var propertyId = properties.Where(o => o.GetCustomAttributes(typeof(Key), true).Length > 0).ToList();
                                if (propertyId?.Count > 0)
                                {
                                    var property = propertyId.FirstOrDefault();
                                    if (property.PropertyType == typeof(Guid))
                                    {
                                        if ((Guid)property.GetValue(item.Value.Entity) == Guid.Empty)
                                        {
                                            bool setvalue = false;
                                            var  att      = property.GetCustomAttributes(typeof(Key), true).FirstOrDefault() as Key;
                                            if (att != null)
                                            {
                                                setvalue = att.AutoGenerated;
                                            }
                                            if (setvalue)
                                            {
                                                property.SetValue(item.Value.Entity, Guid.NewGuid());
                                            }
                                        }
                                    }
                                }
                            }
                            #endregion

                            #region Created Attribute
                            {
                                var propertyRegister = properties.Where(o => o.GetCustomAttributes(typeof(Created), true).Length > 0).ToList();
                                if (propertyRegister?.Count > 0)
                                {
                                    foreach (var itemProperty in propertyRegister)
                                    {
                                        var createdAtt = itemProperty.GetCreatedAtt();
                                        switch (createdAtt?.CreatedType)
                                        {
                                        case Enumerators.CreatedType.User:
                                            itemProperty.SetValue(item.Value.Entity, userid);
                                            break;

                                        case Enumerators.CreatedType.Date:
                                            itemProperty.SetValue(item.Value.Entity, DateTime.Now);
                                            break;
                                        }
                                    }
                                }
                            }
                            #endregion

                            #region CreatedOrUpdated Attribute
                            {
                                var propertyRegister = properties.Where(o => o.GetCustomAttributes(typeof(CreatedOrUpdated), true).Length > 0).ToList();
                                if (propertyRegister?.Count > 0)
                                {
                                    foreach (var itemProperty in propertyRegister)
                                    {
                                        var createdAtt = itemProperty.GetCreatedOrUpdatedAtt();
                                        switch (createdAtt?.CreatedType)
                                        {
                                        case Enumerators.CreatedOrUpdatedType.User:
                                        {
                                            System.Type     propertyType     = itemProperty.GetType();
                                            System.TypeCode propertyTypeCode = Type.GetTypeCode(propertyType);
                                            if (itemProperty.PropertyType.IsGenericType && itemProperty.PropertyType.GetGenericTypeDefinition() == typeof(Nullable <>))
                                            {
                                                propertyTypeCode = Type.GetTypeCode(itemProperty.PropertyType.GetGenericArguments()[0]);
                                            }
                                            switch (propertyTypeCode)
                                            {
                                            case TypeCode.Int16:
                                            case TypeCode.UInt16:
                                                if (!string.IsNullOrWhiteSpace(userid))
                                                {
                                                    itemProperty.SetValue(item.Value.Entity, short.Parse(userid));
                                                }
                                                break;

                                            case TypeCode.Int32:
                                            case TypeCode.UInt32:
                                                if (!string.IsNullOrWhiteSpace(userid))
                                                {
                                                    itemProperty.SetValue(item.Value.Entity, int.Parse(userid));
                                                }
                                                break;

                                            case TypeCode.Int64:
                                            case TypeCode.UInt64:
                                                if (!string.IsNullOrWhiteSpace(userid))
                                                {
                                                    itemProperty.SetValue(item.Value.Entity, long.Parse(userid));
                                                }
                                                break;

                                            case TypeCode.String:
                                                itemProperty.SetValue(item.Value.Entity, userid);
                                                break;
                                            }
                                        }
                                        break;

                                        case Enumerators.CreatedOrUpdatedType.Date:
                                            itemProperty.SetValue(item.Value.Entity, DateTime.Now);
                                            break;
                                        }
                                    }
                                }
                            }
                            #endregion

                            #region SystemId Attribute
                            {
                                var propertyRegister = properties.Where(o => o.GetCustomAttributes(typeof(SystemId), true).Length > 0).ToList();
                                if (propertyRegister?.Count > 0)
                                {
                                    foreach (var itemProperty in propertyRegister)
                                    {
                                        itemProperty.SetValue(item.Value.Entity, systemid);
                                    }
                                }
                            }
                            #endregion

                            #region UserIP Attribute
                            {
                                var propertyRegister = properties.Where(o => o.GetCustomAttributes(typeof(UserIP), true).Length > 0).ToList();
                                if (propertyRegister?.Count > 0)
                                {
                                    foreach (var itemProperty in propertyRegister)
                                    {
                                        itemProperty.SetValue(item.Value.Entity, clientIP);
                                    }
                                }
                            }
                            #endregion

                            if (historicalAtt != null)
                            {
                                added.Add(Guid.NewGuid(), item.Value);
                            }
                        }
                            #endregion
                            break;

                        case System.Data.Entity.EntityState.Deleted:
                            #region Deleted
                        {
                            bool isToInactive = false;

                            #region Historical
                            if (HistoricalDeleted != null && historicalAtt != null)
                            {
                                var    key      = item.Value.EntityKey.EntityKeyValues.FirstOrDefault();
                                string keyName  = key?.Key;
                                string keyValue = key?.Value.ToString();
                                dto.HistoricalEvent histEvent = dto.HistoricalEvent.InstanceDelete(item.Value.Entity, tableName, keyName, keyValue);
                                HistoricalDeleted(this, histEvent);
                            }
                            #endregion

                            #region Inactive Attribute
                            {
                                var propertyRegister = properties.Where(o => o.GetCustomAttributes(typeof(Inactive), true).Length > 0).ToList();
                                if (propertyRegister?.Count > 0)
                                {
                                    item.Value.ChangeState(System.Data.Entity.EntityState.Modified);
                                    foreach (var itemProperty in propertyRegister)
                                    {
                                        itemProperty.SetValue(item.Value.Entity, true);
                                    }
                                    isToInactive = true;
                                }
                            }
                            #endregion

                            if (isToInactive)
                            {
                                #region UpdateDate Attribute
                                {
                                    var propertyRegister = properties.Where(o => o.GetCustomAttributes(typeof(Updated), true).Length > 0).ToList();
                                    if (propertyRegister?.Count > 0)
                                    {
                                        foreach (var itemProperty in propertyRegister)
                                        {
                                            var updatedAtt = itemProperty.GetUpdatedAtt();
                                            switch (updatedAtt?.UpdatedType)
                                            {
                                            case Enumerators.UpdatedType.User:
                                                itemProperty.SetValue(item.Value.Entity, userid);
                                                break;

                                            case Enumerators.UpdatedType.Date:
                                                itemProperty.SetValue(item.Value.Entity, DateTime.Now);
                                                break;
                                            }
                                        }
                                    }
                                }
                                #endregion

                                #region SystemId Attribute
                                {
                                    var propertyRegister = properties.Where(o => o.GetCustomAttributes(typeof(SystemId), true).Length > 0).ToList();
                                    if (propertyRegister?.Count > 0)
                                    {
                                        foreach (var itemProperty in propertyRegister)
                                        {
                                            itemProperty.SetValue(item.Value.Entity, systemid);
                                        }
                                    }
                                }
                                #endregion

                                #region UserIP Attribute
                                {
                                    var propertyRegister = properties.Where(o => o.GetCustomAttributes(typeof(UserIP), true).Length > 0).ToList();
                                    if (propertyRegister?.Count > 0)
                                    {
                                        foreach (var itemProperty in propertyRegister)
                                        {
                                            itemProperty.SetValue(item.Value.Entity, clientIP);
                                        }
                                    }
                                }
                                #endregion
                            }
                        }
                            #endregion
                            break;

                        case System.Data.Entity.EntityState.Modified:
                            #region Modified
                        {
                            var propertyUpdate           = properties.Where(o => o.GetCustomAttributes(typeof(Updated), true).Length > 0).ToList();
                            var propertyInactive         = properties.Where(o => o.GetCustomAttributes(typeof(Inactive), true).Length > 0).ToList();
                            var propertyCreated          = properties.Where(o => o.GetCustomAttributes(typeof(Created), true).Length > 0).ToList();
                            var propertySystemId         = properties.Where(o => o.GetCustomAttributes(typeof(SystemId), true).Length > 0).ToList();
                            var propertyUserIP           = properties.Where(o => o.GetCustomAttributes(typeof(UserIP), true).Length > 0).ToList();
                            var propertyCreatedOrUpdated = properties.Where(o => o.GetCustomAttributes(typeof(CreatedOrUpdated), true).Length > 0).ToList();

                            #region Update Attribute
                            {
                                var propertyRegister = properties.Where(o => o.GetCustomAttributes(typeof(Updated), true).Length > 0).ToList();
                                if (propertyRegister?.Count > 0)
                                {
                                    foreach (var itemProperty in propertyRegister)
                                    {
                                        var updatedAtt = itemProperty.GetUpdatedAtt();
                                        switch (updatedAtt?.UpdatedType)
                                        {
                                        case Enumerators.UpdatedType.User:
                                            itemProperty.SetValue(item.Value.Entity, userid);
                                            break;

                                        case Enumerators.UpdatedType.Date:
                                            itemProperty.SetValue(item.Value.Entity, DateTime.Now);
                                            break;
                                        }
                                    }
                                }
                            }
                            #endregion

                            #region CreatedOrUpdated Attribute
                            {
                                var propertyRegister = properties.Where(o => o.GetCustomAttributes(typeof(CreatedOrUpdated), true).Length > 0).ToList();
                                if (propertyRegister?.Count > 0)
                                {
                                    foreach (var itemProperty in propertyRegister)
                                    {
                                        var createdAtt = itemProperty.GetCreatedOrUpdatedAtt();
                                        switch (createdAtt?.CreatedType)
                                        {
                                        case Enumerators.CreatedOrUpdatedType.User:
                                        {
                                            System.Type     propertyType     = itemProperty.GetType();
                                            System.TypeCode propertyTypeCode = Type.GetTypeCode(propertyType);
                                            if (itemProperty.PropertyType.IsGenericType && itemProperty.PropertyType.GetGenericTypeDefinition() == typeof(Nullable <>))
                                            {
                                                propertyTypeCode = Type.GetTypeCode(itemProperty.PropertyType.GetGenericArguments()[0]);
                                            }
                                            switch (propertyTypeCode)
                                            {
                                            case TypeCode.Int16:
                                            case TypeCode.UInt16:
                                                if (!string.IsNullOrWhiteSpace(userid))
                                                {
                                                    itemProperty.SetValue(item.Value.Entity, short.Parse(userid));
                                                }
                                                break;

                                            case TypeCode.Int32:
                                            case TypeCode.UInt32:
                                                if (!string.IsNullOrWhiteSpace(userid))
                                                {
                                                    itemProperty.SetValue(item.Value.Entity, int.Parse(userid));
                                                }
                                                break;

                                            case TypeCode.Int64:
                                            case TypeCode.UInt64:
                                                if (!string.IsNullOrWhiteSpace(userid))
                                                {
                                                    itemProperty.SetValue(item.Value.Entity, long.Parse(userid));
                                                }
                                                break;

                                            case TypeCode.String:
                                                itemProperty.SetValue(item.Value.Entity, userid);
                                                break;
                                            }
                                        }
                                        break;

                                        case Enumerators.CreatedOrUpdatedType.Date:
                                            itemProperty.SetValue(item.Value.Entity, DateTime.Now);
                                            break;
                                        }
                                    }
                                }
                            }
                            #endregion

                            #region SystemId Attribute
                            {
                                var propertyRegister = properties.Where(o => o.GetCustomAttributes(typeof(SystemId), true).Length > 0).ToList();
                                if (propertyRegister?.Count > 0)
                                {
                                    foreach (var itemProperty in propertyRegister)
                                    {
                                        itemProperty.SetValue(item.Value.Entity, systemid);
                                    }
                                }
                            }
                            #endregion

                            #region UserIP Attribute
                            {
                                var propertyRegister = properties.Where(o => o.GetCustomAttributes(typeof(UserIP), true).Length > 0).ToList();
                                if (propertyRegister?.Count > 0)
                                {
                                    foreach (var itemProperty in propertyRegister)
                                    {
                                        itemProperty.SetValue(item.Value.Entity, clientIP);
                                    }
                                }
                            }
                            #endregion

                            #region Historical
                            if (HistoricalModified != null && historicalAtt != null)
                            {
                                var    key      = item.Value.EntityKey.EntityKeyValues.FirstOrDefault();
                                string keyName  = key?.Key;
                                string keyValue = key?.Value.ToString();

                                switch (historicalAtt.HistoricalType)
                                {
                                case Enumerators.HistoricalType.ByProperty:
                                {
                                    foreach (string propertyName in
                                             item.Value.GetModifiedProperties())
                                    {
                                        if (propertyUpdate.Any(o => o.Name.ToLower() == propertyName.ToLower()) ||
                                            propertyInactive.Any(o => o.Name.ToLower() == propertyName.ToLower()) ||
                                            propertyCreated.Any(o => o.Name.ToLower() == propertyName.ToLower()) ||
                                            propertySystemId.Any(o => o.Name.ToLower() == propertyName.ToLower()) ||
                                            propertyCreatedOrUpdated.Any(o => o.Name.ToLower() == propertyName.ToLower()) ||
                                            propertyUserIP.Any(o => o.Name.ToLower() == propertyName.ToLower()))
                                        {
                                            continue;
                                        }

                                        System.Data.Common.DbDataRecord original = item.Value.OriginalValues;
                                        string oldValue = original.GetValue(original.GetOrdinal(propertyName)).ToString();

                                        System.Data.Entity.Core.Objects.CurrentValueRecord current = item.Value.CurrentValues;
                                        string newValue = current.GetValue(current.GetOrdinal(propertyName)).ToString();

                                        if (oldValue != newValue)                 // probably not necessary
                                        {
                                            dto.HistoricalEvent histEvent = dto.HistoricalEvent.InstanceChanged(item.Value.Entity, tableName, keyName, keyValue,
                                                                                                                propertyName, oldValue, newValue);
                                            HistoricalModified(this, histEvent);
                                        }
                                    }
                                }
                                break;

                                case Enumerators.HistoricalType.ByObject:
                                {
                                    dto.HistoricalEvent histEvent = dto.HistoricalEvent.InstanceChanged(item.Value.Entity, tableName, keyName, keyValue,
                                                                                                        string.Empty, string.Empty, string.Empty);
                                    HistoricalModified(this, histEvent);
                                }
                                break;
                                }
                            }
                            #endregion
                        }
                            #endregion
                            break;
                        }
                    }
                }
            }

            var ret = base.SaveChanges();

            #region Historical Created
            if (doHistorical)
            {
                if (HistoricalCreated != null)
                {
                    foreach (KeyValuePair <Guid, System.Data.Entity.Core.Objects.ObjectStateEntry> item in added)
                    {
                        var    key      = item.Value.EntityKey.EntityKeyValues.FirstOrDefault();
                        string keyName  = key?.Key;
                        string keyValue = key?.Value.ToString();
                        dto.HistoricalEvent histEvent = dto.HistoricalEvent.InstanceAdd(item.Value.Entity, item.Value.EntitySet.Name, keyName, keyValue);
                        HistoricalCreated(this, histEvent);
                    }
                }
            }
            #endregion

            return(ret);
        }
Beispiel #2
0
        protected void dataList_ItemDataBound(object sender, DataListItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                //DataRow row = ((DataRowView)e.Item.DataItem).Row;    //<-- DataSet에서만 가능
                System.Data.Common.DbDataRecord dRecord = (System.Data.Common.DbDataRecord)e.Item.DataItem;
                //((Literal)e.Item.FindControl("litIDX")).Text = e.Item.ItemIndex.ToString();

                //회원등급
                Label mLevel = (Label)e.Item.FindControl("mLevel");
                mLevel.ToolTip = MbrBaseLib.Self.GetStatusText(dRecord.GetByte(dRecord.GetOrdinal("mLevel")));


                //회사명(직업)
                Label lbJobInfo = (Label)e.Item.FindControl("lbJobInfo");
                lbJobInfo.Text    = dRecord.GetValue(dRecord.GetOrdinal("company")).ToString();
                lbJobInfo.ToolTip = dRecord.GetValue(dRecord.GetOrdinal("jobType")).ToString();

                //최근로그인한 날
                Label hobbies = (Label)e.Item.FindControl("hobbies");
                hobbies.ToolTip = dRecord.GetValue(dRecord.GetOrdinal("hobbies")).ToString();
                if (hobbies.ToolTip != "")
                {
                    hobbies.Text = Text.ShortenString(hobbies.ToolTip, 10);
                }

                //메일링크
                HyperLink hlEmail = (HyperLink)e.Item.FindControl("hlEmail");
                hlEmail.Text    = string.Format("{0} ({1})", dRecord.GetString(dRecord.GetOrdinal("MbrName")), dRecord.GetString(dRecord.GetOrdinal("loginID")));
                hlEmail.ToolTip = dRecord.GetValue(dRecord.GetOrdinal("MbrEmail")).ToString();
                string strSendonemail = Text.GetEmailFormat(dRecord.GetValue(dRecord.GetOrdinal("MbrName")).ToString(), hlEmail.ToolTip);
                hlEmail.NavigateUrl = "../Mine/Mail/MailForm.aspx?m=" + strSendonemail;

                //월급정보
                Literal litMonthlyPayText = (Literal)e.Item.FindControl("litMonthlyPayText");
                litMonthlyPayText.Text = MbrBaseLib.Self.GetMonthlyPayText(dRecord.GetValue(dRecord.GetOrdinal("monthlyPay")).ToString());

                //자동차정보
                Literal litCarInfoText = (Literal)e.Item.FindControl("litCarInfoText");
                litCarInfoText.Text = MbrBaseLib.Self.GetCarInfoText(dRecord.GetValue(dRecord.GetOrdinal("carInfo")).ToString());


                URLQuery.Self["mbID"] = ((System.Data.Common.DbDataRecord)e.Item.DataItem).GetValue(0).ToString();
                bool boolDisplay;
                if (MbrBaseLib.Self.CanAccess(string.Empty))
                {
                    //보기버튼
                    HyperLink hlView = (HyperLink)e.Item.FindControl("hlView");
                    hlView.NavigateUrl = "MbrView.aspx?" + URLQuery.Self.GetQueryString();
                    hlView.ToolTip     = "내용보기";
                    hlView.Visible     = true;
                    //수정버튼
                    ImageButton ibModfy = (ImageButton)e.Item.FindControl("ibModfy");
                    ibModfy.CommandName     = "select";
                    ibModfy.CommandArgument = dataList.DataKeys[e.Item.ItemIndex].ToString();
                    //이렇게 할필요 까지???ibModfy.Attributes["onClick"] = "return ConfirmJ('수정하시겠습니까?');";
                    ibModfy.ToolTip = "수정하기";
                    ibModfy.Visible = true;

                    //Response.Write(hlView.Visible + " - " + ibModfy.Visible + " : " + DateTime.Now.ToString() + "<br>");
                    boolDisplay = false;
                }
                else
                {
                    boolDisplay = true;
                }
                //없음표시(뭔가 이상);
                Literal displayNone = (Literal)e.Item.FindControl("displayNone");
                displayNone.Visible = boolDisplay;//DisplayNone(hlView.Visible, ibModfy.Visible);
            }
            else if (e.Item.ItemType == ListItemType.Header)
            {
                Literal litSearchResult = (Literal)e.Item.FindControl("litSearchResult");
                Literal litListBrief    = (Literal)e.Item.FindControl("litListBrief");

                litSearchResult.Text = this.SearchResultDisplay(this.tbSearchString.Text);
                litListBrief.Text    = Paging.Self.ListSummary;
                //각종 상태값 메모리로 올림(상태유지를 위해)
                SetQueryString();
                #region 페이징기능구현
                Paging.Self.Init_Violet();
                this.litPaging.Text = Paging.Self.GeneratePaging();
                #endregion
            }
            else if (e.Item.ItemType == ListItemType.Footer)
            {
                //페이징기능이 여기에 구현되어야 하나 인터페이스에 없으므로 헤더에서 구현
                //Literal litPaging = (Literal)e.Item.FindControl("litPaging");
            }
        }
Beispiel #3
0
        static public Dictionary <string, object> ToDataRow(this System.Data.Common.DbDataRecord me)
        {
            string _message = string.Format("ToDictionary Call Failed");

            try {
                return(Enumerable.Range(0, me.FieldCount).ToDictionary(field => me.GetName(field).ToString(), field => me.GetValue(field)));
            }
            catch (Exception ex) {
                // err.WriteError(_message, ProcessHelper.GetProcessName(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType), ex);
            }
            return(new Dictionary <string, object>());
        }