Example #1
0
        /// <summary>
        /// Writes the JSON representation of the object.
        /// </summary>
        /// <param name="writer">The <see cref="JsonWriter"/> to write to.</param>
        /// <param name="value">The value.</param>
        /// <param name="serializer">The calling serializer.</param>
        public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
        {
            EntityKeyMember entityKeyMember = (EntityKeyMember)value;
            Type            keyType         = (entityKeyMember.Value != null) ? entityKeyMember.Value.GetType() : null;

            writer.WriteStartObject();
            writer.WritePropertyName("Key");
            writer.WriteValue(entityKeyMember.Key);
            writer.WritePropertyName("Type");
            writer.WriteValue((keyType != null) ? keyType.FullName : null);

            writer.WritePropertyName("Value");

            if (keyType != null)
            {
                string valueJson;
                if (JsonSerializerInternalWriter.TryConvertToString(entityKeyMember.Value, keyType, out valueJson))
                {
                    writer.WriteValue(valueJson);
                }
                else
                {
                    writer.WriteValue(entityKeyMember.Value);
                }
            }
            else
            {
                writer.WriteNull();
            }

            writer.WriteEndObject();
        }
Example #2
0
        private static void SetRelationId(Class entity, EntityKeyMember id, ref TAuditTrail audit)
        {
            var idEntity = 0;

            int.TryParse(entity.GetType().GetProperty(id.Key).GetValue(entity).ToString(), out idEntity);
            audit.RelationId = idEntity;
        }
Example #3
0
        public static string BuildKeyQuery(IEnumerable <EntityKeyMember> keyMembers,
                                           out ObjectParameter[] parameters)
        {
            parameters = new ObjectParameter[keyMembers.Count()];

            int index = 0;

            using (IEnumerator <EntityKeyMember> enumerator = keyMembers.GetEnumerator())
            {
                //拼接字符串,例如会出现it.Id = @Id;objcetParameter(Id,"123")
                enumerator.MoveNext();
                EntityKeyMember keyMember = enumerator.Current;
                StringBuilder   builder   = new StringBuilder();
                builder.Append("it.");
                builder.Append(keyMember.Key);
                builder.Append(" = @");
                builder.Append(keyMember.Key);
                parameters[index++] = new ObjectParameter(keyMember.Key, keyMember.Value);

                while (enumerator.MoveNext())
                {
                    builder.Append(" and ");
                    keyMember = enumerator.Current;
                    builder.Append("it.");
                    builder.Append(keyMember.Key);
                    builder.Append(" = @");
                    builder.Append(keyMember.Key);
                    parameters[index++] = new ObjectParameter(keyMember.Key, keyMember.Value);
                }
                return(builder.ToString());
            }
        }
Example #4
0
 /// <summary>
 ///     set value for audit
 /// </summary>
 /// <param name="entity"></param>
 /// <param name="id"></param>
 /// <param name="oldEntity"></param>
 /// <param name="property"></param>
 /// <param name="audit"></param>
 /// <returns></returns>
 private static TAuditTrail SetValueAudit(Class entity, EntityKeyMember id, Class oldEntity,
                                          PropertyInfo property, TAuditTrail audit)
 {
     SetRelationId(entity, id, ref audit);
     SetOldValue(oldEntity, property, ref audit);
     SetNewValue(entity, property, ref audit);
     return(audit);
 }
Example #5
0
        public static string BuildKeyQuery(EntityKeyMember keyMember)
        {
            StringBuilder builder = new StringBuilder();

            builder.Append("it.");
            builder.Append(keyMember.Key);
            builder.Append(">");
            builder.Append(GetValue(keyMember.Value));
            return(builder.ToString());
        }
