Esempio n. 1
0
        private List<TransField> GetTransFieldList(Transaction transaction,
            EntityObject entityObject, EntityState state)
        {
            List<TransField> updateColumnsList = new List<TransField>();

            foreach (var field in transaction.TransFields)
            {
                if (field.UpdateMode == UpdateMode.Disable)
                {
                    continue;
                }

                if (string.IsNullOrWhiteSpace(field.SrcField))
                {
                    if (!string.IsNullOrWhiteSpace(field.SrcGetValue))
                    {
                        if (state == EntityState.Added)
                        {
                            field.OldValue = null;
                        }
                        else
                        {
                            if (field.OldValue == null)
                            {
                                field.OldValue = GetFieldDefaultValue(field, entityObject);
                            }
                        }

                        if (state == EntityState.Deleted)
                        {
                            field.NewValue = null;
                        }
                        else
                        {
                            if (field.NewValue == null)
                            {
                                field.NewValue = GetFieldDefaultValue(field, entityObject);
                            }
                        }
                    }
                    else
                    {
                        continue;
                    }
                }
                else
                {
                    if (field.UpdateMode == UpdateMode.WriteBack)
                    {
                        continue;
                    }

                    if (state == EntityState.Added)
                    {
                        field.OldValue = null;
                    }
                    else
                    {
                        field.OldValue = this.Context.GetObjectOriginalValue(entityObject, field.SrcField);
                    }

                    if (state == EntityState.Deleted)
                    {
                        if (field.UpdateMode == UpdateMode.Replace)
                        {
                            field.NewValue = this.Context.GetObjectOriginalValue(entityObject, field.SrcField);
                        }
                        else
                        {
                            field.NewValue = null;
                        }
                    }
                    else
                    {
                        field.NewValue = this.Context.GetObjectCurrentValue(entityObject, field.SrcField);
                    }
                }

                updateColumnsList.Add(field);
            }

            return updateColumnsList;
        }
