Beispiel #1
0
        /// <summary>
        /// Validates the specified item using an existing validation context to skip over
        /// items already validated
        /// </summary>
        /// <param name="context"></param>
        /// <param name="item"></param>
        /// <param name="recurse"></param>
        /// <returns></returns>
        public static ValidationIssue[] Validate(ResourceValidationContext context, IResource item, bool recurse)
        {
            Check.ArgumentNotNull(item, nameof(item));
            var issueSet = new ValidationResultSet();

            if (!HasValidator(item.ResourceType, item.ResourceVersion))
            {
                issueSet.AddIssue(new ValidationIssue(item, ValidationStatus.Warning, ValidationStatusCode.Warning_General_NoRegisteredValidatorForResource, string.Format(Strings.ERR_NO_REGISTERED_VALIDATOR, item.ResourceType, item.ResourceVersion)));
            }
            else
            {
                foreach (IResourceValidator v in m_validators)
                {
                    //Ensure the current connection is set before validating
                    v.Connection = context.Connection;

                    if (!v.SupportedResourceAndVersion.Equals(item.GetResourceTypeDescriptor()))
                    {
                        continue;
                    }

                    try
                    {
                        ValidationIssue[] tmp = v.Validate(context, item, recurse);
                        if (tmp != null)
                        {
                            issueSet.AddIssues(tmp);
                        }
                    }
                    catch (Exception ex)
                    {
                        string msg = NestedExceptionMessageProcessor.GetFullMessage(ex);
                        if (ex is NullReferenceException)
                        {
                            msg = ex.ToString();
                        }
                        issueSet.AddIssue(new ValidationIssue(item, ValidationStatus.Error, ValidationStatusCode.Error_General_ValidationError, string.Format(Strings.ErrorValidationGeneric, msg)));
                    }
                }
            }
            return(issueSet.GetAllIssues());
        }
        /// <summary>
        /// Performs base validation logic
        /// </summary>
        /// <param name="context"></param>
        /// <param name="resource"></param>
        /// <param name="recurse"></param>
        /// <returns></returns>
        protected static ValidationIssue[] ValidateBase(ResourceValidationContext context, IResource resource, bool recurse)
        {
            Check.NotNull(context, "context"); //NOXLATE

            if (context.IsAlreadyValidated(resource.ResourceID))
                return null;

            if (resource.ResourceType != OSGeo.MapGuide.MaestroAPI.ResourceTypes.LoadProcedure)
                return null;

            if (resource.ResourceVersion != new Version(1, 0, 0))
                return null;

            var set = new ValidationResultSet();

            var loadProc = (resource as ILoadProcedure).SubType;

            if (loadProc.Type == LoadType.Dwg)
            {
                set.AddIssue(new ValidationIssue(resource, ValidationStatus.Warning, ValidationStatusCode.Warning_LoadProcedure_DwgNotSupported, Strings.LPROC_DWGNotSupported));
                return set.GetAllIssues(); //all she wrote
            }

            if (loadProc.Type == LoadType.Raster)
            {
                set.AddIssue(new ValidationIssue(resource, ValidationStatus.Warning, ValidationStatusCode.Warning_LoadProcedure_RasterNotSupported, Strings.LPROC_RasterNotSupported));
                return set.GetAllIssues(); //all she wrote
            }

            if (loadProc.Type == LoadType.Sdf)
            {
                set.AddIssue(new ValidationIssue(resource, ValidationStatus.Warning, ValidationStatusCode.Warning_LoadProcedure_Sdf2OptionsNotSupported, Strings.LPROC_Sdf2OptionsNotSupported));
                set.AddIssue(new ValidationIssue(resource, ValidationStatus.Warning, ValidationStatusCode.Warning_LoadProcedure_GeneralizationNotSupported, Strings.LPROC_GeneralizationNotSupported));
            }

            if (loadProc.Type == LoadType.Shp)
            {
                set.AddIssue(new ValidationIssue(resource, ValidationStatus.Warning, ValidationStatusCode.Warning_LoadProcedure_ConvertToSdf3NotSupported, Strings.LPROC_ConvertToSdf3NotSupported));
                set.AddIssue(new ValidationIssue(resource, ValidationStatus.Warning, ValidationStatusCode.Warning_LoadProcedure_GeneralizationNotSupported, Strings.LPROC_GeneralizationNotSupported));
            }

            if (loadProc.Type == LoadType.Sqlite)
            {
                set.AddIssue(new ValidationIssue(resource, ValidationStatus.Warning, ValidationStatusCode.Warning_LoadProcedure_GeneralizationNotSupported, Strings.LPROC_GeneralizationNotSupported));
            }

            var fproc = loadProc as IBaseLoadProcedure;
            if (fproc != null)
            {
                foreach (var fn in fproc.SourceFile)
                {
                    if (!System.IO.File.Exists(fn))
                    {
                        set.AddIssue(new ValidationIssue(resource, ValidationStatus.Warning, ValidationStatusCode.Warning_LoadProcedure_SourceFileNotFound, string.Format(Strings.LPROC_SourceFileNotFound, fn)));
                    }
                }
            }

            context.MarkValidated(resource.ResourceID);

            return set.GetAllIssues();
        }
        protected object BackgroundValidate(BackgroundWorker worker, DoWorkEventArgs e, params object[] args)
        {
            //Collect all documents to be validated. Some of these selected items
            //may be folders.
            var documents = new List<string>();
            foreach (object a in args)
            {
                string rid = a.ToString();
                if (ResourceIdentifier.Validate(rid))
                {
                    var resId = new ResourceIdentifier(rid);
                    if (resId.IsFolder)
                    {
                        foreach (IRepositoryItem o in _conn.ResourceService.GetRepositoryResources((string)args[0]).Children)
                        {
                            if (!o.IsFolder)
                            {
                                documents.Add(o.ResourceId);
                            }
                        }
                    }
                    else
                    {
                        documents.Add(rid);
                    }
                }
            }

            worker.ReportProgress(0);
            var context = new ResourceValidationContext(_conn.ResourceService, _conn.FeatureService);

            var set = new ValidationResultSet();
            int i = 0;
            foreach (string s in documents)
            {
                worker.ReportProgress((int)((i / (double)documents.Count) * 100), s);
                IResource item = null;
                try
                {
                    item = _conn.ResourceService.GetResource(s);
                    set.AddIssues(ResourceValidatorSet.Validate(context, item, true));
                }
                catch (Exception ex)
                {
                    string msg = NestedExceptionMessageProcessor.GetFullMessage(ex);
                    set.AddIssue(new ValidationIssue(item, ValidationStatus.Error, ValidationStatusCode.Error_General_ValidationError, string.Format(Strings.ValidationResourceLoadFailed, msg)));
                }
                i++;
                worker.ReportProgress((int)((i / (double)documents.Count) * 100), s);
            }

            return set.GetAllIssues();
        }