Example #6
0
        /// <summary>
        /// key中不含有特殊字符,可以不用占位符
        /// </summary>
        public static string BuildKeyQuery(IEnumerable <IEnumerable <EntityKeyMember> > keys)
        {
            StringBuilder sb = new StringBuilder();

            using (IEnumerator <IEnumerable <EntityKeyMember> > tor = keys.GetEnumerator())
            {
                tor.MoveNext();
                IEnumerable <EntityKeyMember> arr = tor.Current;
                using (IEnumerator <EntityKeyMember> key = arr.GetEnumerator())
                {
                    key.MoveNext();
                    EntityKeyMember keyMember = key.Current;
                    sb.Append("(it.").Append(keyMember.Key).Append("=").Append(GetValue(keyMember.Value));
                    while (key.MoveNext())
                    {
                        sb.Append(" and ");
                        sb.Append("it.").Append(keyMember.Key).Append("=").Append(GetValue(keyMember.Value));
                    }
                    sb.Append(")");
                }
                while (tor.MoveNext())
                {
                    sb.Append(" or ");
                    arr = tor.Current;
                    using (IEnumerator <EntityKeyMember> key = arr.GetEnumerator())
                    {
                        key.MoveNext();
                        EntityKeyMember keyMember = key.Current;
                        sb.Append("(it.").Append(keyMember.Key).Append("=").Append(GetValue(keyMember.Value));
                        while (key.MoveNext())
                        {
                            sb.Append(" and ");
                            sb.Append("it.").Append(keyMember.Key).Append("=").Append(GetValue(keyMember.Value));
                        }
                        sb.Append(")");
                    }
                }
            }

            return(sb.ToString());
        }
Example #7
0
        /// <summary>
        /// Reads the JSON representation of the object.
        /// </summary>
        /// <param name="reader">The <see cref="JsonReader"/> to read from.</param>
        /// <param name="objectType">Type of the object.</param>
        /// <param name="serializer">The calling serializer.</param>
        /// <returns>The object value.</returns>
        public override object ReadJson(JsonReader reader, Type objectType, JsonSerializer serializer)
        {
            EntityKeyMember entityKeyMember = new EntityKeyMember();

            ReadAndAssertProperty(reader, "Key");
            ReadAndAssert(reader);
            entityKeyMember.Key = reader.Value.ToString();

            ReadAndAssertProperty(reader, "Type");
            ReadAndAssert(reader);
            string type = reader.Value.ToString();

            Type t = Type.GetType(type);

            ReadAndAssertProperty(reader, "Value");
            ReadAndAssert(reader);
            entityKeyMember.Value = serializer.Deserialize(reader, t);

            ReadAndAssert(reader);

            return(entityKeyMember);
        }
Example #8
0
        public TEntity GetByPkValue(object pkvalue, out OperationResult rState)
        {
            try
            {
                var k       = new EntityKeyMember(this.GetPkName(), pkvalue);
                var keyVals = new List <EntityKeyMember>();
                keyVals.Add(k);
                var       entitySet  = this.getObjSet();
                var       entSetName = string.Format("{0}.{1}", entitySet.EntitySet.EntityContainer.Name, entitySet.Name);
                EntityKey key        = new EntityKey(entSetName, keyVals);

                var result = (TEntity)entitySet.Context.GetObjectByKey(key);

                rState = new OperationResult(null);
                return(result);
            }
            catch (Exception ex)
            {
                rState = new OperationResult(ex);
                return(null);
            }
        }