Esempio n. 2
0
        //private void UpdateObject(EntityObject entityObject, List<TransField> updateColumnsList, EntityState state, bool keyFieldsChanged, bool secondOperation)
        //{
        //    bool isDeleteObject = false;
        //    foreach (var field in updateColumnsList)
        //    {
        //        if (field.ReadOnly)
        //        {
        //            throw new ArgumentException(MessageHelper.EFTransactionMessage.TransDesFieldIsReadOnly);
        //        }

        //        #region Get Field Value
        //        object fieldValue = null;

        //        if (keyFieldsChanged)
        //        {
        //            if (!secondOperation)
        //            {
        //                switch (field.UpdateMode)
        //                {
        //                    case UpdateMode.Increase:
        //                        fieldValue = Operate(entityObject.GetValue(field.DesField), field.OldValue, UpdateMode.Decrease);
        //                        isDeleteObject = CheckValue(field, fieldValue);
        //                        break;
        //                    case UpdateMode.Decrease:
        //                        fieldValue = Operate(entityObject.GetValue(field.DesField), field.OldValue, UpdateMode.Increase);
        //                        break;
        //                    default:
        //                        continue;
        //                }
        //            }
        //            else
        //            {
        //                switch (field.UpdateMode)
        //                {
        //                    case UpdateMode.Increase:
        //                        fieldValue = Operate(entityObject.GetValue(field.DesField), field.NewValue, UpdateMode.Increase);
        //                        break;
        //                    case UpdateMode.Decrease:
        //                        fieldValue = Operate(entityObject.GetValue(field.DesField), field.NewValue, UpdateMode.Decrease);
        //                        isDeleteObject = CheckValue(field, fieldValue);
        //                        break;
        //                    case UpdateMode.Replace:
        //                        fieldValue = field.NewValue;
        //                        break;
        //                    default:
        //                        continue;
        //                }
        //            }
        //        }
        //        else
        //        {
        //            switch (field.UpdateMode)
        //            {
        //                case UpdateMode.Increase:
        //                    fieldValue = Operate(entityObject.GetValue(field.DesField), field.NewValue, UpdateMode.Increase);
        //                    fieldValue = Operate(fieldValue, field.OldValue, UpdateMode.Decrease);
        //                    isDeleteObject = CheckValue(field, fieldValue);
        //                    break;
        //                case UpdateMode.Decrease:
        //                    fieldValue = Operate(entityObject.GetValue(field.DesField), field.NewValue, UpdateMode.Decrease);
        //                    fieldValue = Operate(fieldValue, field.OldValue, UpdateMode.Increase);
        //                    isDeleteObject = CheckValue(field, fieldValue);
        //                    break;
        //                case UpdateMode.Replace:
        //                    if (state == EntityState.Deleted)
        //                    {
        //                        fieldValue = field.OldValue;
        //                    }
        //                    else
        //                    {
        //                        fieldValue = field.NewValue;
        //                    }
        //                    break;
        //                default:
        //                    continue;
        //            }
        //        }
        //        #endregion

        //        entityObject.SetValue(field.DesField, fieldValue);
        //    }

        //    if (isDeleteObject)
        //    {
        //        this.Context.DeleteObject(entityObject);
        //    }
        //    else
        //    {

        //        this.Context.AttachUpdated(entityObject);
        //    }
        //}

        private void AddObject(Transaction transaction, EntityObject entityObject, EntityState state, List<TransKeyField> transKeyFields, List<TransField> transFields, bool isKeyChanged, bool isGetOldValue, bool isAlawysAppend)
        {
            EntityObject transactionTable = this.Context.CreateObject(transaction.TransTableName);

            EFAutoNumber autoNumber = transaction.AutoNumber;
            if (autoNumber != null)
            {
                autoNumber.Context = this.Context;
                autoNumber.GetNumber(entityObject, true);
                transactionTable.SetValue(autoNumber.TargetColumn, autoNumber.Number);
            }

            foreach (var keyField in transKeyFields)
            {
                object fieldValue = null;
                if (!keyField.ReadOnly)
                {
                    if (autoNumber != null)
                    {
                        if (keyField.DesField != autoNumber.TargetColumn)
                        {
                            fieldValue = keyField.OldValue;
                        }
                        else
                        {
                            continue;
                        }
                    }

                    if (isKeyChanged || isGetOldValue)
                    {
                        fieldValue = keyField.OldValue;
                    }
                    else
                    {
                        fieldValue = keyField.NewValue;
                    }

                    transactionTable.SetValue(keyField.DesField, fieldValue);
                }
            }



            foreach (var field in transFields)
            {
                object fieldValue = null;

                if (!field.ReadOnly)
                {
                    if (autoNumber != null)
                    {
                        if (field.DesField != autoNumber.TargetColumn)
                        {
                            fieldValue = field.OldValue;
                        }
                        else
                        {
                            continue;
                        }
                    }

                    if (isKeyChanged && !isAlawysAppend)
                    {
                        if (field.UpdateMode != UpdateMode.WriteBack)
                        {
                            fieldValue = field.OldValue;
                        }
                    }
                    else
                    {
                        if (field.UpdateMode != UpdateMode.WriteBack)
                        {
                            fieldValue = this.GetFieldValue(field, isGetOldValue, isAlawysAppend);
                        }
                    }

                    if (fieldValue != null)
                    {
                        transactionTable.SetValue(field.DesField, fieldValue);
                    }
                }
            }

            this.Context.AddObject(transaction.TransTableName, transactionTable);
        }
