View Code
public enum MappingType { ImportData, MSSQLTable } public class ValidationError { public string FieldName { get; set; } public string ErrorMessage { get; set; } public ValidationError() { } public ValidationError(string fieldName, string errorMessage) { this.FieldName = fieldName; this.ErrorMessage = errorMessage; } } public class RegexType { public const string IsNumber = @"^\d+$"; public const string IsDateTime = @"(\d{2,4})[-/]?([0]?[1-9]|[1][12])[-/]?([0][1-9] |[12]\d|[3][01])\s*([01]\d|[2][0-4])?[:]?([012345]?\d)?[:]?([012345]? \d)?"; public const string IsDecimal = @"^\d+[.]?\d+$"; public const string IsTelphone = @"^(\d{3,4}-)?\d{6,8}$"; public const string IsMobile = @"^[1]+[3,5]+\d{9}$"; public const string IsEmail = @"^(\w)+(\.\w+)*@(\w)+((\.\w{2,3}){1,6})$"; public const string IsCost = @"(?!=^[0.]*$)(?:[0-4]?\d(\.\d\d?)|999999999\.99)"; //public const string IsCost = @"([0-9]{9}?\.{1}[0-9]{2})"; }
[AttributeUsage(AttributeTargets.Property , Inherited = true, AllowMultiple = true)]
public class MappingNameAttribute : Attribute
{
public string MappingName { get; set; }
public MappingType MappingNameType { get; set; }
public MappingNameAttribute(string mappingName )
{
this.MappingName = mappingName;
}
public MappingNameAttribute(string mappingName, MappingType mappingNameType)
{
this.MappingName = mappingName;
this.MappingNameType = mappingNameType;
}
}
[AttributeUsage(AttributeTargets.All, AllowMultiple = true)]
public class ValidateAttribute : Attribute
{
private bool m_Required;
public bool Required
{
get { return this.m_Required; }
set
{
this.m_Required = value;
}
}
private int m_MinLength;
public int MinLength
{
get { return this.m_MinLength; }
set { this.m_MinLength = value; }
}
private int m_MaxLength;
public int MaxLength
{
get { return this.m_MaxLength; }
set { this.m_MaxLength = value; }
}
private string m_RegexText;
public string RegexText
{
get { return this.m_RegexText; }
set { this.m_RegexText = value; }
}
private string m_ErrorMessage;
public string ErrorMessage
{
get { return this.m_ErrorMessage; }
set { this.m_ErrorMessage = value; }
}
public ValidateAttribute()
{
}
public ValidateAttribute(bool required,string regexText, string errorMessage)
{
this.m_Required = required;
this.m_RegexText = regexText;
this.m_ErrorMessage = errorMessage;
}
public ValidateAttribute(string regexText, string errorMessage)
{
this.m_RegexText = regexText;
this.m_ErrorMessage = errorMessage;
}
}
public class EntityValidateHelper
{
public static Dictionary<string, Dictionary<string, List<ValidateAttribute>>> entityList = new Dictionary<string,Dictionary<string,List<ValidateAttribute>>> ();
private static Dictionary<string, List<ValidateAttribute>> GetValidateAttribute(Type type)
{
Dictionary<string, List<ValidateAttribute>> dict = new Dictionary<string, List<ValidateAttribute>>();
foreach (PropertyInfo property in type.GetProperties())
{
object[] validateContent = property.GetCustomAttributes(typeof(ValidateAttribute), true);
if (validateContent != null)
{
List<ValidateAttribute> list = null;
foreach (ValidateAttribute validateAttribute in validateContent)
{
if (list == null)
list = new List<ValidateAttribute>();
list.Add(validateAttribute);
}
if (list != null)
dict.Add(property.Name, list);
}
}
return dict;
}
public static System.Globalization.CultureInfo Culture { get; set; }
private static string GetErrorMessage(string key)
{
//if (Culture==null)
// return Message_CommonText.ResourceManager.GetString(key);
//else
// return Message_CommonText.ResourceManager.GetString(key, Culture);
return key;
}
public static List<ValidationError> GetValidateResult(object entityObject)
{
if (entityObject == null)
throw new ArgumentNullException("entityObject");
List<ValidationError> validateResultList = null;
Type type = entityObject.GetType();
Dictionary<string, List<ValidateAttribute>> propertyDict;
if (!entityList.TryGetValue(type.FullName, out propertyDict))
{
propertyDict = GetValidateAttribute(type);
entityList.Add(type.FullName, propertyDict);
}
if (propertyDict != null)
{
validateResultList = new List<ValidationError>();
foreach (var item in propertyDict)
{
Object value= type.GetProperty(item.Key).GetValue(entityObject,null); ;
foreach (var validateAttribute in item.Value)
{
string str = value == null ? string.Empty : value.ToString();
bool validateResult = true;
if (validateResult && validateAttribute.Required && (value == null || string.IsNullOrWhiteSpace(str)))
validateResult = false;
if (validateResult && validateAttribute.MinLength > 0 && validateAttribute.MaxLength > 0 && str.Length > 0 && (str.Length < validateAttribute.MinLength || str.Length > validateAttribute.MaxLength))
validateResult = false;
if (validateResult && !string.IsNullOrWhiteSpace(validateAttribute.RegexText) && string.IsNullOrWhiteSpace(str) && Regex.IsMatch(str, validateAttribute.RegexText))
validateResult = false;
if (!validateResult)
validateResultList.Add(new ValidationError(item.Key, GetErrorMessage(validateAttribute.ErrorMessage)));
}
}
}
return validateResultList;
}
}
View Code
private bool CompareTemplate(string mappingName) { return columnNames.FirstOrDefault(n => string.Equals(n,mappingName,StringComparison.CurrentCultureIgnoreCase)) ==null ? false : true; } private void CheckValidateEntity() { Type entityType = assembly.GetType("PO.Services.Entity." + entityName); Type ValidateType = assembly.GetType("PO.Services.Entity.Attributes.ValidateAttribute"); if (!EntityValidateHelper.entityList.ContainsKey(entityType.FullName)) { Dictionary<string, List<ValidateAttribute>> dictProperty = new Dictionary<string, List<ValidateAttribute>>(); foreach (PropertyInfo item in entityType.GetProperties()) { Object[] attributes = item.GetCustomAttributes(true); if (attributes != null) { List<ValidateAttribute> validateList = new List<ValidateAttribute>(); foreach (Object validateAttribute in attributes.Where(a => a.GetType() == ValidateType)) { Object objRequired = ValidateType.GetProperty("Required").GetValue(validateAttribute, null); Object objMinLength = ValidateType.GetProperty("MinLength").GetValue(validateAttribute, null); Object objMaxLength = ValidateType.GetProperty("MaxLength").GetValue(validateAttribute, null); Object objRegexText = ValidateType.GetProperty("RegexText").GetValue(validateAttribute, null); Object objErrorMessage = ValidateType.GetProperty("ErrorMessage").GetValue(validateAttribute, null); ValidateAttribute validateEntity = new ValidateAttribute(); validateEntity.Required = string.Equals(objRequired.ToString(), "true", StringComparison.OrdinalIgnoreCase); if (objMinLength != null && !string.IsNullOrWhiteSpace(objMinLength.ToString())) validateEntity.MinLength = Int32.Parse(objMinLength.ToString()); if (objMaxLength != null && !string.IsNullOrWhiteSpace(objMaxLength.ToString())) validateEntity.MaxLength = Int32.Parse(objMaxLength.ToString()); if (objRegexText != null) validateEntity.RegexText = objRegexText.ToString(); if (objErrorMessage != null) validateEntity.ErrorMessage = objErrorMessage.ToString(); validateList.Add(validateEntity); } if (validateList.Count>0) { dictProperty.Add(item.Name, validateList); } } } if(dictProperty.Count>0) EntityValidateHelper.entityList.Add(entityType.FullName, dictProperty); } } private bool CheckTemplateColumnName() { Boolean isValid = true; Type entityType = assembly.GetType("PO.Services.Entity." + entityName); Type mappingType = assembly.GetType("PO.Services.Entity.Attributes.MappingNameAttribute"); foreach (PropertyInfo item in entityType.GetProperties()) { Object[] mappingAttributes = item.GetCustomAttributes(true); if (mappingAttributes != null) { foreach (Object mappingAttribute in mappingAttributes.Where(a => a.GetType() == mappingType)) { bool isExist = false; Object objMappingName = mappingType.GetProperty("MappingName").GetValue(mappingAttribute, null); Object objMappingNameType = mappingType.GetProperty("MappingNameType").GetValue(mappingAttribute, null); if (objMappingName != null && objMappingNameType != null && string.Equals(objMappingNameType.ToString(), MappingType.ImportData.ToString())) { string tempMappingName = objMappingName.ToString(); isExist = CompareTemplate(tempMappingName); if (isExist) { if (!mappingName.ContainsKey(tempMappingName)) mappingName.Add(tempMappingName, item.Name); } else { return false; } } } } } return isValid; } private string CheckEntityIsInvalid(Object entity) { String errorDescription = string.Empty; List<PO.Services.Entity.Attributes.ValidationError> checkResult = EntityValidateHelper.GetValidateResult(entity); if (checkResult != null && checkResult.Count > 0) checkResult.ForEach(s=>errorDescription+=";"+s.ErrorMessage); return string.IsNullOrWhiteSpace(errorDescription) ? string.Empty : errorDescription.Remove(0,1);; }
[Validate(Required=true,MinLength = 1, MaxLength = 6, ErrorMessage = "PLEASE_CHECK_THE_TEMPLATE_IS_CORRECT")]
[MappingName("VendorNumber", MappingType.ImportData)]
public string VendorNumber { get; set; }
[Validate(true,RegexType.IsCost, "PLEASE_CHECK_THE_TEMPLATE_IS_CORRECT")]
[MappingName("LastCost", MappingType.ImportData)]
public Decimal LastCost { get; set; }
