/// <summary>
        /// Perform base validation logic
        /// </summary>
        /// <param name="context"></param>
        /// <param name="resource"></param>
        /// <param name="recurse"></param>
        /// <returns></returns>
        protected ValidationIssue[] ValidateBase(ResourceValidationContext context, IResource resource, bool recurse)
        {
            Check.ArgumentNotNull(context, nameof(context));

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

            var ldef  = resource as ILayerDefinition;
            var vldef = ldef.SubLayer as IVectorLayerDefinition;
            var gldef = ldef.SubLayer as IRasterLayerDefinition;
            var dldef = ldef.SubLayer as IDrawingLayerDefinition;

            List <ValidationIssue> issues = new List <ValidationIssue>();

            if (ldef.SubLayer == null)
            {
                issues.Add(new ValidationIssue(resource, ValidationStatus.Error, ValidationStatusCode.Error_LayerDefinition_LayerNull, Strings.LDF_LayerNullError));
            }

            ClassDefinition cls = null;

            if (vldef != null || gldef != null)
            {
                //Load referenced feature source
                IFeatureSource fs = null;

                try
                {
                    fs = (IFeatureSource)context.GetResource(ldef.SubLayer.ResourceId);
                }
                catch (Exception)
                {
                    issues.Add(new ValidationIssue(resource, ValidationStatus.Error, ValidationStatusCode.Error_LayerDefinition_FeatureSourceLoadError, string.Format(Strings.LDF_FeatureSourceLoadError)));
                }

                if (fs != null)
                {
                    //Verify specified feature class and geometry check out
                    try
                    {
                        string qualClassName = vldef == null ? gldef.FeatureName : vldef.FeatureName;
                        string geometry      = vldef == null ? gldef.Geometry : vldef.Geometry;

                        bool foundSchema   = false;
                        bool foundGeometry = false;

                        cls = this.Connection.FeatureService.GetClassDefinition(fs.ResourceID, qualClassName);
                        if (cls != null)
                        {
                            foundSchema = true;
                            foreach (PropertyDefinition col in cls.Properties)
                            {
                                if (col.Name == geometry)
                                {
                                    foundGeometry = true;
                                    break;
                                }
                            }

                            if (vldef != null && vldef.PropertyMapping != null)
                            {
                                foreach (INameStringPair s in vldef.PropertyMapping)
                                {
                                    bool found = false;
                                    foreach (PropertyDefinition col in cls.Properties)
                                    {
                                        if (col.Name == s.Name)
                                        {
                                            found = true;
                                            break;
                                        }
                                    }
                                    if (!found)
                                    {
                                        issues.Add(new ValidationIssue(resource, ValidationStatus.Error, ValidationStatusCode.Error_LayerDefinition_InvalidPropertyMapping, string.Format(Strings.LDF_InvalidPropertyMapping, s.Name, qualClassName)));
                                    }
                                }
                            }
                        }

                        if (!foundSchema)
                        {
                            issues.Add(new ValidationIssue(resource, ValidationStatus.Error, ValidationStatusCode.Error_LayerDefinition_ClassNotFound, string.Format(Strings.LDF_SchemaMissingError, qualClassName, fs.ResourceID)));
                        }
                        else if (!foundGeometry)
                        {
                            issues.Add(new ValidationIssue(resource, ValidationStatus.Error, ValidationStatusCode.Error_LayerDefinition_GeometryNotFound, string.Format(Strings.LDF_GeometryMissingError, geometry, qualClassName, fs.ResourceID)));
                        }
                    }
                    catch (Exception ex)
                    {
                        issues.Add(new ValidationIssue(fs, ValidationStatus.Error, ValidationStatusCode.Error_LayerDefinition_Generic, string.Format(Strings.LDF_SchemaAndColumnReadFailedError, ex.ToString())));
                    }

                    if (recurse)
                    {
                        issues.AddRange(ResourceValidatorSet.Validate(context, fs, recurse));
                    }
                }
            }

            if (vldef != null)
            {
                if (string.IsNullOrEmpty(vldef.FeatureName))
                {
                    issues.Add(new ValidationIssue(resource, ValidationStatus.Error, ValidationStatusCode.Error_LayerDefinition_MissingFeatureName, Strings.LDF_MissingFeatureSourceError));
                }
                if (string.IsNullOrEmpty(vldef.Geometry))
                {
                    issues.Add(new ValidationIssue(resource, ValidationStatus.Error, ValidationStatusCode.Error_LayerDefinition_MissingGeometry, Strings.LDF_MissingGeometryError));
                }

                if (vldef.VectorScaleRange == null || !vldef.HasVectorScaleRanges())
                {
                    issues.Add(new ValidationIssue(resource, ValidationStatus.Error, ValidationStatusCode.Error_LayerDefinition_MissingScaleRanges, Strings.LDF_MissingScaleRangesError));
                }
                else
                {
                    //Test for overlapping scale ranges
                    List <KeyValuePair <double, double> > ranges = new List <KeyValuePair <double, double> >();
                    foreach (IVectorScaleRange vsr in vldef.VectorScaleRange)
                    {
                        IVectorScaleRange2 vsr2 = vsr as IVectorScaleRange2;
                        if (vsr2 != null)
                        {
                            var area  = vsr2.AreaStyle;
                            var line  = vsr2.LineStyle;
                            var point = vsr2.PointStyle;

                            if (vsr2.CompositeStyleCount > 0 && (area != null || line != null || point != null))
                            {
                                issues.Add(new ValidationIssue(resource, ValidationStatus.Warning, ValidationStatusCode.Warning_LayerDefinition_CompositeStyleDefinedAlongsideBasicStyle, string.Format(
                                                                   Strings.LDF_CompositeStyleDefinedAlongsideBasicStyle,
                                                                   vsr2.MinScale.HasValue ? vsr2.MinScale.Value : 0,
                                                                   vsr2.MaxScale.HasValue ? vsr2.MaxScale.Value.ToString() : Strings.Infinity)));
                            }

                            if (vsr2.CompositeStyleCount > 0)
                            {
                                foreach (var cs in vsr2.CompositeStyle)
                                {
                                    foreach (var cr in cs.CompositeRule)
                                    {
                                        if (cr.CompositeSymbolization != null)
                                        {
                                            foreach (var si in cr.CompositeSymbolization.SymbolInstance)
                                            {
                                                //verify that symbol references point to valid symbol definitions
                                                if (si.Reference.Type == SymbolInstanceType.Reference)
                                                {
                                                    var symRef = (ISymbolInstanceReferenceLibrary)si.Reference;
                                                    if (!context.ResourceExists(symRef.ResourceId))
                                                    {
                                                        issues.Add(new ValidationIssue(resource, ValidationStatus.Error, ValidationStatusCode.Error_LayerDefinition_SymbolDefintionReferenceNotFound, string.Format(
                                                                                           Strings.LDF_SymbolDefintionReferenceNotFound, symRef.ResourceId)));
                                                    }

                                                    if (cls != null)
                                                    {
                                                        //warn if the symbol definition has usage contexts that are irrelevant against the layer's feature source
                                                        var prop = cls.FindProperty(vldef.Geometry) as GeometricPropertyDefinition;
                                                        if (prop != null)
                                                        {
                                                            var gtypes = new List <FeatureGeometricType>(prop.GetIndividualGeometricTypes());
                                                            var sym    = context.GetResource(symRef.ResourceId) as ISimpleSymbolDefinition;
                                                            if (sym != null)
                                                            {
                                                                if (sym.LineUsage != null && !gtypes.Contains(FeatureGeometricType.Curve))
                                                                {
                                                                    issues.Add(new ValidationIssue(resource, ValidationStatus.Information, ValidationStatusCode.Info_LayerDefinition_IrrelevantUsageContext, string.Format(
                                                                                                       Strings.LDF_IrrelevantUsageContext, symRef.ResourceId, FeatureGeometricType.Curve.ToString())));
                                                                }

                                                                if (sym.AreaUsage != null && !gtypes.Contains(FeatureGeometricType.Surface))
                                                                {
                                                                    issues.Add(new ValidationIssue(resource, ValidationStatus.Information, ValidationStatusCode.Info_LayerDefinition_IrrelevantUsageContext, string.Format(
                                                                                                       Strings.LDF_IrrelevantUsageContext, symRef.ResourceId, FeatureGeometricType.Surface.ToString())));
                                                                }

                                                                if (sym.PointUsage != null && !gtypes.Contains(FeatureGeometricType.Point))
                                                                {
                                                                    issues.Add(new ValidationIssue(resource, ValidationStatus.Information, ValidationStatusCode.Info_LayerDefinition_IrrelevantUsageContext, string.Format(
                                                                                                       Strings.LDF_IrrelevantUsageContext, symRef.ResourceId, FeatureGeometricType.Point.ToString())));
                                                                }
                                                            }
                                                        }
                                                    }
                                                }

                                                //verify that the overrides specified are for symbol parameters that actually exist
                                                if (si.ParameterOverrides.Count > 0)
                                                {
                                                    if (si.Reference.Type == SymbolInstanceType.Reference)
                                                    {
                                                        var symRef    = (ISymbolInstanceReferenceLibrary)si.Reference;
                                                        var sym       = (ISymbolDefinitionBase)context.GetResource(symRef.ResourceId);
                                                        var paramList = new Dictionary <string, IParameter>();
                                                        foreach (var p in sym.GetParameters())
                                                        {
                                                            paramList[p.Identifier] = p;
                                                        }

                                                        foreach (var ov in si.ParameterOverrides.Override)
                                                        {
                                                            if (!paramList.ContainsKey(ov.ParameterIdentifier))
                                                            {
                                                                issues.Add(new ValidationIssue(resource, ValidationStatus.Warning, ValidationStatusCode.Warning_LayerDefinition_SymbolParameterOverrideToNonExistentParameter, string.Format(
                                                                                                   Strings.LDF_SymbolParameterOverrideToNonExistentParameter, symRef.ResourceId, ov.ParameterIdentifier)));
                                                            }
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }

                        ranges.Add(new KeyValuePair <double, double>(
                                       vsr.MaxScale.HasValue ? vsr.MaxScale.Value : double.PositiveInfinity,
                                       vsr.MinScale.HasValue ? vsr.MinScale.Value : double.NegativeInfinity));
                    }

                    double min = double.PositiveInfinity;
                    double max = double.NegativeInfinity;
                    foreach (KeyValuePair <double, double> sr in ranges)
                    {
                        min = Math.Min(min, sr.Value);
                        max = Math.Max(max, sr.Key);
                        if (sr.Key < sr.Value)
                        {
                            issues.Add(new ValidationIssue(resource, ValidationStatus.Error, ValidationStatusCode.Error_LayerDefinition_MinMaxScaleSwapped, string.Format(Strings.LDF_MinAndMaxScaleSwappedError, sr.Value, sr.Key)));
                        }
                    }

                    ranges.Sort(CompareScales);

                    //TODO: Detect gaps in scale ranges
                    for (int i = 0; i < ranges.Count; i++)
                    {
                        for (int j = i + 1; j < ranges.Count; j++)
                        {
                            if (ranges[i].Key > ranges[j].Value || ranges[i].Value > ranges[j].Value)
                            {
                                issues.Add(new ValidationIssue(resource, ValidationStatus.Information, ValidationStatusCode.Info_LayerDefinition_ScaleRangeOverlap, string.Format(Strings.LDF_ScaleRangesOverlapInformation, ranges[i].Value, ranges[i].Key, ranges[j].Value, ranges[j].Key)));
                            }
                        }
                    }
                }
            }
            else if (gldef != null)
            {
                if (string.IsNullOrEmpty(gldef.FeatureName))
                {
                    issues.Add(new ValidationIssue(resource, ValidationStatus.Error, ValidationStatusCode.Error_LayerDefinition_MissingFeatureName, Strings.LDF_MissingFeatureSourceError));
                }
                if (string.IsNullOrEmpty(gldef.Geometry))
                {
                    issues.Add(new ValidationIssue(resource, ValidationStatus.Error, ValidationStatusCode.Error_LayerDefinition_MissingGeometry, Strings.LDF_MissingGeometryError));
                }
                if (gldef.GridScaleRange == null || gldef.GridScaleRangeCount == 0)
                {
                    issues.Add(new ValidationIssue(resource, ValidationStatus.Error, ValidationStatusCode.Error_LayerDefinition_NoGridScaleRanges, Strings.LDF_MissingScaleRangesError));
                }
                else if (gldef.GridScaleRangeCount != 1)
                {
                    issues.Add(new ValidationIssue(resource, ValidationStatus.Warning, ValidationStatusCode.Warning_LayerDefinition_MultipleGridScaleRanges, Strings.LDF_MultipleScaleRangesWarning));
                }
            }
            else if (dldef != null)
            {
                IDrawingSource dws = null;
                try
                {
                    dws = (IDrawingSource)context.GetResource(dldef.ResourceId);
                }
                catch (Exception)
                {
                    issues.Add(new ValidationIssue(resource, ValidationStatus.Error, ValidationStatusCode.Error_LayerDefinition_DrawingSourceError, Strings.LDF_DrawingSourceError));
                }

                if (dws != null)
                {
                    if (Array.IndexOf(this.Connection.Capabilities.SupportedServices, (int)ServiceType.Drawing) >= 0)
                    {
                        var dwSvc = (IDrawingService)this.Connection.GetService((int)ServiceType.Drawing);

                        //Check if specified section exists
                        var shtList = dwSvc.EnumerateDrawingSections(dws.ResourceID);
                        DrawingSectionListSection sheet = null;
                        foreach (var sht in shtList.Section)
                        {
                            if (sht.Name.Equals(dldef.Sheet))
                            {
                                sheet = sht;
                                break;
                            }
                        }

                        if (sheet == null)
                        {
                            issues.Add(new ValidationIssue(resource, ValidationStatus.Error, ValidationStatusCode.Error_LayerDefinition_DrawingSourceSheetNotFound, string.Format(Strings.LDF_SheetNotFound, dldef.Sheet)));
                        }
                        else
                        {
                            //null or empty filter means all layers, in that case do nothing
                            if (!string.IsNullOrEmpty(dldef.LayerFilter))
                            {
                                //Check if specified layers all exist in specified section
                                var specifiedLayers = dldef.LayerFilter.Split(','); //NOXLATE
                                var dwLayers        = new Dictionary <string, string>();

                                var shtLayers = dwSvc.EnumerateDrawingLayers(dws.ResourceID, sheet.Name);
                                foreach (var sl in shtLayers)
                                {
                                    dwLayers.Add(sl, sl);
                                }

                                foreach (var sl in specifiedLayers)
                                {
                                    if (!dwLayers.ContainsKey(sl.Trim()))
                                    {
                                        issues.Add(new ValidationIssue(resource, ValidationStatus.Error, ValidationStatusCode.Error_LayerDefinition_DrawingSourceSheetLayerNotFound, string.Format(Strings.LDF_SheetLayerNotFound, sl.Trim(), sheet.Name)));
                                    }
                                }
                            }
                        }
                    }

                    if (recurse)
                    {
                        issues.AddRange(ResourceValidatorSet.Validate(context, dws, recurse));
                    }
                }
            }
            else
            {
                issues.Add(new ValidationIssue(resource, ValidationStatus.Warning, ValidationStatusCode.Warning_LayerDefinition_UnsupportedLayerType, Strings.LDF_UnsupportedLayerTypeWarning));
            }

            context.MarkValidated(resource.ResourceID);

            return(issues.ToArray());
        }
Example #2
0
        /// <summary>
        /// Performs base resource validation
        /// </summary>
        /// <param name="context"></param>
        /// <param name="resource"></param>
        /// <param name="recurse"></param>
        /// <returns></returns>
        protected ValidationIssue[] ValidateBase(ResourceValidationContext context, IResource resource, bool recurse)
        {
            Check.ArgumentNotNull(context, nameof(context));

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

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

            List <ValidationIssue> issues = new List <ValidationIssue>();

            IMapDefinition mdef = resource as IMapDefinition;

            if (string.IsNullOrEmpty(mdef.CoordinateSystem))
            {
                issues.Add(new ValidationIssue(mdef, ValidationStatus.Warning, ValidationStatusCode.Warning_MapDefinition_MissingCoordinateSystem, Strings.MDF_NoCoordinateSystem));
            }

            foreach (IMapLayerGroup g in mdef.MapLayerGroup)
            {
                if (g.ShowInLegend && (g.LegendLabel == null || g.LegendLabel.Trim().Length == 0))
                {
                    issues.Add(new ValidationIssue(mdef, ValidationStatus.Information, ValidationStatusCode.Info_MapDefinition_GroupMissingLabelInformation, string.Format(Strings.MDF_GroupMissingLabelInformation, g.Name)));
                }
                else if (g.ShowInLegend && g.LegendLabel.Trim().ToLower() == "layer group") //NOXLATE
                {
                    issues.Add(new ValidationIssue(mdef, ValidationStatus.Information, ValidationStatusCode.Info_MapDefinition_GroupHasDefaultLabel, string.Format(Strings.MDF_GroupHasDefaultLabelInformation, g.Name)));
                }

                if (!string.IsNullOrEmpty(g.Group))
                {
                    var grp = mdef.GetGroupByName(g.Group);
                    if (grp == null)
                    {
                        issues.Add(new ValidationIssue(mdef, ValidationStatus.Error, ValidationStatusCode.Error_MapDefinition_GroupWithNonExistentGroup, string.Format(Strings.MDF_GroupWithNonExistentGroup, g.Name, g.Group)));
                    }
                }
            }

            List <IBaseMapLayer> layers = new List <IBaseMapLayer>();

            foreach (IBaseMapLayer l in mdef.MapLayer)
            {
                layers.Add(l);
            }

            if (mdef.BaseMap != null && mdef.BaseMap.HasGroups())
            {
                if (mdef.BaseMap.ScaleCount == 0)
                {
                    issues.Add(new ValidationIssue(mdef, ValidationStatus.Error, ValidationStatusCode.Error_MapDefinition_NoFiniteDisplayScales, Strings.MDF_NoFiniteDisplayScalesSpecified));
                }

                foreach (IBaseMapGroup g in mdef.BaseMap.BaseMapLayerGroups)
                {
                    foreach (IBaseMapLayer l in g.BaseMapLayer)
                    {
                        layers.Add(l);
                    }
                }
            }
            Dictionary <string, IBaseMapLayer> nameCounter = new Dictionary <string, IBaseMapLayer>();

            foreach (IBaseMapLayer l in layers)
            {
                if (nameCounter.ContainsKey(l.Name))
                {
                    issues.Add(new ValidationIssue(mdef, ValidationStatus.Warning, ValidationStatusCode.Error_MapDefinition_DuplicateLayerName, string.Format(Strings.MDF_LayerNameDuplicateWarning, l.Name, l.ResourceId, nameCounter[l.Name].ResourceId)));
                }
                else
                {
                    nameCounter.Add(l.Name, l);
                }

                var ml = l as IMapLayer;
                if (ml != null && !string.IsNullOrEmpty(ml.Group))
                {
                    var grp = mdef.GetGroupByName(ml.Group);
                    if (grp == null)
                    {
                        issues.Add(new ValidationIssue(mdef, ValidationStatus.Error, ValidationStatusCode.Error_MapDefinition_LayerWithNonExistentGroup, string.Format(Strings.MDF_LayerWithNonExistentGroup, ml.Name, ml.Group)));
                    }
                }

                if (l.ShowInLegend && (string.IsNullOrEmpty(l.LegendLabel) || l.LegendLabel.Trim().Length == 0))
                {
                    issues.Add(new ValidationIssue(mdef, ValidationStatus.Information, ValidationStatusCode.Warning_MapDefinition_LayerMissingLegendLabel, string.Format(Strings.MDF_LayerMissingLabelInformation, l.Name)));
                }

                var mapEnv = ObjectFactory.CreateEnvelope(mdef.Extents.MinX, mdef.Extents.MinY, mdef.Extents.MaxX, mdef.Extents.MaxY);

                try
                {
                    ILayerDefinition layer = null;
                    IResource        res   = context.GetResource(l.ResourceId);
                    if (!ResourceValidatorSet.HasValidator(res.ResourceType, res.ResourceVersion))
                    {
                        //Need to trap the no registered validator message
                        issues.AddRange(ResourceValidatorSet.Validate(context, res, true));
                        continue;
                    }

                    layer = (ILayerDefinition)res;
                    if (recurse)
                    {
                        issues.AddRange(ResourceValidatorSet.Validate(context, layer, recurse));
                    }

                    IVectorLayerDefinition vl = null;
                    if (layer.SubLayer.LayerType == LayerType.Vector)
                    {
                        vl = (IVectorLayerDefinition)layer.SubLayer;
                    }

                    if (vl != null)
                    {
                        try
                        {
                            IFeatureSource fs = (IFeatureSource)context.GetResource(vl.ResourceId);
                            if (l.Selectable)
                            {
                                //Test selectability requirement
                                string[] idProps = fs.GetIdentityProperties(this.Connection, vl.FeatureName);
                                if (idProps == null || idProps.Length == 0)
                                {
                                    issues.Add(new ValidationIssue(resource, ValidationStatus.Warning, ValidationStatusCode.Warning_MapDefinition_UnselectableLayer, string.Format(Strings.MDF_UnselectableLayer, l.Name, vl.FeatureName, fs.ResourceID)));
                                }
                            }

                            try
                            {
                                FdoSpatialContextList scList = context.GetSpatialContexts(fs.ResourceID);

                                if (scList.SpatialContext == null || scList.SpatialContext.Count == 0)
                                {
                                    issues.Add(new ValidationIssue(resource, ValidationStatus.Warning, ValidationStatusCode.Warning_MapDefinition_MissingSpatialContext, string.Format(Strings.MDF_MissingSpatialContextWarning, fs.ResourceID)));
                                }
                                else
                                {
                                    if (scList.SpatialContext.Count > 1)
                                    {
                                        issues.Add(new ValidationIssue(resource, ValidationStatus.Information, ValidationStatusCode.Info_MapDefinition_MultipleSpatialContexts, string.Format(Strings.MDF_MultipleSpatialContextsInformation, fs.ResourceID)));
                                    }

                                    bool skipGeomCheck = false;

                                    //TODO: Switch to the correct version (2.1), once released
                                    if (scList.SpatialContext[0].CoordinateSystemWkt != mdef.CoordinateSystem)
                                    {
                                        if (layer.SubLayer.LayerType == LayerType.Raster && this.Connection.SiteVersion <= SiteVersions.GetVersion(OSGeo.MapGuide.MaestroAPI.KnownSiteVersions.MapGuideOS2_0_2))
                                        {
                                            issues.Add(new ValidationIssue(resource, ValidationStatus.Error, ValidationStatusCode.Error_MapDefinition_RasterReprojection, string.Format(Strings.MDF_RasterReprojectionError, fs.ResourceID)));
                                        }
                                        else
                                        {
                                            issues.Add(new ValidationIssue(resource, ValidationStatus.Warning, ValidationStatusCode.Warning_MapDefinition_LayerReprojection, string.Format(Strings.MDF_DataReprojectionWarning, fs.ResourceID)));
                                        }

                                        skipGeomCheck = true;
                                    }

                                    if (vl.Geometry != null && !skipGeomCheck)
                                    {
                                        var env = this.Connection.FeatureService.GetSpatialExtent(fs.ResourceID, vl.FeatureName, vl.Geometry);
                                        if (!env.Intersects(mapEnv))
                                        {
                                            issues.Add(new ValidationIssue(resource, ValidationStatus.Warning, ValidationStatusCode.Warning_MapDefinition_DataOutsideMapBounds, string.Format(Strings.MDF_DataOutsideMapWarning, fs.ResourceID)));
                                        }
                                    }
                                }
                            }
                            catch (Exception ex)
                            {
                                var nex = ex as NullExtentException;
                                if (nex != null)
                                {
                                    issues.Add(new ValidationIssue(resource, ValidationStatus.Warning, ValidationStatusCode.Warning_MapDefinition_FeatureSourceWithNullExtent, string.Format(Strings.MDF_LayerWithNullExtent, fs.ResourceID)));
                                }
                                else
                                {
                                    string msg = NestedExceptionMessageProcessor.GetFullMessage(ex);
                                    issues.Add(new ValidationIssue(resource, ValidationStatus.Error, ValidationStatusCode.Error_MapDefinition_ResourceRead, string.Format(Strings.MDF_ResourceReadError, fs.ResourceID, msg)));
                                }
                            }
                        }
                        catch (Exception ex)
                        {
                            string msg = NestedExceptionMessageProcessor.GetFullMessage(ex);
                            issues.Add(new ValidationIssue(resource, ValidationStatus.Error, ValidationStatusCode.Error_MapDefinition_FeatureSourceRead, string.Format(Strings.MDF_FeatureSourceReadError, l.ResourceId, msg)));
                        }
                    }
                }
                catch (Exception ex)
                {
                    string msg = NestedExceptionMessageProcessor.GetFullMessage(ex);
                    issues.Add(new ValidationIssue(resource, ValidationStatus.Error, ValidationStatusCode.Error_MapDefinition_LayerRead, string.Format(Strings.MDF_LayerReadError, l.ResourceId, msg)));
                }
            }

            context.MarkValidated(resource.ResourceID);

            return(issues.ToArray());
        }
Example #3
0
        /// <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.WebLayout.ToString())
            {
                return(null);
            }

            List <ValidationIssue> issues = new List <ValidationIssue>();

            IWebLayout layout = resource as IWebLayout;

            if (layout.Map == null || layout.Map.ResourceId == null)
            {
                issues.Add(new ValidationIssue(layout, ValidationStatus.Error, ValidationStatusCode.Error_WebLayout_MissingMap, string.Format(Strings.WL_MissingMapError)));
            }
            else
            {
                //Check for duplicate command names
                var cmdSet = layout.CommandSet;
                Dictionary <string, ICommand> cmds = new Dictionary <string, ICommand>();
                foreach (ICommand cmd in cmdSet.Commands)
                {
                    if (cmds.ContainsKey(cmd.Name))
                    {
                        issues.Add(new ValidationIssue(layout, ValidationStatus.Error, ValidationStatusCode.Error_WebLayout_DuplicateCommandName, string.Format(Strings.WL_DuplicateCommandName, cmd.Name)));
                    }
                    else
                    {
                        cmds[cmd.Name] = cmd;
                    }
                }

                //Check for duplicate property references in search commands
                foreach (ISearchCommand search in cmdSet.Commands.OfType <ISearchCommand>())
                {
                    Dictionary <string, string> resColProps = new Dictionary <string, string>();
                    foreach (IResultColumn resCol in search.ResultColumns.Column)
                    {
                        if (resColProps.ContainsKey(resCol.Property))
                        {
                            issues.Add(new ValidationIssue(layout, ValidationStatus.Error, ValidationStatusCode.Error_WebLayout_DuplicateSearchCommandResultColumn, string.Format(Strings.WL_DuplicateSearchResultColumn, search.Name, resCol.Property)));
                        }
                        else
                        {
                            resColProps.Add(resCol.Property, resCol.Property);
                        }
                    }
                }

                //Check for command references to non-existent commands
                foreach (IUIItem item in layout.ContextMenu.Items)
                {
                    if (item.Function == UIItemFunctionType.Command)
                    {
                        ICommandItem cmdRef = (ICommandItem)item;
                        if (!cmds.ContainsKey(cmdRef.Command))
                        {
                            issues.Add(new ValidationIssue(layout, ValidationStatus.Error, ValidationStatusCode.Error_WebLayout_NonExistentContextMenuCommandReference, string.Format(Strings.WL_NonExistentMenuCommandReference, cmdRef.Command)));
                        }
                    }
                }

                foreach (IUIItem item in layout.TaskPane.TaskBar.Items)
                {
                    if (item.Function == UIItemFunctionType.Command)
                    {
                        ICommandItem cmdRef = (ICommandItem)item;
                        if (!cmds.ContainsKey(cmdRef.Command))
                        {
                            issues.Add(new ValidationIssue(layout, ValidationStatus.Error, ValidationStatusCode.Error_WebLayout_NonExistentTaskPaneCommandReference, string.Format(Strings.WL_NonExistentTaskPaneCommandReference, cmdRef.Command)));
                        }
                    }
                }

                foreach (IUIItem item in layout.ToolBar.Items)
                {
                    if (item.Function == UIItemFunctionType.Command)
                    {
                        ICommandItem cmdRef = (ICommandItem)item;
                        if (!cmds.ContainsKey(cmdRef.Command))
                        {
                            issues.Add(new ValidationIssue(layout, ValidationStatus.Error, ValidationStatusCode.Error_WebLayout_NonExistentToolbarCommandReference, string.Format(Strings.WL_NonExistentToolbarCommandReference, cmdRef.Command)));
                        }
                    }
                }

                try
                {
                    IMapDefinition mdef = (IMapDefinition)context.GetResource(layout.Map.ResourceId);
                    if (layout.Map.InitialView != null)
                    {
                        var mapEnv = ObjectFactory.CreateEnvelope(mdef.Extents.MinX, mdef.Extents.MinY, mdef.Extents.MaxX, mdef.Extents.MaxY);
                        if (!mapEnv.Contains(layout.Map.InitialView.CenterX, layout.Map.InitialView.CenterY))
                        {
                            issues.Add(new ValidationIssue(mdef, ValidationStatus.Warning, ValidationStatusCode.Warning_WebLayout_InitialViewOutsideMapExtents, string.Format(Strings.WL_StartViewOutsideExtentsWarning)));
                        }
                    }

                    if (recurse)
                    {
                        issues.AddRange(ResourceValidatorSet.Validate(context, mdef, true));
                    }
                }
                catch (Exception ex)
                {
                    string msg = NestedExceptionMessageProcessor.GetFullMessage(ex);
                    issues.Add(new ValidationIssue(layout, ValidationStatus.Error, ValidationStatusCode.Error_WebLayout_Generic, string.Format(Strings.WL_MapValidationError, layout.Map.ResourceId, msg)));
                }
            }

            context.MarkValidated(resource.ResourceID);

            return(issues.ToArray());
        }
        /// <summary>
        /// Validats the specified resources for common issues associated with this
        /// resource type
        /// </summary>
        /// <param name="context"></param>
        /// <param name="resource"></param>
        /// <param name="recurse"></param>
        /// <returns></returns>
        public ValidationIssue[] Validate(ResourceValidationContext context, IResource resource, bool recurse)
        {
            if (resource.ResourceType != ResourceTypes.ApplicationDefinition.ToString())
            {
                return(null);
            }

            //TODO: Other items to check for
            //
            // - References to non-existent widgets
            // - MapWidget checks
            //   - Ensure map id checks out
            //   - Ensure context menu id checks out
            // - Verify containers of template are all referenced within this flexible layout
            // - Check required parameters of widgets are satisfied

            List <ValidationIssue> issues = new List <ValidationIssue>();

            IApplicationDefinition fusionApp = resource as IApplicationDefinition;

            if (fusionApp.MapSet == null || fusionApp.MapSet.MapGroupCount == 0)
            {
                issues.Add(new ValidationIssue(fusionApp, ValidationStatus.Error, ValidationStatusCode.Error_Fusion_MissingMap, string.Format(Strings.ADF_MapMissingError)));
            }
            else
            {
                foreach (IMapGroup mapGroup in fusionApp.MapSet.MapGroups)
                {
                    bool checkCmsProjection = false;
                    List <IMapDefinition> mapDefsInGroup = new List <IMapDefinition>();
                    foreach (IMap map in mapGroup.Map)
                    {
                        if (IsCommercialOverlay(map))
                        {
                            checkCmsProjection = true;
                        }
                        if (map.Type.ToLower() == "virtualearth")
                        {
                            //As of July 1, 2017 we need an API key on Bing Maps
                            var bingMapsKey = fusionApp.GetValue("BingMapKey");
                            if (string.IsNullOrEmpty(bingMapsKey))
                            {
                                issues.Add(new ValidationIssue(fusionApp, ValidationStatus.Error, ValidationStatusCode.Error_Fusion_BingMapsMissingApiKey, Strings.ADF_BingMapsMissingApiKey));
                            }

                            //If this is still referencing the "Hybrid" base layer type, that no longer exists in the v8 API
                            if (map.CmsMapOptions.Type == "Hybrid")
                            {
                                issues.Add(new ValidationIssue(fusionApp, ValidationStatus.Error, ValidationStatusCode.Error_Fusion_BingMapsHybridBaseLayerNoLongerAvailable, Strings.ADF_BingMapsHybridLayerNoLongerAvailable));
                            }
                        }
                        try
                        {
                            if (map.Type.ToLower() == "mapguide") //NOXLATE
                            {
                                var mdfId = map.GetMapDefinition();
                                if (string.IsNullOrEmpty(mdfId) || !this.Connection.ResourceService.ResourceExists(mdfId))
                                {
                                    issues.Add(new ValidationIssue(fusionApp, ValidationStatus.Error, ValidationStatusCode.Error_Fusion_InvalidMap, string.Format(Strings.ADF_MapInvalidError, mapGroup.id)));
                                }
                                else
                                {
                                    IMapDefinition mdef = (IMapDefinition)context.GetResource(mdfId);
                                    mapDefsInGroup.Add(mdef);

                                    IEnvelope mapEnv = ObjectFactory.CreateEnvelope(mdef.Extents.MinX, mdef.Extents.MinY, mdef.Extents.MaxX, mdef.Extents.MaxY);

                                    if (mapGroup.InitialView != null)
                                    {
                                        if (!mapEnv.Contains(mapGroup.InitialView.CenterX, mapGroup.InitialView.CenterY))
                                        {
                                            issues.Add(new ValidationIssue(mdef, ValidationStatus.Warning, ValidationStatusCode.Warning_Fusion_InitialViewOutsideMapExtents, string.Format(Strings.ADF_ViewOutsideMapExtents)));
                                        }
                                    }

                                    if (recurse)
                                    {
                                        issues.AddRange(ResourceValidatorSet.Validate(context, mdef, recurse));
                                    }
                                }
                            }
                        }
                        catch (Exception ex)
                        {
                            string msg = NestedExceptionMessageProcessor.GetFullMessage(ex);
                            issues.Add(new ValidationIssue(fusionApp, ValidationStatus.Error, ValidationStatusCode.Error_Fusion_MapValidationError, string.Format(Strings.ADF_MapValidationError, mapGroup.id, msg)));
                        }
                    }

                    if (checkCmsProjection)
                    {
                        foreach (var mdf in mapDefsInGroup)
                        {
                            var wkt    = mdf.CoordinateSystem;
                            var csCode = this.Connection.CoordinateSystemCatalog.ConvertWktToCoordinateSystemCode(wkt);
                            if (csCode.ToUpper() != "WGS84.PSEUDOMERCATOR") //NOXLATE
                            {
                                issues.Add(new ValidationIssue(resource, ValidationStatus.Warning, ValidationStatusCode.Warning_Fusion_MapCoordSysIncompatibleWithCommericalLayers, string.Format(Strings.ADF_MapWithIncompatibleCommericalCs, mdf.ResourceID)));
                            }
                        }
                    }
                }
            }

            //Check labels of referenced widgets
            foreach (var wset in fusionApp.WidgetSets)
            {
                foreach (var cnt in wset.Containers)
                {
                    var menu = cnt as IMenu;
                    if (menu != null)
                    {
                        ValidateWidgetReferencesForMenu(fusionApp, menu, issues, context, resource);
                    }
                }
            }

            context.MarkValidated(resource.ResourceID);

            return(issues.ToArray());
        }