/// <summary> /// 判断该业务对象对应的键值是否已经进行了提交处理。 /// 区别于CheckHasSubmit 最主要的部分是增加和是否审核通过的判断。 /// </summary> /// <param name="dbCmd"></param> /// <param name="busObj"></param> /// <param name="keyValue"></param> /// <returns></returns> public bool CheckParentHasSubmit(Database db, MB.RuleBase.IFace.IBaseRule baseRule, object mainEntity) { RuleSettingAttribute ruleSettAtt = MB.RuleBase.Atts.AttributeConfigHelper.Instance.GetRuleSettingAtt(baseRule); //如果用户没有配置那么表示不需要上级引用的约束控制 if (ruleSettAtt == null || ruleSettAtt.BaseDataType == null) { return(true); } //providerAtt.BaseDataType ParentProviderAttribute providerAtt = MB.RuleBase.Atts.AttributeConfigHelper.Instance.GetParentProviderAttByType(ruleSettAtt.BaseDataType); if (providerAtt == null || providerAtt.ProviderTableName == null || providerAtt.ProviderTableName.Length == 0) { return(true); } if (!MB.Util.MyReflection.Instance.CheckObjectExistsProperty(mainEntity, providerAtt.ForeingKeyField)) { throw new MB.Util.APPException(string.Format("在检查引用上级对象时 属性{0}在对象{1}中不存在。请检查配置信息!", providerAtt.ForeingKeyField, mainEntity.GetType().FullName), MB.Util.APPMessageType.DisplayToUser); } object refrenceKeyValue = MB.Util.MyReflection.Instance.InvokePropertyForGet(mainEntity, providerAtt.ForeingKeyField); return(CheckParentHasSubmit(db, providerAtt.ProviderTableName, providerAtt.ProviderKeyName, refrenceKeyValue.ToString())); }
/// <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> /// 从ObjectDataList 创建可进行数据库操作的DbCommand。 /// </summary> /// <param name="baseRule">指定操作的业务类(为空将根据集合中的实体类型中的配置信息来自动创建DBCommand)。</param> /// <param name="db">数据库操作DB.</param> /// <param name="dataList">数据</param> /// <returns></returns> public IList <EntityDbCommandInfo> CreateCmdsFromObjectDataList(IBaseRule baseRule, Database db, ObjectDataList dataList) { RuleSettingAttribute mappingAtt = null; if (baseRule != null) { mappingAtt = AttributeConfigHelper.Instance.GetRuleSettingAtt(baseRule); } if (mappingAtt != null && mappingAtt.BaseDataType == null) { throw new MB.Util.APPException("业务类:" + baseRule.GetType().FullName + " 必须要先配置主表数据在单据中的类型值。", MB.Util.APPMessageType.SysErrInfo); } //获取存储变化的数据对象。 IEnumerable <ObjectDataInfo> vals = dataList.GetCanSaveAndOrder(); //必须要进行排序以获取主表的键值 List <EntityDbCommandInfo> cmds = new List <EntityDbCommandInfo>(); object mainKeyValue = null; bool currentIsMain = false; MB.Orm.Persistence.PersistenceManagerHelper persistenceManager = MB.Orm.Persistence.PersistenceManagerHelper.NewInstance; foreach (ObjectDataInfo dataInfo in vals) { if (mappingAtt != null && (int)dataInfo.DataInDocType == (int)mappingAtt.BaseDataType)//获取基本对象的主键值。 { if (mappingAtt.GenerateKeyModel == GenerateKeyModel.OnDataSave) { //设置对象的主键 MB.Orm.Persistence.EntityIdentityHelper.NewInstance.FillEntityIdentity(dataInfo.ObjectData); } mainKeyValue = GetObjectKeyValue(baseRule, dataInfo); currentIsMain = true; } else { currentIsMain = false; } object data = dataInfo.ObjectData; //MB.Orm.Enums.OperationType operationType = ConvertDataStateToOperationType(dataInfo.DataState); //if (operationType == MB.Orm.Enums.OperationType.None) // continue; if (data == null) { continue; } if (data.GetType().IsSubclassOf(typeof(MB.Orm.Common.BaseModel))) { DbCommand[] acmd = null; if (string.IsNullOrEmpty(dataInfo.ExecuteXmlCfgSqlName)) { MB.Orm.Enums.OperationType operationType = ConvertEntityStateToOperationType((data as MB.Orm.Common.BaseModel).EntityState); if (operationType == MB.Orm.Enums.OperationType.None) { continue; } acmd = persistenceManager.GetDbCommand(db, data as MB.Orm.Common.BaseModel, operationType, dataInfo.SavePropertys); } else { acmd = persistenceManager.GetDbCommand(db, data as MB.Orm.Common.BaseModel, dataInfo.ExecuteXmlCfgSqlName); } //如果存在多个Command 上只需要记录一次主体对象就可以,避免多次进行引用约束判断 bool hasRegister = false; foreach (DbCommand d in acmd) { EntityDbCommandInfo ecmd = new EntityDbCommandInfo(data, d); if (!hasRegister) { ecmd.IsMainEntity = currentIsMain; hasRegister = true; } //目前只对主体数据进行删除的约束标记。 if (ecmd.IsMainEntity && dataInfo.DataState == ObjectDataState.Deleted) { ecmd.OperationType = OperationType.Delete; } ecmd.IsPartPropertyUpdate = (dataInfo.SavePropertys != null && dataInfo.SavePropertys.Length > 0); cmds.Add(ecmd); } } else if (data.GetType().IsSubclassOf(typeof(System.Data.DataSet))) { if (mainKeyValue == null) { throw new MB.Util.APPException("在批量存储对象明细前必须先获取主表键值!", MB.Util.APPMessageType.SysErrInfo); } if (baseRule == null) { throw new MB.Util.APPException("通过CreateCmdsFromObjectDataList 执行DataSet时,业务对象不能为空", Util.APPMessageType.SysErrInfo); } IList <EntityDbCommandInfo> tempCmds = CreateCmdsFromDataSet(db, baseRule, data as DataSet, mainKeyValue, dataInfo.DataInDocType); foreach (EntityDbCommandInfo tCmd in tempCmds) { cmds.Add(tCmd); } } else { throw new MB.RuleBase.Exceptions.DataTypeUnSupportException(data.GetType().FullName); } } return(cmds); }