public Tuple <bool, List <RuleValidation> > ExecuteValidation(List <BaseEntity> ToValidate) { try { if (!ToValidate.Any()) { return(Tuple.Create(true, new List <RuleValidation>())); } List <RuleValidation> validations = new List <RuleValidation>(); var jstr = new StringBuilder(); var companyData = ToValidate.FirstOrDefault() as ICompanyData; var ids = ToValidate.Select(p => p.ID).ToList(); PropertyInfo fldInstance = companyData.GetType().GetProperty("InstanceID"); var valueInstance = fldInstance.GetValue(companyData); Dictionary <string, string> dictDuplicates = new Dictionary <string, string>(); //duplicates in updates :) jstr.Append($"("); for (int i = 0; i < ids.Count; i++) { jstr.Append($"!ID.Equals(Guid.Parse(\"{ids[i]}\"))"); if (i < ids.Count - 1) { jstr.Append($" and "); } } jstr.Append($") and "); //normal query jstr.Append($"InstanceID.Equals(Guid.Parse(\"{valueInstance.ToString()}\")) and ("); for (int i = 0; i < ToValidate.Count; i++) { var obj = ToValidate[i]; StringBuilder key = new StringBuilder(); jstr.Append("("); for (int j = 0; j < FieldName.Count(); j++) { PropertyInfo fld = obj.GetType().GetProperty(FieldName[j]); var value = fld.GetValue(obj); key.Append(FieldName[j] + value); if (value == null) { validations.Add(new RuleValidation() { ID = Guid.Empty, IsValidValue = false, Message = $"El campo {FieldName[j]} no fue proporcionado y es obligatorio.", ErrorCode = ErrorCode, Field = FieldName[0], ValueSent = $"Campo compuesto: {key.ToString()}" }); break; } if (fld.PropertyType.FullName.Contains("Int")) { jstr.Append($"{fld.Name}.Equals(Int32.Parse(\"{value.ToString()}\"))"); } else if (fld.PropertyType.FullName.Contains("System.Nullable`1[[System.Guid")) { jstr.Append($"{fld.Name}.Value.Equals(Guid.Parse(\"{value.ToString()}\"))"); } else if (fld.PropertyType.FullName.Contains("Guid")) { jstr.Append($"{fld.Name}.Equals(Guid.Parse(\"{value.ToString()}\"))"); } else if (fld.PropertyType.FullName.Contains("DateTime")) { jstr.Append($"{fld.Name}.Equals(DateTime.Parse(\"{value.ToString()}\"))"); } else { jstr.Append($"{fld.Name} == \"{value.ToString()}\""); } if (j < (FieldName.Count() - 1)) { jstr.Append(" and "); } } jstr.Append(")"); if (!dictDuplicates.TryAdd(key.ToString(), "")) { validations.Add(new RuleValidation() { ID = Guid.Empty, IsValidValue = false, Message = $"Existe 1 o más registros duplicados en la lista que se quieren insertar con el campo: {FriendlyFieldName}.", ErrorCode = ErrorCode, Field = FieldName[0], ValueSent = $"Campo compuesto: {key.ToString()}" }); } if (i == (ToValidate.Count() - 1)) { jstr = jstr.Append(" ) "); } else { jstr = jstr.Append(" or "); } } if (!validations.Any()) { if (null == _validator.MiddlewareManager) { _validator.MiddlewareManager = new MiddlewareManager <T>(new BaseRecordManager <T>(), _validator); } var result = _validator.MiddlewareManager.Find(jstr.ToString(), companyData.company, null); if (result.Any()) { validations.Add(new RuleValidation() { ID = Guid.Empty, IsValidValue = false, Message = $"Existe 1 o más registros que ya existen con el campo: {FriendlyFieldName}.", ErrorCode = ErrorCode, Field = FieldName[0], ValueSent = $"ID: {String.Join(String.Empty, result.Select(p => p.ID.ToString()))}" }); } } if (validations.Any()) { return(Tuple.Create(false, validations)); } } catch (Exception ex) { throw new Exception("Ocurrió un error al validar duplicados.", ex); } return(Tuple.Create(true, new List <RuleValidation>())); }
public Tuple <bool, List <RuleValidation> > ExecuteValidation(List <BaseEntity> ToValidate) { List <RuleValidation> validations = new List <RuleValidation>(); var jstr = new StringBuilder(); var companyData = ToValidate.FirstOrDefault() as ICompanyData; PropertyInfo fldInstance = companyData.GetType().GetProperty("InstanceID"); var valueInstance = fldInstance.GetValue(companyData); Dictionary <string, string> dictDuplicates = new Dictionary <string, string>(); jstr.Append($"InstanceID.Equals(Guid.Parse(\"{valueInstance.ToString()}\")) and ("); for (int i = 0; i < ToValidate.Count; i++) { var obj = ToValidate[i]; for (int j = 0; j < FieldName.Count(); j++) { PropertyInfo fld = obj.GetType().GetProperty(FieldName[j]); var value = fld.GetValue(obj); jstr.Append($"ID.Equals(Guid.Parse(\"{value.ToString()}\"))"); if (j < (FieldName.Count() - 1)) { jstr = jstr.Append(" and "); } } if (i == (ToValidate.Count() - 1)) { jstr = jstr.Append(" ) "); } else { jstr = jstr.Append(" or "); } } if (null == _validator.MiddlewareManager) { _validator.MiddlewareManager = new MiddlewareManager <T>(new BaseRecordManager <T>(), _validator); } var result = _validator.MiddlewareManager.Find(jstr.ToString(), companyData.company, null); if (!result.Any()) { validations.Add(new RuleValidation() { ID = Guid.Empty, IsValidValue = false, Message = $"No existe la relación con: {FriendlyFieldName}.", ErrorCode = ErrorCode, Field = FieldName[0], ValueSent = $"ID: {String.Join(String.Empty, result.Select(p => p.ID.ToString()))}" }); } if (validations.Any()) { return(Tuple.Create(false, validations)); } return(Tuple.Create(true, new List <RuleValidation>())); }