/// <summary> /// 获取实体对象的配置主键。 /// </summary> /// <param name="entity"></param> /// <param name="entityMappingInfo"></param> /// <returns></returns> public string GetPrimaryKey(object entity, ref MB.Orm.Mapping.ModelMappingInfo entityMappingInfo) { Type entityType = entity.GetType(); MB.Orm.Mapping.ModelMappingInfo mappingInfo = MB.Orm.Mapping.AttMappingManager.Instance.GetModelMappingInfo(entityType); if (string.IsNullOrEmpty(mappingInfo.MapTable)) { throw new MB.Util.APPException(string.Format("数据实体{0} 配置有误,没有指定映射到的表。", entityType.FullName), MB.Util.APPMessageType.SysErrInfo); } if (mappingInfo.PrimaryKeys == null || mappingInfo.PrimaryKeys.Count != 1) { throw new MB.Util.APPException(string.Format("数据实体{0} 配置有误,没有指定主键或者不是单一主键配置。", entityType.FullName), MB.Util.APPMessageType.SysErrInfo); } MB.Orm.Mapping.FieldPropertyInfo keyInfo = mappingInfo.PrimaryKeys.Values.FirstOrDefault <MB.Orm.Mapping.FieldPropertyInfo>(); if (keyInfo == null || !MB.Util.MyReflection.Instance.CheckObjectExistsProperty(entity, keyInfo.FieldName)) { throw new MB.Util.APPException(string.Format("在检查下级对象引用时,对象主键{0}在对象{1}中不存在。请检查配置信息!", keyInfo.FieldName, entity.GetType().FullName), MB.Util.APPMessageType.DisplayToUser); } entityMappingInfo = mappingInfo; return(keyInfo.FieldName); }
public BuilderData(Type entityType) { _EntityType = entityType; _EntityMapping = MB.Orm.Mapping.AttMappingManager.Instance.GetModelMappingInfo(entityType); Parameters = new List <SqlParamInfo>(); Columns = new List <SmartTableColumnInfo>(); Where = new List <SmartTableColumnInfo>(); }
/// <summary> /// 批量创建实体对象。 /// </summary> /// <param name="baseRule"></param> /// <param name="dataInDocType"></param> /// <param name="createCount"></param> /// <returns></returns> public IList CreateNewEntityBatch(IBaseRule baseRule, object dataInDocType, int createCount) { ObjectDataMappingAttribute mappingAtt = AttributeConfigHelper.Instance.GetObjectDataMappingAttribute(dataInDocType); if (mappingAtt == null) { throw new MB.RuleBase.Exceptions.RequireConfigDataMappingException(dataInDocType); } Type entityType = mappingAtt.EntityType; if (string.Compare(entityType.FullName, "System.Object", true) == 0) { if (mappingAtt.EntityType == null) { throw new MB.Util.APPException("业务类:" + baseRule.GetType().FullName + " 中的:" + dataInDocType.GetType() + " 的数据值:" + dataInDocType.ToString() + " 在配置ObjectDataMapping时需要配置 EntityType", MB.Util.APPMessageType.SysErrInfo); } } List <object> lstData = new List <object>(); int id = 0; //判断是否为自增列 if (mappingAtt.KeyIsSelfAdd) { id = MB.Orm.Persistence.EntityIdentityHelper.NewInstance.GetEntityIdentity(mappingAtt.MappingTableName, createCount); } for (int i = 0; i < createCount; i++) { object entity = MB.Util.DllFactory.Instance.CreateInstance(entityType); //判断是否为自增列 if (id > 0) { MB.Util.MyReflection.Instance.InvokePropertyForSet(entity, mappingAtt.KeyName, id++); } if (entity.GetType().IsSubclassOf(typeof(MB.Orm.Common.BaseModel))) { (entity as MB.Orm.Common.BaseModel).EntityState = EntityState.New; } //设置默认值 MB.Orm.Mapping.ModelMappingInfo mappingInfo = MB.Orm.Mapping.AttMappingManager.Instance.GetModelMappingInfo(entityType); foreach (MB.Orm.Mapping.FieldPropertyInfo colProperty in mappingInfo.FieldPropertys.Values) { if (colProperty.DefaultValue == null) { continue; } System.Reflection.PropertyInfo proInfo = entityType.GetProperty(colProperty.PropertyName); proInfo.SetValue(entity, colProperty.DefaultValue, null); } lstData.Add(entity); } return(lstData); }
/// <summary> /// 提交指定的业务处理数据。 /// </summary> /// <param name="baseRule"></param> /// <param name="mainEntity"></param> /// <returns></returns> public int ObjectSubmit(MB.RuleBase.IFace.IBaseRule baseRule, object mainEntity) { MB.Orm.Mapping.ModelMappingInfo mappingInfo = null; string keyName = MB.Orm.Mapping.AttMappingManager.Instance.GetPrimaryKey(mainEntity, ref mappingInfo); object keyValue = MB.Util.MyReflection.Instance.InvokePropertyForGet(mainEntity, keyName); string sql = string.Format("UPDATE {0} SET {1} = '1',{2} = SYSTIMESTAMP WHERE {3} = '{4}' ", mappingInfo.MapTable, ENTITY_DOC_STATE, ENTITY_LAST_MODIFIED_DATE, keyName, keyValue.ToString()); Database db = MB.Orm.Persistence.DatabaseHelper.CreateDatabase(); //在提交之前先检查服务器日期 MB.Orm.Persistence.EntityDistributedHelper.NewInstance.CheckEntityCanSave(mainEntity); DbCommand cmd = db.GetSqlStringCommand(sql); _DbExexuteHelper.ExecuteNonQuery(db, cmd); return(1); }
/// <summary> /// 检查当前对象是否已被其它对象引用。 /// </summary> /// <param name="db"></param> /// <param name="baseRule"></param> /// <param name="mainEntity"></param> /// <param name="restrictOption"></param> /// <returns></returns> public bool ObjectOwnerless(Database db, MB.RuleBase.IFace.IBaseRule baseRule, object mainEntity, LinkRestrictOption restrictOption) { if (restrictOption == LinkRestrictOption.CancelSubmit) { if (!CheckExistsDocState(mainEntity)) { return(true); } var docState = GetEntityDocState(mainEntity); if (docState == DocState.Progress) { return(true); } } RuleSettingAttribute ruleSettAtt = MB.RuleBase.Atts.AttributeConfigHelper.Instance.GetRuleSettingAtt(baseRule); //如果用户没有配置那么表示不需要上级引用的约束控制 if (ruleSettAtt == null || ruleSettAtt.BaseDataType == null) { return(true); } NextOwnAttribute[] atts = MB.RuleBase.Atts.AttributeConfigHelper.Instance.GetNextOwnAttByType(ruleSettAtt.BaseDataType); if (atts == null || atts.Length == 0) { return(true); } List <DbCommand> cmds = new List <DbCommand>(); string objDescription = string.Empty; MB.Orm.Mapping.ModelMappingInfo mappingInfo = null; string keyName = MB.Orm.Mapping.AttMappingManager.Instance.GetPrimaryKey(mainEntity, ref mappingInfo); object keyValue = MB.Util.MyReflection.Instance.InvokePropertyForGet(mainEntity, keyName); foreach (NextOwnAttribute att in atts) { //只有配置成 阻止撤消提交的,才进行这样的处理 if (att.RestrictOption != restrictOption) { continue; } if (string.IsNullOrEmpty(att.CfgXmlSqlName)) { string sql = string.Format("SELECT 1 FROM {0} WHERE {1}='{2}'", att.OwnTableName, att.OwnFieldName, keyValue.ToString()); if (!string.IsNullOrEmpty(att.OwnFilter)) { sql += string.Format(att.OwnFilter, keyValue.ToString()); } DbCommand tempCmd = db.GetSqlStringCommand(sql); cmds.Add(tempCmd); } else { System.Data.Common.DbCommand[] tempCmds = MB.Orm.Persistence.PersistenceManagerHelper.NewInstance.CreateDbCommandByXml(db, mappingInfo.XmlConfigFileName, att.CfgXmlSqlName, keyValue); if (tempCmds.Length != 1) { throw new MB.RuleBase.Exceptions.SelectSqlXmlConfigException(mappingInfo.XmlConfigFileName, att.CfgXmlSqlName); } cmds.AddRange(tempCmds); } if (objDescription.Length == 0) { objDescription = "<<" + att.OwnDescription + ">>"; } else { objDescription += "或 <<" + att.OwnDescription + ">>"; } } foreach (DbCommand dbCmd in cmds) { object val = _DbExexuteHelper.ExecuteScalar(db, dbCmd); if (val != null && val != System.DBNull.Value) { throw new MB.Util.APPException(string.Format("由于该对象已生成{0},不能再进行重做。", objDescription), MB.Util.APPMessageType.DisplayToUser); } } return(true); }
/// <summary> /// 根据键值获取包含值的实体对象。 /// </summary> /// <typeparam name="T">从MB.Orm.Common.BaseModel 中继承的数据对象类型。</typeparam> /// <param name="baseRule">获取实体对象集合的业务类。</param> /// <param name="dataInDocType">在业务类中的数据类型。</param> /// <param name="keyValue">需要获取实体键值。</param> /// <returns>返回指定类型的实体对象。</returns> public T GetObjectByKey <T>(IBaseRule baseRule, object dataInDocType, object keyValue) { ObjectDataMappingAttribute mappingAtt = AttributeConfigHelper.Instance.GetObjectDataMappingAttribute(dataInDocType); if (mappingAtt == null) { throw new MB.RuleBase.Exceptions.RequireConfigDataMappingException(dataInDocType); } Database db = MB.Orm.Persistence.DatabaseHelper.CreateDatabase(); System.Data.Common.DbCommand[] cmds = MB.Orm.Persistence.PersistenceManagerHelper.NewInstance.CreateDbCommandByXml(db, mappingAtt.MappingXmlFileName, MB.Orm.Mapping.Xml.XmlSqlMappingInfo.SQL_SELECT_BY_KEY, keyValue); if (cmds.Length != 1) { throw new MB.RuleBase.Exceptions.SelectSqlXmlConfigException(mappingAtt.MappingXmlFileName, MB.Orm.Mapping.Xml.XmlSqlMappingInfo.SQL_SELECT_BY_KEY); } System.Data.Common.DbCommand cmd = cmds[0]; Type entityType = typeof(T); if (string.Compare(entityType.FullName, "System.Object", true) == 0) { if (mappingAtt.EntityType == null) { throw new MB.Util.APPException("业务类:" + baseRule.GetType().FullName + " 中的:" + dataInDocType.GetType() + " 的数据值:" + dataInDocType.ToString() + " 在配置ObjectDataMapping时需要配置 EntityType", MB.Util.APPMessageType.SysErrInfo); } entityType = mappingAtt.EntityType; } bool isBaseModelSub = entityType.IsSubclassOf(typeof(BaseModel)); MB.Orm.Mapping.ModelMappingInfo modelMapping = MB.Orm.Mapping.AttMappingManager.Instance.GetModelMappingInfo(entityType); object entity = MB.Util.DllFactory.Instance.CreateInstance(entityType); string cmdMsg = MB.Orm.Persistence.DbCommandExecuteTrack.Instance.CommandToTrackMessage(db, cmd); MB.Util.TraceEx.Write("正在执行:" + cmdMsg); using (new Util.MethodTraceWithTime(cmdMsg)) { using (IDataReader reader = db.ExecuteReader(cmd)) { try { DataTable dtReader = reader.GetSchemaTable(); while (reader.Read()) { foreach (MB.Orm.Mapping.FieldPropertyInfo proInfo in modelMapping.FieldPropertys.Values) { if (dtReader.Select(string.Format("ColumnName='{0}'", proInfo.FieldName)).Length <= 0) { continue; } if (reader[proInfo.FieldName] != System.DBNull.Value) { MB.Util.MyReflection.Instance.InvokePropertyForSet(entity, proInfo.PropertyName, reader[proInfo.FieldName]); } } break; } } catch (Exception ex) { throw new MB.RuleBase.Exceptions.DatabaseExecuteException("执行" + baseRule.GetType().FullName + "GetObjectByKey<T> 出错!", ex); } finally { } } } if (isBaseModelSub) { MB.Orm.Common.BaseModel temp = entity as MB.Orm.Common.BaseModel; //if (temp == null) // throw new MB.Util.APPException(string.Format("所有的数据实体对象都必须从MB.Orm.Common.BaseModel 继承,请检查{0}", entity.GetType())); temp.EntityState = EntityState.Persistent; } return((T)entity); }