Example #9
0
        public void Save()
        {
            ObservableCollection<FBEntity> listOfFBEntity = new ObservableCollection<FBEntity>();
            for (int i = 0; i < EntityList.Count; i++)
            {
                OrderEntity item = EntityList[i];

                FBEntity fbEntitySave = item.GetModifiedFBEntity();
                ObservableCollection<FBEntity> listSave = null;
                if (fbEntitySave.Entity.GetType() == typeof(VirtualDepartment))
                {
                    listSave = fbEntitySave.GetRelationFBEntities(typeof(T_FB_SUBJECTDEPTMENT).Name);

                    //活动经费
                    CheckMoneyAssign(listSave);
                }
                else
                {
                    listSave = fbEntitySave.GetRelationFBEntities(typeof(T_FB_SUBJECTPOST).Name);

                    // 删除 不是active的岗位项目记录
                    //listSave.RemoveAll(itemRemove =>
                    //{
                    //    bool isAdded = itemRemove.FBEntityState == FBEntityState.Added;
                    //  //  bool isNew = (itemRemove.Entity as T_FB_SUBJECTPOST).ACTIVED != 1;
                    //    //return isAdded && isNew;
                    //    return isAdded;
                    //});
                }

                listSave.ToList().ForEach(fbEntity =>
                {
                    bool tf = true;//外键为空的不处理
                    #region 公共参数赋值
                    if (fbEntity.IsNewEntity())
                    {
                        fbEntity.FBEntityState = FBEntityState.Added;


                        fbEntity.SetObjValue("Entity.CREATECOMPANYID", item.LoginUser.Company.Value);
                        fbEntity.SetObjValue("Entity.CREATECOMPANYNAME", item.LoginUser.Company.Text);
                        fbEntity.SetObjValue("Entity.CREATEDEPARTMENTID", item.LoginUser.Department.Value);
                        fbEntity.SetObjValue("Entity.CREATEDEPARTMENTNAME", item.LoginUser.Department.Text);
                        fbEntity.SetObjValue("Entity.CREATEPOSTID", item.LoginUser.Post.Value);
                        fbEntity.SetObjValue("Entity.CREATEPOSTNAME", item.LoginUser.Post.Text);
                        fbEntity.SetObjValue("Entity.CREATEUSERID", item.LoginUser.Value);
                        fbEntity.SetObjValue("Entity.CREATEUSERNAME", item.LoginUser.Text);

                        fbEntity.SetObjValue("Entity.EDITSTATES", decimal.Parse("1"));

                    }
                    fbEntity.SetObjValue("Entity.UPDATEUSERID", item.LoginUser.Value);
                    fbEntity.SetObjValue("Entity.UPDATEUSERNAME", item.LoginUser.Text);
                    #endregion

                    T_FB_SUBJECTPOST sp = fbEntity.Entity as T_FB_SUBJECTPOST;

                    // 去除实体之间的关联,如有关联存在,上传服务端时,会有异常
                    if (sp == null)
                    {
                        T_FB_SUBJECTDEPTMENT sd = fbEntity.Entity as T_FB_SUBJECTDEPTMENT;

                        if (sd.T_FB_SUBJECTCOMPANY != null && sd.T_FB_SUBJECT != null)
                        {
                            sd.T_FB_SUBJECTCOMPANYReference.EntityKey = sd.T_FB_SUBJECTCOMPANY.EntityKey;
                            sd.T_FB_SUBJECTCOMPANY = null;

                            sd.T_FB_SUBJECTReference.EntityKey = sd.T_FB_SUBJECT.EntityKey;
                            sd.T_FB_SUBJECT = null;
                            sd.T_FB_SUBJECTPOST = null;
                            fbEntity.CollectionEntity.Clear();
                        }
                        else
                        {
                            tf = false;
                        }
                    }
                    else
                    {
                        if (sp.T_FB_SUBJECTDEPTMENT != null && sp.T_FB_SUBJECT != null)
                        {
                            EntityKey parentKey = sp.T_FB_SUBJECTDEPTMENT.EntityKey;
                            if (parentKey.EntityKeyValues == null)
                            {
                                EntityKeyMember em = new EntityKeyMember();
                                em.Key = "SUBJECTDEPTMENTID";
                                em.Value = sp.T_FB_SUBJECTDEPTMENT.SUBJECTDEPTMENTID;

                                EntityKey newKey = new EntityKey();
                                newKey.EntityContainerName = parentKey.EntityContainerName;
                                newKey.EntitySetName = parentKey.EntitySetName;
                                newKey.EntityKeyValues = new ObservableCollection<EntityKeyMember>();
                                newKey.EntityKeyValues.Add(em);
                                parentKey = newKey;
                            }
                            sp.T_FB_SUBJECTDEPTMENTReference = new EntityReferenceOfT_FB_SUBJECTDEPTMENTZ5CrhPbu();
                            sp.T_FB_SUBJECTDEPTMENTReference.EntityKey = parentKey;
                            sp.T_FB_SUBJECTDEPTMENT = null;

                            sp.T_FB_SUBJECTReference = new EntityReferenceOfT_FB_SUBJECTZ5CrhPbu();
                            sp.T_FB_SUBJECTReference.EntityKey = sp.T_FB_SUBJECT.EntityKey;
                            sp.T_FB_SUBJECT = null;

                            // 清除已删除实体,这些实体会在 T_FB_SUBJECTDEPTMENT 实体中处理 .
                            fbEntity.GetRelationFBEntities(Args.DELETE_ENTITYTYPE).Clear();
                        }
                        else
                        {
                            tf = false;
                        }
                    }
                    if (tf)
                        listOfFBEntity.Add(fbEntity);

                });
            }

            if (listOfFBEntity.Count > 0)
            {
                this.CurrentOrderEntity = new OrderEntity(typeof(VirtualCompany));


                this.orderEntityService.SaveList(listOfFBEntity);
            }
            else
            {
                CloseProcess();
            }
        }