Esempio n. 3
0
        private List<TransKeyField> GetTransKeyFieldsList(Transaction transaction,
            EntityObject entityObject, EntityState state)
        {
            List<TransKeyField> transKeyFieldsList = new List<TransKeyField>();

            foreach (var keyField in transaction.TransKeyFields)
            {
                if (string.IsNullOrWhiteSpace(keyField.SrcField))
                {
                    if (!string.IsNullOrWhiteSpace(keyField.SrcGetValue))
                    {
                        if (state == EntityState.Added)
                        {
                            keyField.OldValue = null;
                        }
                        else
                        {
                            if (keyField.OldValue == null)
                            {
                                keyField.OldValue = GetFieldDefaultValue(keyField, entityObject);
                            }
                        }

                        if (state == EntityState.Deleted)
                        {
                            keyField.NewValue = null;
                        }
                        else
                        {
                            if (keyField.NewValue == null)
                            {
                                keyField.NewValue = GetFieldDefaultValue(keyField, entityObject);
                            }
                        }
                    }
                    else
                    {
                        continue;
                    }
                }
                else
                {
                    if (keyField.WhereMode == WhereMode.InsertOnly)
                    {
                        keyField.ReadOnly = true;
                    }
                    else
                    {
                        keyField.ReadOnly = false;
                    }

                    if (state == EntityState.Added)
                    {
                        keyField.OldValue = null;
                    }
                    else
                    {
                        keyField.OldValue = this.Context.GetObjectOriginalValue(entityObject, keyField.SrcField);
                    }

                    if (state == EntityState.Deleted)
                    {
                        keyField.NewValue = null;
                    }
                    else
                    {
                        keyField.NewValue = this.Context.GetObjectCurrentValue(entityObject, keyField.SrcField);
                    }
                }
                transKeyFieldsList.Add(keyField);
            }

            return transKeyFieldsList;
        }
Esempio n. 4
0
        private void WriteBack(Transaction transaction, EntityObject entityObject, EntityObject destinationEntitiyObject, List<TransField> transFields)
        {
            foreach (var field in transFields)
            {
                object desFieldValue = null;
                if (field.UpdateMode == UpdateMode.WriteBack)
                {
                    if (transaction.AutoNumber != null && string.Compare(field.DesField, transaction.AutoNumber.TargetColumn, true) == 0)
                    {
                        desFieldValue = transaction.AutoNumber.Number;
                    }
                    else
                    {
                        if (string.IsNullOrWhiteSpace(field.DesField))
                        {
                            desFieldValue = this.GetFieldDefaultValue(field, entityObject);
                        }
                        else
                        {
                            desFieldValue = destinationEntitiyObject.GetValue(field.DesField);
                        }
                    }

                    entityObject.SetValue(field.SrcField, desFieldValue);
                }

            }
            this.Context.AttachUpdated(entityObject);
        }
Esempio n. 5
0
        private EntityObject GetDestinationObject(Transaction transaction, List<TransKeyField> transKeyFields, EntityState state, bool isGetOldKeyObject)
        {
            EntityObject resValue = null;

            string sql = string.Format("Select value c from {0}.{1} as c", Context.DefaultContainerName, transaction.TransTableName);
            ObjectQuery<EntityObject> query = this.Context.CreateQuery<EntityObject>(sql);
            var parameterPrefix = EntityProvider.GetParameterPrefix(query.Context);
            List<WhereParameter> listWhereParameter = new List<WhereParameter>();

            foreach (var keyField in transKeyFields)
            {
                if (keyField.WhereMode != WhereMode.InsertOnly)
                {
                    WhereParameter whereParameter = new WhereParameter();
                    whereParameter.And = true;
                    whereParameter.Field = keyField.DesField;
                    switch (state)
                    {
                        case EntityState.Added:
                            whereParameter.Value = keyField.NewValue;
                            break;
                        case EntityState.Deleted:
                            whereParameter.Value = keyField.OldValue;
                            break;
                        case EntityState.Modified:
                            whereParameter.Value = isGetOldKeyObject == true ? keyField.OldValue : keyField.NewValue;
                            break;
                    }
                    listWhereParameter.Add(whereParameter);
                }
            }

            query = EntityProvider.SetWhere(query, listWhereParameter);
            List<EntityObject> objects = query.ToList();
            if (objects.Count != 0)
            {
                resValue = objects[0];
            }
            return resValue;
        }
Esempio n. 6
0
        /// <summary>
        /// 
        /// </summary>
        /// <param name="transaction"></param>
        /// <returns></returns>
        private bool CheckFieldsValidate(Transaction transaction)
        {
            bool result = true;
            foreach (var keyField in transaction.TransKeyFields)
            {
                if (!CheckFieldValidate(transaction, keyField, false))
                {
                    result = false;
                    break;
                }
            }

            if (result)
            {
                foreach (var field in transaction.TransFields)
                {
                    if (!CheckFieldValidate(transaction, field, field.UpdateMode == UpdateMode.WriteBack))
                    {
                        result = false;
                        break;
                    }
                }
            }

            return result;
        }
