Files
BeWoPlaner/BeWo/Validation/ValidationAttribute.cs

108 lines
3.9 KiB
C#

using System;
using System.Collections;
using System.Globalization;
using BS.Shared.Extensions;
namespace BeWo.Validation
{
[AttributeUsage(AttributeTargets.Property, AllowMultiple = false)]
public class ValidationAttribute : Attribute
{
public ValidationAttribute()
{
this.ValidationRule = ValidationRules.AlwaysValid;
}
public ValidationRules ValidationRule { get; set; }
// Claude: pContext liefert den Klientenbezug fuer EmptyNoticeInNewServiceRecordsAllowed.
// null bedeutet "unbekannt" -> bisheriges Verhalten (Pflichtfeld bleibt).
public bool Validate(object pValue, ICustomerRelatedContext pContext = null)
{
bool lResult = true;
switch (ValidationRule)
{
case ValidationRules.NotZero:
double lNumber;
var lIsNumber = Double.TryParse(pValue.ToStringNullCheck(), out lNumber);
if (lIsNumber)
{
lResult = Math.Abs(lNumber) > 0d;
}
break;
case ValidationRules.GreaterThanZero:
if (pValue == null)
{
lResult = false;
}
else
{
double lNumber2;
var lIsNumber2 = Double.TryParse(pValue.ToStringNullCheck(), out lNumber2);
if (lIsNumber2)
{
lResult = lNumber2 > 0d;
}
}
break;
case ValidationRules.GreaterThanOrEqualZero:
if (pValue == null)
{
lResult = false;
}
else
{
double lNumber3;
var lIsNumber3 = Double.TryParse(pValue.ToStringNullCheck(), out lNumber3);
if (lIsNumber3)
{
lResult = lNumber3 >= 0d;
}
}
break;
case ValidationRules.NotNullOrStringEmpty:
if (pValue is string)
{
lResult = ((string)pValue).Trim() != string.Empty;
}
else
{
lResult = pValue != null;
}
break;
case ValidationRules.EmptyNoticeInNewServiceRecordsAllowed:
// Claude: Dokumentationspflicht greift nur noch bei klientenbezogenen
// Eintraegen (Customer/Hilfeplan). Ohne Klientenbezug - in
// ServiceRecordView2 also bei abgewaehlter CheckBox "chkWithClient" -
// ist die Dokumentation niemals Pflicht.
if (BeWoApp.AppSettings.IsServiceRecordNoticeMandatory
&& (pContext == null || pContext.IsCustomerRelated))
{
if (pValue is string)
{
lResult = ((string)pValue).Trim() != string.Empty;
}
else
{
lResult = pValue != null;
}
}
break;
case ValidationRules.CollectionNotEmpty:
if (pValue is ICollection)
{
lResult = (pValue as ICollection).Count > 0;
}
break;
}
return lResult;
}
}
}