Example #10
0
        public void Save()
        {
            ObservableCollection <FBEntity> listOfFBEntity = new ObservableCollection <FBEntity>();

            for (int i = 0; i < EntityList.Count; i++)
            {
                OrderEntity item = EntityList[i];

                FBEntity fbEntitySave = item.GetModifiedFBEntity();
                ObservableCollection <FBEntity> listSave = null;
                if (fbEntitySave.Entity.GetType() == typeof(VirtualDepartment))
                {
                    listSave = fbEntitySave.GetRelationFBEntities(typeof(T_FB_SUBJECTDEPTMENT).Name);

                    //活动经费
                    // CheckMoneyAssign(listSave);
                }
                else
                {
                    listSave = fbEntitySave.GetRelationFBEntities(typeof(T_FB_SUBJECTPOST).Name);

                    // 删除 不是active的岗位项目记录
                    //listSave.RemoveAll(itemRemove =>
                    //{
                    //    bool isAdded = itemRemove.FBEntityState == FBEntityState.Added;
                    //  //  bool isNew = (itemRemove.Entity as T_FB_SUBJECTPOST).ACTIVED != 1;
                    //    //return isAdded && isNew;
                    //    return isAdded;
                    //});
                }

                listSave.ToList().ForEach(fbEntity =>
                {
                    bool tf = true;//外键为空的不处理
                    #region 公共参数赋值
                    if (fbEntity.IsNewEntity())
                    {
                        fbEntity.FBEntityState = FBEntityState.Added;


                        fbEntity.SetObjValue("Entity.CREATECOMPANYID", item.LoginUser.Company.Value);
                        fbEntity.SetObjValue("Entity.CREATECOMPANYNAME", item.LoginUser.Company.Text);
                        fbEntity.SetObjValue("Entity.CREATEDEPARTMENTID", item.LoginUser.Department.Value);
                        fbEntity.SetObjValue("Entity.CREATEDEPARTMENTNAME", item.LoginUser.Department.Text);
                        fbEntity.SetObjValue("Entity.CREATEPOSTID", item.LoginUser.Post.Value);
                        fbEntity.SetObjValue("Entity.CREATEPOSTNAME", item.LoginUser.Post.Text);
                        fbEntity.SetObjValue("Entity.CREATEUSERID", item.LoginUser.Value);
                        fbEntity.SetObjValue("Entity.CREATEUSERNAME", item.LoginUser.Text);

                        fbEntity.SetObjValue("Entity.EDITSTATES", decimal.Parse("1"));
                    }
                    fbEntity.SetObjValue("Entity.UPDATEUSERID", item.LoginUser.Value);
                    fbEntity.SetObjValue("Entity.UPDATEUSERNAME", item.LoginUser.Text);
                    #endregion

                    T_FB_SUBJECTPOST sp = fbEntity.Entity as T_FB_SUBJECTPOST;

                    // 去除实体之间的关联,如有关联存在,上传服务端时,会有异常
                    if (sp == null)
                    {
                        T_FB_SUBJECTDEPTMENT sd = fbEntity.Entity as T_FB_SUBJECTDEPTMENT;

                        if (sd.T_FB_SUBJECTCOMPANY != null && sd.T_FB_SUBJECT != null)
                        {
                            sd.T_FB_SUBJECTCOMPANYReference.EntityKey = sd.T_FB_SUBJECTCOMPANY.EntityKey;
                            sd.T_FB_SUBJECTCOMPANY = null;

                            sd.T_FB_SUBJECTReference.EntityKey = sd.T_FB_SUBJECT.EntityKey;
                            sd.T_FB_SUBJECT     = null;
                            sd.T_FB_SUBJECTPOST = null;
                            fbEntity.CollectionEntity.Clear();
                        }
                        else
                        {
                            tf = false;
                        }
                    }
                    else
                    {
                        if (sp.T_FB_SUBJECTDEPTMENT != null && sp.T_FB_SUBJECT != null)
                        {
                            EntityKey parentKey = sp.T_FB_SUBJECTDEPTMENT.EntityKey;
                            if (parentKey.EntityKeyValues == null)
                            {
                                EntityKeyMember em = new EntityKeyMember();
                                em.Key             = "SUBJECTDEPTMENTID";
                                em.Value           = sp.T_FB_SUBJECTDEPTMENT.SUBJECTDEPTMENTID;

                                EntityKey newKey           = new EntityKey();
                                newKey.EntityContainerName = parentKey.EntityContainerName;
                                newKey.EntitySetName       = parentKey.EntitySetName;
                                newKey.EntityKeyValues     = new ObservableCollection <EntityKeyMember>();
                                newKey.EntityKeyValues.Add(em);
                                parentKey = newKey;
                            }
                            sp.T_FB_SUBJECTDEPTMENTReference           = new EntityReferenceOfT_FB_SUBJECTDEPTMENTZ5CrhPbu();
                            sp.T_FB_SUBJECTDEPTMENTReference.EntityKey = parentKey;
                            sp.T_FB_SUBJECTDEPTMENT = null;

                            sp.T_FB_SUBJECTReference           = new EntityReferenceOfT_FB_SUBJECTZ5CrhPbu();
                            sp.T_FB_SUBJECTReference.EntityKey = sp.T_FB_SUBJECT.EntityKey;
                            sp.T_FB_SUBJECT = null;

                            // 清除已删除实体,这些实体会在 T_FB_SUBJECTDEPTMENT 实体中处理 .
                            fbEntity.GetRelationFBEntities(Args.DELETE_ENTITYTYPE).Clear();
                        }
                        else
                        {
                            tf = false;
                        }
                    }
                    if (tf)
                    {
                        listOfFBEntity.Add(fbEntity);
                    }
                });
            }

            if (listOfFBEntity.Count > 0)
            {
                this.CurrentOrderEntity = new OrderEntity(typeof(VirtualCompany));


                this.orderEntityService.SaveList(listOfFBEntity);
            }
            else
            {
                CloseProcess();
            }
        }