Esempio n. 7
0
        private bool CheckFieldValidate(Transaction transaction, TransFieldBase field, bool writeBack)
        {
            if (writeBack)
            {
                if (string.IsNullOrWhiteSpace(field.SrcField))
                {
                    throw new ArgumentNullException("SrcField");
                }

                if (string.IsNullOrWhiteSpace(field.DesField) && string.IsNullOrWhiteSpace(field.SrcGetValue))
                {
                    throw new ArgumentNullException("DesField or SrcGetValue");
                }
            }
            else
            {
                if (string.IsNullOrWhiteSpace(field.DesField))
                {
                    throw new ArgumentNullException("DesField");
                }

                if (string.IsNullOrWhiteSpace(field.SrcField) && string.IsNullOrWhiteSpace(field.SrcGetValue))
                {
                    throw new ArgumentNullException("SrcField or SrcGetValue");
                }
            }

            if (string.IsNullOrWhiteSpace(field.SrcField)) 
            {
                PropertyInfo sourceColumn = transaction.CurrentObject.GetType().GetProperty(field.SrcField);
                if (sourceColumn == null)
                {
                    throw new ArgumentException(string.Format(MessageHelper.EFTransactionMessage.ColumnNotInTable, field.SrcField, transaction.CurrentObject.EntityKey.EntitySetName));
                }
            }

            PropertyInfo destinationColumn = this.Context.CreateObject(transaction.TransTableName).GetType().GetProperty(field.DesField);
            if (destinationColumn == null)
            {
                throw new ArgumentException(string.Format(MessageHelper.EFTransactionMessage.ColumnNotInTable, field.DesField, transaction.TransTableName));
            }

            return true;
        }
Esempio n. 8
0
 private EntityObject GetDestinationEntityObject(Transaction transaction)
 {
     List<TransKeyField> transKeyFields = this.GetTransKeyFieldsList(transaction, transaction.CurrentObject, transaction.CurrentState);
     return this.GetDestinationObject(transaction, transKeyFields, transaction.CurrentState, false);
 }
Esempio n. 9
0
        public object GetDestinationColumnValue(Transaction transaction, string columnName)
        {
            EntityObject entityObject = GetDestinationEntityObject(transaction);

            return entityObject.GetValue(columnName);
        }
Esempio n. 10
0
        public Transaction Copy()
        {
            Transaction transPrivateCopy = new Transaction();

            transPrivateCopy.AutoNumber = this.AutoNumber;

            foreach (TransField field in this.TransFields)
            {
                TransField tf = new TransField();
                tf.DesField = field.DesField;
                tf.OldValue = field.OldValue;
                tf.SrcField = field.SrcField;
                tf.SrcGetValue = field.SrcGetValue;
                tf.NewValue = field.NewValue;
                tf.UpdateMode = field.UpdateMode;

                transPrivateCopy.TransFields.Add(tf);
            }

            foreach (TransKeyField keyField in this.TransKeyFields)
            {
                TransKeyField tkf = new TransKeyField();
                tkf.DesField = keyField.DesField;
                tkf.SrcField = keyField.SrcField;
                tkf.SrcGetValue = keyField.SrcGetValue;
                tkf.NewValue = keyField.NewValue;
                tkf.WhereMode = keyField.WhereMode;

                transPrivateCopy.TransKeyFields.Add(tkf);
            }
            transPrivateCopy.TransMode = this.TransMode;
            transPrivateCopy.TransStep = this.TransStep;
            transPrivateCopy.TransTableName = this.TransTableName;
            transPrivateCopy.WhenInsert = this.WhenInsert;
            transPrivateCopy.WhenUpdate = this.WhenUpdate;
            transPrivateCopy.WhenDelete = this.WhenDelete;

            return transPrivateCopy;
        }
Esempio n. 11
0
 public EFTransactionBeforeTransEventArgs(Transaction trans)
     : base()
 {
     _transaction = trans;
 }
Esempio n. 12
0
 // Get transstep order rule.
 private int GetTransStepRule(Transaction transaction1, Transaction transaction2)
 {
     if (transaction1.TransStep >= transaction2.TransStep)
     {
         return transaction1.TransStep;
     }
     else
     {
         return transaction2.TransStep;
     }
 }