389 lines
12 KiB
C#
389 lines
12 KiB
C#
using System;
|
|
using System.ComponentModel.DataAnnotations;
|
|
using System.Resources;
|
|
using Kreta.BusinessLogic.Utils;
|
|
using Kreta.Enums.ManualEnums;
|
|
using Kreta.Resources;
|
|
|
|
namespace Kreta.BusinessLogic.Classes
|
|
{
|
|
public class KretaRequiredAttribute : RequiredAttribute
|
|
{
|
|
public int StringResourcesId { get; set; }
|
|
public override string FormatErrorMessage(string name)
|
|
{
|
|
return StringResourcesId != 0 ? StringResourcesUtils.GetString(StringResourcesId) : base.FormatErrorMessage(name);
|
|
}
|
|
|
|
public KretaRequiredAttribute()
|
|
{
|
|
ErrorMessageResourceName = nameof(ErrorResource.Required);
|
|
ErrorMessageResourceType = typeof(ErrorResource);
|
|
}
|
|
|
|
public KretaRequiredAttribute(Type resourceType, string resourceName)
|
|
{
|
|
ErrorMessageResourceName = resourceName;
|
|
ErrorMessageResourceType = resourceType;
|
|
}
|
|
}
|
|
|
|
public class KretaStringLengthAttribute : StringLengthAttribute
|
|
{
|
|
public KretaStringLengthAttribute(int maximumLength) : base(maximumLength)
|
|
{
|
|
|
|
}
|
|
|
|
public int StringResourcesId { get; set; }
|
|
public override string FormatErrorMessage(string name)
|
|
{
|
|
return string.IsNullOrWhiteSpace(ErrorMessageResourceName) ? StringResourcesUtils.GetString(StringResourcesId) : base.FormatErrorMessage(ErrorMessageResourceName);
|
|
}
|
|
}
|
|
|
|
public class KretaCompareAttribute : CompareAttribute
|
|
{
|
|
public KretaCompareAttribute(string otherProperty) : base(otherProperty)
|
|
{
|
|
|
|
}
|
|
|
|
public int StringResourcesId { get; set; }
|
|
public override string FormatErrorMessage(string name)
|
|
{
|
|
|
|
return StringResourcesUtils.GetString(StringResourcesId);
|
|
}
|
|
}
|
|
|
|
public class KretaRangeAttribute : RangeAttribute
|
|
{
|
|
public KretaRangeAttribute(int minimum, int maximum) : base(minimum, maximum)
|
|
{
|
|
|
|
}
|
|
public KretaRangeAttribute(double minimum, double maximum) : base(minimum, maximum)
|
|
{
|
|
|
|
}
|
|
|
|
public int StringResourcesId { get; set; }
|
|
|
|
public override string FormatErrorMessage(string name)
|
|
{
|
|
return string.IsNullOrWhiteSpace(ErrorMessageResourceName) ? StringResourcesUtils.GetString(StringResourcesId) : base.FormatErrorMessage(ErrorMessageResourceName);
|
|
}
|
|
}
|
|
|
|
public class KretaParameterizedRangeAttribute : RangeAttribute
|
|
{
|
|
public double Min { get; set; }
|
|
public double Max { get; set; }
|
|
|
|
public KretaParameterizedRangeAttribute(int minimum, int maximum) : base(minimum, maximum)
|
|
{
|
|
Min = minimum;
|
|
Max = maximum;
|
|
}
|
|
public KretaParameterizedRangeAttribute(double minimum, double maximum) : base(minimum, maximum)
|
|
{
|
|
Min = minimum;
|
|
Max = maximum;
|
|
}
|
|
|
|
public int StringResourcesId { get; set; }
|
|
|
|
public override string FormatErrorMessage(string name)
|
|
{
|
|
return string.IsNullOrWhiteSpace(ErrorMessageResourceName) ? StringResourcesUtils.GetString(StringResourcesId) :
|
|
string.Format(base.ErrorMessageString, Min.ToString(), Max.ToString());
|
|
}
|
|
}
|
|
|
|
public class KretaDateRangeAttribute : ValidationAttribute
|
|
{
|
|
public int StringResourcesId { get; set; }
|
|
public string Min { get; set; }
|
|
public string Max { get; set; }
|
|
private DateTime? Minimum
|
|
{
|
|
get
|
|
{
|
|
DateTime val;
|
|
if (DateTime.TryParse(Min, out val))
|
|
{
|
|
return val;
|
|
}
|
|
return null;
|
|
}
|
|
}
|
|
private DateTime? Maximum
|
|
{
|
|
get
|
|
{
|
|
DateTime val;
|
|
if (DateTime.TryParse(Max, out val))
|
|
{
|
|
return val;
|
|
}
|
|
return null;
|
|
}
|
|
}
|
|
|
|
public KretaDateRangeAttribute()
|
|
{
|
|
Min = "1/1/1900";
|
|
Max = "1/1/2100";
|
|
}
|
|
|
|
public override bool IsValid(object value)
|
|
{
|
|
if (value == null)
|
|
{
|
|
return true;
|
|
}
|
|
var val = (DateTime)value;
|
|
var minOk = (!Minimum.HasValue || val >= Minimum);
|
|
var maxOk = (!Maximum.HasValue || val <= Maximum);
|
|
return minOk && maxOk;
|
|
}
|
|
|
|
public override string FormatErrorMessage(string name)
|
|
{
|
|
return string.IsNullOrWhiteSpace(ErrorMessageResourceName) ? StringResourcesUtils.GetString(StringResourcesId) : base.FormatErrorMessage(ErrorMessageResourceName);
|
|
}
|
|
}
|
|
|
|
public class DateRangeAttribute : ValidationAttribute
|
|
{
|
|
public string ErrorMessageResourceName { get; set; }
|
|
public Type ErrorMessageResourceType { get; set; }
|
|
public string Min { get; set; }
|
|
public string Max { get; set; }
|
|
private DateTime? Minimum
|
|
{
|
|
get
|
|
{
|
|
DateTime val;
|
|
if (DateTime.TryParse(Min, out val))
|
|
{
|
|
return val;
|
|
}
|
|
return null;
|
|
}
|
|
}
|
|
private DateTime? Maximum
|
|
{
|
|
get
|
|
{
|
|
DateTime val;
|
|
if (DateTime.TryParse(Max, out val))
|
|
{
|
|
return val;
|
|
}
|
|
return null;
|
|
}
|
|
}
|
|
|
|
public DateRangeAttribute()
|
|
{
|
|
Min = "1/1/1900";
|
|
Max = "1/1/2100";
|
|
}
|
|
|
|
public override bool IsValid(object value)
|
|
{
|
|
if (value == null)
|
|
{
|
|
return true;
|
|
}
|
|
var val = (DateTime)value;
|
|
var minOk = (!Minimum.HasValue || val >= Minimum);
|
|
var maxOk = (!Maximum.HasValue || val <= Maximum);
|
|
return minOk && maxOk;
|
|
}
|
|
|
|
public override string FormatErrorMessage(string name)
|
|
{
|
|
ResourceManager rm = new ResourceManager(ErrorMessageResourceType);
|
|
return rm.GetString(ErrorMessageResourceName);
|
|
}
|
|
}
|
|
|
|
public class KretaMinLengthAttribute : MinLengthAttribute
|
|
{
|
|
public KretaMinLengthAttribute(int length) : base(length)
|
|
{
|
|
|
|
}
|
|
|
|
public int StringResourcesId { get; set; }
|
|
public override string FormatErrorMessage(string name)
|
|
{
|
|
|
|
return StringResourcesUtils.GetString(StringResourcesId);
|
|
}
|
|
}
|
|
|
|
public class KretaMaxLengthAttribute : MaxLengthAttribute
|
|
{
|
|
public KretaMaxLengthAttribute(int length) : base(length)
|
|
{
|
|
|
|
}
|
|
|
|
public int StringResourcesId { get; set; }
|
|
public override string FormatErrorMessage(string name)
|
|
{
|
|
|
|
return StringResourcesUtils.GetString(StringResourcesId);
|
|
}
|
|
}
|
|
|
|
public class KretaDataTypeAttribute : DataTypeAttribute
|
|
{
|
|
public KretaDataTypeAttribute(DataType dataType) : base(dataType)
|
|
{
|
|
|
|
}
|
|
|
|
public KretaDataTypeAttribute() : base(DataType.Custom)
|
|
{
|
|
|
|
}
|
|
|
|
public new KretaCustomDataTypeEnum CustomDataType { get; set; }
|
|
|
|
public int StringResourcesId { get; set; }
|
|
public override string FormatErrorMessage(string name)
|
|
{
|
|
|
|
return StringResourcesUtils.GetString(StringResourcesId);
|
|
}
|
|
|
|
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
|
|
{
|
|
if (DataType == DataType.Custom)
|
|
{
|
|
if (value != null)
|
|
{
|
|
var isValid = true;
|
|
switch (CustomDataType)
|
|
{
|
|
case KretaCustomDataTypeEnum.Integer:
|
|
isValid = int.TryParse(value.ToString(), out _);
|
|
break;
|
|
case KretaCustomDataTypeEnum.Decimal:
|
|
isValid = decimal.TryParse(value.ToString(), out _);
|
|
break;
|
|
case KretaCustomDataTypeEnum.Float:
|
|
isValid = float.TryParse(value.ToString(), out _);
|
|
break;
|
|
case KretaCustomDataTypeEnum.Double:
|
|
isValid = double.TryParse(value.ToString(), out _);
|
|
break;
|
|
}
|
|
|
|
if (!isValid)
|
|
{
|
|
return new ValidationResult("Must be numeric");
|
|
}
|
|
}
|
|
|
|
return ValidationResult.Success;
|
|
}
|
|
|
|
return base.IsValid(value, validationContext);
|
|
}
|
|
}
|
|
|
|
public class KretaRequiredIfAttribute : KretaRequiredAttribute
|
|
{
|
|
private string PropertyName { get; }
|
|
private object DesiredValue { get; }
|
|
|
|
public KretaRequiredIfAttribute(string propertyName, object desiredvalue)
|
|
{
|
|
PropertyName = propertyName;
|
|
DesiredValue = desiredvalue;
|
|
}
|
|
|
|
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
|
|
{
|
|
object instance = validationContext.ObjectInstance;
|
|
Type type = instance.GetType();
|
|
object propertyValue = type.GetProperty(PropertyName).GetValue(instance, null);
|
|
if (propertyValue.ToString() == DesiredValue.ToString())
|
|
{
|
|
ValidationResult result = base.IsValid(value, validationContext);
|
|
return result;
|
|
}
|
|
return ValidationResult.Success;
|
|
}
|
|
}
|
|
|
|
public class KretaRequiredIfOtherPropertyIsNullAttribute : KretaRequiredAttribute
|
|
{
|
|
private string PropertyName { get; }
|
|
|
|
public KretaRequiredIfOtherPropertyIsNullAttribute(string propertyName)
|
|
{
|
|
PropertyName = propertyName;
|
|
}
|
|
|
|
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
|
|
{
|
|
object instance = validationContext.ObjectInstance;
|
|
Type type = instance.GetType();
|
|
object propertyValue = type.GetProperty(PropertyName).GetValue(instance, null);
|
|
if (propertyValue == null)
|
|
{
|
|
ValidationResult result = base.IsValid(value, validationContext);
|
|
return result;
|
|
}
|
|
return ValidationResult.Success;
|
|
}
|
|
}
|
|
|
|
public class KretaRegularExpressionAttribute : RegularExpressionAttribute
|
|
{
|
|
public KretaRegularExpressionAttribute(string pattern) : base(pattern)
|
|
{
|
|
|
|
}
|
|
|
|
public int StringResourcesId { get; set; }
|
|
public override string FormatErrorMessage(string name)
|
|
{
|
|
return string.IsNullOrWhiteSpace(ErrorMessageResourceName) ? StringResourcesUtils.GetString(StringResourcesId) : base.FormatErrorMessage(ErrorMessageResourceName);
|
|
}
|
|
}
|
|
|
|
//public class KretaIntegerAttribute<T> : ValidationAttribute<T> where T : class
|
|
//{
|
|
// protected override ValidationResult IsValid(object value, ValidationContext validationContext)
|
|
// {
|
|
// if (value != null)
|
|
// {
|
|
// decimal val;
|
|
// var isNumeric = decimal.TryParse(value.ToString(), out val);
|
|
|
|
// if (!isNumeric)
|
|
// {
|
|
// return new ValidationResult("Must be numeric");
|
|
// }
|
|
// }
|
|
|
|
// return ValidationResult.Success;
|
|
// }
|
|
|
|
// public int StringResourcesId { get; set; }
|
|
// public override string FormatErrorMessage(string name)
|
|
// {
|
|
|
|
// return StringResourcesUtils.GetString(StringResourcesId);
|
|
// }
|
|
//}
|
|
|
|
}
|