Example #11
0
        internal void WriteAudit(SADFMEntities context, Guid lastUpdateAccountId)
        {
            if (!context.ChangeTracker.HasChanges())
            {
                return;
            }

            var allChanges = context.ChangeTracker.Entries();

            foreach (DbEntityEntry ee in allChanges)
            {
                if (ee.State == System.Data.Entity.EntityState.Unchanged)
                {
                    continue;
                }
                if (ee.State == System.Data.Entity.EntityState.Deleted)
                {
                    //Deletes should be tracked by the parent object or status change
                    continue;
                }

                ObjectContext    ox   = ((IObjectContextAdapter)context).ObjectContext;
                ObjectStateEntry ose  = ox.ObjectStateManager.GetObjectStateEntry(ee.Entity);
                Guid             pkid = Guid.Empty;
                if (ose.EntityKey.EntityKeyValues != null)
                {
                    EntityKeyMember member = ose.EntityKey.EntityKeyValues[0];
                    pkid = (Guid)member.Value;
                }
                bool   added     = ee.State == System.Data.Entity.EntityState.Added;
                string tableName = ose.EntitySet.Name;

                System.Data.Entity.Infrastructure.DbPropertyValues original = null;
                if (!added)
                {
                    original = ee.OriginalValues;
                }
                System.Data.Entity.Infrastructure.DbPropertyValues current = null;
                if (ee.State != System.Data.Entity.EntityState.Deleted)
                {
                    current = ee.CurrentValues;
                }

                foreach (string property in current.PropertyNames)
                {
                    object originalValue = original == null ? null : original.GetValue <object>(property);
                    object currentValue  = current == null ? null : current.GetValue <object>(property);
                    if (originalValue == null && currentValue == null)
                    {
                        continue;
                    }
                    if (originalValue == null || currentValue == null || originalValue.ToString() != currentValue.ToString())
                    {
                        context.Audits.Add(new Data.Audit
                        {
                            TableName    = tableName,
                            ChangeTypeId = DataAccess.ListItem.GetListItem("ChangeType", added ? "Insert" : "Update").GUID,
                            AccountId    = lastUpdateAccountId,
                            FieldName    = property,
                            OldValue     = originalValue == null ? null : originalValue.ToString().Substring(0, originalValue.ToString().Length > 1000 ? 1000 : originalValue.ToString().Length),
                            NewValue     = currentValue == null ? null : currentValue.ToString().Substring(0, currentValue.ToString().Length > 1000 ? 1000 : currentValue.ToString().Length),
                            PrimaryKeyId = pkid,
                            UpdateDate   = DateTime.Now
                        });
                    }
                }
            }
        }
Example #12
0
 private Class GetOldEntityFromRepository(Class entity, EntityKeyMember id)
 {
     return
         (new SqlGenericRepository <Class>((DbContext)Activator.CreateInstance(Context.GetType()), Logger).GetByID
              (entity.GetType().GetProperty(id.Key).GetValue(entity)));
 }