Beispiel #1
0
        protected override void DoValidate(string objectToValidate, object currentTarget, string key, ValidationResults validationResults)
        {
            string           elementNamePropertyValue = ValidationEngine.GetUniquePropertyValue(currentTarget, elementNameProperty);
            string           projectPropertyValue     = ValidationEngine.GetUniquePropertyValue(currentTarget, projectProperty);
            ModelElement     element         = currentTarget as ModelElement;
            IServiceProvider serviceProvider = element.Store as IServiceProvider;

            if (string.IsNullOrEmpty(projectPropertyValue))
            {
                //The Microsoft.Practices.EnterpriseLibrary.Validation.Validators.StringLengthValidator is fired
                return;
            }

            try
            {
                IVsSolution vsSolution = GetService <IVsSolution, SVsSolution>(serviceProvider);
                using (HierarchyNode hNode = new HierarchyNode(vsSolution, projectPropertyValue))
                {
                    if (hNode == null)
                    {
                        validationResults.AddResult(
                            new ValidationResult(string.Format(CultureInfo.CurrentUICulture, this.MessageTemplate, elementNamePropertyValue), objectToValidate, key, String.Empty, this)
                            );
                    }
                }
            }
            catch (Exception)
            {
                //Thrown if Project doesn't exist on solution
                validationResults.AddResult(
                    new ValidationResult(string.Format(CultureInfo.CurrentUICulture, this.MessageTemplate, elementNamePropertyValue), objectToValidate, key, String.Empty, this)
                    );
            }
        }
Beispiel #2
0
        /// <summary>
        /// Does the validate.
        /// </summary>
        /// <param name="objectToValidate">The object to validate.</param>
        /// <param name="currentTarget">The current target.</param>
        /// <param name="key">The key.</param>
        /// <param name="validationResults">The validation results.</param>
        protected override void DoValidate(ModelBusReference objectToValidate, object currentTarget, string key, ValidationResults validationResults)
        {
            ModelElement currentElement = currentTarget as ModelElement;

            Debug.Assert(currentElement != null);
            string elementNamePropertyValue = ValidationEngine.GetUniquePropertyValue(currentTarget, elementNameProperty);

            if (!validationResults.IsValid)
            {
                return;
            }

            if (objectToValidate == null)
            {
                validationResults.AddResult(
                    new ValidationResult(String.Format(CultureInfo.CurrentUICulture, Resources.ModelReferenceValidatorMessage, key, currentTarget.GetType().Name, elementNamePropertyValue), currentTarget, key, String.Empty, this));
                return;
            }

            //Check if we need to refresh an updated DC reference.
            ModelElement element = ModelBusReferenceResolver.ResolveAndDispose(objectToValidate);

            if (element == null)
            {
                validationResults.AddResult(
                    new ValidationResult(
                        String.Format(CultureInfo.CurrentUICulture,
                                      Resources.CannotResolveReference, currentTarget.GetType().Name, elementNamePropertyValue, objectToValidate.GetDisplayName()), currentTarget, key, String.Empty, this));
                return;
            }

            string referenceElementDisplayName = ValidationEngine.GetUniquePropertyValue(element, "Name");

            if (!objectToValidate.ElementDisplayName.Equals(referenceElementDisplayName, StringComparison.CurrentCulture))
            {
                using (Transaction tx = ((ModelElement)currentTarget).Store.TransactionManager.BeginTransaction("Update reference value"))
                {
                    ModelBusReference refresh = new ModelBusReference(
                        objectToValidate.ModelBus,
                        objectToValidate.LogicalAdapterId,
                        objectToValidate.ModelDisplayName,
                        referenceElementDisplayName,
                        objectToValidate.SerializedAdapterReference,
                        objectToValidate.ReferenceContext);
                    // refresh value
                    PropertyInfo typeProp = currentTarget.GetType().GetProperty("Type");
                    typeProp.SetValue(currentTarget, null, null);    //Set it null to force updating
                    typeProp.SetValue(currentTarget, refresh, null); //Now set the final value
                    tx.Commit();
                }
                validationResults.AddResult(new ValidationResult(
                                                string.Format(CultureInfo.CurrentCulture, Resources.ModelBusRefrenceUpdate, elementNamePropertyValue, referenceElementDisplayName),
                                                currentTarget, key, Constants.LogWarningTag, this));
            }
        }
        /// <summary>
        /// Validate a single item in the collection.
        /// </summary>
        /// <param name="objectToValidate"></param>
        /// <param name="currentTarget"></param>
        /// <param name="key"></param>
        /// <param name="validationResults"></param>
        /// <remarks>
        /// Implement this in order to validate individual collection elements.
        /// </remarks>
        protected override void DoValidateCollectionItem(T objectToValidate, object currentTarget, string key, ValidationResults validationResults)
        {
            string currentTargetName = GetObjectName(currentTarget);

            string uniquePropertyValue = ValidationEngine.GetUniquePropertyValue(objectToValidate, this.UniquePropertyName);

            if (String.IsNullOrEmpty(uniquePropertyValue))
            {
                return;
            }

            if (nameCounter.Contains(uniquePropertyValue))
            {
                validationResults.AddResult(
                    new ValidationResult(String.Format(CultureInfo.CurrentUICulture, this.MessageTemplate, currentTargetName, this.UniquePropertyName), objectToValidate, key, String.Empty, this)
                    );
            }
            else
            {
                nameCounter.Add(uniquePropertyValue, true);
            }
        }