Beispiel #4
0
        /// <summary>
        /// Validates the specified item using an existing validation context to skip over
        /// items already validated
        /// </summary>
        /// <param name="context"></param>
        /// <param name="item"></param>
        /// <param name="recurse"></param>
        /// <returns></returns>
        public static ValidationIssue[] Validate(ResourceValidationContext context, IResource item, bool recurse)
        {
            Check.NotNull(item, "item"); //NOXLATE
            var issueSet = new ValidationResultSet();
            if (!HasValidator(item.ResourceType, item.ResourceVersion))
            {
                issueSet.AddIssue(new ValidationIssue(item, ValidationStatus.Warning, ValidationStatusCode.Warning_General_NoRegisteredValidatorForResource, string.Format(Strings.ERR_NO_REGISTERED_VALIDATOR, item.ResourceType, item.ResourceVersion)));
            }
            else
            {
                foreach (IResourceValidator v in m_validators)
                {
                    if (!v.SupportedResourceAndVersion.Equals(item.GetResourceTypeDescriptor()))
                        continue;

                    try
                    {
                        ValidationIssue[] tmp = v.Validate(context, item, recurse);
                        if (tmp != null)
                            issueSet.AddIssues(tmp);
                    }
                    catch (Exception ex)
                    {
                        string msg = NestedExceptionMessageProcessor.GetFullMessage(ex);
                        if (ex is NullReferenceException)
                            msg = ex.ToString();
                        issueSet.AddIssue(new ValidationIssue(item, ValidationStatus.Error, ValidationStatusCode.Error_General_ValidationError, string.Format(Strings.ErrorValidationGeneric, msg)));
                    }
                }
            }
            return issueSet.GetAllIssues();
        }
