using System; using System.Collections; using System.Collections.Generic; using System.Text.RegularExpressions; using log4net; using Sleis.Models; using Sleis.Utility; using Sleis.ViewModels; using Spring.Validation; using System.Text; using Spring.Expressions; using Spring.Objects; using System.Reflection; namespace Sleis.Validation.Spring { public class ListMatchItem : BaseSimpleValidator { public string ProviderFormat { get; set; } public string MessageId { get; set; } public Dictionary Keys { get; set; } public string ItemToMatch { get; set; } internal static ILog Log; public ListMatchItem() { Log = LogManager.GetLogger(this.GetType()); } public override string ToString() { return ReflectionUtility.GetPublicPropertiesString(this); } public override bool Validate(object validationContext, IDictionary contextParams, IValidationErrors errors) { Log.DebugFormat("Validate({0},{1},{2})", validationContext, contextParams, errors); if (Keys == null) { throw new ArgumentNullException("Keys"); } if (ItemToMatch == null) { throw new ArgumentNullException("Keys"); } if (base.EvaluateWhen(validationContext, contextParams)) { object objectToValidate = base.EvaluateTest(validationContext, contextParams); IEnumerable objects = objectToValidate as IEnumerable; IEnumerator enumerator = objects.GetEnumerator(); int counter = 0; //List uniqKeys = new List(); while (enumerator.MoveNext()) { object candidateObject = enumerator.Current; Log.DebugFormat("Validating: {0}", candidateObject); ObjectWrapper wrapper1 = new ObjectWrapper(validationContext); ObjectWrapper wrapper2 = new ObjectWrapper(candidateObject); IValidationContextProvider contextProvider = candidateObject as IValidationContextProvider; //Spool all the values into a collection foreach (KeyValuePair key in Keys) { try { string outterProvider = String.Format(ProviderFormat, counter); object keyPartValue = wrapper2.GetPropertyValue(key.Key); object ItemToMatchValue = wrapper1.GetPropertyValue(ItemToMatch); if (ItemToMatchValue == null) { string innerProvider = outterProvider + key.Value; Log.DebugFormat("List Provider: {0}", innerProvider); errors.AddError(innerProvider, new ErrorMessage(MessageId, (contextProvider == null) ? null : contextProvider.ValidationContxt)); } else if ((keyPartValue.ToString() != ItemToMatchValue.ToString())) { string innerProvider = outterProvider + key.Value; Log.DebugFormat("List Provider: {0}", innerProvider); errors.AddError(innerProvider, new ErrorMessage(MessageId, (contextProvider == null) ? null : contextProvider.ValidationContxt)); } //keyParts.Add((keyPartValue == null) ? String.Empty : keyPartValue.ToString()); } catch (Exception wrapperEx) { Log.ErrorFormat("Error while getting {0} value from {1}. Exception: {2}", key.Key, wrapper2.WrappedInstance, ExceptionUtility.GetDeepExceptionMessageOnly(wrapperEx)); } } counter++; } } return errors.IsEmpty; } protected override bool Validate(object objectToValidate) { return Validate(objectToValidate, null, new ValidationErrors()); } } }