Beispiel #5
0
        /// <summary>
        /// Performs any pre-save validation on the currently edited resource, by default
        /// this returns the results of a non-cascading <see cref="ResourceValidatorSet"/>
        /// validation run. Override this if you have a custom method of validation.
        /// </summary>
        protected virtual ICollection<ValidationIssue> ValidateEditedResource() 
        {
            var context = new ResourceValidationContext(_svc.ResourceService, _svc.FeatureService);
            //Don't recurse as we only want to validate the current resource
            var issues = ResourceValidatorSet.Validate(context, this.Resource, false);
            var set = new ValidationResultSet(issues);

            var errors = set.GetIssuesForResource(this.Resource.ResourceID, ValidationStatus.Error);
            return errors;
        }
        /// <summary>
        /// Performs base validation logic
        /// </summary>
        /// <param name="context"></param>
        /// <param name="resource"></param>
        /// <param name="recurse"></param>
        /// <returns></returns>
        protected static ValidationIssue[] ValidateBase(ResourceValidationContext context, IResource resource, bool recurse)
        {
            Check.ArgumentNotNull(context, nameof(context));

            if (context.IsAlreadyValidated(resource.ResourceID))
            {
                return(null);
            }

            if (resource.ResourceType != ResourceTypes.LoadProcedure.ToString())
            {
                return(null);
            }

            if (resource.ResourceVersion != new Version(1, 0, 0))
            {
                return(null);
            }

            var set = new ValidationResultSet();

            var loadProc = (resource as ILoadProcedure).SubType;

            if (loadProc.Type == LoadType.Dwg)
            {
                set.AddIssue(new ValidationIssue(resource, ValidationStatus.Warning, ValidationStatusCode.Warning_LoadProcedure_DwgNotSupported, Strings.LPROC_DWGNotSupported));
                return(set.GetAllIssues()); //all she wrote
            }

            if (loadProc.Type == LoadType.Raster)
            {
                set.AddIssue(new ValidationIssue(resource, ValidationStatus.Warning, ValidationStatusCode.Warning_LoadProcedure_RasterNotSupported, Strings.LPROC_RasterNotSupported));
                return(set.GetAllIssues()); //all she wrote
            }

            if (loadProc.Type == LoadType.Sdf)
            {
                set.AddIssue(new ValidationIssue(resource, ValidationStatus.Warning, ValidationStatusCode.Warning_LoadProcedure_Sdf2OptionsNotSupported, Strings.LPROC_Sdf2OptionsNotSupported));
                set.AddIssue(new ValidationIssue(resource, ValidationStatus.Warning, ValidationStatusCode.Warning_LoadProcedure_GeneralizationNotSupported, Strings.LPROC_GeneralizationNotSupported));
            }

            if (loadProc.Type == LoadType.Shp)
            {
                set.AddIssue(new ValidationIssue(resource, ValidationStatus.Warning, ValidationStatusCode.Warning_LoadProcedure_ConvertToSdf3NotSupported, Strings.LPROC_ConvertToSdf3NotSupported));
                set.AddIssue(new ValidationIssue(resource, ValidationStatus.Warning, ValidationStatusCode.Warning_LoadProcedure_GeneralizationNotSupported, Strings.LPROC_GeneralizationNotSupported));
            }

            if (loadProc.Type == LoadType.Sqlite)
            {
                set.AddIssue(new ValidationIssue(resource, ValidationStatus.Warning, ValidationStatusCode.Warning_LoadProcedure_GeneralizationNotSupported, Strings.LPROC_GeneralizationNotSupported));
            }

            var fproc = loadProc as IBaseLoadProcedure;

            if (fproc != null)
            {
                foreach (var fn in fproc.SourceFile)
                {
                    if (!System.IO.File.Exists(fn))
                    {
                        set.AddIssue(new ValidationIssue(resource, ValidationStatus.Warning, ValidationStatusCode.Warning_LoadProcedure_SourceFileNotFound, string.Format(Strings.LPROC_SourceFileNotFound, fn)));
                    }
                }
            }

            context.MarkValidated(resource.ResourceID);

            return(set.GetAllIssues());
        }
Beispiel #7
0
        protected override ICollection<ValidationIssue> ValidateEditedResource()
        {
            string[] warnings;
            string[] errors;

            Func<ValidationIssue[]> validator = () =>
            {
                ValidateXml(out errors, out warnings);
                var issues = new List<ValidationIssue>();
                foreach (string err in errors)
                {
                    issues.Add(new ValidationIssue(this.Resource, ValidationStatus.Error, ValidationStatusCode.Error_General_ValidationError, err));
                }
                foreach (string warn in warnings)
                {
                    issues.Add(new ValidationIssue(this.Resource, ValidationStatus.Warning, ValidationStatusCode.Warning_General_ValidationWarning, warn));
                }

                //Put through ValidationResultSet to weed out redundant messages
                var set = new ValidationResultSet(issues);

                try
                {
                    var res = ResourceTypeRegistry.Deserialize(editor.XmlContent);
                    var context = new ResourceValidationContext(_edSvc.ResourceService, _edSvc.FeatureService);
                    //We don't care about dependents, we just want to validate *this* resource
                    var resIssues = ResourceValidatorSet.Validate(context, res, false);
                    set.AddIssues(resIssues);
                }
                catch
                {
                    //This can fail because the XML may be for something that Maestro does not offer a strongly-typed class for yet.
                    //So the XML may be legit, just not for this version of Maestro that is doing the validating
                }

                //Only care about errors. Warnings and other types should not derail us from saving
                return set.GetAllIssues(ValidationStatus.Error);
            };

            if (this.InvokeRequired)
                return (ValidationIssue[])this.Invoke(validator);
            else
                return validator();
        }